Code example for Field

Methods: getDeclaringClass, getName, getType, set, setAccessible

0
 
            try { 
 
                final Resources resources = application.getResources();
                final int id = getId(resources,annotation);
                final Class<?> t = field.getType();
 
                if (String.class.isAssignableFrom(t)) {
                    value = resources.getString(id);
                } else if (boolean.class.isAssignableFrom(t) || Boolean.class.isAssignableFrom(t)) {
                    value = resources.getBoolean(id);
                } else if (ColorStateList.class.isAssignableFrom(t)  ) {
                    value = resources.getColorStateList(id);
                } else if (int.class.isAssignableFrom(t) || Integer.class.isAssignableFrom(t)) {
                    value = resources.getInteger(id);
                } else if (Drawable.class.isAssignableFrom(t)) {
                    value = resources.getDrawable(id);
                } else if (String[].class.isAssignableFrom(t)) {
                    value = resources.getStringArray(id);
                } else if (int[].class.isAssignableFrom(t) || Integer[].class.isAssignableFrom(t)) {
                    value = resources.getIntArray(id);
                } else if (Animation.class.isAssignableFrom(t)) {
                    value = AnimationUtils.loadAnimation(application, id);
                } else if (Movie.class.isAssignableFrom(t)  ) {
                    value = resources.getMovie(id);
                } 
                 
                if (value == null && Nullable.notNullable(field) ) {
                    throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(), field
                            .getName()));
                } 
 
                field.setAccessible(true);
                field.set(instance, value);
 
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
 
            } catch (IllegalArgumentException f) {
                throw new IllegalArgumentException(String.format("Can't assign %s value %s to %s field %s", value != null ? value.getClass() : "(null)", value,
                        field.getType(), field.getName()));
            } 
        } 
 
        protected int getId(Resources resources, InjectResource annotation) {
            int id = annotation.value();