@NotNull @Override public PsiClass[] findClassByShortName(@NotNull String name, @NotNull final GlobalSearchScope scope) { PsiClass[] allClasses = getCachedClassesByName(name, scope); if (allClasses.length == 0) return allClasses; if (allClasses.length == 1) { return PsiSearchScopeUtil.isInScope(scope, allClasses[0]) ? allClasses : PsiClass.EMPTY_ARRAY; } PsiClass[] array = ContainerUtil.findAllAsArray(allClasses, new Condition<PsiClass>() { @Override public boolean value(PsiClass aClass) { return PsiSearchScopeUtil.isInScope(scope, aClass); } }); Arrays.sort(array, new Comparator<PsiClass>() { @Override public int compare(PsiClass o1, PsiClass o2) { VirtualFile file1 = o1.getContainingFile().getVirtualFile(); VirtualFile file2 = o2.getContainingFile().getVirtualFile(); if (file1 == null) return file2 == null ? 0 : -1; if (file2 == null) return 1; return scope.compare(file2, file1); } }); return array; }
@NotNull private List<PsiClassType.ClassResolveResult> calcImmediateSupersWithCapturing() { List<PsiClassType.ClassResolveResult> list; list = ContainerUtil.newArrayList(); for(PsiClassType type : myPlaceClass.getSuperTypes()) { PsiClassType corrected = PsiClassImplUtil.correctType(type, myResolveScope); if(corrected == null) { continue; } PsiClassType.ClassResolveResult result = ((PsiClassType) PsiUtil.captureToplevelWildcards(corrected, myPlaceClass)).resolveGenerics(); PsiClass superClass = result.getElement(); if(superClass == null || !PsiSearchScopeUtil.isInScope(myResolveScope, superClass)) { continue; } list.add(result); } return list; }
private static boolean hasSuperMethodCandidates(final PsiMethod method, final GlobalSearchScope scope, final Condition<PsiMember> qualifiedMatcher) { if (method.hasModifierProperty(PsiModifier.PRIVATE) || method.hasModifierProperty(PsiModifier.STATIC)) return false; final PsiClass containingClass = method.getContainingClass(); if (containingClass == null) return false; final int parametersCount = method.getParameterList().getParametersCount(); return !InheritanceUtil.processSupers(containingClass, false, new Processor<PsiClass>() { @Override public boolean process(PsiClass superClass) { if (PsiSearchScopeUtil.isInScope(scope, superClass)) { for (PsiMethod candidate : superClass.findMethodsByName(method.getName(), false)) { if (parametersCount == candidate.getParameterList().getParametersCount() && !candidate.hasModifierProperty(PsiModifier.PRIVATE) && !candidate.hasModifierProperty(PsiModifier.STATIC) && qualifiedMatcher.value(candidate)) { return false; } } } return true; } }); }
private static boolean hasSuperMethod(final PsiMethod method, final GlobalSearchScope scope, final Condition<PsiMember> qualifiedMatcher) { if (!hasSuperMethodCandidates(method, scope, qualifiedMatcher)) { return false; } for (HierarchicalMethodSignature signature : method.getHierarchicalMethodSignature().getSuperSignatures()) { PsiMethod superMethod = signature.getMethod(); if (PsiSearchScopeUtil.isInScope(scope, superMethod) && qualifiedMatcher.value(superMethod)) { return true; } } return false; }
@Override public TypeConversionDescriptorBase findConversion(PsiType from, PsiType to, PsiMember member, PsiExpression context, TypeMigrationLabeler labeler) { final PsiMethodCallExpression callExpression = PsiTreeUtil.getParentOfType(context, PsiMethodCallExpression.class, false); if (callExpression != null) { final PsiMethod resolved = callExpression.resolveMethod(); if (resolved != null) { final SearchScope searchScope = labeler.getRules().getSearchScope(); if (!PsiSearchScopeUtil.isInScope(searchScope, resolved)) { return null; } } } final PsiField field = PsiTreeUtil.getParentOfType(context, PsiField.class); if (field != null && !myEnumConstants.contains(field) && field.hasModifierProperty(PsiModifier.STATIC) && field.hasModifierProperty(PsiModifier.FINAL) && field.hasInitializer()) { return null; } final PsiClass toClass = PsiUtil.resolveClassInType(to); if (toClass != null && toClass.isEnum()) { final PsiMethod[] constructors = toClass.getConstructors(); if (constructors.length == 1) { final PsiMethod constructor = constructors[0]; final PsiParameter[] parameters = constructor.getParameterList().getParameters(); if (parameters.length == 1) { if (TypeConversionUtil.isAssignable(parameters[0].getType(), from)) { return new TypeConversionDescriptorBase(); } } } } return null; }
private static List<LanguageDefinition> collectLanguageDefinitions(final ConvertContext context) { final PsiClass languageClass = DomJavaUtil.findClass(Language.class.getName(), context.getInvocationElement()); if (languageClass == null) { return Collections.emptyList(); } final Project project = context.getProject(); final GlobalSearchScope projectProductionScope = GlobalSearchScopesCore.projectProductionScope(project); final Collection<PsiClass> allLanguages = CachedValuesManager.getCachedValue(languageClass, new CachedValueProvider<Collection<PsiClass>>() { @Nullable @Override public Result<Collection<PsiClass>> compute() { GlobalSearchScope allScope = projectProductionScope.union(ProjectScope.getLibrariesScope(project)); return Result.create(ClassInheritorsSearch.search(languageClass, allScope, true).findAll(), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); } }); ; final List<LanguageDefinition> libraryDefinitions = collectLibraryLanguages(context, allLanguages); final Collection<PsiClass> projectLanguages = ContainerUtil.filter(allLanguages, new Condition<PsiClass>() { @Override public boolean value(PsiClass aClass) { return PsiSearchScopeUtil.isInScope(projectProductionScope, aClass); } }); final List<LanguageDefinition> projectDefinitions = collectProjectLanguages(projectLanguages, libraryDefinitions); final List<LanguageDefinition> all = new ArrayList<LanguageDefinition>(libraryDefinitions); all.addAll(projectDefinitions); return all; }
@Override public TypeConversionDescriptorBase findConversion(PsiType from, PsiType to, PsiMember member, PsiExpression context, TypeMigrationLabeler labeler) { final PsiMethodCallExpression callExpression = PsiTreeUtil.getParentOfType(context, PsiMethodCallExpression.class, false); if(callExpression != null) { final PsiMethod resolved = callExpression.resolveMethod(); if(resolved != null) { final SearchScope searchScope = labeler.getRules().getSearchScope(); if(!PsiSearchScopeUtil.isInScope(searchScope, resolved)) { return null; } } } final PsiField field = PsiTreeUtil.getParentOfType(context, PsiField.class); if(field != null && !myEnumConstants.contains(field) && field.hasModifierProperty(PsiModifier.STATIC) && field.hasModifierProperty(PsiModifier.FINAL) && field.hasInitializer()) { return null; } final PsiClass toClass = PsiUtil.resolveClassInType(to); if(toClass != null && toClass.isEnum()) { final PsiMethod[] constructors = toClass.getConstructors(); if(constructors.length == 1) { final PsiMethod constructor = constructors[0]; final PsiParameter[] parameters = constructor.getParameterList().getParameters(); if(parameters.length == 1) { if(TypeConversionUtil.isAssignable(parameters[0].getType(), from)) { return new TypeConversionDescriptorBase(); } } } } return null; }
private static boolean canBeRoot(@Nullable PsiElement element, @NotNull SearchScope migrationScope) { if(element == null) { return false; } return element.isValid() && element.isPhysical() && PsiSearchScopeUtil.isInScope(migrationScope, element); }
private static void processInheritors(@NotNull final Processor<PsiClass> consumer, @NotNull final PsiClass baseClass, @NotNull final SearchScope searchScope, @NotNull final ClassInheritorsSearch.SearchParameters parameters) { if (baseClass instanceof PsiAnonymousClass || isFinal(baseClass)) return; Project project = PsiUtilCore.getProjectInReadAction(baseClass); if (isJavaLangObject(baseClass)) { AllClassesSearch.search(searchScope, project, parameters.getNameCondition()).forEach(new Processor<PsiClass>() { @Override public boolean process(final PsiClass aClass) { ProgressManager.checkCanceled(); return isJavaLangObject(aClass) || consumer.process(aClass); } }); return; } final Ref<PsiClass> currentBase = Ref.create(null); final Stack<PsiAnchor> stack = new Stack<PsiAnchor>(); final Set<PsiAnchor> processed = ContainerUtil.newTroveSet(); final Processor<PsiClass> processor = new ReadActionProcessor<PsiClass>() { @Override public boolean processInReadAction(PsiClass candidate) { ProgressManager.checkCanceled(); if (parameters.isCheckInheritance() || parameters.isCheckDeep() && !(candidate instanceof PsiAnonymousClass)) { if (!candidate.isInheritor(currentBase.get(), false)) { return true; } } if (PsiSearchScopeUtil.isInScope(searchScope, candidate)) { if (candidate instanceof PsiAnonymousClass) { return consumer.process(candidate); } final String name = candidate.getName(); if (name != null && parameters.getNameCondition().value(name) && !consumer.process(candidate)) { return false; } } if (parameters.isCheckDeep() && !(candidate instanceof PsiAnonymousClass) && !isFinal(candidate)) { stack.push(PsiAnchor.create(candidate)); } return true; } }; ApplicationManager.getApplication().runReadAction(new Runnable() { @Override public void run() { stack.push(PsiAnchor.create(baseClass)); } }); final GlobalSearchScope projectScope = GlobalSearchScope.allScope(project); while (!stack.isEmpty()) { ProgressManager.checkCanceled(); final PsiAnchor anchor = stack.pop(); if (!processed.add(anchor)) continue; PsiClass psiClass = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() { @Override public PsiClass compute() { return (PsiClass)anchor.retrieve(); } }); if (psiClass == null) continue; currentBase.set(psiClass); if (!DirectClassInheritorsSearch.search(psiClass, projectScope, parameters.isIncludeAnonymous(), false).forEach(processor)) return; } }
private boolean processSuperOrThis(@NotNull PsiClass inheritor, @NotNull PsiMethod constructor, final boolean constructorCanBeCalledImplicitly, @NotNull SearchScope searchScope, @NotNull Project project, final boolean isStrictSignatureSearch, @NotNull String superOrThisKeyword, @NotNull Processor<PsiReference> processor) { PsiMethod[] constructors = inheritor.getConstructors(); if (constructors.length == 0 && constructorCanBeCalledImplicitly) { if (!processImplicitConstructorCall(inheritor, processor, constructor, project, inheritor)) return false; } for (PsiMethod method : constructors) { PsiCodeBlock body = method.getBody(); if (body == null || method == constructor && isStrictSignatureSearch) { continue; } PsiStatement[] statements = body.getStatements(); if (statements.length != 0) { PsiStatement statement = statements[0]; if (statement instanceof PsiExpressionStatement) { PsiExpression expr = ((PsiExpressionStatement)statement).getExpression(); if (expr instanceof PsiMethodCallExpression) { PsiReferenceExpression refExpr = ((PsiMethodCallExpression)expr).getMethodExpression(); if (PsiSearchScopeUtil.isInScope(searchScope, refExpr)) { if (refExpr.textMatches(superOrThisKeyword)) { PsiElement referencedElement = refExpr.resolve(); if (referencedElement instanceof PsiMethod) { PsiMethod constructor1 = (PsiMethod)referencedElement; boolean match = isStrictSignatureSearch ? myManager.areElementsEquivalent(constructor1, constructor) : myManager.areElementsEquivalent(constructor.getContainingClass(), constructor1.getContainingClass()); if (match && !processor.process(refExpr)) return false; } //as long as we've encountered super/this keyword, no implicit ctr calls are possible here continue; } } } } } if (constructorCanBeCalledImplicitly) { if (!processImplicitConstructorCall(method, processor, constructor, project, inheritor)) return false; } } return true; }
@Override public boolean value(VirtualFile virtualFile) { final PsiFile element = manager.findFile(virtualFile); return element != null && PsiSearchScopeUtil.isInScope(myCustomScope, element); }
private boolean processSuperOrThis(@NotNull PsiClass inheritor, @NotNull PsiMethod constructor, final boolean constructorCanBeCalledImplicitly, @NotNull SearchScope searchScope, final boolean isStrictSignatureSearch, @NotNull String superOrThisKeyword, @NotNull Processor<PsiReference> processor) { PsiMethod[] constructors = inheritor.getConstructors(); if (constructors.length == 0 && constructorCanBeCalledImplicitly) { if (!processImplicitConstructorCall(inheritor, processor, constructor, inheritor)) return false; } for (PsiMethod method : constructors) { PsiCodeBlock body = method.getBody(); if (body == null) { continue; } PsiStatement[] statements = body.getStatements(); if (statements.length != 0) { PsiStatement statement = statements[0]; if (statement instanceof PsiExpressionStatement) { PsiExpression expr = ((PsiExpressionStatement)statement).getExpression(); if (expr instanceof PsiMethodCallExpression) { PsiReferenceExpression refExpr = ((PsiMethodCallExpression)expr).getMethodExpression(); if (PsiSearchScopeUtil.isInScope(searchScope, refExpr)) { if (refExpr.textMatches(superOrThisKeyword)) { PsiElement referencedElement = refExpr.resolve(); if (referencedElement instanceof PsiMethod) { PsiMethod constructor1 = (PsiMethod)referencedElement; boolean match = isStrictSignatureSearch ? myManager.areElementsEquivalent(constructor1, constructor) : myManager.areElementsEquivalent(constructor.getContainingClass(), constructor1.getContainingClass()); if (match && !processor.process(refExpr)) return false; } //as long as we've encountered super/this keyword, no implicit ctr calls are possible here continue; } } } } } if (constructorCanBeCalledImplicitly) { if (!processImplicitConstructorCall(method, processor, constructor, inheritor)) return false; } } return true; }
public boolean value(VirtualFile virtualFile) { final PsiFile element = manager.findFile(virtualFile); return element != null && PsiSearchScopeUtil.isInScope(myCustomScope, element); }
private boolean processSuperOrThis(@NotNull PsiClass inheritor, @NotNull PsiMethod constructor, final boolean constructorCanBeCalledImplicitly, @NotNull SearchScope searchScope, @NotNull Project project, final boolean isStrictSignatureSearch, @NotNull String superOrThisKeyword, @NotNull Processor<PsiReference> processor) { PsiMethod[] constructors = inheritor.getConstructors(); if(constructors.length == 0 && constructorCanBeCalledImplicitly) { if(!processImplicitConstructorCall(inheritor, processor, constructor, project, inheritor)) { return false; } } for(PsiMethod method : constructors) { PsiCodeBlock body = method.getBody(); if(body == null || method == constructor && isStrictSignatureSearch) { continue; } PsiStatement[] statements = body.getStatements(); if(statements.length != 0) { PsiStatement statement = statements[0]; if(statement instanceof PsiExpressionStatement) { PsiExpression expr = ((PsiExpressionStatement) statement).getExpression(); if(expr instanceof PsiMethodCallExpression) { PsiReferenceExpression refExpr = ((PsiMethodCallExpression) expr).getMethodExpression(); if(PsiSearchScopeUtil.isInScope(searchScope, refExpr)) { if(refExpr.textMatches(superOrThisKeyword)) { PsiElement referencedElement = refExpr.resolve(); if(referencedElement instanceof PsiMethod) { PsiMethod constructor1 = (PsiMethod) referencedElement; boolean match = isStrictSignatureSearch ? myManager.areElementsEquivalent(constructor1, constructor) : myManager.areElementsEquivalent(constructor .getContainingClass(), constructor1.getContainingClass()); if(match && !processor.process(refExpr)) { return false; } } //as long as we've encountered super/this keyword, no implicit ctr calls are possible here continue; } } } } } if(constructorCanBeCalledImplicitly) { if(!processImplicitConstructorCall(method, processor, constructor, project, inheritor)) { return false; } } } return true; }