use of com.sun.tools.javac.code.Symbol.MethodSymbol in project bazel by bazelbuild.
the class TreeUtils method hasExplicitConstructor.
/**
* Determine whether the given class contains an explicit constructor.
*
* @param node A class tree.
* @return True, iff there is an explicit constructor.
*/
public static boolean hasExplicitConstructor(ClassTree node) {
TypeElement elem = TreeUtils.elementFromDeclaration(node);
for (ExecutableElement ee : ElementFilter.constructorsIn(elem.getEnclosedElements())) {
MethodSymbol ms = (MethodSymbol) ee;
long mod = ms.flags();
if ((mod & Flags.SYNTHETIC) == 0) {
return true;
}
}
return false;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class ChainingConstructorIgnoresParameter method matchMethod.
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol symbol = getSymbol(tree);
if (!symbol.isConstructor()) {
return NO_MATCH;
}
paramTypesForMethod.put(symbol, unmodifiableList(tree.getParameters()));
return evaluateCallers(symbol);
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class CheckReturnValue method checkEnclosingClasses.
private static Optional<Boolean> checkEnclosingClasses(MethodSymbol method, VisitorState state) {
Symbol enclosingClass = enclosingClass(method);
while (enclosingClass instanceof ClassSymbol) {
Optional<Boolean> result = shouldCheckReturnValue(enclosingClass, state);
if (result.isPresent()) {
return result;
}
enclosingClass = enclosingClass.owner;
}
return Optional.absent();
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class FunctionalInterfaceClash method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ClassSymbol origin = getSymbol(tree);
Types types = state.getTypes();
// collect declared and inherited methods whose signature contains a functional interface
Multimap<String, MethodSymbol> methods = HashMultimap.create();
for (Symbol sym : types.membersClosure(getType(tree), /*skipInterface=*/
false).getSymbols()) {
if (!(sym instanceof MethodSymbol)) {
continue;
}
if (isBugCheckerSuppressed((MethodSymbol) sym)) {
continue;
}
MethodSymbol msym = (MethodSymbol) sym;
if (msym.getParameters().stream().noneMatch(p -> maybeFunctionalInterface(p.type, types))) {
continue;
}
if (msym.isConstructor() && !msym.owner.equals(origin)) {
continue;
}
methods.put(functionalInterfaceSignature(state, msym), msym);
}
// (don't report clashes between inherited members)
for (Tree member : tree.getMembers()) {
if (!(member instanceof MethodTree)) {
continue;
}
MethodSymbol msym = getSymbol((MethodTree) member);
if (msym.getParameters().stream().noneMatch(p -> maybeFunctionalInterface(p.type, types))) {
continue;
}
Collection<MethodSymbol> clash = new ArrayList<>(methods.removeAll(functionalInterfaceSignature(state, msym)));
clash.remove(msym);
// ignore inherited methods that are overridden in the original class
clash.removeIf(m -> msym.overrides(m, origin, types, false));
if (!clash.isEmpty()) {
String message = "When passing lambda arguments to this function, callers will need a cast to" + " disambiguate with: " + clash.stream().map(m -> Signatures.prettyMethodSignature(origin, m)).collect(joining("\n "));
state.reportMatch(buildDescription(member).setMessage(message).build());
}
}
return NO_MATCH;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class InfiniteRecursion method matchMethod.
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
if (tree.getBody() == null || tree.getBody().getStatements().size() != 1) {
return NO_MATCH;
}
Tree statement = TreeInfo.skipParens((JCTree) Iterables.getOnlyElement(tree.getBody().getStatements()));
ExpressionTree expr = statement.accept(new SimpleTreeVisitor<ExpressionTree, Void>() {
@Override
public ExpressionTree visitExpressionStatement(ExpressionStatementTree tree, Void unused) {
return tree.getExpression();
}
@Override
public ExpressionTree visitReturn(ReturnTree tree, Void unused) {
return tree.getExpression();
}
}, null);
if (!(expr instanceof MethodInvocationTree)) {
return NO_MATCH;
}
ExpressionTree select = ((MethodInvocationTree) expr).getMethodSelect();
switch(select.getKind()) {
case IDENTIFIER:
break;
case MEMBER_SELECT:
ExpressionTree receiver = ((MemberSelectTree) select).getExpression();
if (receiver.getKind() != Kind.IDENTIFIER) {
return NO_MATCH;
}
if (!((IdentifierTree) receiver).getName().contentEquals("this")) {
return NO_MATCH;
}
break;
default:
return NO_MATCH;
}
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null || !sym.equals(ASTHelpers.getSymbol(expr))) {
return NO_MATCH;
}
return describeMatch(statement);
}
Aggregations