Search in sources :

Example 1 with JavaElementVisitor

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

the class HighlightSeverityTest method testErrorLikeUnusedSymbol.

public void testErrorLikeUnusedSymbol() throws Exception {
    enableInspectionTool(new LocalInspectionTool() {

        @NotNull
        @Override
        public String getShortName() {
            return getDisplayName();
        }

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

                @Override
                public void visitIdentifier(PsiIdentifier identifier) {
                    if (identifier.getText().equals("k")) {
                        holder.registerProblem(identifier, "Variable 'k' is never used");
                    }
                }
            };
        }

        @NotNull
        @Override
        public HighlightDisplayLevel getDefaultLevel() {
            return HighlightDisplayLevel.ERROR;
        }

        @Nls
        @NotNull
        @Override
        public String getDisplayName() {
            return "x";
        }

        @Nls
        @NotNull
        @Override
        public String getGroupDisplayName() {
            return getDisplayName();
        }
    });
    doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false);
}
Also used : HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) JavaElementVisitor(com.intellij.psi.JavaElementVisitor) Nls(org.jetbrains.annotations.Nls) NonNls(org.jetbrains.annotations.NonNls) LocalInspectionToolSession(com.intellij.codeInspection.LocalInspectionToolSession) PsiIdentifier(com.intellij.psi.PsiIdentifier) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder)

Example 2 with JavaElementVisitor

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

the class AssertEqualsBetweenInconvertibleTypesTestNGInspection method buildVisitor.

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

        @Override
        public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
            super.visitMethodCallExpression(expression);
            final String errorMessage = AssertHint.areExpectedActualTypesCompatible(expression, true);
            if (errorMessage != null) {
                final PsiElement referenceNameElement = expression.getMethodExpression().getReferenceNameElement();
                if (referenceNameElement == null) {
                    return;
                }
                holder.registerProblem(referenceNameElement, errorMessage);
            }
        }
    };
}
Also used : JavaElementVisitor(com.intellij.psi.JavaElementVisitor) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with JavaElementVisitor

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

the class JavaPsiVisitor method visitFile.

void visitFile(@NonNull final JavaContext context) {
    try {
        final PsiJavaFile javaFile = mParser.parseJavaToPsi(context);
        if (javaFile == null) {
            // with details, location, etc.
            return;
        }
        try {
            context.setJavaFile(javaFile);
            mParser.runReadAction(new Runnable() {

                @Override
                public void run() {
                    for (VisitingDetector v : mAllDetectors) {
                        v.setContext(context);
                        v.getDetector().beforeCheckFile(context);
                    }
                }
            });
            if (!mSuperClassDetectors.isEmpty()) {
                mParser.runReadAction(new Runnable() {

                    @Override
                    public void run() {
                        SuperclassPsiVisitor visitor = new SuperclassPsiVisitor(context);
                        javaFile.accept(visitor);
                    }
                });
            }
            for (final VisitingDetector v : mFullTreeDetectors) {
                mParser.runReadAction(new Runnable() {

                    @Override
                    public void run() {
                        JavaElementVisitor visitor = v.getVisitor();
                        javaFile.accept(visitor);
                    }
                });
            }
            if (!mMethodDetectors.isEmpty() || !mResourceFieldDetectors.isEmpty() || !mConstructorDetectors.isEmpty() || !mReferenceDetectors.isEmpty()) {
                mParser.runReadAction(new Runnable() {

                    @Override
                    public void run() {
                        // TODO: Do we need to break this one up into finer grain
                        // locking units
                        JavaElementVisitor visitor = new DelegatingPsiVisitor(context);
                        javaFile.accept(visitor);
                    }
                });
            } else {
                if (!mNodePsiTypeDetectors.isEmpty()) {
                    mParser.runReadAction(new Runnable() {

                        @Override
                        public void run() {
                            // TODO: Do we need to break this one up into finer grain
                            // locking units
                            JavaElementVisitor visitor = new DispatchPsiVisitor();
                            javaFile.accept(visitor);
                        }
                    });
                }
            }
            mParser.runReadAction(new Runnable() {

                @Override
                public void run() {
                    for (VisitingDetector v : mAllDetectors) {
                        v.getDetector().afterCheckFile(context);
                    }
                }
            });
        } finally {
            mParser.dispose(context, javaFile);
            context.setJavaFile(null);
        }
    } catch (ProcessCanceledException ignore) {
    // Cancelling inspections in the IDE
    } catch (RuntimeException e) {
        if (sExceptionCount++ > MAX_REPORTED_CRASHES) {
            // are tripping up ECJ, they get the picture.
            return;
        }
        if (e.getClass().getSimpleName().equals("IndexNotReadyException")) {
            // See http://b.android.com/176644 for an example.
            return;
        }
        // Work around ECJ bugs; see https://code.google.com/p/android/issues/detail?id=172268
        // Don't allow lint bugs to take down the whole build. TRY to log this as a
        // lint error instead!
        StringBuilder sb = new StringBuilder(100);
        sb.append("Unexpected failure during lint analysis of ");
        sb.append(context.file.getName());
        sb.append(" (this is a bug in lint or one of the libraries it depends on)\n");
        sb.append(e.getClass().getSimpleName());
        sb.append(':');
        StackTraceElement[] stackTrace = e.getStackTrace();
        int count = 0;
        for (StackTraceElement frame : stackTrace) {
            if (count > 0) {
                sb.append("<-");
            }
            String className = frame.getClassName();
            sb.append(className.substring(className.lastIndexOf('.') + 1));
            sb.append('.').append(frame.getMethodName());
            sb.append('(');
            sb.append(frame.getFileName()).append(':').append(frame.getLineNumber());
            sb.append(')');
            count++;
            // Only print the top 3-4 frames such that we can identify the bug
            if (count == 4) {
                break;
            }
        }
        // NOT e: this makes for very noisy logs
        Throwable throwable = null;
        //noinspection ConstantConditions
        context.log(throwable, sb.toString());
    }
}
Also used : PsiJavaFile(com.intellij.psi.PsiJavaFile) JavaElementVisitor(com.intellij.psi.JavaElementVisitor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 4 with JavaElementVisitor

use of com.intellij.psi.JavaElementVisitor in project Main by SpartanRefactoring.

the class JavadocMarkerNanoPattern method createReplacement.

@Override
public final PsiElement createReplacement(PsiMethod m) {
    String docOld = step.docCommentString(m), docNew = docOld + tag();
    final Wrapper<String> methodText = new Wrapper<>("");
    m.acceptChildren(new JavaElementVisitor() {

        @Override
        public void visitElement(PsiElement e) {
            if (!iz.javadoc(e))
                methodText.set(methodText.get() + e.getText());
            super.visitElement(e);
        }
    });
    return JavaPsiFacade.getElementFactory(m.getProject()).createMethodFromText("/**" + docNew + "*/" + methodText, m.getContext());
}
Also used : Wrapper(il.org.spartan.Leonidas.auxilary_layer.Wrapper) JavaElementVisitor(com.intellij.psi.JavaElementVisitor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

JavaElementVisitor (com.intellij.psi.JavaElementVisitor)4 PsiElement (com.intellij.psi.PsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)1 LocalInspectionTool (com.intellij.codeInspection.LocalInspectionTool)1 LocalInspectionToolSession (com.intellij.codeInspection.LocalInspectionToolSession)1 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 PsiIdentifier (com.intellij.psi.PsiIdentifier)1 PsiJavaFile (com.intellij.psi.PsiJavaFile)1 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)1 Wrapper (il.org.spartan.Leonidas.auxilary_layer.Wrapper)1 Nls (org.jetbrains.annotations.Nls)1 NonNls (org.jetbrains.annotations.NonNls)1