public void testAnnotationIndex() throws Exception { getJavaFacade().setAssertOnFileLoadingFilter(new VirtualFileFilter() { @Override public boolean accept(final VirtualFile file) { return !"package-info.java".equals(file.getName()); } }, myTestRootDisposable); final PsiClass annotationTypeClass = findClass("annotations.AnnotationType"); assertTrue(annotationTypeClass.isAnnotationType()); final Collection<PsiMember> all = AnnotatedMembersSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll(); assertEquals(2, all.size()); Set<String> correctNames = new HashSet<String>(Arrays.asList("AnnotatedClass", "correctMethod")); for (PsiMember member : all) { assertTrue(correctNames.contains(member.getName())); } final Collection<PsiPackage> packages = AnnotatedPackagesSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll(); assertEquals(1, packages.size()); assertEquals("annotated", packages.iterator().next().getQualifiedName()); tearDownLoadingFilter(); }
public void testAnnotationIndex() throws Exception { getJavaFacade().setAssertOnFileLoadingFilter(new VirtualFileFilter() { @Override public boolean accept(final VirtualFile file) { return !"package-info.java".equals(file.getName()); } }); final PsiClass annotationTypeClass = findClass("annotations.AnnotationType"); assertTrue(annotationTypeClass.isAnnotationType()); final Collection<PsiMember> all = AnnotatedMembersSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll(); assertEquals(2, all.size()); Set<String> correctNames = new HashSet<String>(Arrays.asList("AnnotatedClass", "correctMethod")); for (PsiMember member : all) { assertTrue(correctNames.contains(member.getName())); } final Collection<PsiPackage> packages = AnnotatedPackagesSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll(); assertEquals(1, packages.size()); assertEquals("annotated", packages.iterator().next().getQualifiedName()); tearDownLoadingFilter(); }
@NotNull private static Query<PsiMember> createQuery(@NotNull PsiClass target, @NotNull PsiClass annClass) { PsiImmediateClassType type = new PsiImmediateClassType(target, PsiSubstitutor.EMPTY); return new FilteredQuery<>(AnnotatedMembersSearch.search(annClass), psiMember -> ReadAction.compute(() -> { if(psiMember instanceof PsiMethod && !psiMember.hasModifierProperty(PsiModifier.STATIC)) { PsiParameterList parameterList = ((PsiMethod) psiMember).getParameterList(); PsiParameter[] parameters = parameterList.getParameters(); if(parameters.length == 1 && parameters[0].getType().equals(type)) { return true; } } return false; })); }
public void testAnnotationIndex() throws Exception { getJavaFacade().setAssertOnFileLoadingFilter(new VirtualFileFilter() { @Override public boolean accept(final VirtualFile file) { return !"package-info.java".equals(file.getName()); } }, null); final PsiClass annotationTypeClass = findClass("annotations.AnnotationType"); assertTrue(annotationTypeClass.isAnnotationType()); final Collection<PsiMember> all = AnnotatedMembersSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll(); assertEquals(2, all.size()); Set<String> correctNames = new HashSet<String>(Arrays.asList("AnnotatedClass", "correctMethod")); for (PsiMember member : all) { assertTrue(correctNames.contains(member.getName())); } final Collection<PsiJavaPackage> packages = AnnotatedPackagesSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll(); assertEquals(1, packages.size()); assertEquals("annotated", packages.iterator().next().getQualifiedName()); tearDownLoadingFilter(); }
private static <T extends PsiElement> Collection<? extends PsiType> suggestFunctionalInterfaces(final @NotNull T element, final NullableFunction<PsiClass, PsiType> acceptanceChecker) { final Project project = element.getProject(); final Set<PsiType> types = new HashSet<PsiType>(); final Processor<PsiMember> consumer = new Processor<PsiMember>() { @Override public boolean process(PsiMember member) { if (member instanceof PsiClass && !Java15APIUsageInspectionBase.isForbiddenApiUsage(member, PsiUtil.getLanguageLevel(element))) { if (!JavaResolveUtil.isAccessible(member, null, member.getModifierList(), element, null, null)) { return true; } ContainerUtil.addIfNotNull(types, acceptanceChecker.fun((PsiClass)member)); } return true; } }; final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final GlobalSearchScope allScope = GlobalSearchScope.allScope(project); final PsiClass functionalInterfaceClass = psiFacade.findClass(CommonClassNames.JAVA_LANG_FUNCTIONAL_INTERFACE, allScope); if (functionalInterfaceClass != null) { AnnotatedMembersSearch.search(functionalInterfaceClass, element.getResolveScope()).forEach(consumer); } for (String functionalInterface : FUNCTIONAL_INTERFACES) { final PsiClass aClass = psiFacade.findClass(functionalInterface, allScope); if (aClass != null) { consumer.process(aClass); } } final ArrayList<PsiType> typesToSuggest = new ArrayList<PsiType>(types); Collections.sort(typesToSuggest, new Comparator<PsiType>() { @Override public int compare(PsiType o1, PsiType o2) { return o1.getCanonicalText().compareTo(o2.getCanonicalText()); } }); return typesToSuggest; }
@Nullable private static Query<PsiMember> findAnnotated(ExpressionContext context, Expression[] params) { if (params == null || params.length == 0) return null; PsiManager instance = PsiManager.getInstance(context.getProject()); final String paramResult = params[0].calculateResult(context).toString(); if (paramResult == null) return null; final GlobalSearchScope scope = GlobalSearchScope.allScope(context.getProject()); final PsiClass myBaseClass = JavaPsiFacade.getInstance(instance.getProject()).findClass(paramResult, scope); if (myBaseClass != null) { return AnnotatedMembersSearch.search(myBaseClass, scope); } return null; }
protected String[] getAnnotationNames(Project project) { synchronized (myAnnotations) { myAnnotations.clear(); final Pair<String, ? extends Set<String>> patternAnnotation = Configuration.getProjectInstance(project).getAdvancedConfiguration().getPatternAnnotationPair(); final GlobalSearchScope scope = GlobalSearchScope.allScope(project); final PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass(patternAnnotation.first, scope); if (psiClass == null) { // annotation is not present in project's classpath, nothing to instrument return ArrayUtil.EMPTY_STRING_ARRAY; } final Query<PsiMember> query = AnnotatedMembersSearch.search(psiClass, GlobalSearchScope.allScope(project)); query.forEach(new Processor<PsiMember>() { public boolean process(PsiMember psiMember) { if (psiMember instanceof PsiClass) { final PsiClass clazz = (PsiClass)psiMember; if (clazz.isAnnotationType()) { final PsiAnnotation annotation = AnnotationUtil.findAnnotation(clazz, patternAnnotation.second); if (annotation != null) { final String s = AnnotationUtilEx.calcAnnotationValue(annotation, "value"); if (s != null) { myAnnotations.put(clazz.getQualifiedName(), s); } } } } return true; } }); myAnnotations.put(patternAnnotation.first, null); final Set<String> names = myAnnotations.keySet(); return ArrayUtil.toStringArray(names); } }
@Nullable private LineMarkerInfo getNavigationLineMarker(@NotNull final PsiIdentifier element, @Nullable ButterKnifeLink link) { if (link == null) { return null; } final PsiAnnotation srcAnnotation = getAnnotation(element.getParent(), link.srcAnnotation); if (srcAnnotation != null) { final PsiAnnotationParameterList annotationParameters = srcAnnotation.getParameterList(); if (annotationParameters.getAttributes().length > 0) { final PsiAnnotationMemberValue value = annotationParameters.getAttributes()[0].getValue(); if (value == null) { return null; } final String resourceId = value.getText(); final PsiClass dstAnnotationClass = JavaPsiFacade.getInstance(element.getProject()) .findClass(link.dstAnnotation, ProjectScope.getLibrariesScope(element.getProject())); if (dstAnnotationClass == null) { return null; } final ClassMemberProcessor processor = new ClassMemberProcessor(resourceId, link); AnnotatedMembersSearch.search(dstAnnotationClass, GlobalSearchScope.fileScope(element.getContainingFile())).forEach(processor); final PsiMember dstMember = processor.getResultMember(); if (dstMember != null) { return new NavigationMarker.Builder().from(element).to(dstMember).build(); } } } return null; }
private static boolean addAnnotatedMethodsAnSubclasses(final PsiManager manager, final GlobalSearchScope scope, final TestClassFilter testClassFilter, final Set<PsiClass> found, final String annotation, final boolean isMethod) { final Ref<Boolean> isJUnit4 = new Ref<Boolean>(Boolean.FALSE); // annotated with @Test final PsiClass testAnnotation = ApplicationManager.getApplication().runReadAction( new Computable<PsiClass>() { @Nullable public PsiClass compute() { return JavaPsiFacade.getInstance(manager.getProject()).findClass(annotation, GlobalSearchScope.allScope(manager.getProject())); } } ); if (testAnnotation != null) { AnnotatedMembersSearch.search(testAnnotation, GlobalSearchScope.allScope(manager.getProject())).forEach(new Processor<PsiMember>() { public boolean process(final PsiMember annotated) { final PsiClass containingClass = annotated instanceof PsiClass ? (PsiClass)annotated : annotated.getContainingClass(); if (containingClass != null && annotated instanceof PsiMethod == isMethod) { if (ApplicationManager.getApplication().runReadAction( new Computable<Boolean>() { @Override public Boolean compute() { return scope.contains(PsiUtilCore.getVirtualFile(containingClass)) && testClassFilter.isAccepted(containingClass); } })) { found.add(containingClass); isJUnit4.set(Boolean.TRUE); } ClassInheritorsSearch.search(containingClass, scope, true) .forEach(new PsiElementProcessorAdapter<PsiClass>(new PsiElementProcessor<PsiClass>() { public boolean execute(@NotNull final PsiClass aClass) { if (testClassFilter.isAccepted(aClass)) { found.add(aClass); isJUnit4.set(Boolean.TRUE); } return true; } })); } return true; } }); } return isJUnit4.get().booleanValue(); }
private static <T extends PsiElement> Collection<? extends PsiType> suggestFunctionalInterfaces(final @NotNull T element, final NullableFunction<PsiClass, PsiType> acceptanceChecker) { final Project project = element.getProject(); final Set<PsiType> types = new HashSet<PsiType>(); final Processor<PsiMember> consumer = new Processor<PsiMember>() { @Override public boolean process(PsiMember member) { if(member instanceof PsiClass && !Java15APIUsageInspectionBase.isForbiddenApiUsage(member, PsiUtil.getLanguageLevel(element))) { if(!JavaResolveUtil.isAccessible(member, null, member.getModifierList(), element, null, null)) { return true; } ContainerUtil.addIfNotNull(types, acceptanceChecker.fun((PsiClass) member)); } return true; } }; final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final GlobalSearchScope allScope = GlobalSearchScope.allScope(project); final PsiClass functionalInterfaceClass = psiFacade.findClass(CommonClassNames.JAVA_LANG_FUNCTIONAL_INTERFACE, allScope); if(functionalInterfaceClass != null) { AnnotatedMembersSearch.search(functionalInterfaceClass, element.getResolveScope()).forEach(consumer); } for(String functionalInterface : FUNCTIONAL_INTERFACES) { final PsiClass aClass = psiFacade.findClass(functionalInterface, allScope); if(aClass != null) { consumer.process(aClass); } } final ArrayList<PsiType> typesToSuggest = new ArrayList<PsiType>(types); Collections.sort(typesToSuggest, new Comparator<PsiType>() { @Override public int compare(PsiType o1, PsiType o2) { return o1.getCanonicalText().compareTo(o2.getCanonicalText()); } }); return typesToSuggest; }