@Override public void fillTestObjects(Map<PsiClass, Map<PsiMethod, List<String>>> classes) throws CantRunException { final String packageName = myConfig.getPersistantData().getPackageName(); PsiPackage psiPackage = ApplicationManager.getApplication().runReadAction( new Computable<PsiPackage>() { @Nullable public PsiPackage compute() { return JavaPsiFacade.getInstance(myConfig.getProject()).findPackage(packageName); } } ); if (psiPackage == null) { throw CantRunException.packageNotFound(packageName); } else { TestSearchScope scope = myConfig.getPersistantData().getScope(); //TODO we should narrow this down by module really, if that's what's specified TestClassFilter projectFilter = new TestClassFilter(scope.getSourceScope(myConfig).getGlobalSearchScope(), myConfig.getProject(), true, true); TestClassFilter filter = projectFilter.intersectionWith(PackageScope.packageScope(psiPackage, true)); calculateDependencies(null, classes, getSearchScope(), TestNGUtil.getAllTestClasses(filter, false)); if (classes.size() == 0) { throw new CantRunException("No tests found in the package \"" + packageName + '\"'); } } }
protected GlobalSearchScope filterScope(final JUnitConfiguration.Data data) throws CantRunException { final Ref<CantRunException> ref = new Ref<CantRunException>(); final GlobalSearchScope aPackage = ApplicationManager.getApplication().runReadAction(new Computable<GlobalSearchScope>() { @Override public GlobalSearchScope compute() { try { return PackageScope.packageScope(getPackage(data), true); } catch (CantRunException e) { ref.set(e); return null; } } }); final CantRunException exception = ref.get(); if (exception != null) throw exception; return aPackage; }
@NotNull public static SearchScope getMemberUseScope(@NotNull PsiMember member) { PsiFile file = member.getContainingFile(); PsiElement topElement = file == null ? member : file; Project project = topElement.getProject(); final GlobalSearchScope maximalUseScope = ResolveScopeManager.getInstance(project).getUseScope(topElement); if (isInServerPage(file)) return maximalUseScope; PsiClass aClass = member.getContainingClass(); if (aClass instanceof PsiAnonymousClass) { //member from anonymous class can be called from outside the class PsiElement methodCallExpr = PsiUtil.isLanguageLevel8OrHigher(aClass) ? PsiTreeUtil.getTopmostParentOfType(aClass, PsiStatement.class) : PsiTreeUtil.getParentOfType(aClass, PsiMethodCallExpression.class); return new LocalSearchScope(methodCallExpr != null ? methodCallExpr : aClass); } PsiModifierList modifierList = member.getModifierList(); int accessLevel = modifierList == null ? PsiUtil.ACCESS_LEVEL_PUBLIC : PsiUtil.getAccessLevel(modifierList); if (accessLevel == PsiUtil.ACCESS_LEVEL_PUBLIC || accessLevel == PsiUtil.ACCESS_LEVEL_PROTECTED) { return maximalUseScope; // class use scope doesn't matter, since another very visible class can inherit from aClass } if (accessLevel == PsiUtil.ACCESS_LEVEL_PRIVATE) { PsiClass topClass = PsiUtil.getTopLevelClass(member); return topClass != null ? new LocalSearchScope(topClass) : file == null ? maximalUseScope : new LocalSearchScope(file); } if (file instanceof PsiJavaFile) { PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(((PsiJavaFile)file).getPackageName()); if (aPackage != null) { SearchScope scope = PackageScope.packageScope(aPackage, false); return scope.intersectWith(maximalUseScope); } } return maximalUseScope; }
@NotNull @Override public SearchScope toSearchScope() { if (myType == PACKAGE) { return new PackageScope((PsiPackage)myElement, true, true); } return super.toSearchScope(); }
public static String generateEverything(final PsiPackage psiPackage, final PsiClass rootClass, final String visitorName) { final String visitorQName = PsiNameHelper.getShortClassName(visitorName).equals(visitorName)? psiPackage.getQualifiedName()+"."+ visitorName : visitorName; final PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(rootClass.getProject(), StringUtil.getPackageName(visitorQName), null, false); generateVisitorClass(visitorQName, rootClass, directory, new PackageScope(psiPackage, false, false)); return visitorQName; }
public void processSubclassVariants(@NotNull PsiPackage context, @NotNull String[] extendClasses, Consumer<LookupElement> result) { GlobalSearchScope packageScope = PackageScope.packageScope(context, true); GlobalSearchScope scope = myJavaClassReferenceSet.getProvider().getScope(getElement().getProject()); if (scope != null) { packageScope = packageScope.intersectWith(scope); } final GlobalSearchScope allScope = ProjectScope.getAllScope(context.getProject()); final boolean instantiatable = JavaClassReferenceProvider.INSTANTIATABLE.getBooleanValue(getOptions()); final boolean notInterface = JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(getOptions()); final boolean notEnum = JavaClassReferenceProvider.NOT_ENUM.getBooleanValue(getOptions()); final boolean concrete = JavaClassReferenceProvider.CONCRETE.getBooleanValue(getOptions()); final ClassKind classKind = getClassKind(); for (String extendClassName : extendClasses) { final PsiClass extendClass = JavaPsiFacade.getInstance(context.getProject()).findClass(extendClassName, allScope); if (extendClass != null) { // add itself if (packageScope.contains(extendClass.getContainingFile().getVirtualFile())) { if (isClassAccepted(extendClass, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(extendClass, extendClassName)); } } for (final PsiClass clazz : ClassInheritorsSearch.search(extendClass, packageScope, true)) { String qname = clazz.getQualifiedName(); if (qname != null && isClassAccepted(clazz, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(clazz, qname)); } } } } }
private static void findPublicClassConflicts(PsiClass aClass, final MyClassInstanceReferenceVisitor instanceReferenceVisitor) { //noinspection MismatchedQueryAndUpdateOfCollection NonPublicClassMemberWrappersSet members = new NonPublicClassMemberWrappersSet(); members.addElements(aClass.getFields()); members.addElements(aClass.getMethods()); members.addElements(aClass.getInnerClasses()); final RefactoringUtil.IsDescendantOf isDescendantOf = new RefactoringUtil.IsDescendantOf(aClass); final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(aClass.getContainingFile().getContainingDirectory()); final GlobalSearchScope packageScope = aPackage == null ? aClass.getResolveScope() : PackageScope.packageScopeWithoutLibraries(aPackage, false); for (final ClassMemberWrapper memberWrapper : members) { ReferencesSearch.search(memberWrapper.getMember(), packageScope, false).forEach(new Processor<PsiReference>() { public boolean process(final PsiReference reference) { final PsiElement element = reference.getElement(); if (element instanceof PsiReferenceExpression) { final PsiReferenceExpression expression = (PsiReferenceExpression)element; final PsiExpression qualifierExpression = expression.getQualifierExpression(); if (qualifierExpression != null) { final PsiType type = qualifierExpression.getType(); if (type != null) { final PsiClass resolvedTypeClass = PsiUtil.resolveClassInType(type); if (isDescendantOf.value(resolvedTypeClass)) { instanceReferenceVisitor.visitMemberReference(memberWrapper.getMember(), expression, isDescendantOf); } } } else { instanceReferenceVisitor.visitMemberReference(memberWrapper.getMember(), expression, isDescendantOf); } } return true; } }); } }
@NotNull public static SearchScope getClassUseScope(@NotNull PsiClass aClass) { if (aClass instanceof PsiAnonymousClass) { return new LocalSearchScope(aClass); } final GlobalSearchScope maximalUseScope = ResolveScopeManager.getElementUseScope(aClass); PsiFile file = aClass.getContainingFile(); if (PsiImplUtil.isInServerPage(file)) return maximalUseScope; final PsiClass containingClass = aClass.getContainingClass(); if (aClass.hasModifierProperty(PsiModifier.PUBLIC) || aClass.hasModifierProperty(PsiModifier.PROTECTED)) { return containingClass == null ? maximalUseScope : containingClass.getUseScope(); } else if (aClass.hasModifierProperty(PsiModifier.PRIVATE) || aClass instanceof PsiTypeParameter) { PsiClass topClass = PsiUtil.getTopLevelClass(aClass); return new LocalSearchScope(topClass == null ? aClass.getContainingFile() : topClass); } else { PsiPackage aPackage = null; if (file instanceof PsiJavaFile) { aPackage = JavaPsiFacade.getInstance(aClass.getProject()).findPackage(((PsiJavaFile)file).getPackageName()); } if (aPackage == null) { PsiDirectory dir = file.getContainingDirectory(); if (dir != null) { aPackage = JavaDirectoryService.getInstance().getPackage(dir); } } if (aPackage != null) { SearchScope scope = PackageScope.packageScope(aPackage, false); scope = scope.intersectWith(maximalUseScope); return scope; } return new LocalSearchScope(file); } }
@NotNull public static SearchScope getMemberUseScope(@NotNull PsiMember member) { final GlobalSearchScope maximalUseScope = ResolveScopeManager.getElementUseScope(member); PsiFile file = member.getContainingFile(); if (isInServerPage(file)) return maximalUseScope; PsiClass aClass = member.getContainingClass(); if (aClass instanceof PsiAnonymousClass) { //member from anonymous class can be called from outside the class PsiElement methodCallExpr = PsiTreeUtil.getParentOfType(aClass, PsiMethodCallExpression.class); return new LocalSearchScope(methodCallExpr != null ? methodCallExpr : aClass); } if (member.hasModifierProperty(PsiModifier.PUBLIC)) { return maximalUseScope; // class use scope doesn't matter, since another very visible class can inherit from aClass } else if (member.hasModifierProperty(PsiModifier.PROTECTED)) { return maximalUseScope; // class use scope doesn't matter, since another very visible class can inherit from aClass } else if (member.hasModifierProperty(PsiModifier.PRIVATE)) { PsiClass topClass = PsiUtil.getTopLevelClass(member); return topClass != null ? new LocalSearchScope(topClass) : file != null ? new LocalSearchScope(file) : maximalUseScope; } else { if (file instanceof PsiJavaFile) { PsiPackage aPackage = JavaPsiFacade.getInstance(member.getProject()).findPackage(((PsiJavaFile)file).getPackageName()); if (aPackage != null) { SearchScope scope = PackageScope.packageScope(aPackage, false); scope = scope.intersectWith(maximalUseScope); return scope; } } return maximalUseScope; } }
@NotNull public void processSubclassVariants(@NotNull PsiPackage context, @NotNull String[] extendClasses, Consumer<LookupElement> result) { GlobalSearchScope packageScope = PackageScope.packageScope(context, true); GlobalSearchScope scope = myJavaClassReferenceSet.getProvider().getScope(getElement().getProject()); if (scope != null) { packageScope = packageScope.intersectWith(scope); } final GlobalSearchScope allScope = ProjectScope.getAllScope(context.getProject()); final boolean instantiatable = JavaClassReferenceProvider.INSTANTIATABLE.getBooleanValue(getOptions()); final boolean notInterface = JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(getOptions()); final boolean notEnum = JavaClassReferenceProvider.NOT_ENUM.getBooleanValue(getOptions()); final boolean concrete = JavaClassReferenceProvider.CONCRETE.getBooleanValue(getOptions()); final ClassKind classKind = getClassKind(); for (String extendClassName : extendClasses) { final PsiClass extendClass = JavaPsiFacade.getInstance(context.getProject()).findClass(extendClassName, allScope); if (extendClass != null) { // add itself if (packageScope.contains(extendClass.getContainingFile().getVirtualFile())) { if (isClassAccepted(extendClass, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(extendClass, extendClassName)); } } for (final PsiClass clazz : ClassInheritorsSearch.search(extendClass, packageScope, true)) { String qname = clazz.getQualifiedName(); if (qname != null && isClassAccepted(clazz, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(clazz, qname)); } } } } }
public static String generateEverything(final PsiJavaPackage psiPackage, final PsiClass rootClass, final String visitorName) { final String visitorQName = PsiNameHelper.getShortClassName(visitorName).equals(visitorName)? psiPackage.getQualifiedName()+"."+ visitorName : visitorName; final PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(rootClass.getProject(), StringUtil.getPackageName(visitorQName), null, false); generateVisitorClass(visitorQName, rootClass, directory, new PackageScope(psiPackage, false, false)); return visitorQName; }
@NotNull @Override public SearchScope toSearchScope() { if(myType == PACKAGE) { return new PackageScope((PsiJavaPackage) myElement, true, true); } return super.toSearchScope(); }
public void processSubclassVariants(@NotNull PsiJavaPackage context, @NotNull String[] extendClasses, Consumer<LookupElement> result) { GlobalSearchScope packageScope = PackageScope.packageScope(context, true); GlobalSearchScope scope = myJavaClassReferenceSet.getProvider().getScope(getElement().getProject()); if(scope != null) { packageScope = packageScope.intersectWith(scope); } final GlobalSearchScope allScope = ProjectScope.getAllScope(context.getProject()); final boolean instantiatable = JavaClassReferenceProvider.INSTANTIATABLE.getBooleanValue(getOptions()); final boolean notInterface = JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(getOptions()); final boolean notEnum = JavaClassReferenceProvider.NOT_ENUM.getBooleanValue(getOptions()); final boolean concrete = JavaClassReferenceProvider.CONCRETE.getBooleanValue(getOptions()); final ClassKind classKind = getClassKind(); for(String extendClassName : extendClasses) { final PsiClass extendClass = JavaPsiFacade.getInstance(context.getProject()).findClass(extendClassName, allScope); if(extendClass != null) { // add itself if(packageScope.contains(extendClass.getContainingFile().getVirtualFile())) { if(isClassAccepted(extendClass, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(extendClass, extendClassName)); } } for(final PsiClass clazz : ClassInheritorsSearch.search(extendClass, packageScope, true)) { String qname = clazz.getQualifiedName(); if(qname != null && isClassAccepted(clazz, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(clazz, qname)); } } } } }
private static void findPublicClassConflicts(PsiClass aClass, final MyClassInstanceReferenceVisitor instanceReferenceVisitor) { //noinspection MismatchedQueryAndUpdateOfCollection NonPublicClassMemberWrappersSet members = new NonPublicClassMemberWrappersSet(); members.addElements(aClass.getFields()); members.addElements(aClass.getMethods()); members.addElements(aClass.getInnerClasses()); final RefactoringUtil.IsDescendantOf isDescendantOf = new RefactoringUtil.IsDescendantOf(aClass); final PsiJavaPackage aPackage = JavaDirectoryService.getInstance().getPackage(aClass.getContainingFile().getContainingDirectory()); final GlobalSearchScope packageScope = aPackage == null ? aClass.getResolveScope() : PackageScope.packageScopeWithoutLibraries(aPackage, false); for (final ClassMemberWrapper memberWrapper : members) { ReferencesSearch.search(memberWrapper.getMember(), packageScope, false).forEach(new Processor<PsiReference>() { public boolean process(final PsiReference reference) { final PsiElement element = reference.getElement(); if (element instanceof PsiReferenceExpression) { final PsiReferenceExpression expression = (PsiReferenceExpression)element; final PsiExpression qualifierExpression = expression.getQualifierExpression(); if (qualifierExpression != null) { final PsiType type = qualifierExpression.getType(); if (type != null) { final PsiClass resolvedTypeClass = PsiUtil.resolveClassInType(type); if (isDescendantOf.value(resolvedTypeClass)) { instanceReferenceVisitor.visitMemberReference(memberWrapper.getMember(), expression, isDescendantOf); } } } else { instanceReferenceVisitor.visitMemberReference(memberWrapper.getMember(), expression, isDescendantOf); } } return true; } }); } }
protected GlobalSearchScope filterScope(final JUnitConfiguration.Data data) throws CantRunException { final PsiPackage aPackage = getPackage(data); return PackageScope.packageScope(aPackage, true); }
@NotNull public static SearchScope getClassUseScope(@NotNull PsiClass aClass) { if(aClass instanceof PsiAnonymousClass) { return new LocalSearchScope(aClass); } final GlobalSearchScope maximalUseScope = ResolveScopeManager.getElementUseScope(aClass); PsiFile file = aClass.getContainingFile(); if(PsiImplUtil.isInServerPage(file)) { return maximalUseScope; } final PsiClass containingClass = aClass.getContainingClass(); if(aClass.hasModifierProperty(PsiModifier.PUBLIC) || aClass.hasModifierProperty(PsiModifier.PROTECTED)) { return containingClass == null ? maximalUseScope : containingClass.getUseScope(); } else if(aClass.hasModifierProperty(PsiModifier.PRIVATE) || aClass instanceof PsiTypeParameter) { PsiClass topClass = PsiUtil.getTopLevelClass(aClass); return new LocalSearchScope(topClass == null ? aClass.getContainingFile() : topClass); } else { PsiJavaPackage aPackage = null; if(file instanceof PsiJavaFile) { aPackage = JavaPsiFacade.getInstance(aClass.getProject()).findPackage(((PsiJavaFile) file).getPackageName()); } if(aPackage == null) { PsiDirectory dir = file.getContainingDirectory(); if(dir != null) { aPackage = JavaDirectoryService.getInstance().getPackage(dir); } } if(aPackage != null) { SearchScope scope = PackageScope.packageScope(aPackage, false); scope = scope.intersectWith(maximalUseScope); return scope; } return new LocalSearchScope(file); } }
@NotNull public static SearchScope getMemberUseScope(@NotNull PsiMember member) { PsiFile file = member.getContainingFile(); PsiElement topElement = file == null ? member : file; Project project = topElement.getProject(); final GlobalSearchScope maximalUseScope = ResolveScopeManager.getInstance(project).getUseScope(topElement); if(isInServerPage(file)) { return maximalUseScope; } PsiClass aClass = member.getContainingClass(); if(aClass instanceof PsiAnonymousClass) { //member from anonymous class can be called from outside the class PsiElement methodCallExpr = PsiUtil.isLanguageLevel8OrHigher(aClass) ? PsiTreeUtil.getTopmostParentOfType(aClass, PsiStatement.class) : PsiTreeUtil.getParentOfType(aClass, PsiMethodCallExpression.class); return new LocalSearchScope(methodCallExpr != null ? methodCallExpr : aClass); } PsiModifierList modifierList = member.getModifierList(); int accessLevel = modifierList == null ? PsiUtil.ACCESS_LEVEL_PUBLIC : PsiUtil.getAccessLevel(modifierList); if(accessLevel == PsiUtil.ACCESS_LEVEL_PUBLIC || accessLevel == PsiUtil.ACCESS_LEVEL_PROTECTED) { return maximalUseScope; // class use scope doesn't matter, since another very visible class can inherit from aClass } if(accessLevel == PsiUtil.ACCESS_LEVEL_PRIVATE) { PsiClass topClass = PsiUtil.getTopLevelClass(member); return topClass != null ? new LocalSearchScope(topClass) : file == null ? maximalUseScope : new LocalSearchScope(file); } if(file instanceof PsiJavaFile) { PsiJavaPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(((PsiJavaFile) file).getPackageName()); if(aPackage != null) { SearchScope scope = PackageScope.packageScope(aPackage, false); return scope.intersectWith(maximalUseScope); } } return maximalUseScope; }
private void processFieldRemoved(FieldInfo info, final PsiClass aClass) throws CacheCorruptedException { if (info.isPrivate()) { return; // optimization: don't need to search, cause may be used only in this class } SearchScope searchScope = GlobalSearchScope.projectScope(myProject); if (info.isPackageLocal()) { final PsiFile containingFile = aClass.getContainingFile(); if (containingFile instanceof PsiJavaFile) { final String packageName = ((PsiJavaFile)containingFile).getPackageName(); final PsiJavaPackage aPackage = JavaPsiFacade.getInstance(myProject).findPackage(packageName); if (aPackage != null) { searchScope = PackageScope.packageScope(aPackage, false); searchScope = searchScope.intersectWith(aClass.getUseScope()); } } } final PsiSearchHelper psiSearchHelper = PsiSearchHelper.SERVICE.getInstance(myProject); final Ref<CacheCorruptedException> exRef = new Ref<CacheCorruptedException>(null); processIdentifiers(psiSearchHelper, new PsiElementProcessor<PsiIdentifier>() { @Override public synchronized boolean execute(@NotNull PsiIdentifier identifier) { try { final PsiElement parent = identifier.getParent(); if (parent instanceof PsiReferenceExpression) { final PsiClass ownerClass = getOwnerClass(parent); if (ownerClass != null && !ownerClass.equals(aClass)) { final String _qName = ownerClass.getQualifiedName(); if (_qName != null) { int qualifiedName = myDependencyCache.getSymbolTable().getId(_qName); // should force marking of the class no matter was it compiled or not // This will ensure the class was recompiled _after_ all the constants get their new values if (myDependencyCache.markClass(qualifiedName, true)) { if (LOG.isDebugEnabled()) { LOG.debug("Mark dependent class " + myDependencyCache.resolve(qualifiedName) + "; reason: some constants were removed from " + myDependencyCache.resolve(myQName)); } } } else { LOG.warn("Class with null qualified name was not expected here: " + ownerClass); } } } return true; } catch (CacheCorruptedException e) { exRef.set(e); return false; } } }, myDependencyCache.resolve(info.getName()), searchScope, UsageSearchContext.IN_CODE); final CacheCorruptedException cacheCorruptedException = exRef.get(); if (cacheCorruptedException != null) { throw cacheCorruptedException; } }
private boolean isSuperCallWithSameArguments(PsiCodeBlock body, PsiMethod method, PsiMethod superMethod) { final PsiStatement[] statements = body.getStatements(); if(statements.length != 1) { return false; } final PsiStatement statement = statements[0]; final PsiExpression expression; if(PsiType.VOID.equals(method.getReturnType())) { if(statement instanceof PsiExpressionStatement) { final PsiExpressionStatement expressionStatement = (PsiExpressionStatement) statement; expression = expressionStatement.getExpression(); } else { return false; } } else { if(statement instanceof PsiReturnStatement) { final PsiReturnStatement returnStatement = (PsiReturnStatement) statement; expression = ParenthesesUtils.stripParentheses(returnStatement.getReturnValue()); } else { return false; } } if(!(expression instanceof PsiMethodCallExpression)) { return false; } final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expression; if(!MethodCallUtils.isSuperMethodCall(methodCallExpression, method)) { return false; } if(superMethod.hasModifierProperty(PsiModifier.PROTECTED)) { final PsiJavaFile file = (PsiJavaFile) method.getContainingFile(); // implementing a protected method in another package makes it available to that package. PsiJavaPackage aPackage = JavaPsiFacade.getInstance(method.getProject()).findPackage(file.getPackageName()); if(aPackage == null) { return false; // when package statement is incorrect } final PackageScope scope = new PackageScope(aPackage, false, false); if(isOnTheFly()) { final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(method.getProject()); final PsiSearchHelper.SearchCostResult cost = searchHelper.isCheapEnoughToSearch(method.getName(), scope, null, null); if(cost == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES) { return true; } if(cost == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) { return false; } } final Query<PsiReference> search = ReferencesSearch.search(method, scope); final PsiClass containingClass = method.getContainingClass(); for(PsiReference reference : search) { if(!PsiTreeUtil.isAncestor(containingClass, reference.getElement(), true)) { return false; } } } return areSameArguments(methodCallExpression, method); }