Search in sources :

Example 1 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project lombok by rzwitserloot.

the class HandleExtensionMethod method getExtension.

public Extension getExtension(final JavacNode typeNode, final ClassType extensionMethodProviderType) {
    List<MethodSymbol> extensionMethods = new ArrayList<MethodSymbol>();
    TypeSymbol tsym = extensionMethodProviderType.asElement();
    if (tsym != null)
        for (Symbol member : tsym.getEnclosedElements()) {
            if (member.getKind() != ElementKind.METHOD)
                continue;
            MethodSymbol method = (MethodSymbol) member;
            if ((method.flags() & (STATIC | PUBLIC)) == 0)
                continue;
            if (method.params().isEmpty())
                continue;
            extensionMethods.add(method);
        }
    return new Extension(extensionMethods, tsym);
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) ArrayList(java.util.ArrayList) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 2 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.

the class TypeParameterUnusedInFormals method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree);
    if (methodSymbol == null) {
        return Description.NO_MATCH;
    }
    // Only match methods where the return type is just a type parameter.
    // e.g. the following is OK: <T> List<T> newArrayList();
    TypeVar retType;
    switch(methodSymbol.getReturnType().getKind()) {
        case TYPEVAR:
            retType = (TypeVar) methodSymbol.getReturnType();
            break;
        default:
            return Description.NO_MATCH;
    }
    if (!methodSymbol.equals(retType.tsym.owner)) {
        return Description.NO_MATCH;
    }
    // e.g.: <T extends Enum<T>> T unsafeEnumDeserializer();
    if (retType.bound != null && TypeParameterFinder.visit(retType.bound).contains(retType.tsym)) {
        return Description.NO_MATCH;
    }
    // e.g.: <T> T noop(T t);
    for (VarSymbol formalParam : methodSymbol.getParameters()) {
        if (TypeParameterFinder.visit(formalParam.type).contains(retType.tsym)) {
            return Description.NO_MATCH;
        }
    }
    return describeMatch(tree);
}
Also used : TypeVar(com.sun.tools.javac.code.Type.TypeVar) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 3 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.

the class UnnecessaryTypeArgument method check.

private Description check(Tree tree, List<? extends Tree> arguments, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(tree);
    if (!(sym instanceof MethodSymbol)) {
        return Description.NO_MATCH;
    }
    MethodSymbol methodSymbol = (MethodSymbol) sym;
    int expected = methodSymbol.getTypeParameters().size();
    int actual = arguments.size();
    if (actual <= expected) {
        return Description.NO_MATCH;
    }
    for (MethodSymbol superMethod : ASTHelpers.findSuperMethods(methodSymbol, state.getTypes())) {
        if (!superMethod.getTypeParameters().isEmpty()) {
            // two types.
            return Description.NO_MATCH;
        }
    }
    return describeMatch(tree, buildFix(tree, arguments, state));
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Example 4 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.

the class TryFailThrowable method getMessageSnippet.

private static String getMessageSnippet(StatementTree failStatement, VisitorState state, HasOtherParameters hasOtherParameters) {
    ExpressionTree expression = ((ExpressionStatementTree) failStatement).getExpression();
    MethodSymbol sym = (MethodSymbol) getSymbol(expression);
    String tail = hasOtherParameters == HasOtherParameters.TRUE ? ", " : "";
    // The above casts were checked earlier by failOrAssert.
    return hasInitialStringParameter(sym, state) ? state.getSourceForNode(((MethodInvocationTree) expression).getArguments().get(0)) + tail : "";
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree)

Example 5 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.

the class MustBeClosedChecker method matchMethodInvocation.

/**
   * Check that invocations of methods annotated with {@link MustBeClosed} are called within the
   * resource variable initializer of a try-with-resources statement.
   */
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    MethodSymbol msym = getSymbol(tree);
    if (msym == null) {
        return Description.NO_MATCH;
    }
    String methodName = msym.getSimpleName().toString();
    return matchNewClassOrMethodInvocation(methodName, tree, state);
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol)

Aggregations

MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)98 Symbol (com.sun.tools.javac.code.Symbol)35 Type (com.sun.tools.javac.code.Type)32 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)27 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)22 MethodTree (com.sun.source.tree.MethodTree)22 Tree (com.sun.source.tree.Tree)21 ArrayList (java.util.ArrayList)18 ExpressionTree (com.sun.source.tree.ExpressionTree)17 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)13 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)13 Description (com.google.errorprone.matchers.Description)11 ClassTree (com.sun.source.tree.ClassTree)11 VisitorState (com.google.errorprone.VisitorState)10 JCTree (com.sun.tools.javac.tree.JCTree)9 Types (com.sun.tools.javac.code.Types)8 BugPattern (com.google.errorprone.BugPattern)7 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7 MethodType (com.sun.tools.javac.code.Type.MethodType)7