Search in sources :

Example 86 with MethodSymbol

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

the class Template method callCheckMethod.

/**
 * Reflectively invoke Resolve.checkMethod(), which despite being package-private is apparently
 * the only useful entry-point into javac8's type inference implementation.
 */
private MethodType callCheckMethod(Warner warner, Inliner inliner, Object resultInfo, List<Type> actualArgTypes, MethodSymbol methodSymbol, Type site, Env<AttrContext> env) throws InferException {
    try {
        Method checkMethod;
        checkMethod = Resolve.class.getDeclaredMethod("checkMethod", Env.class, Type.class, Symbol.class, Class.forName(// ResultInfo is package-private
        "com.sun.tools.javac.comp.Attr$ResultInfo"), List.class, List.class, Warner.class);
        checkMethod.setAccessible(true);
        return (MethodType) checkMethod.invoke(Resolve.instance(inliner.getContext()), env, site, methodSymbol, resultInfo, actualArgTypes, /*freeTypeVariables=*/
        List.<Type>nil(), warner);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof Resolve.InapplicableMethodException) {
            throw new InferException(ImmutableList.of(((Resolve.InapplicableMethodException) e.getTargetException()).getDiagnostic()));
        }
        throw new LinkageError(e.getMessage(), e.getCause());
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage(), e);
    }
}
Also used : MethodType(com.sun.tools.javac.code.Type.MethodType) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) Method(java.lang.reflect.Method) Env(com.sun.tools.javac.comp.Env) Resolve(com.sun.tools.javac.comp.Resolve) InvocationTargetException(java.lang.reflect.InvocationTargetException) Warner(com.sun.tools.javac.util.Warner) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) List(com.sun.tools.javac.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 87 with MethodSymbol

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

the class InconsistentOverloads method processGroupMethods.

private void processGroupMethods(List<MethodTree> groupMethodTrees, VisitorState state) {
    Preconditions.checkArgument(!groupMethodTrees.isEmpty());
    for (ParameterOrderingViolation violation : getViolations(groupMethodTrees)) {
        MethodSymbol methodSymbol = getSymbol(violation.methodTree());
        if (ASTHelpers.findSuperMethods(methodSymbol, state.getTypes()).isEmpty()) {
            Description.Builder description = buildDescription(violation.methodTree());
            description.setMessage(violation.getDescription());
            state.reportMatch(description.build());
        }
    }
}
Also used : Description(com.google.errorprone.matchers.Description) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol)

Example 88 with MethodSymbol

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

the class ConstructorInvokesOverridable method traverse.

@Override
protected void traverse(Tree tree, VisitorState state) {
    // If class is final, no method is overridable.
    ClassTree classTree = state.findEnclosing(ClassTree.class);
    if (classTree.getModifiers().getFlags().contains(Modifier.FINAL)) {
        return;
    }
    ClassSymbol classSym = ASTHelpers.getSymbol(classTree);
    tree.accept(new TreeScanner<Void, Void>() {

        @Override
        public Void visitMethodInvocation(MethodInvocationTree node, Void data) {
            MethodSymbol method = ASTHelpers.getSymbol(node);
            if (method != null && !method.isConstructor() && !method.isStatic() && !method.isPrivate() && !method.getModifiers().contains(Modifier.FINAL) && method.isMemberOf(classSym, state.getTypes())) {
                state.reportMatch(describeMatch(node));
            }
            return super.visitMethodInvocation(node, data);
        }
    }, null);
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ClassTree(com.sun.source.tree.ClassTree)

Example 89 with MethodSymbol

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

the class EqualsHashCode method matchClass.

@Override
public Description matchClass(ClassTree classTree, VisitorState state) {
    TypeSymbol symbol = ASTHelpers.getSymbol(classTree);
    if (symbol.getKind() != ElementKind.CLASS) {
        return Description.NO_MATCH;
    }
    MethodTree equals = null;
    for (Tree member : classTree.getMembers()) {
        if (!(member instanceof MethodTree)) {
            continue;
        }
        MethodTree methodTree = (MethodTree) member;
        if (EQUALS_MATCHER.matches(methodTree, state)) {
            equals = methodTree;
        }
    }
    if (equals == null || isSuppressed(equals)) {
        return Description.NO_MATCH;
    }
    MethodSymbol hashCodeSym = ASTHelpers.resolveExistingMethod(state, symbol, state.getName("hashCode"), ImmutableList.<Type>of(), ImmutableList.<Type>of());
    if (hashCodeSym.owner.equals(state.getSymtab().objectType.tsym)) {
        return describeMatch(equals);
    }
    return Description.NO_MATCH;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) MethodTree(com.sun.source.tree.MethodTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 90 with MethodSymbol

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

the class EqualsIncompatibleType method compatibilityOfTypes.

public static TypeCompatibilityReport compatibilityOfTypes(Type receiverType, Type argumentType, Set<Type> previousReceiverTypes, Set<Type> previousArgumentTypes, VisitorState state) {
    if (receiverType == null || argumentType == null) {
        return TypeCompatibilityReport.createCompatibleReport();
    }
    // 1.7: java.lang.Object can be cast to primitives (implicitly through the boxed primitive type)
    if (ASTHelpers.isCastable(argumentType, receiverType, state)) {
        return leastUpperBoundGenericMismatch(receiverType, argumentType, previousReceiverTypes, previousArgumentTypes, state);
    }
    // Otherwise, we explore the superclasses of the receiver type as well as the interfaces it
    // implements and we collect all overrides of java.lang.Object.equals(). If one of those
    // overrides is inherited by the argument, then we don't flag the equality test.
    Types types = state.getTypes();
    Predicate<MethodSymbol> equalsPredicate = methodSymbol -> !methodSymbol.isStatic() && ((methodSymbol.flags() & Flags.SYNTHETIC) == 0) && types.isSameType(methodSymbol.getReturnType(), state.getSymtab().booleanType) && methodSymbol.getParameters().size() == 1 && types.isSameType(methodSymbol.getParameters().get(0).type, state.getSymtab().objectType);
    Set<MethodSymbol> overridesOfEquals = ASTHelpers.findMatchingMethods(state.getName("equals"), equalsPredicate, receiverType, types);
    Symbol argumentClass = ASTHelpers.getUpperBound(argumentType, state.getTypes()).tsym;
    for (MethodSymbol method : overridesOfEquals) {
        ClassSymbol methodClass = method.enclClass();
        if (argumentClass.isSubClass(methodClass, types) && !methodClass.equals(state.getSymtab().objectType.tsym) && !methodClass.equals(state.getSymtab().enumSym)) {
            // These should be compatible, but check any generic types for their compatbilities.
            return leastUpperBoundGenericMismatch(receiverType, argumentType, previousReceiverTypes, previousArgumentTypes, state);
        }
    }
    return TypeCompatibilityReport.incompatible(receiverType, argumentType);
}
Also used : Matchers.anyOf(com.google.errorprone.matchers.Matchers.anyOf) Matchers.toType(com.google.errorprone.matchers.Matchers.toType) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Matchers.instanceMethod(com.google.errorprone.matchers.Matchers.instanceMethod) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Signatures(com.google.errorprone.util.Signatures) TypeTag(com.sun.tools.javac.code.TypeTag) Matchers.allOf(com.google.errorprone.matchers.Matchers.allOf) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Matcher(com.google.errorprone.matchers.Matcher) Matchers.isSameType(com.google.errorprone.matchers.Matchers.isSameType) Tree(com.sun.source.tree.Tree) Nullable(javax.annotation.Nullable) MethodInvocationTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher) ImmutableSet(com.google.common.collect.ImmutableSet) Matchers.staticMethod(com.google.errorprone.matchers.Matchers.staticMethod) Symbol(com.sun.tools.javac.code.Symbol) Set(java.util.Set) Streams(com.google.common.collect.Streams) BOOLEAN_TYPE(com.google.errorprone.suppliers.Suppliers.BOOLEAN_TYPE) Sets(com.google.common.collect.Sets) List(java.util.List) Types(com.sun.tools.javac.code.Types) Predicate(com.google.common.base.Predicate) Description(com.google.errorprone.matchers.Description) AutoValue(com.google.auto.value.AutoValue) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) Flags(com.sun.tools.javac.code.Flags) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) Types(com.sun.tools.javac.code.Types) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

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