Search in sources :

Example 46 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.

the class EndpointPsiElementVisitor method hasTransformer.

/**
 * Returns true if the class containing <code>psiElement</code> has a transformer specified by
 * using the @ApiTransformer annotation on a class or by using the transformer attribute of the
 *
 * @return True if the class containing <code>psiElement</code> has a transformer and false
 *     otherwise. @Api annotation. Returns false otherwise.
 */
public boolean hasTransformer(PsiElement psiElement) {
    PsiClass psiClass = PsiUtils.findClass(psiElement);
    if (psiClass == null) {
        return false;
    }
    PsiModifierList modifierList = psiClass.getModifierList();
    if (modifierList == null) {
        return false;
    }
    // Check if class has @ApiTransformer to specify a transformer
    PsiAnnotation apiTransformerAnnotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API_TRANSFORMER);
    if (apiTransformerAnnotation != null) {
        return true;
    }
    // Check if class utilizes the transformer attribute of the @Api annotation
    // to specify its transformer
    PsiAnnotation apiAnnotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API);
    if (apiAnnotation != null) {
        PsiAnnotationMemberValue transformerMember = apiAnnotation.findAttributeValue(API_TRANSFORMER_ATTRIBUTE);
        if (transformerMember != null && !transformerMember.getText().equals("{}")) {
            return true;
        }
    }
    return false;
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiModifierList(com.intellij.psi.PsiModifierList) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue)

Example 47 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.

the class InvalidParameterAnnotationsInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new EndpointPsiElementVisitor() {

        @Override
        public void visitMethod(PsiMethod method) {
            if (!EndpointUtilities.isEndpointClass(method)) {
                return;
            }
            if (!EndpointUtilities.isApiMethod(method)) {
                return;
            }
            // Get all the method parameters
            PsiParameterList parameterList = method.getParameterList();
            if (parameterList.getParametersCount() == 0) {
                return;
            }
            // Check for @ApiMethod
            PsiAnnotation apiMethodAnnotation = method.getModifierList().findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API_METHOD);
            if (apiMethodAnnotation == null) {
                return;
            }
            // path has a default value of ""
            PsiAnnotationMemberValue pathMember = apiMethodAnnotation.findAttributeValue("path");
            String path = EndpointUtilities.removeBeginningAndEndingQuotes(pathMember.getText());
            // Check for path parameter, @ApiMethod(path="xys/{xy}")
            Collection<String> pathParameters = getPathParameters(path);
            if (pathParameters.size() == 0) {
                return;
            }
            // parameters has the @Nullable or @DefaultValue
            for (PsiParameter psiParameter : parameterList.getParameters()) {
                PsiAnnotationMemberValue namedAnnotation = getNamedAnnotationValue(psiParameter);
                if (namedAnnotation == null) {
                    continue;
                }
                String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(namedAnnotation.getText());
                // Check is @Named value is in list of path parameters
                if (!pathParameters.contains(nameValue)) {
                    continue;
                }
                // Check if @Named parameter also has @Nullable or @DefaultValue
                if ((psiParameter.getModifierList().findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NULLABLE) != null) || (psiParameter.getModifierList().findAnnotation("javax.annotation.Nullable") != null) || (psiParameter.getModifierList().findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE) != null)) {
                    holder.registerProblem(psiParameter, "Invalid parameter configuration. " + "A parameter in the method path should not be marked @Nullable or " + "@DefaultValue.", new MyQuickFix());
                }
            }
        }

        /**
         * Gets the parameters in <code>path</code>
         *
         * @param path The path whose parameters are to be parsed.
         * @return A collection of the parameter in <code>path</code>
         */
        private Collection<String> getPathParameters(String path) {
            Pattern pathPattern = Pattern.compile("\\{([^\\}]*)\\}");
            Matcher pathMatcher = pathPattern.matcher(path);
            Collection<String> pathParameters = new HashSet<String>();
            while (pathMatcher.find()) {
                pathParameters.add(pathMatcher.group(1));
            }
            return pathParameters;
        }
    };
}
Also used : Pattern(java.util.regex.Pattern) PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) Matcher(java.util.regex.Matcher) PsiParameterList(com.intellij.psi.PsiParameterList) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.

the class ApiNameInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final com.intellij.codeInspection.ProblemsHolder holder, boolean isOnTheFly) {
    return new EndpointPsiElementVisitor() {

        @Override
        public void visitAnnotation(PsiAnnotation annotation) {
            if (annotation == null) {
                return;
            }
            if (!GctConstants.APP_ENGINE_ANNOTATION_API.equals(annotation.getQualifiedName())) {
                return;
            }
            // Need to check for user added attributes because default values are used when not
            // specified by user and we are only interested in the user specified values
            PsiAnnotationParameterList parameterList = annotation.getParameterList();
            if (parameterList.getAttributes().length == 0) {
                return;
            }
            PsiAnnotationMemberValue annotationMemberValue = annotation.findAttributeValue(API_NAME_ATTRIBUTE);
            if (annotationMemberValue == null) {
                return;
            }
            String nameValueWithQuotes = annotationMemberValue.getText();
            String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(nameValueWithQuotes);
            // Empty API name is valid
            if (nameValue.isEmpty()) {
                return;
            }
            if (!API_NAME_PATTERN.matcher(nameValue).matches()) {
                holder.registerProblem(annotationMemberValue, "Invalid api name: it must start with a lower case letter and consists only of " + "letter and digits", new MyQuickFix());
            }
        }
    };
}
Also used : PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationParameterList(com.intellij.psi.PsiAnnotationParameterList) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) NotNull(org.jetbrains.annotations.NotNull)

Example 49 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.

the class ApiParameterInspection method hasParameterName.

private boolean hasParameterName(PsiParameter psiParameter) {
    PsiModifierList modifierList = psiParameter.getModifierList();
    if (modifierList == null) {
        return false;
    }
    PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named");
    if (annotation != null) {
        return true;
    }
    annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED);
    if (annotation != null) {
        return true;
    }
    return false;
}
Also used : PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 50 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.

the class NamedResourceInspectionTest method testQuickFix_duplicateParameter.

/**
 * Tests that the NamedResourceInspection's quick fix flagged with {@link
 * NamedResourceError#DUPLICATE_PARAMETER} for an @Named annotation updates the query name by
 * adding "_1" as a suffix.
 */
public void testQuickFix_duplicateParameter() {
    Project myProject = myFixture.getProject();
    String annotationString = "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + "(\"someName\")";
    PsiAnnotation annotation = JavaPsiFacade.getInstance(myProject).getElementFactory().createAnnotationFromText(annotationString, null);
    NamedResourceInspection.DuplicateNameQuickFix myQuickFix = new NamedResourceInspection().new DuplicateNameQuickFix();
    MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotation, "", ProblemHighlightType.ERROR);
    myQuickFix.applyFix(myProject, problemDescriptor);
    assertEquals("@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + "(\"someName_1\")", annotation.getText());
}
Also used : Project(com.intellij.openapi.project.Project) MockProblemDescriptor(com.intellij.testFramework.MockProblemDescriptor) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Aggregations

PsiAnnotation (com.intellij.psi.PsiAnnotation)61 PsiModifierList (com.intellij.psi.PsiModifierList)18 PsiAnnotationMemberValue (com.intellij.psi.PsiAnnotationMemberValue)14 PsiMethod (com.intellij.psi.PsiMethod)14 NotNull (org.jetbrains.annotations.NotNull)13 Project (com.intellij.openapi.project.Project)12 PsiClass (com.intellij.psi.PsiClass)11 PsiElement (com.intellij.psi.PsiElement)9 MockProblemDescriptor (com.intellij.testFramework.MockProblemDescriptor)6 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)5 PsiParameter (com.intellij.psi.PsiParameter)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5 Nullable (org.jetbrains.annotations.Nullable)5 PsiParameterList (com.intellij.psi.PsiParameterList)4 TestSize (com.google.idea.blaze.base.dependencies.TestSize)3 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)3 PsiNameValuePair (com.intellij.psi.PsiNameValuePair)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Nullable (javax.annotation.Nullable)3