Search in sources :

Example 41 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project kotlin by JetBrains.

the class ResourceEvaluator method getTypesFromAnnotations.

@Nullable
private EnumSet<ResourceType> getTypesFromAnnotations(PsiModifierListOwner owner) {
    if (mEvaluator == null) {
        return null;
    }
    for (PsiAnnotation annotation : mEvaluator.getAllAnnotations(owner)) {
        String signature = annotation.getQualifiedName();
        if (signature == null) {
            continue;
        }
        if (signature.equals(COLOR_INT_ANNOTATION)) {
            return EnumSet.of(COLOR_INT_MARKER_TYPE);
        }
        if (signature.equals(PX_ANNOTATION)) {
            return EnumSet.of(PX_MARKER_TYPE);
        }
        if (signature.endsWith(RES_SUFFIX) && signature.startsWith(SUPPORT_ANNOTATIONS_PREFIX)) {
            String typeString = signature.substring(SUPPORT_ANNOTATIONS_PREFIX.length(), signature.length() - RES_SUFFIX.length()).toLowerCase(Locale.US);
            ResourceType type = ResourceType.getEnum(typeString);
            if (type != null) {
                return EnumSet.of(type);
            } else if (typeString.equals("any")) {
                // @AnyRes
                return getAnyRes();
            }
        }
    }
    return null;
}
Also used : ResourceType(com.android.resources.ResourceType) PsiAnnotation(com.intellij.psi.PsiAnnotation) Nullable(com.android.annotations.Nullable)

Example 42 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project kotlin by JetBrains.

the class CallSuperDetector method getRequiredSuperMethod.

/**
     * Checks whether the given method overrides a method which requires the super method
     * to be invoked, and if so, returns it (otherwise returns null)
     */
@Nullable
private static PsiMethod getRequiredSuperMethod(@NonNull JavaContext context, @NonNull PsiMethod method) {
    JavaEvaluator evaluator = context.getEvaluator();
    PsiMethod directSuper = evaluator.getSuperMethod(method);
    if (directSuper == null) {
        return null;
    }
    String name = method.getName();
    if (ON_DETACHED_FROM_WINDOW.equals(name)) {
        // compileSdkVersion >= N).
        if (!evaluator.isMemberInSubClassOf(method, CLASS_VIEW, false)) {
            return null;
        }
        return directSuper;
    } else if (ON_VISIBILITY_CHANGED.equals(name)) {
        // the support library
        if (!evaluator.isMemberInSubClassOf(method, "android.support.wearable.watchface.WatchFaceService.Engine", false)) {
            return null;
        }
        return directSuper;
    }
    // Look up annotations metadata
    PsiMethod superMethod = directSuper;
    while (superMethod != null) {
        PsiAnnotation[] annotations = superMethod.getModifierList().getAnnotations();
        annotations = filterRelevantAnnotations(context.getEvaluator(), annotations);
        for (PsiAnnotation annotation : annotations) {
            String signature = annotation.getQualifiedName();
            if (CALL_SUPER_ANNOTATION.equals(signature)) {
                return directSuper;
            } else if (signature != null && signature.endsWith(".OverrideMustInvoke")) {
                // Handle findbugs annotation on the fly too
                return directSuper;
            }
        }
        superMethod = evaluator.getSuperMethod(superMethod);
    }
    return null;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiAnnotation(com.intellij.psi.PsiAnnotation) JavaEvaluator(com.android.tools.klint.client.api.JavaEvaluator) Nullable(com.android.annotations.Nullable)

Example 43 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation 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 44 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation 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 45 with PsiAnnotation

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

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