Search in sources :

Example 21 with Types

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

the class AssistedParameters method partitionParametersByType.

// Since Type doesn't have strong equality semantics, we have to use Types.isSameType to
// determine which parameters are conflicting with each other.
private Multimap<Type, VariableTree> partitionParametersByType(List<VariableTree> parameters, VisitorState state) {
    Types types = state.getTypes();
    Multimap<Type, VariableTree> multimap = LinkedListMultimap.create();
    variables: for (VariableTree node : parameters) {
        // Normalize Integer => int
        Type type = types.unboxedTypeOrType(ASTHelpers.getType(node));
        for (Type existingType : multimap.keySet()) {
            if (types.isSameType(existingType, type)) {
                multimap.put(existingType, node);
                continue variables;
            }
        }
        // A new type for the map.
        multimap.put(type, node);
    }
    return multimap;
}
Also used : Types(com.sun.tools.javac.code.Types) MatchType(com.google.errorprone.matchers.ChildMultiMatcher.MatchType) Type(com.sun.tools.javac.code.Type) VariableTree(com.sun.source.tree.VariableTree)

Example 22 with Types

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

the class BoxedPrimitiveConstructor method matchNewClass.

@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(tree.getIdentifier());
    if (sym == null) {
        return NO_MATCH;
    }
    Types types = state.getTypes();
    Symtab symtab = state.getSymtab();
    // TODO(cushon): consider handling String also
    if (sym.equals(types.boxedClass(symtab.byteType)) || sym.equals(types.boxedClass(symtab.charType)) || sym.equals(types.boxedClass(symtab.shortType)) || sym.equals(types.boxedClass(symtab.intType)) || sym.equals(types.boxedClass(symtab.longType)) || sym.equals(types.boxedClass(symtab.doubleType)) || sym.equals(types.boxedClass(symtab.floatType)) || sym.equals(types.boxedClass(symtab.booleanType))) {
        return describeMatch(tree, buildFix(tree, state));
    }
    return NO_MATCH;
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) Types(com.sun.tools.javac.code.Types) Symbol(com.sun.tools.javac.code.Symbol)

Example 23 with Types

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

the class HashtableContains method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (!CONTAINS_MATCHER.matches(tree, state)) {
        return Description.NO_MATCH;
    }
    Description.Builder result = buildDescription(tree);
    // If the collection is not raw, try to figure out if the argument looks like a key
    // or a value.
    List<Type> tyargs = ASTHelpers.getReceiverType(tree).getTypeArguments();
    if (tyargs.size() == 2) {
        // map capture variables to their bounds, e.g. `? extends Number` -> `Number`
        Types types = state.getTypes();
        Type key = ASTHelpers.getUpperBound(tyargs.get(0), types);
        Type value = ASTHelpers.getUpperBound(tyargs.get(1), types);
        Type arg = ASTHelpers.getType(Iterables.getOnlyElement(tree.getArguments()));
        boolean valueShaped = types.isAssignable(arg, value);
        boolean keyShaped = types.isAssignable(arg, key);
        if (keyShaped && !valueShaped) {
            // definitely a key
            result.addFix(replaceMethodName(tree, state, "containsKey"));
            result.setMessage(String.format("contains() is a legacy method that is equivalent to containsValue(), but the " + "argument type '%s' looks like a key", key));
        } else if (valueShaped && !keyShaped) {
            // definitely a value
            result.addFix(replaceMethodName(tree, state, "containsValue"));
        } else if (valueShaped && keyShaped) {
            // ambiguous
            result.addFix(replaceMethodName(tree, state, "containsValue"));
            result.addFix(replaceMethodName(tree, state, "containsKey"));
            result.setMessage(String.format("contains() is a legacy method that is equivalent to containsValue(), but the " + "argument type '%s' could be a key or a value", key));
        } else {
            // this shouldn't have compiled!
            throw new AssertionError(String.format("unexpected argument to contains(): key: %s, value: %s, argument: %s", key, value, arg));
        }
    } else {
        result.addFix(replaceMethodName(tree, state, "containsValue"));
    }
    return result.build();
}
Also used : Types(com.sun.tools.javac.code.Types) Type(com.sun.tools.javac.code.Type) Description(com.google.errorprone.matchers.Description)

Example 24 with Types

use of com.sun.tools.javac.code.Types in project checker-framework by typetools.

the class TreeUtils method findFunction.

/**
 * The type of the lambda or method reference tree is a functional interface type. This method
 * returns the single abstract method declared by that functional interface. (The type of this
 * method is referred to as the function type.)
 *
 * @param tree lambda or member reference tree
 * @param env ProcessingEnvironment
 * @return the single abstract method declared by the type of the tree
 */
public static Symbol findFunction(Tree tree, ProcessingEnvironment env) {
    Context ctx = ((JavacProcessingEnvironment) env).getContext();
    Types javacTypes = Types.instance(ctx);
    return javacTypes.findDescriptorSymbol(((Type) typeOf(tree)).asElement());
}
Also used : Context(com.sun.tools.javac.util.Context) Types(com.sun.tools.javac.code.Types) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Example 25 with Types

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

the class StaticImports method tryAsStaticMember.

/**
 * Returns a {@code StaticImportInfo} for a static field or method import.
 */
private static StaticImportInfo tryAsStaticMember(JCTree.JCFieldAccess access, VisitorState state) {
    Name identifier = access.getIdentifier();
    if (identifier.contentEquals("*")) {
        // so there's nothing to do here.
        return null;
    }
    String importedTypeName = access.getExpression().toString();
    Type importedType = state.getTypeFromString(importedTypeName);
    if (importedType == null) {
        return null;
    }
    Types types = state.getTypes();
    Type canonicalType = types.erasure(importedType);
    if (canonicalType == null) {
        return null;
    }
    Symbol.TypeSymbol baseType;
    {
        Symbol sym = ASTHelpers.getSymbol(access.getExpression());
        if (!(sym instanceof Symbol.TypeSymbol)) {
            return null;
        }
        baseType = (Symbol.TypeSymbol) sym;
    }
    Symbol.PackageSymbol pkgSym = ((JCTree.JCCompilationUnit) state.getPath().getCompilationUnit()).packge;
    ImmutableSet<Symbol> members = lookup(baseType, baseType, identifier, types, pkgSym);
    if (members.isEmpty()) {
        return null;
    }
    /* Find the most specific subtype that defines one of the members that is imported.
     * TODO(gak): we should instead find the most specific subtype with a member that is _used_ */
    Type canonicalOwner = null;
    for (Symbol member : members) {
        Type owner = types.erasure(member.owner.type);
        if (canonicalOwner == null || types.isSubtype(owner, canonicalOwner)) {
            canonicalOwner = owner;
        }
    }
    if (canonicalOwner == null) {
        return null;
    }
    return StaticImportInfo.create(importedTypeName, canonicalOwner.toString(), identifier.toString(), members);
}
Also used : Types(com.sun.tools.javac.code.Types) Type(com.sun.tools.javac.code.Type) Symbol(com.sun.tools.javac.code.Symbol) Name(com.sun.tools.javac.util.Name)

Aggregations

Types (com.sun.tools.javac.code.Types)39 Type (com.sun.tools.javac.code.Type)29 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)10 Tree (com.sun.source.tree.Tree)9 Symbol (com.sun.tools.javac.code.Symbol)9 VisitorState (com.google.errorprone.VisitorState)8 ASTHelpers.getType (com.google.errorprone.util.ASTHelpers.getType)7 ExpressionTree (com.sun.source.tree.ExpressionTree)6 Symtab (com.sun.tools.javac.code.Symtab)6 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)5 Description (com.google.errorprone.matchers.Description)5 ClassTree (com.sun.source.tree.ClassTree)5 MethodTree (com.sun.source.tree.MethodTree)5 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)5 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)5 ArrayList (java.util.ArrayList)5 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)4 List (java.util.List)4 BugPattern (com.google.errorprone.BugPattern)3