@@ -454,73 +454,54 @@ private void loadAttrs(AttributeSet attrs, int defStyle) {
454454
455455 Field styleableFieldInR = null ;
456456 TypedArray typedAttrs = null ;
457-
458- final String appPkg = getContext ().getPackageName ();
459- Class styleableClass = null ;
457+
458+ Class styleableClass = R .styleable .class ;
459+ String styleableName = getClass ().getName ().substring (BASE_PACKAGE .length ());
460+ styleableName = styleableName .replace ('.' , '_' );
460461 try {
461462 /**
462- * Need to retrieve R$styleable.class dynamically to avoid exceptions
463- * in environments where this class does not exist, such as .jar users
464- * that have not merged the library's attrs.xml with their own.
463+ * Use reflection to safely check for the existence of styleable defs for Plot
464+ * and it's derivatives. This safety check is necessary to avoid runtime exceptions
465+ * in apps that don't include Androidplot as a .aar and won't have access to
466+ * the resources defined in the core library.
465467 */
466- styleableClass = Class .forName (appPkg + ".R$styleable" );
467-
468- } catch (ClassNotFoundException e ) {
469- // when running as a preview in IntelliJ or AndroidStudio it seems that the package of R is
470- // something else; this fixes that issue:
471- if (isInEditMode ()) {
472- styleableClass = R .styleable .class ;
473- }
468+ styleableFieldInR = styleableClass .getField (styleableName );
469+ } catch (NoSuchFieldException e ) {
470+ Log .d (TAG , "Styleable definition not found for: " + styleableName );
474471 }
475-
476- if (styleableClass != null ) {
477- String styleableName = getClass ().getName ().substring (BASE_PACKAGE .length ());
478- styleableName = styleableName .replace ('.' , '_' );
479- try {
480- /**
481- * Use reflection to safely check for the existence of styleable defs for Plot
482- * and it's derivatives. This safety check is necessary to avoid runtime exceptions
483- * in apps that don't include Androidplot as a .aar and won't have access to
484- * the resources defined in the core library.
485- */
486- styleableFieldInR = styleableClass .getField (styleableName );
487- } catch (NoSuchFieldException e ) {
488- Log .d (TAG , "Styleable definition not found for: " + styleableName );
489- }
490- if (styleableFieldInR != null ) {
491- try {
492- int [] resIds = (int []) styleableFieldInR .get (null );
493- typedAttrs = getContext ().obtainStyledAttributes (attrs , resIds , defStyle , 0 );
494- } catch (IllegalAccessException e ) {
495- // nothing to do
496- } finally {
497- if (typedAttrs != null ) {
498- // apply derived class' attrs:
499- processAttrs (typedAttrs );
500- typedAttrs .recycle ();
501- }
502- }
503- }
504-
472+ if (styleableFieldInR != null ) {
505473 try {
506- styleableFieldInR = styleableClass .getField (Plot .class .getSimpleName ());
507- if (styleableFieldInR != null ) {
508- int [] resIds = (int []) styleableFieldInR .get (null );
509- typedAttrs = getContext ().obtainStyledAttributes (attrs , resIds , defStyle , 0 );
510- }
474+ int [] resIds = (int []) styleableFieldInR .get (null );
475+ typedAttrs = getContext ().obtainStyledAttributes (attrs , resIds , defStyle , 0 );
511476 } catch (IllegalAccessException e ) {
512477 // nothing to do
513- } catch (NoSuchFieldException e ) {
514- Log .d (TAG , "Styleable definition not found for: " + Plot .class .getSimpleName ());
515478 } finally {
516479 if (typedAttrs != null ) {
517- // apply base attrs:
518- processBaseAttrs (typedAttrs );
480+ // apply derived class' attrs:
481+ processAttrs (typedAttrs );
519482 typedAttrs .recycle ();
520483 }
521484 }
522485 }
523486
487+ try {
488+ styleableFieldInR = styleableClass .getField (Plot .class .getSimpleName ());
489+ if (styleableFieldInR != null ) {
490+ int [] resIds = (int []) styleableFieldInR .get (null );
491+ typedAttrs = getContext ().obtainStyledAttributes (attrs , resIds , defStyle , 0 );
492+ }
493+ } catch (IllegalAccessException e ) {
494+ // nothing to do
495+ } catch (NoSuchFieldException e ) {
496+ Log .d (TAG , "Styleable definition not found for: " + Plot .class .getSimpleName ());
497+ } finally {
498+ if (typedAttrs != null ) {
499+ // apply base attrs:
500+ processBaseAttrs (typedAttrs );
501+ typedAttrs .recycle ();
502+ }
503+ }
504+
524505 // apply "configurator" attrs: (overrides any previously applied styleable attrs)
525506 // filter out androidplot prefixed attrs:
526507 HashMap <String , String > attrHash = new HashMap <String , String >();
0 commit comments