Search in sources :

Example 36 with PsiModifierList

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

the class ResourceParameterInspection method buildVisitor.

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

        private int resourceParameterCount = 0;

        @Override
        public void visitMethod(PsiMethod method) {
            if (!EndpointUtilities.isEndpointClass(method)) {
                return;
            }
            if (method.isConstructor()) {
                return;
            }
            // Check if method is public or non-static
            if (!EndpointUtilities.isApiMethod(method)) {
                return;
            }
            Project project;
            try {
                project = method.getContainingFile().getProject();
                if (project == null) {
                    return;
                }
            } catch (PsiInvalidElementAccessException ex) {
                LOG.error("Error getting project with parameter " + method.getText(), ex);
                return;
            }
            resourceParameterCount = 0;
            for (PsiParameter param : method.getParameterList().getParameters()) {
                validateMethodParameters(param, project);
            }
            // Check that there is no more than one resource (entity) parameter for this method.
            if (resourceParameterCount > 1) {
                holder.registerProblem(method, "Multiple entity parameters. There can only be a single entity parameter per method.", LocalQuickFix.EMPTY_ARRAY);
            }
        }

        private void validateMethodParameters(PsiParameter psiParameter, Project project) {
            // Check if parameter is of entity (resource) type which is not of parameter type or
            // injected type.
            PsiType type = psiParameter.getType();
            if (!isEntityParameter(type, project)) {
                return;
            }
            // Update count of resource (entity) parameters for this method
            resourceParameterCount++;
            // Check that parameter is not a collection or an array
            if (type instanceof PsiArrayType || isCollectionType(type, project)) {
                holder.registerProblem(psiParameter, "Illegal parameter type (\'" + psiParameter.getType().getPresentableText() + "\'). Arrays or collections of entity types are not allowed.", LocalQuickFix.EMPTY_ARRAY);
            }
            // Check that parameter does not have an @Named annotation
            PsiModifierList modifierList = psiParameter.getModifierList();
            PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named");
            if (annotation == null) {
                annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED);
                if (annotation == null) {
                    return;
                }
            }
            holder.registerProblem(psiParameter, "Bad parameter name. Parameter is entity (resource)" + " type and should not be named.", LocalQuickFix.EMPTY_ARRAY);
        }
    };
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) Project(com.intellij.openapi.project.Project) PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiArrayType(com.intellij.psi.PsiArrayType) PsiType(com.intellij.psi.PsiType) PsiModifierList(com.intellij.psi.PsiModifierList) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with PsiModifierList

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

the class RestSignatureInspection method getResourceProperty.

@Nullable
private String getResourceProperty(PsiMethod psiMethod) {
    PsiClass psiClass = psiMethod.getContainingClass();
    PsiModifierList modifierList = psiClass.getModifierList();
    String resource = null;
    if (modifierList == null) {
        return null;
    }
    // Get @ApiClass's resource attribute if it exists
    for (PsiAnnotation annotation : modifierList.getAnnotations()) {
        try {
            resource = getAttributeFromAnnotation(annotation, GctConstants.APP_ENGINE_ANNOTATION_API_CLASS, "resource");
        } catch (InvalidAnnotationException ex) {
        // do nothing
        } catch (MissingAttributeException ex) {
            break;
        }
        // resource attribute is "" by default
        if (resource != null && !resource.isEmpty()) {
            return resource;
        }
    }
    // Get @Api's resource attribute if it exists
    for (PsiAnnotation annotation : modifierList.getAnnotations()) {
        try {
            resource = getAttributeFromAnnotation(annotation, GctConstants.APP_ENGINE_ANNOTATION_API, "resource");
        } catch (InvalidAnnotationException ex) {
        // do nothing
        } catch (MissingAttributeException ex) {
            break;
        }
        if (resource != null) {
            // resource attribute is "" by default
            if (resource.isEmpty()) {
                break;
            } else {
                return resource;
            }
        }
    }
    return null;
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiModifierList(com.intellij.psi.PsiModifierList) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with PsiModifierList

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

the class EndpointPsiElementVisitor method getNamedAnnotationValue.

/**
 * Returns the value for @Named if it exists for <code>psiParameter</code> or null if it does not
 * exist.
 *
 * @param psiParameter The parameter whose @Named value is to be returned.
 * @return The @Named value if it exists for <code>psiParameter</code> or null if it does not
 *     exist.
 */
@Nullable
public PsiAnnotationMemberValue getNamedAnnotationValue(PsiParameter psiParameter) {
    PsiModifierList modifierList = psiParameter.getModifierList();
    if (modifierList == null) {
        return null;
    }
    PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named");
    if (annotation == null) {
        annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED);
        if (annotation == null) {
            return null;
        }
    }
    PsiNameValuePair[] nameValuePairs = annotation.getParameterList().getAttributes();
    if (nameValuePairs.length != 1) {
        return null;
    }
    if (nameValuePairs[0] == null) {
        return null;
    }
    return nameValuePairs[0].getValue();
}
Also used : PsiNameValuePair(com.intellij.psi.PsiNameValuePair) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiModifierList(com.intellij.psi.PsiModifierList) Nullable(org.jetbrains.annotations.Nullable)

Example 39 with PsiModifierList

use of com.intellij.psi.PsiModifierList 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 40 with PsiModifierList

use of com.intellij.psi.PsiModifierList 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)

Aggregations

PsiModifierList (com.intellij.psi.PsiModifierList)43 PsiAnnotation (com.intellij.psi.PsiAnnotation)18 PsiClass (com.intellij.psi.PsiClass)13 PsiMethod (com.intellij.psi.PsiMethod)12 PsiAnnotationMemberValue (com.intellij.psi.PsiAnnotationMemberValue)6 PsiElement (com.intellij.psi.PsiElement)6 NotNull (org.jetbrains.annotations.NotNull)6 Project (com.intellij.openapi.project.Project)5 Module (com.intellij.openapi.module.Module)3 PsiModifierListOwner (com.intellij.psi.PsiModifierListOwner)3 PsiParameter (com.intellij.psi.PsiParameter)3 Test (org.junit.Test)3 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)2 ClassFilter (com.intellij.ide.util.ClassFilter)2 TreeClassChooser (com.intellij.ide.util.TreeClassChooser)2 Document (com.intellij.openapi.editor.Document)2 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)2 PsiFile (com.intellij.psi.PsiFile)2 PsiMember (com.intellij.psi.PsiMember)2 PsiNameValuePair (com.intellij.psi.PsiNameValuePair)2