private boolean hasAnnotation(PsiModifierList modifierList, String annotationName) { PsiAnnotation[] annotations = modifierList.getAnnotations(); if (annotations != null && annotations.length > 0) { for (PsiAnnotation annotation : annotations) { if (annotationName.equals(annotation.getQualifiedName())) { return true; } } } return false; }
PsiClass psiClass = ...; PsiModifierList psiModifierList = psiClass.getModifierList(); PsiAnnotation[] annotations = psiModifierList.getAnnotations();
@NotNull public static PsiAnnotation[] findAnnotations(@Nullable PsiModifierListOwner modifierListOwner, @NotNull Collection<String> annotationNames) { if (modifierListOwner == null) return PsiAnnotation.EMPTY_ARRAY; final PsiModifierList modifierList = modifierListOwner.getModifierList(); if (modifierList == null) return PsiAnnotation.EMPTY_ARRAY; final PsiAnnotation[] annotations = modifierList.getAnnotations(); ArrayList<PsiAnnotation> result = null; for (final PsiAnnotation psiAnnotation : annotations) { if (annotationNames.contains(psiAnnotation.getQualifiedName())) { if (result == null) result = new ArrayList<PsiAnnotation>(); result.add(psiAnnotation); } } return result == null ? PsiAnnotation.EMPTY_ARRAY : result.toArray(new PsiAnnotation[result.size()]); }
private static PsiAnnotation getNullabilityDefault(@NotNull PsiModifierListOwner container, boolean nullable, PsiAnnotation.TargetType[] placeTargetTypes) { PsiModifierList modifierList = container.getModifierList(); if (modifierList == null) return null; for (PsiAnnotation annotation : modifierList.getAnnotations()) { if (isNullabilityDefault(annotation, nullable, placeTargetTypes)) { return annotation; } } return null; }
private static void getPropInfosForTarget(PsiClass psiClass, Set<PropertyDetector.PropInfo> mSet) { final PsiModifierList list = psiClass.getModifierList(); if(list == null){ return; } for(PsiAnnotation ua : list.getAnnotations()){ if(!getPropInfosOfFields(ua, mSet)){ return; } } }
if (fqns == null) { fqns = new ArrayList<String>(); final PsiAnnotation[] annos = modList.getAnnotations(); for (PsiAnnotation anno : annos) { final String qName = anno.getQualifiedName();
for (PsiAnnotation annotation : modifierList.getAnnotations()) { try { resource = for (PsiAnnotation annotation : modifierList.getAnnotations()) { try { resource =
@NotNull private static List<String> getAnnotationsInner(PsiModifierListOwner element) { PsiModifierList modifierList = element.getModifierList(); if (modifierList == null) return ContainerUtilRt.emptyList(); List<String> result = new ArrayList<>(); for (PsiAnnotation annotation : modifierList.getAnnotations()) { if (annotation.getParameterList().getAttributes().length > 0) continue; ContainerUtil.addIfNotNull(result, annotation.getQualifiedName()); } return result; } }
Stream<PsiAnnotation> mappingsAnnotations; if ( mappings == null ) { mappingsAnnotations = Stream.of( method.getModifierList().getAnnotations() ) .filter( TargetUtils::isMappingAnnotation );
for (PsiAnnotation annotation : modifierList.getAnnotations()) { try { path =
/** * Returns the http method of the specified psiMethod. The httpMethod can be set by the user by * setting the httpMethod attribute in @ApiMethod. If the httpMethod attribute of the @ApiMethod * is not set, the default value of the method is used. * * @param psiMethod the method hose HTTP method is to be determined * @return the http Method pf psiMethod */ public String getHttpMethod(PsiMethod psiMethod) { PsiModifierList modifierList = psiMethod.getModifierList(); String httpMethod = null; // Check if the httpMethod was specified by uses in @ApiMethod's httpMethod attribute for (PsiAnnotation annotation : modifierList.getAnnotations()) { try { httpMethod = getAttributeFromAnnotation( annotation, GctConstants.APP_ENGINE_ANNOTATION_API_METHOD, "httpMethod"); } catch (InvalidAnnotationException ex) { // do nothing } catch (MissingAttributeException ex) { break; } if (httpMethod != null && !httpMethod.isEmpty()) { return httpMethod; } } // Create httpMethod from method name return getDefaultRestMethod(psiMethod).getHttpMethod(); }
for (PsiAnnotation annotation : method.getModifierList().getAnnotations()) { String name = annotation.getQualifiedName(); if (name != null && name.startsWith(SUPPORT_ANNOTATIONS_PREFIX) PsiModifierList modifierList = cls.getModifierList(); if (modifierList != null) { for (PsiAnnotation annotation : modifierList.getAnnotations()) { String name = annotation.getQualifiedName(); if (name != null && name.startsWith(SUPPORT_ANNOTATIONS_PREFIX)
/** * Returns true if the given AST modifier has a suppress annotation for the * given issue (which can be null to check for the "all" annotation) * * @param issue the issue to be checked * @param modifierList the modifier to check * @return true if the issue or all issues should be suppressed for this * modifier */ public static boolean isSuppressed(@NonNull Issue issue, @Nullable PsiModifierList modifierList) { if (modifierList == null) { return false; } for (PsiAnnotation annotation : modifierList.getAnnotations()) { String fqcn = annotation.getQualifiedName(); if (fqcn != null && (fqcn.equals(FQCN_SUPPRESS_LINT) || fqcn.equals(SUPPRESS_WARNINGS_FQCN) || fqcn.equals(SUPPRESS_LINT))) { // when missing imports PsiAnnotationParameterList parameterList = annotation.getParameterList(); for (PsiNameValuePair pair : parameterList.getAttributes()) { if (isSuppressed(issue, pair.getValue())) { return true; } } } } return false; }
private void initializePsiClass(String apiResource, String apiClassResource) { PsiAnnotationMemberValue mockAnnotationMemberValue1 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue1.getText()).thenReturn(apiResource); PsiAnnotationMemberValue mockAnnotationMemberValue2 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue2.getText()).thenReturn(apiClassResource); // Mock @Api(resource = "") PsiAnnotation mockAnnotation1 = mock(PsiAnnotation.class); when(mockAnnotation1.getQualifiedName()).thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API); when(mockAnnotation1.findAttributeValue("resource")).thenReturn(mockAnnotationMemberValue1); // Mock @ApiClass(resource = "") PsiAnnotation mockAnnotation2 = mock(PsiAnnotation.class); when(mockAnnotation2.getQualifiedName()) .thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_CLASS); when(mockAnnotation2.findAttributeValue("resource")).thenReturn(mockAnnotationMemberValue2); PsiAnnotation[] mockAnnotationsArray = {mockAnnotation1, mockAnnotation2}; PsiModifierList mockModifierList = mock(PsiModifierList.class); when(mockModifierList.getAnnotations()).thenReturn(mockAnnotationsArray); mockPsiClass = mock(PsiClass.class); when(mockPsiClass.getModifierList()).thenReturn(mockModifierList); } }
private void initializePsiMethod(String methodName, String httpMethodValue, String pathValue) { PsiAnnotationMemberValue mockAnnotationMemberValue1 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue1.getText()).thenReturn(httpMethodValue); PsiAnnotationMemberValue mockAnnotationMemberValue2 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue2.getText()).thenReturn(pathValue); PsiAnnotation mockAnnotation = mock(PsiAnnotation.class); when(mockAnnotation.getQualifiedName()) .thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_METHOD); when(mockAnnotation.findAttributeValue("httpMethod")).thenReturn(mockAnnotationMemberValue1); when(mockAnnotation.findAttributeValue("path")).thenReturn(mockAnnotationMemberValue2); PsiAnnotation[] mockAnnotationsArray = {mockAnnotation}; PsiModifierList mockModifierList = mock(PsiModifierList.class); when(mockModifierList.getAnnotations()).thenReturn(mockAnnotationsArray); mockPsiMethod = mock(PsiMethod.class); when(mockPsiMethod.getModifierList()).thenReturn(mockModifierList); when(mockPsiMethod.getName()).thenReturn(methodName); when(mockPsiMethod.getContainingClass()).thenReturn(mockPsiClass); PsiParameterList mockParameterList = mock(PsiParameterList.class); when(mockParameterList.getParameters()).thenReturn(new PsiParameter[0]); when(mockPsiMethod.getParameterList()).thenReturn(mockParameterList); }
for (PsiAnnotation annotation : modifierList.getAnnotations()) { String name = annotation.getQualifiedName(); if (RESTRICT_TO_ANNOTATION.equals(name)) {
private boolean checkRequiresApi(PsiCallExpression expression, PsiMethod method, PsiModifierList modifierList) { for (PsiAnnotation annotation : modifierList.getAnnotations()) { if (REQUIRES_API_ANNOTATION.equals(annotation.getQualifiedName())) { int api = (int) SupportAnnotationDetector.getLongAttribute(annotation,
for (PsiAnnotation annotation : modifierList.getAnnotations()) { String fqcn = annotation.getQualifiedName(); if (fqcn != null &&
PsiAnnotation[] annotations = PsiAnnotation.EMPTY_ARRAY; if (list != null) { annotations = list.getAnnotations();
/** * Tests that the NamedResourceInspection's quick fix flagged with {@link * NamedResourceError#MISSING_NAME} for an @Named annotation with a {@link PsiParameter} parent * updates the query name to to the name of the {@link PsiParameter}. */ public void testQuickFix_noQueryNameSpecifiedWithParameter() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameterFromText("@javax.inject.Named() String foobar", null); PsiAnnotation[] annotationsList = parameter.getModifierList().getAnnotations(); assert (annotationsList.length == 1); NamedResourceInspection.MissingNameQuickFix myQuickFix = new NamedResourceInspection().new MissingNameQuickFix(); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotationsList[0], "", ProblemHighlightType.ERROR); myQuickFix.applyFix(myProject, problemDescriptor); assertEquals("@javax.inject.Named(\"foobar\")", annotationsList[0].getText()); }