Search in sources :

Example 6 with IntArrayWrapper

use of com.android.ide.common.resources.IntArrayWrapper in project android_frameworks_base by ResurrectionRemix.

the class LayoutLibTestCallback method initResources.

public void initResources() throws ClassNotFoundException {
    Class<?> rClass = mModuleClassLoader.loadClass(PACKAGE_NAME + ".R");
    Class<?>[] nestedClasses = rClass.getDeclaredClasses();
    for (Class<?> resClass : nestedClasses) {
        final ResourceType resType = ResourceType.getEnum(resClass.getSimpleName());
        if (resType != null) {
            final Map<String, Integer> resName2Id = Maps.newHashMap();
            mResources.put(resType, resName2Id);
            for (Field field : resClass.getDeclaredFields()) {
                final int modifiers = field.getModifiers();
                if (Modifier.isStatic(modifiers)) {
                    // May not be final in library projects
                    final Class<?> type = field.getType();
                    try {
                        if (type.isArray() && type.getComponentType() == int.class) {
                            mStyleableValueToNameMap.put(new IntArrayWrapper((int[]) field.get(null)), field.getName());
                        } else if (type == int.class) {
                            final Integer value = (Integer) field.get(null);
                            mProjectResources.put(value, Pair.of(resType, field.getName()));
                            resName2Id.put(field.getName(), value);
                        } else {
                            mLog.error(null, "Unknown field type in R class: %1$s", type);
                        }
                    } catch (IllegalAccessException ignored) {
                        mLog.error(ignored, "Malformed R class: %1$s", PACKAGE_NAME + ".R");
                    }
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) ResourceType(com.android.resources.ResourceType) IntArrayWrapper(com.android.ide.common.resources.IntArrayWrapper)

Example 7 with IntArrayWrapper

use of com.android.ide.common.resources.IntArrayWrapper in project android_frameworks_base by crdroidandroid.

the class LayoutLibTestCallback method initResources.

public void initResources() throws ClassNotFoundException {
    Class<?> rClass = mModuleClassLoader.loadClass(PACKAGE_NAME + ".R");
    Class<?>[] nestedClasses = rClass.getDeclaredClasses();
    for (Class<?> resClass : nestedClasses) {
        final ResourceType resType = ResourceType.getEnum(resClass.getSimpleName());
        if (resType != null) {
            final Map<String, Integer> resName2Id = Maps.newHashMap();
            mResources.put(resType, resName2Id);
            for (Field field : resClass.getDeclaredFields()) {
                final int modifiers = field.getModifiers();
                if (Modifier.isStatic(modifiers)) {
                    // May not be final in library projects
                    final Class<?> type = field.getType();
                    try {
                        if (type.isArray() && type.getComponentType() == int.class) {
                            mStyleableValueToNameMap.put(new IntArrayWrapper((int[]) field.get(null)), field.getName());
                        } else if (type == int.class) {
                            final Integer value = (Integer) field.get(null);
                            mProjectResources.put(value, Pair.of(resType, field.getName()));
                            resName2Id.put(field.getName(), value);
                        } else {
                            mLog.error(null, "Unknown field type in R class: %1$s", type);
                        }
                    } catch (IllegalAccessException ignored) {
                        mLog.error(ignored, "Malformed R class: %1$s", PACKAGE_NAME + ".R");
                    }
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) ResourceType(com.android.resources.ResourceType) IntArrayWrapper(com.android.ide.common.resources.IntArrayWrapper)

Aggregations

IntArrayWrapper (com.android.ide.common.resources.IntArrayWrapper)7 ResourceType (com.android.resources.ResourceType)7 Field (java.lang.reflect.Field)6 TObjectIntHashMap (gnu.trove.TObjectIntHashMap)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 Pair (com.android.util.Pair)1 PsiClass (com.intellij.psi.PsiClass)1 TIntObjectHashMap (gnu.trove.TIntObjectHashMap)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1