Search in sources :

Example 1 with IntArrayWrapper

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

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 2 with IntArrayWrapper

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

the class ViewLoader method loadAndParseRClass.

@VisibleForTesting
void loadAndParseRClass(@NotNull String className) throws ClassNotFoundException, InconvertibleClassError {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("loadAndParseRClass(%s)", anonymizeClassName(className)));
    }
    Class<?> aClass = myLoadedClasses.get(className);
    AppResourceRepository appResources = AppResourceRepository.getAppResources(myModule, true);
    if (aClass == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("  The R class is not loaded.");
        }
        final ModuleClassLoader moduleClassLoader = getModuleClassLoader();
        final boolean isClassLoaded = moduleClassLoader.isClassLoaded(className);
        aClass = moduleClassLoader.loadClass(className);
        if (!isClassLoaded && aClass != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("  Class found in module %s, first time load.", anonymize(myModule)));
            }
            // This is the first time we've found the resources. The dynamic R classes generated for aar libraries are now stale and must be
            // regenerated. Clear the ModuleClassLoader and reload the R class.
            myLoadedClasses.clear();
            ModuleClassLoader.clearCache(myModule);
            myModuleClassLoader = null;
            aClass = getModuleClassLoader().loadClass(className);
            if (appResources != null) {
                appResources.resetDynamicIds(true);
            }
        } else {
            if (LOG.isDebugEnabled()) {
                if (isClassLoaded) {
                    LOG.debug(String.format("  Class already loaded in module %s.", anonymize(myModule)));
                }
            }
        }
        if (aClass != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("  Class loaded");
            }
            assert myLogger != null;
            myLoadedClasses.put(className, aClass);
            myLogger.setHasLoadedClasses(true);
        }
    }
    if (aClass != null) {
        final Map<ResourceType, TObjectIntHashMap<String>> res2id = new EnumMap<>(ResourceType.class);
        final TIntObjectHashMap<Pair<ResourceType, String>> id2res = new TIntObjectHashMap<>();
        final Map<IntArrayWrapper, String> styleableId2res = new HashMap<>();
        if (parseClass(aClass, id2res, styleableId2res, res2id)) {
            if (appResources != null) {
                appResources.setCompiledResources(id2res, styleableId2res, res2id);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("END loadAndParseRClass(%s)", anonymizeClassName(className)));
    }
}
Also used : TObjectIntHashMap(gnu.trove.TObjectIntHashMap) HashMap(java.util.HashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceType(com.android.resources.ResourceType) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) IntArrayWrapper(com.android.ide.common.resources.IntArrayWrapper) EnumMap(java.util.EnumMap) Pair(com.android.util.Pair) VisibleForTesting(com.android.annotations.VisibleForTesting)

Example 3 with IntArrayWrapper

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

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 4 with IntArrayWrapper

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

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 5 with IntArrayWrapper

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

the class ViewLoader method parseClass.

private static boolean parseClass(@NotNull Class<?> rClass, @NotNull TIntObjectHashMap<Pair<ResourceType, String>> id2res, @NotNull Map<IntArrayWrapper, String> styleableId2Res, @NotNull Map<ResourceType, TObjectIntHashMap<String>> res2id) throws ClassNotFoundException {
    try {
        final Class<?>[] nestedClasses;
        try {
            nestedClasses = rClass.getDeclaredClasses();
        } catch (LinkageError e) {
            final Throwable cause = e.getCause();
            LOG.debug(e);
            if (cause instanceof ClassNotFoundException) {
                throw (ClassNotFoundException) cause;
            }
            throw e;
        }
        for (Class<?> resClass : nestedClasses) {
            final ResourceType resType = ResourceType.getEnum(resClass.getSimpleName());
            if (resType == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("  '%s' is not a valid resource type", anonymizeClassName(resClass.getSimpleName())));
                }
                continue;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("  Defining resource type '%s'", anonymizeClassName(resClass.getSimpleName())));
            }
            final TObjectIntHashMap<String> resName2Id = new TObjectIntHashMap<>();
            res2id.put(resType, resName2Id);
            for (Field field : resClass.getDeclaredFields()) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    // May not be final in library projects
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' field is not static, skipping", field.getName()));
                    }
                    continue;
                }
                final Class<?> type = field.getType();
                if (type.isArray() && type.getComponentType() == int.class) {
                    styleableId2Res.put(new IntArrayWrapper((int[]) field.get(null)), field.getName());
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' defined as int[]", field.getName()));
                    }
                } else if (type == int.class) {
                    final Integer value = (Integer) field.get(null);
                    id2res.put(value, Pair.of(resType, field.getName()));
                    resName2Id.put(field.getName(), value);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' defined as int", field.getName()));
                    }
                } else {
                    LOG.error("Unknown field type in R class: " + type);
                }
            }
        }
    } catch (IllegalAccessException e) {
        LOG.info(e);
        return false;
    }
    return true;
}
Also used : ResourceType(com.android.resources.ResourceType) Field(java.lang.reflect.Field) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) PsiClass(com.intellij.psi.PsiClass) 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