Search in sources :

Example 6 with PsiModifierListOwner

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

the class ToggleSourceInferredAnnotations method isAvailable.

@Override
public boolean isAvailable(@NotNull final Project project, Editor editor, PsiFile file) {
    final PsiElement leaf = file.findElementAt(editor.getCaretModel().getOffset());
    final PsiModifierListOwner owner = getAnnotationOwner(leaf);
    if (owner != null && isSourceCode(owner)) {
        boolean hasSrcInferredAnnotation = ContainerUtil.or(findSignatureNonCodeAnnotations(owner, true), annotation -> AnnotationUtil.isInferredAnnotation(annotation));
        if (hasSrcInferredAnnotation) {
            setText((CodeInsightSettings.getInstance().SHOW_SOURCE_INFERRED_ANNOTATIONS ? "Hide" : "Show") + " annotations inferred from source code");
            return true;
        }
    }
    return false;
}
Also used : PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PsiModifierListOwner

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

the class SameReturnValueInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor processor) {
    if (refEntity instanceof RefMethod) {
        final RefMethod refMethod = (RefMethod) refEntity;
        if (refMethod.isConstructor())
            return null;
        if (refMethod.hasSuperMethods())
            return null;
        String returnValue = refMethod.getReturnValueIfSame();
        if (returnValue != null) {
            final String message;
            if (refMethod.getDerivedMethods().isEmpty()) {
                message = InspectionsBundle.message("inspection.same.return.value.problem.descriptor", "<code>" + returnValue + "</code>");
            } else if (refMethod.hasBody()) {
                message = InspectionsBundle.message("inspection.same.return.value.problem.descriptor1", "<code>" + returnValue + "</code>");
            } else {
                message = InspectionsBundle.message("inspection.same.return.value.problem.descriptor2", "<code>" + returnValue + "</code>");
            }
            final PsiModifierListOwner element = refMethod.getElement();
            if (element != null) {
                return new ProblemDescriptor[] { manager.createProblemDescriptor(element.getNavigationElement(), message, false, null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING) };
            }
        }
    }
    return null;
}
Also used : PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with PsiModifierListOwner

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

the class AddAnnotationIntention method isAvailable.

// include not in project files
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    final PsiModifierListOwner owner = AddAnnotationPsiFix.getContainer(file, editor.getCaretModel().getOffset());
    if (owner == null || !ExternalAnnotationsManagerImpl.areExternalAnnotationsApplicable(owner)) {
        return false;
    }
    Pair<String, String[]> annotations = getAnnotations(project);
    String toAdd = annotations.first;
    String[] toRemove = annotations.second;
    if (toRemove.length > 0 && isAnnotatedSkipInferred(owner, toRemove)) {
        return false;
    }
    setText(AddAnnotationPsiFix.calcText(owner, toAdd));
    if (isAnnotatedSkipInferred(owner, toAdd))
        return false;
    return canAnnotate(owner);
}
Also used : PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner)

Example 9 with PsiModifierListOwner

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

the class AddAnnotationIntention method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PsiModifierListOwner owner = AddAnnotationPsiFix.getContainer(file, editor.getCaretModel().getOffset());
    if (owner == null || !owner.isValid())
        return;
    Pair<String, String[]> annotations = getAnnotations(project);
    String toAdd = annotations.first;
    String[] toRemove = annotations.second;
    AddAnnotationFix fix = new AddAnnotationFix(toAdd, owner, toRemove);
    fix.invoke(project, editor, file);
}
Also used : PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix)

Example 10 with PsiModifierListOwner

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

the class GrDeprecatedAPIUsageInspection method buildVisitor.

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

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
            super.visitReferenceExpression(ref);
            checkRef(ref);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement ref) {
            super.visitCodeReferenceElement(ref);
            checkRef(ref);
        }

        private void checkRef(GrReferenceElement ref) {
            PsiElement resolved = ref.resolve();
            if (isDeprecated(resolved)) {
                PsiElement toHighlight = getElementToHighlight(ref);
                registerError(toHighlight, GroovyBundle.message("0.is.deprecated", ref.getReferenceName()), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.LIKE_DEPRECATED);
            }
        }

        @NotNull
        public PsiElement getElementToHighlight(@NotNull GrReferenceElement refElement) {
            final PsiElement refNameElement = refElement.getReferenceNameElement();
            return refNameElement != null ? refNameElement : refElement;
        }

        private boolean isDeprecated(PsiElement resolved) {
            if (resolved instanceof PsiDocCommentOwner) {
                return ((PsiDocCommentOwner) resolved).isDeprecated();
            }
            if (resolved instanceof PsiModifierListOwner && PsiImplUtil.isDeprecatedByAnnotation((PsiModifierListOwner) resolved)) {
                return true;
            }
            return false;
        }
    };
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) PsiDocCommentOwner(com.intellij.psi.PsiDocCommentOwner) PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) NotNull(org.jetbrains.annotations.NotNull) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiModifierListOwner (com.intellij.psi.PsiModifierListOwner)12 PsiElement (com.intellij.psi.PsiElement)4 PsiModifierList (com.intellij.psi.PsiModifierList)3 AddAnnotationFix (com.intellij.codeInsight.intention.AddAnnotationFix)2 Project (com.intellij.openapi.project.Project)2 PsiAnnotation (com.intellij.psi.PsiAnnotation)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiDocCommentOwner (com.intellij.psi.PsiDocCommentOwner)1 PsiFile (com.intellij.psi.PsiFile)1 PsiLiteralExpressionImpl (com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl)1 AndroidLintQuickFix (org.jetbrains.android.inspections.lint.AndroidLintQuickFix)1 JavaAnnotationImpl (org.jetbrains.kotlin.load.java.structure.impl.JavaAnnotationImpl)1 JavaModifierListOwnerImpl (org.jetbrains.kotlin.load.java.structure.impl.JavaModifierListOwnerImpl)1 BaseInspectionVisitor (org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor)1 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)1