use of com.sun.source.tree.ClassTree in project error-prone by google.
the class JdkObsolete method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
Tree parent = state.getPath().getParentPath().getLeaf();
if (parent instanceof NewClassTree && tree.equals(((NewClassTree) parent).getClassBody())) {
// don't double-report anonymous implementations of obsolete interfaces
return NO_MATCH;
}
ClassSymbol symbol = ASTHelpers.getSymbol(tree);
if (symbol == null) {
return NO_MATCH;
}
return describeIfObsolete(null, state.getTypes().directSupertypes(symbol.asType()), state);
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class InputStreamSlowMultibyteRead method maybeMatchReadByte.
private Description maybeMatchReadByte(MethodTree readByteMethod, VisitorState state) {
if (readByteMethod.getBody() != null) {
// Null-check for native/abstract overrides of read()
List<? extends StatementTree> statements = readByteMethod.getBody().getStatements();
if (statements.size() == 1) {
Tree tree = statements.get(0);
if (tree.getKind() == Kind.RETURN && ASTHelpers.constValue(((ReturnTree) tree).getExpression()) != null) {
return Description.NO_MATCH;
}
}
}
// Streams within JUnit test cases are likely to be OK as well.
TreePath enclosingPath = ASTHelpers.findPathFromEnclosingNodeToTopLevel(state.getPath(), ClassTree.class);
while (enclosingPath != null) {
ClassTree klazz = (ClassTree) enclosingPath.getLeaf();
if (JUnitMatchers.isTestCaseDescendant.matches(klazz, state) || hasAnnotation(JUnitMatchers.JUNIT4_RUN_WITH_ANNOTATION).matches(klazz, state)) {
return Description.NO_MATCH;
}
enclosingPath = ASTHelpers.findPathFromEnclosingNodeToTopLevel(enclosingPath, ClassTree.class);
}
return describeMatch(readByteMethod);
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class MultipleTopLevelClasses method matchCompilationUnit.
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
if (tree.getTypeDecls().size() <= 1) {
// else should have exactly one.
return Description.NO_MATCH;
}
List<String> names = new ArrayList<>();
for (Tree member : tree.getTypeDecls()) {
if (member instanceof ClassTree) {
ClassTree classMember = (ClassTree) member;
switch(classMember.getKind()) {
case CLASS:
case INTERFACE:
case ANNOTATION_TYPE:
case ENUM:
if (isSuppressed(classMember)) {
// and @SuppressWarnings can't be applied to packages.
return Description.NO_MATCH;
}
names.add(classMember.getSimpleName().toString());
break;
default:
break;
}
}
}
if (names.size() <= 1) {
// empty (e.g. ";" at the top level counts as an empty type decl)
return Description.NO_MATCH;
}
String message = String.format("Expected at most one top-level class declaration, instead found: %s", Joiner.on(", ").join(names));
return buildDescription(firstNonNull(tree.getPackageName(), tree.getTypeDecls().get(0))).setMessage(message).build();
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class OverrideThrowableToString method matchClass.
@Override
public Description matchClass(ClassTree classTree, VisitorState visitorState) {
Symbol throwableClass = visitorState.getSymbolFromString("java.lang.Throwable");
if (Objects.equals(ASTHelpers.getSymbol(classTree.getExtendsClause()), throwableClass)) {
Optional<? extends Tree> methodTree = classTree.getMembers().stream().filter(m -> m instanceof MethodTree && ((MethodTree) m).getName().contentEquals("toString")).findFirst();
if (methodTree.isPresent()) {
SuggestedFix.Builder builder = SuggestedFix.builder();
MethodTree tree = (MethodTree) methodTree.get();
if (!tree.getParameters().isEmpty()) {
return Description.NO_MATCH;
}
String newTree = tree.getModifiers().toString().replaceAll("@Override[(][)]", "@Override") + "String getMessage()\n" + visitorState.getSourceForNode(tree.getBody());
builder.replace(tree, newTree);
return describeMatch(classTree, builder.build());
}
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.ClassTree in project robolectric by robolectric.
the class DeprecatedMethodsCheck method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
if (isInShadowClass(state.getPath(), state)) {
return NO_MATCH;
}
final SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
HashMap<Tree, Runnable> possibleFixes = new HashMap<>();
new TreeScanner<Void, VisitorState>() {
private boolean inShadowClass;
@Override
public Void visitClass(ClassTree classTree, VisitorState visitorState) {
boolean priorInShadowClass = inShadowClass;
inShadowClass = hasAnnotation(classTree, Implements.class, visitorState);
try {
return super.visitClass(classTree, visitorState);
} finally {
inShadowClass = priorInShadowClass;
}
}
@Override
public Void visitMethodInvocation(MethodInvocationTree tree, VisitorState state) {
VisitorState nowState = state.withPath(TreePath.getPath(state.getPath(), tree));
if (!inShadowClass) {
for (MethodInvocationMatcher matcher : matchers) {
if (matcher.matcher().matches(tree, state)) {
matcher.replace(tree, nowState, fixBuilder, possibleFixes);
return null;
}
}
}
return super.visitMethodInvocation(tree, nowState);
}
}.scan(tree, state);
for (Runnable runnable : possibleFixes.values()) {
runnable.run();
}
Fix fix = fixBuilder.build();
return fix.isEmpty() ? NO_MATCH : describeMatch(tree, fix);
}
Aggregations