use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class JavaCompilingVisitor method buildDescendants.
private static List<PsiClass> buildDescendants(String className, boolean includeSelf, OptimizingSearchHelper searchHelper, CompileContext context) {
if (!searchHelper.doOptimizing())
return Collections.emptyList();
final SearchScope scope = context.getOptions().getScope();
if (!(scope instanceof GlobalSearchScope))
return Collections.emptyList();
final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(context.getProject());
final PsiClass[] classes = cache.getClassesByName(className, (GlobalSearchScope) scope);
final List<PsiClass> results = new ArrayList<>();
final Processor<PsiClass> processor = aClass -> {
results.add(aClass);
return true;
};
for (PsiClass aClass : classes) {
ClassInheritorsSearch.search(aClass, scope, true).forEach(processor);
}
if (includeSelf) {
Collections.addAll(results, classes);
}
return results;
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class GenericsHighlightUtil method registerVariableParameterizedTypeFixes.
private static void registerVariableParameterizedTypeFixes(@Nullable HighlightInfo highlightInfo, @NotNull PsiVariable variable, @NotNull PsiReferenceParameterList parameterList, @NotNull JavaSdkVersion version) {
PsiType type = variable.getType();
if (!(type instanceof PsiClassType) || highlightInfo == null)
return;
if (DumbService.getInstance(variable.getProject()).isDumb())
return;
String shortName = ((PsiClassType) type).getClassName();
PsiManager manager = parameterList.getManager();
final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
PsiShortNamesCache shortNamesCache = PsiShortNamesCache.getInstance(parameterList.getProject());
PsiClass[] classes = shortNamesCache.getClassesByName(shortName, GlobalSearchScope.allScope(manager.getProject()));
PsiElementFactory factory = facade.getElementFactory();
for (PsiClass aClass : classes) {
if (checkReferenceTypeArgumentList(aClass, parameterList, PsiSubstitutor.EMPTY, false, version) == null) {
PsiType[] actualTypeParameters = parameterList.getTypeArguments();
PsiTypeParameter[] classTypeParameters = aClass.getTypeParameters();
Map<PsiTypeParameter, PsiType> map = new java.util.HashMap<>();
for (int j = 0; j < classTypeParameters.length; j++) {
PsiTypeParameter classTypeParameter = classTypeParameters[j];
PsiType actualTypeParameter = actualTypeParameters[j];
map.put(classTypeParameter, actualTypeParameter);
}
PsiSubstitutor substitutor = factory.createSubstitutor(map);
PsiType suggestedType = factory.createType(aClass, substitutor);
HighlightUtil.registerChangeVariableTypeFixes(variable, suggestedType, variable.getInitializer(), highlightInfo);
}
}
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class StaticImportConstantFix method getMembersToImport.
@NotNull
@Override
protected List<PsiField> getMembersToImport(boolean applicableOnly) {
final Project project = myRef.getProject();
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
final PsiJavaCodeReferenceElement element = myRef.getElement();
String name = element != null ? element.getReferenceName() : null;
if (name == null)
return Collections.emptyList();
if (element instanceof PsiExpression && PsiUtil.isAccessedForWriting((PsiExpression) element) || element.getParent() instanceof PsiTypeElement) {
return Collections.emptyList();
}
final StaticMembersProcessor<PsiField> processor = new StaticMembersProcessor<PsiField>(element) {
@Override
protected boolean isApplicable(PsiField field, PsiElement place) {
final PsiType expectedType = getExpectedType();
return expectedType == null || TypeConversionUtil.isAssignable(expectedType, field.getType());
}
};
cache.processFieldsWithName(name, processor, element.getResolveScope(), null);
return processor.getMembersToImport(applicableOnly);
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class CreateFromUsageUtils method handleObjectMethod.
private static boolean handleObjectMethod(Set<String> possibleClassNames, final JavaPsiFacade facade, final GlobalSearchScope searchScope, final boolean method, final String memberName, final boolean staticAccess, boolean addInheritors) {
final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(facade.getProject());
final boolean[] allClasses = { false };
ApplicationManager.getApplication().runReadAction(() -> {
final PsiClass objectClass = facade.findClass(CommonClassNames.JAVA_LANG_OBJECT, searchScope);
if (objectClass != null) {
if (method && objectClass.findMethodsByName(memberName, false).length > 0) {
allClasses[0] = true;
} else if (!method) {
final PsiField field = objectClass.findFieldByName(memberName, false);
if (hasCorrectModifiers(field, staticAccess)) {
allClasses[0] = true;
}
}
}
});
if (allClasses[0]) {
possibleClassNames.add(CommonClassNames.JAVA_LANG_OBJECT);
if (!addInheritors) {
return true;
}
final String[] strings = ApplicationManager.getApplication().runReadAction(new Computable<String[]>() {
@Override
public String[] compute() {
return cache.getAllClassNames();
}
});
for (final String className : strings) {
final PsiClass[] classes = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass[]>() {
@Override
public PsiClass[] compute() {
return cache.getClassesByName(className, searchScope);
}
});
for (final PsiClass aClass : classes) {
final String qname = getQualifiedName(aClass);
ContainerUtil.addIfNotNull(possibleClassNames, qname);
}
}
return true;
}
return false;
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class CreateFromUsageUtils method guessType.
@Nullable
static PsiType[] guessType(PsiExpression expression, final boolean allowVoidType) {
final PsiManager manager = expression.getManager();
final GlobalSearchScope resolveScope = expression.getResolveScope();
List<ExpectedTypeInfo[]> typesList = new ArrayList<>();
final List<String> expectedMethodNames = new ArrayList<>();
final List<String> expectedFieldNames = new ArrayList<>();
getExpectedInformation(expression, typesList, expectedMethodNames, expectedFieldNames);
if (typesList.size() == 1 && (!expectedFieldNames.isEmpty() || !expectedMethodNames.isEmpty())) {
ExpectedTypeInfo[] infos = typesList.get(0);
if (infos.length == 1 && infos[0].getKind() == ExpectedTypeInfo.TYPE_OR_SUBTYPE && infos[0].getType().equals(PsiType.getJavaLangObject(manager, resolveScope))) {
typesList.clear();
}
}
if (typesList.isEmpty()) {
final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(expression.getProject());
PsiElementFactory factory = facade.getElementFactory();
for (String fieldName : expectedFieldNames) {
PsiField[] fields = cache.getFieldsByNameIfNotMoreThan(fieldName, resolveScope, MAX_RAW_GUESSED_MEMBERS_COUNT);
addMemberInfo(fields, expression, typesList, factory);
}
for (String methodName : expectedMethodNames) {
PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(methodName, resolveScope, MAX_RAW_GUESSED_MEMBERS_COUNT);
addMemberInfo(methods, expression, typesList, factory);
}
}
ExpectedTypeInfo[] expectedTypes = ExpectedTypeUtil.intersect(typesList);
if (expectedTypes.length == 0 && !typesList.isEmpty()) {
List<ExpectedTypeInfo> union = new ArrayList<>();
for (ExpectedTypeInfo[] aTypesList : typesList) {
ContainerUtil.addAll(union, (ExpectedTypeInfo[]) aTypesList);
}
expectedTypes = union.toArray(new ExpectedTypeInfo[union.size()]);
}
if (expectedTypes.length == 0) {
return allowVoidType ? new PsiType[] { PsiType.VOID } : new PsiType[] { PsiType.getJavaLangObject(manager, resolveScope) };
} else {
//Double check to avoid expensive operations on PsiClassTypes
final Set<PsiType> typesSet = new HashSet<>();
PsiTypeVisitor<PsiType> visitor = new PsiTypeVisitor<PsiType>() {
@Override
@Nullable
public PsiType visitType(PsiType type) {
if (PsiType.NULL.equals(type) || PsiType.VOID.equals(type) && !allowVoidType) {
type = PsiType.getJavaLangObject(manager, resolveScope);
}
if (!typesSet.contains(type)) {
if (type instanceof PsiClassType && (!expectedFieldNames.isEmpty() || !expectedMethodNames.isEmpty())) {
PsiClass aClass = ((PsiClassType) type).resolve();
if (aClass != null) {
for (String fieldName : expectedFieldNames) {
if (aClass.findFieldByName(fieldName, true) == null)
return null;
}
for (String methodName : expectedMethodNames) {
PsiMethod[] methods = aClass.findMethodsByName(methodName, true);
if (methods.length == 0)
return null;
}
}
}
typesSet.add(type);
return type;
}
return null;
}
@Override
public PsiType visitCapturedWildcardType(PsiCapturedWildcardType capturedWildcardType) {
return capturedWildcardType.getUpperBound().accept(this);
}
};
PsiType[] types = ExpectedTypesProvider.processExpectedTypes(expectedTypes, visitor, manager.getProject());
if (types.length == 0) {
return allowVoidType ? new PsiType[] { PsiType.VOID } : new PsiType[] { PsiType.getJavaLangObject(manager, resolveScope) };
}
return types;
}
}
Aggregations