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);
}
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);
}
}
};
}
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());
}
}
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());
}
Aggregations