use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class TreeJavaClassChooserDialog method getClassesByName.
@NotNull
protected List<PsiClass> getClassesByName(final String name, final boolean checkBoxState, final String pattern, final GlobalSearchScope searchScope) {
final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(getProject());
PsiClass[] classes = cache.getClassesByName(name, checkBoxState ? searchScope : GlobalSearchScope.projectScope(getProject()).intersectWith(searchScope));
return ContainerUtil.newArrayList(classes);
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class StaticImportMethodFix method getMembersToImport.
@NotNull
@Override
protected List<PsiMethod> getMembersToImport(boolean applicableOnly) {
final Project project = myMethodCall.getProject();
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
final PsiMethodCallExpression element = myMethodCall.getElement();
PsiReferenceExpression reference = element == null ? null : element.getMethodExpression();
String name = reference == null ? null : reference.getReferenceName();
if (name == null)
return Collections.emptyList();
final StaticMembersProcessor<PsiMethod> processor = new MyStaticMethodProcessor(element);
cache.processMethodsWithName(name, element.getResolveScope(), processor);
return processor.getMembersToImport(applicableOnly);
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class CreateFromUsageUtils method addClassesWithMember.
public static void addClassesWithMember(final String memberName, final PsiFile file, final Set<String> possibleClassNames, final boolean method, final boolean staticAccess, final boolean addObjectInheritors) {
final Project project = file.getProject();
final Module moduleForFile = ModuleUtilCore.findModuleForPsiElement(file);
if (moduleForFile == null)
return;
final GlobalSearchScope searchScope = ApplicationManager.getApplication().runReadAction(new Computable<GlobalSearchScope>() {
@Override
public GlobalSearchScope compute() {
return file.getResolveScope();
}
});
GlobalSearchScope descendantsSearchScope = GlobalSearchScope.moduleWithDependenciesScope(moduleForFile);
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
if (handleObjectMethod(possibleClassNames, facade, searchScope, method, memberName, staticAccess, addObjectInheritors)) {
return;
}
final PsiMember[] members = ApplicationManager.getApplication().runReadAction(new Computable<PsiMember[]>() {
@Override
public PsiMember[] compute() {
return method ? cache.getMethodsByName(memberName, searchScope) : cache.getFieldsByName(memberName, searchScope);
}
});
for (int i = 0; i < members.length; ++i) {
final PsiMember member = members[i];
if (hasCorrectModifiers(member, staticAccess)) {
final PsiClass containingClass = member.getContainingClass();
if (containingClass != null) {
final String qName = getQualifiedName(containingClass);
if (qName == null)
continue;
ClassInheritorsSearch.search(containingClass, descendantsSearchScope, true, true, false).forEach(psiClass -> {
ContainerUtil.addIfNotNull(possibleClassNames, getQualifiedName(psiClass));
return true;
});
possibleClassNames.add(qName);
}
}
members[i] = null;
}
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class HighlightStressTest method testRandomEditingForUnused.
public void testRandomEditingForUnused() throws Exception {
configureFromFileText("Stress.java", "class X {<caret>}");
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(getProject());
String[] names = cache.getAllClassNames();
final StringBuilder imports = new StringBuilder();
final StringBuilder usages = new StringBuilder();
int v = 0;
outer: for (String name : names) {
PsiClass[] classes = cache.getClassesByName(name, GlobalSearchScope.allScope(getProject()));
if (classes.length == 0)
continue;
PsiClass aClass = classes[0];
if (!aClass.hasModifierProperty(PsiModifier.PUBLIC))
continue;
if (aClass.getSuperClass() == null)
continue;
PsiClassType[] superTypes = aClass.getSuperTypes();
if (superTypes.length == 0)
continue;
for (PsiClassType superType : superTypes) {
PsiClass superClass = superType.resolve();
if (superClass == null || !superClass.hasModifierProperty(PsiModifier.PUBLIC))
continue outer;
}
String qualifiedName = aClass.getQualifiedName();
// java.lang.invoke.MethodHandle has weird access attributes in recent rt.jar which causes spurious highlighting errors
if (qualifiedName.startsWith("java.lang.invoke"))
continue;
if (!accessible(aClass, new THashSet<>()))
continue;
imports.append("import " + qualifiedName + ";\n");
usages.append("/**/ " + aClass.getName() + " var" + v + " = null; var" + v + ".toString();\n");
v++;
if (v > 100)
break;
}
final String text = imports + "\n class X {{\n" + usages + "}}";
WriteCommandAction.runWriteCommandAction(null, () -> getEditor().getDocument().setText(text));
List<HighlightInfo> errors = DaemonAnalyzerTestCase.filter(doHighlighting(), HighlightSeverity.WARNING);
assertEmpty(text, errors);
Random random = new Random();
int unused = 0;
for (int i = 0; i < 100; i++) {
String s = myFile.getText();
int offset;
while (true) {
offset = random.nextInt(s.length());
if (CharArrayUtil.regionMatches(s, offset, "/**/") || CharArrayUtil.regionMatches(s, offset, "//"))
break;
}
char next = offset < s.length() - 1 ? s.charAt(offset + 1) : 0;
if (next == '/') {
myEditor.getCaretModel().moveToOffset(offset + 1);
type("**");
unused--;
} else if (next == '*') {
myEditor.getCaretModel().moveToOffset(offset + 1);
delete();
delete();
unused++;
} else {
continue;
}
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
getFile().accept(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(PsiElement element) {
assertTrue(element.toString(), element.isValid());
super.visitElement(element);
}
});
//System.out.println("i = " + i + " " + next + " at "+offset);
List<HighlightInfo> infos = doHighlighting();
errors = DaemonAnalyzerTestCase.filter(infos, HighlightSeverity.ERROR);
assertEmpty(errors);
List<HighlightInfo> warns = DaemonAnalyzerTestCase.filter(infos, HighlightSeverity.WARNING);
if (unused != warns.size()) {
assertEquals(warns.toString(), unused, warns.size());
}
}
FileEditorManagerEx.getInstanceEx(getProject()).closeAllFiles();
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class JavaTestFinder method findClassesForTest.
@NotNull
public Collection<PsiElement> findClassesForTest(@NotNull PsiElement element) {
PsiClass klass = findSourceElement(element);
if (klass == null)
return Collections.emptySet();
GlobalSearchScope scope = getSearchScope(element, true);
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(element.getProject());
List<Pair<? extends PsiNamedElement, Integer>> classesWithWeights = new ArrayList<>();
for (Pair<String, Integer> eachNameWithWeight : TestFinderHelper.collectPossibleClassNamesWithWeights(klass.getName())) {
for (PsiClass eachClass : cache.getClassesByName(eachNameWithWeight.first, scope)) {
if (isTestSubjectClass(eachClass)) {
classesWithWeights.add(Pair.create(eachClass, eachNameWithWeight.second));
}
}
}
return TestFinderHelper.getSortedElements(classesWithWeights, false);
}
Aggregations