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;
}
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;
}
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();
}
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());
}
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);
}
Aggregations