Search in sources :

Example 6 with TObjectIntHashMap

use of gnu.trove.TObjectIntHashMap in project intellij-community by JetBrains.

the class IElementTypeTest method testCount.

// load all parser definitions, instantiate all lexers & parsers to initialize all IElementType constants
@SuppressWarnings("UnusedDeclaration")
public void testCount() throws Exception {
    int count = IElementType.getAllocatedTypesCount();
    System.out.println("Preloaded: " + count + " element types");
    LanguageExtensionPoint[] extensions = Extensions.getExtensions(new ExtensionPointName<LanguageExtensionPoint>("com.intellij.lang.parserDefinition"));
    System.out.println("ParserDefinitions: " + extensions.length);
    THashMap<Language, String> languageMap = new THashMap<>();
    languageMap.put(Language.ANY, "platform");
    final TObjectIntHashMap<String> map = new TObjectIntHashMap<>();
    for (LanguageExtensionPoint e : extensions) {
        String key = e.getPluginDescriptor().getPluginId().getIdString();
        int curCount = IElementType.getAllocatedTypesCount();
        ParserDefinition definition = (ParserDefinition) e.getInstance();
        IFileElementType type = definition.getFileNodeType();
        Language language = type.getLanguage();
        languageMap.put(language, key);
        if (language.getBaseLanguage() != null && !languageMap.containsKey(language.getBaseLanguage())) {
            languageMap.put(language.getBaseLanguage(), key);
        }
        try {
            Lexer lexer = definition.createLexer(getProject());
            PsiParser parser = definition.createParser(getProject());
        } catch (UnsupportedOperationException e1) {
        }
    // language-based calculation: per-class-loading stuff commented
    //int diff = IElementType.getAllocatedTypesCount() - curCount;
    //map.put(key, map.get(key) + diff);
    }
    // language-based calculation
    count = IElementType.getAllocatedTypesCount();
    for (short i = 0; i < count; i++) {
        IElementType type = IElementType.find(i);
        Language language = type.getLanguage();
        String key = null;
        for (Language cur = language; cur != null && key == null; cur = cur.getBaseLanguage()) {
            key = languageMap.get(cur);
        }
        key = StringUtil.notNullize(key, "unknown");
        map.put(key, map.get(key) + 1);
    //if (key.equals("unknown")) System.out.println(type +"   " + language);
    }
    System.out.println("Total: " + IElementType.getAllocatedTypesCount() + " element types");
    // Show per-plugin statistics
    Object[] keys = map.keys();
    Arrays.sort(keys, (o1, o2) -> map.get((String) o2) - map.get((String) o1));
    int sum = 0;
    for (Object key : keys) {
        int value = map.get((String) key);
        if (value == 0)
            continue;
        sum += value;
        System.out.println("  " + key + ": " + value);
    }
    // leave some index-space for plugin developers
    assertTrue(IElementType.getAllocatedTypesCount() < 10000);
    assertEquals(IElementType.getAllocatedTypesCount(), sum);
// output on 11.05.2012
//   Preloaded: 3485 types
//   95 definitions
//   Total: 7694 types
// 14.05.2015:
// Preloaded: 4106 element types
// ParserDefinitions: 128
// Total: 8864 element types
// 19.04.2016:
// Preloaded: 4397 element types
// ParserDefinitions: 135
// Total: 9231 element types
}
Also used : Lexer(com.intellij.lexer.Lexer) THashMap(gnu.trove.THashMap) TObjectIntHashMap(gnu.trove.TObjectIntHashMap)

Example 7 with TObjectIntHashMap

use of gnu.trove.TObjectIntHashMap in project android by JetBrains.

the class ResourceClassGenerator method generate.

/**
   * @param fqcn Fully qualified class name (as accepted by ClassLoader, or as returned by Class.getName())
   */
@Nullable
public byte[] generate(String fqcn) {
    String className = fqcn.replace('.', '/');
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("generate(%s)", anonymizeClassName(className)));
    }
    // Don't compute MAXS and FRAMES.
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, className, null, Type.getInternalName(Object.class), null);
    int index = className.lastIndexOf('$');
    if (index != -1) {
        String typeName = className.substring(index + 1);
        ResourceType type = ResourceType.getEnum(typeName);
        if (type == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("  type '%s' doesn't exist", typeName));
            }
            return null;
        }
        cw.visitInnerClass(className, className.substring(0, index), typeName, ACC_PUBLIC + ACC_FINAL + ACC_STATIC);
        if (myCache == null) {
            myCache = Maps.newHashMap();
        }
        if (type == ResourceType.STYLEABLE) {
            if (myStyleableCache == null) {
                TObjectIntHashMap<String> styleableIntCache = new TObjectIntHashMap<String>();
                myCache.put(type, styleableIntCache);
                myStyleableCache = Maps.newHashMap();
                generateStyleable(cw, styleableIntCache, className);
            } else {
                TObjectIntHashMap<String> styleableIntCache = myCache.get(type);
                assert styleableIntCache != null;
                generateFields(cw, styleableIntCache);
                generateIntArrayFromCache(cw, className, myStyleableCache);
            }
        } else {
            TObjectIntHashMap<String> typeCache = myCache.get(type);
            if (typeCache == null) {
                typeCache = new TObjectIntHashMap<String>();
                myCache.put(type, typeCache);
                generateValuesForType(cw, type, typeCache);
            } else {
                generateFields(cw, typeCache);
            }
        }
    } else {
        // Default R class.
        boolean styleableAdded = false;
        for (ResourceType t : myAppResources.getAvailableResourceTypes()) {
            // getAvailableResourceTypes() sometimes returns both styleable and declare styleable. Make sure that we only create one subclass.
            if (t == ResourceType.DECLARE_STYLEABLE) {
                t = ResourceType.STYLEABLE;
            }
            if (t == ResourceType.STYLEABLE) {
                if (styleableAdded) {
                    continue;
                } else {
                    styleableAdded = true;
                }
            }
            cw.visitInnerClass(className + "$" + t.getName(), className, t.getName(), ACC_PUBLIC + ACC_FINAL + ACC_STATIC);
        }
    }
    generateConstructor(cw);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : TObjectIntHashMap(gnu.trove.TObjectIntHashMap) ResourceType(com.android.resources.ResourceType) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with TObjectIntHashMap

use of gnu.trove.TObjectIntHashMap 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 9 with TObjectIntHashMap

use of gnu.trove.TObjectIntHashMap in project intellij-community by JetBrains.

the class ExternalSystemTasksTreeModel method ensureSubProjectsStructure.

public void ensureSubProjectsStructure(@NotNull ExternalProjectPojo topLevelProject, @NotNull Collection<ExternalProjectPojo> subProjects) {
    ExternalSystemNode<ExternalProjectPojo> topLevelProjectNode = ensureProjectNodeExists(topLevelProject);
    Map<String, ExternalProjectPojo> /*config path*/
    toAdd = ContainerUtilRt.newHashMap();
    for (ExternalProjectPojo subProject : subProjects) {
        toAdd.put(subProject.getPath(), subProject);
    }
    toAdd.remove(topLevelProject.getPath());
    final TObjectIntHashMap<Object> taskWeights = new TObjectIntHashMap<>();
    for (int i = 0; i < topLevelProjectNode.getChildCount(); i++) {
        ExternalSystemNode<?> child = topLevelProjectNode.getChildAt(i);
        Object childElement = child.getDescriptor().getElement();
        if (childElement instanceof ExternalTaskExecutionInfo) {
            taskWeights.put(childElement, subProjects.size() + i);
            continue;
        }
        if (toAdd.remove(((ExternalProjectPojo) childElement).getPath()) == null) {
            removeNodeFromParent(child);
            //noinspection AssignmentToForLoopParameter
            i--;
        }
    }
    if (!toAdd.isEmpty()) {
        for (Map.Entry<String, ExternalProjectPojo> entry : toAdd.entrySet()) {
            ExternalProjectPojo element = new ExternalProjectPojo(entry.getValue().getName(), entry.getValue().getPath());
            insertNodeInto(new ExternalSystemNode<>(descriptor(element, myUiAware.getProjectIcon())), topLevelProjectNode);
        }
    }
}
Also used : ExternalTaskExecutionInfo(com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) ExternalProjectPojo(com.intellij.openapi.externalSystem.model.project.ExternalProjectPojo) TObjectIntHashMap(gnu.trove.TObjectIntHashMap)

Example 10 with TObjectIntHashMap

use of gnu.trove.TObjectIntHashMap in project intellij-community by JetBrains.

the class DuplocatorHashCallback method getInfo.

public DupInfo getInfo() {
    final TObjectIntHashMap<PsiFragment[]> duplicateList = new TObjectIntHashMap<>();
    myDuplicates.forEachEntry(new TIntObjectProcedure<List<List<PsiFragment>>>() {

        public boolean execute(final int hash, final List<List<PsiFragment>> listList) {
            for (List<PsiFragment> list : listList) {
                final int len = list.size();
                if (len > 1) {
                    PsiFragment[] filtered = new PsiFragment[len];
                    int idx = 0;
                    for (final PsiFragment fragment : list) {
                        fragment.markDuplicate();
                        filtered[idx++] = fragment;
                    }
                    duplicateList.put(filtered, hash);
                }
            }
            return true;
        }
    });
    myDuplicates = null;
    for (TObjectIntIterator<PsiFragment[]> dups = duplicateList.iterator(); dups.hasNext(); ) {
        dups.advance();
        PsiFragment[] fragments = dups.key();
        LOG.assertTrue(fragments.length > 1);
        boolean nested = false;
        for (PsiFragment fragment : fragments) {
            if (fragment.isNested()) {
                nested = true;
                break;
            }
        }
        if (nested) {
            dups.remove();
        }
    }
    final Object[] duplicates = duplicateList.keys();
    Arrays.sort(duplicates, (x, y) -> ((PsiFragment[]) y)[0].getCost() - ((PsiFragment[]) x)[0].getCost());
    return new DupInfo() {

        private final TIntObjectHashMap<GroupNodeDescription> myPattern2Description = new TIntObjectHashMap<>();

        public int getPatterns() {
            return duplicates.length;
        }

        public int getPatternCost(int number) {
            return ((PsiFragment[]) duplicates[number])[0].getCost();
        }

        public int getPatternDensity(int number) {
            return ((PsiFragment[]) duplicates[number]).length;
        }

        public PsiFragment[] getFragmentOccurences(int pattern) {
            return (PsiFragment[]) duplicates[pattern];
        }

        public UsageInfo[] getUsageOccurences(int pattern) {
            PsiFragment[] occs = getFragmentOccurences(pattern);
            UsageInfo[] infos = new UsageInfo[occs.length];
            for (int i = 0; i < infos.length; i++) {
                infos[i] = occs[i].getUsageInfo();
            }
            return infos;
        }

        public int getFileCount(final int pattern) {
            if (myPattern2Description.containsKey(pattern)) {
                return myPattern2Description.get(pattern).getFilesCount();
            }
            return cacheGroupNodeDescription(pattern).getFilesCount();
        }

        private GroupNodeDescription cacheGroupNodeDescription(final int pattern) {
            final Set<PsiFile> files = new HashSet<>();
            final PsiFragment[] occurencies = getFragmentOccurences(pattern);
            for (PsiFragment occurency : occurencies) {
                final PsiFile file = occurency.getFile();
                if (file != null) {
                    files.add(file);
                }
            }
            final int fileCount = files.size();
            final PsiFile psiFile = occurencies[0].getFile();
            DuplicatesProfile profile = DuplicatesProfileCache.getProfile(this, pattern);
            String comment = profile != null ? profile.getComment(this, pattern) : "";
            final GroupNodeDescription description = new GroupNodeDescription(fileCount, psiFile != null ? psiFile.getName() : "unknown", comment);
            myPattern2Description.put(pattern, description);
            return description;
        }

        @Nullable
        public String getTitle(int pattern) {
            if (getFileCount(pattern) == 1) {
                if (myPattern2Description.containsKey(pattern)) {
                    return myPattern2Description.get(pattern).getTitle();
                }
                return cacheGroupNodeDescription(pattern).getTitle();
            }
            return null;
        }

        @Nullable
        public String getComment(int pattern) {
            if (getFileCount(pattern) == 1) {
                if (myPattern2Description.containsKey(pattern)) {
                    return myPattern2Description.get(pattern).getComment();
                }
                return cacheGroupNodeDescription(pattern).getComment();
            }
            return null;
        }

        public int getHash(final int i) {
            return duplicateList.get((PsiFragment[]) duplicates[i]);
        }
    };
}
Also used : PsiFragment(com.intellij.dupLocator.util.PsiFragment) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) PsiFile(com.intellij.psi.PsiFile) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

TObjectIntHashMap (gnu.trove.TObjectIntHashMap)18 TIntObjectHashMap (gnu.trove.TIntObjectHashMap)4 NotNull (org.jetbrains.annotations.NotNull)4 ResourceType (com.android.resources.ResourceType)3 Pair (com.intellij.openapi.util.Pair)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)3 IntArrayWrapper (com.android.ide.common.resources.IntArrayWrapper)2 PsiClass (com.intellij.psi.PsiClass)2 THashMap (gnu.trove.THashMap)2 THashSet (gnu.trove.THashSet)2 Set (java.util.Set)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 Pair (com.android.util.Pair)1 QuickFixBundle (com.intellij.codeInsight.daemon.QuickFixBundle)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 OfflineProblemDescriptor (com.intellij.codeInspection.offline.OfflineProblemDescriptor)1 PsiFragment (com.intellij.dupLocator.util.PsiFragment)1