use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class RestrictedApiChecker method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
RestrictedApi annotation = ASTHelpers.getAnnotation(tree, RestrictedApi.class);
if (annotation != null) {
return checkRestriction(annotation, tree, state);
}
MethodSymbol methSymbol = ASTHelpers.getSymbol(tree);
if (methSymbol == null) {
// This shouldn't happen, but has. (See b/33758055)
return Description.NO_MATCH;
}
// Try each super method for @RestrictedApi
Optional<MethodSymbol> superWithRestrictedApi = ASTHelpers.findSuperMethods(methSymbol, state.getTypes()).stream().filter((t) -> ASTHelpers.hasAnnotation(t, RestrictedApi.class, state)).findFirst();
if (!superWithRestrictedApi.isPresent()) {
return Description.NO_MATCH;
}
return checkRestriction(ASTHelpers.getAnnotation(superWithRestrictedApi.get(), RestrictedApi.class), tree, state);
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class IncompatibleArgumentType method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
Type calledMethodType = ASTHelpers.getType(methodInvocationTree.getMethodSelect());
Type calledClazzType = ASTHelpers.getReceiverType(methodInvocationTree);
List<? extends ExpressionTree> arguments = methodInvocationTree.getArguments();
MethodSymbol declaredMethod = ASTHelpers.getSymbol(methodInvocationTree);
if (arguments.isEmpty() || declaredMethod == null) {
return null;
}
List<RequiredType> requiredTypesAtCallSite = new ArrayList<>(Collections.nCopies(arguments.size(), null));
Types types = state.getTypes();
if (!populateTypesToEnforce(declaredMethod, calledMethodType, calledClazzType, requiredTypesAtCallSite, state)) {
// No annotations on this method, try the supers;
for (MethodSymbol method : ASTHelpers.findSuperMethods(declaredMethod, types)) {
if (populateTypesToEnforce(method, calledMethodType, calledClazzType, requiredTypesAtCallSite, state)) {
break;
}
}
}
reportAnyViolations(arguments, requiredTypesAtCallSite, state);
// We manually report ourselves, so we don't pass any errors up the chain.
return Description.NO_MATCH;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class WaitNotInLoop method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!matcher.matches(tree, state)) {
return Description.NO_MATCH;
}
Description.Builder description = buildDescription(tree);
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym != null) {
description.setMessage(String.format(MESSAGE_TEMPLATE, sym));
}
// mechanically, so we provide detailed instructions in the wiki content.
if (!waitMethodWithTimeout.matches(tree, state)) {
JCIf enclosingIf = ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), JCIf.class);
if (enclosingIf != null && enclosingIf.getElseStatement() == null) {
CharSequence ifSource = state.getSourceForNode(enclosingIf);
if (ifSource == null) {
// Source isn't available, so we can't construct a fix
return description.build();
}
String replacement = ifSource.toString().replaceFirst("if", "while");
return description.addFix(SuggestedFix.replace(enclosingIf, replacement)).build();
}
}
return description.build();
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class ProvidesNull method matchReturn.
/**
* Matches explicit "return null" statements in methods annotated with {@code @Provides} but not
* {@code @Nullable}. Suggests either annotating the method with {@code @Nullable} or throwing a
* {@link RuntimeException} instead.
*/
// TODO(eaftan): Use nullness dataflow analysis when it's ready
@Override
public Description matchReturn(ReturnTree returnTree, VisitorState state) {
ExpressionTree returnExpression = returnTree.getExpression();
if (returnExpression == null || returnExpression.getKind() != Kind.NULL_LITERAL) {
return Description.NO_MATCH;
}
TreePath path = state.getPath();
MethodTree enclosingMethod = null;
while (true) {
if (path == null || path.getLeaf() instanceof LambdaExpressionTree) {
return Description.NO_MATCH;
} else if (path.getLeaf() instanceof MethodTree) {
enclosingMethod = (MethodTree) path.getLeaf();
break;
} else {
path = path.getParentPath();
}
}
MethodSymbol enclosingMethodSym = ASTHelpers.getSymbol(enclosingMethod);
if (enclosingMethodSym == null) {
return Description.NO_MATCH;
}
if (!ASTHelpers.hasAnnotation(enclosingMethodSym, "dagger.Provides", state) || ASTHelpers.hasDirectAnnotationWithSimpleName(enclosingMethodSym, "Nullable")) {
return Description.NO_MATCH;
}
Fix addNullableFix = SuggestedFix.builder().prefixWith(enclosingMethod, "@Nullable\n").addImport("javax.annotation.Nullable").build();
CatchTree enclosingCatch = ASTHelpers.findEnclosingNode(state.getPath(), CatchTree.class);
if (enclosingCatch == null) {
// If not in a catch block, suggest adding @Nullable first, then throwing an exception.
Fix throwRuntimeExceptionFix = SuggestedFix.replace(returnTree, "throw new RuntimeException();");
return buildDescription(returnTree).addFix(addNullableFix).addFix(throwRuntimeExceptionFix).build();
} else {
// If in a catch block, suggest throwing an exception first, then adding @Nullable.
String replacement = String.format("throw new RuntimeException(%s);", enclosingCatch.getParameter().getName());
Fix throwRuntimeExceptionFix = SuggestedFix.replace(returnTree, replacement);
return buildDescription(returnTree).addFix(throwRuntimeExceptionFix).addFix(addNullableFix).build();
}
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class ReferenceEquality method inEqualsOrCompareTo.
private boolean inEqualsOrCompareTo(Type classType, Type type, VisitorState state) {
MethodTree methodTree = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
if (methodTree == null) {
return false;
}
MethodSymbol sym = ASTHelpers.getSymbol(methodTree);
if (sym == null || sym.isStatic()) {
return false;
}
Symbol compareTo = getOnlyMember(state, state.getSymtab().comparableType, "compareTo");
Symbol equals = getOnlyMember(state, state.getSymtab().objectType, "equals");
if (!sym.overrides(compareTo, classType.tsym, state.getTypes(), false) && !sym.overrides(equals, classType.tsym, state.getTypes(), false)) {
return false;
}
if (!ASTHelpers.isSameType(type, classType, state)) {
return false;
}
return true;
}
Aggregations