Search in sources :

Example 31 with PsiAnnotation

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

the class AndroidResolveHelperTest method testResolution.

private void testResolution(String text) {
    PsiElement element = getPsiElement(text);
    assertNotNull(element);
    PsiAnnotation annotation = AndroidResolveHelper.getAnnotationForLocal(element, "color");
    assertNotNull(annotation);
    assertEquals(ResourceEvaluator.COLOR_INT_ANNOTATION, annotation.getQualifiedName());
}
Also used : PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiElement(com.intellij.psi.PsiElement)

Example 32 with PsiAnnotation

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

the class AnnotationsRendererTest method createFakeAnnotation.

@NotNull
private static PsiAnnotation createFakeAnnotation(String fqcn) {
    PsiAnnotation annotation = mock(PsiAnnotation.class);
    when(annotation.getQualifiedName()).thenReturn(fqcn);
    return annotation;
}
Also used : PsiAnnotation(com.intellij.psi.PsiAnnotation) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with PsiAnnotation

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

the class AndroidResolveHelperTest method testColorInt.

public void testColorInt() {
    @Language("JAVA") String text = "package p1.p2;\n" + "\n" + "public class Foo {\n" + "    @android.support.annotation.ColorInt int mColor;\n" + "    \n" + "    public void setColor(@android.support.annotation.ColorInt int c) {\n" + "        mColor = <caret>c;\n" + "    }\n" + "}\n";
    PsiElement element = getPsiElement(text);
    assertNotNull(element);
    PsiAnnotation annotation = AndroidResolveHelper.getAnnotationForLocal(element, "c");
    assertNotNull(annotation);
    assertEquals(ResourceEvaluator.COLOR_INT_ANNOTATION, annotation.getQualifiedName());
    annotation = AndroidResolveHelper.getAnnotationForField(element, "p1.p2.Foo", "mColor");
    assertNotNull(annotation);
    assertEquals(ResourceEvaluator.COLOR_INT_ANNOTATION, annotation.getQualifiedName());
}
Also used : Language(org.intellij.lang.annotations.Language) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiElement(com.intellij.psi.PsiElement)

Example 34 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project intellij-plugins by JetBrains.

the class CucumberJavaStepDefinitionSearch method execute.

@Override
public boolean execute(@NotNull final ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) {
    final PsiElement myElement = queryParameters.getElementToSearch();
    if (!(myElement instanceof PsiMethod)) {
        return true;
    }
    final PsiMethod method = (PsiMethod) myElement;
    Boolean isStepDefinition = ReadAction.compute(() -> CucumberJavaUtil.isStepDefinition(method));
    if (!isStepDefinition) {
        return true;
    }
    final PsiAnnotation stepAnnotation = ReadAction.compute(() -> CucumberJavaUtil.getCucumberStepAnnotation(method));
    final String regexp = CucumberJavaUtil.getPatternFromStepDefinition(stepAnnotation);
    if (regexp == null) {
        return true;
    }
    return CucumberUtil.findGherkinReferencesToElement(myElement, regexp, consumer, queryParameters.getEffectiveSearchScope());
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiElement(com.intellij.psi.PsiElement)

Example 35 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project intellij-community by JetBrains.

the class DelegatesToInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitAnnotation(@NotNull GrAnnotation annotation) {
            checkTarget(annotation);
            checkDelegatesTo(annotation);
        }

        private void checkTarget(GrAnnotation annotation) {
            if (!GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO_TARGET.equals(annotation.getQualifiedName()))
                return;
            final PsiElement owner = annotation.getParent().getParent();
            if (!(owner instanceof GrParameter))
                return;
            final boolean isTargetDeclared = annotation.findDeclaredAttributeValue("value") != null;
            String targetName = GrAnnotationUtil.inferStringAttribute(annotation, "value");
            final GrParameterList parameterList = DefaultGroovyMethods.asType(owner.getParent(), GrParameterList.class);
            for (GrParameter parameter : parameterList.getParameters()) {
                final PsiAnnotation delegatesTo = parameter.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO);
                if (delegatesTo != null) {
                    if (isTargetDeclared) {
                        final String curTarget = GrAnnotationUtil.inferStringAttribute(delegatesTo, "target");
                        if (curTarget != null && curTarget.equals(targetName)) {
                            //target is used
                            return;
                        }
                    } else {
                        if (delegatesTo.findDeclaredAttributeValue("target") == null && delegatesTo.findDeclaredAttributeValue("value") == null) {
                            // target is used
                            return;
                        }
                    }
                }
            }
            registerError(annotation.getClassReference(), GroovyInspectionBundle.message("target.annotation.is.unused"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
        }

        private void checkDelegatesTo(GrAnnotation annotation) {
            if (!GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO.equals(annotation.getQualifiedName()))
                return;
            final PsiElement owner = annotation.getParent().getParent();
            if (!(owner instanceof GrParameter))
                return;
            final PsiAnnotationMemberValue targetPair = annotation.findDeclaredAttributeValue("target");
            if (targetPair == null)
                return;
            String targetName = GrAnnotationUtil.inferStringAttribute(annotation, "target");
            final GrParameterList parameterList = DefaultGroovyMethods.asType(owner.getParent(), GrParameterList.class);
            for (GrParameter parameter : parameterList.getParameters()) {
                final PsiAnnotation target = parameter.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO_TARGET);
                if (target != null) {
                    final String curTarget = GrAnnotationUtil.inferStringAttribute(target, "value");
                    if (curTarget != null && curTarget.equals(targetName)) {
                        //target is used
                        return;
                    }
                }
            }
            registerError(targetPair, GroovyInspectionBundle.message("target.0.does.not.exist", targetName != null ? targetName : "?"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
        }
    };
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) PsiAnnotation(com.intellij.psi.PsiAnnotation) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) NotNull(org.jetbrains.annotations.NotNull)

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