use of com.sun.tools.javac.code.Types in project error-prone by google.
the class JdkObsolete method linkedListFix.
// rewrite e.g. `List<Object> xs = new LinkedList<>()` -> `... = new ArrayList<>()`
private static Optional<Fix> linkedListFix(Tree tree, VisitorState state) {
Type type = getTargetType(state);
if (type == null) {
return Optional.empty();
}
Types types = state.getTypes();
for (String replacement : ImmutableList.of("java.util.ArrayList", "java.util.ArrayDeque")) {
Symbol sym = state.getSymbolFromString(replacement);
if (sym == null) {
continue;
}
if (types.isAssignable(types.erasure(sym.asType()), types.erasure(type))) {
SuggestedFix.Builder fix = SuggestedFix.builder();
while (tree instanceof ParameterizedTypeTree) {
tree = ((ParameterizedTypeTree) tree).getType();
}
fix.replace(tree, SuggestedFixes.qualifyType(state, fix, sym));
return Optional.of(fix.build());
}
}
return Optional.empty();
}
use of com.sun.tools.javac.code.Types in project error-prone by google.
the class NCopiesOfChar method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!MATCHER.matches(tree, state)) {
return NO_MATCH;
}
List<? extends ExpressionTree> arguments = tree.getArguments();
Symtab syms = state.getSymtab();
Types types = state.getTypes();
if (types.isSameType(types.unboxedTypeOrType(getType(arguments.get(1))), syms.intType) && types.isSameType(types.unboxedTypeOrType(getType(arguments.get(0))), syms.charType)) {
return describeMatch(tree, SuggestedFix.builder().replace(arguments.get(0), state.getSourceForNode(arguments.get(1))).replace(arguments.get(1), state.getSourceForNode(arguments.get(0))).build());
}
return NO_MATCH;
}
use of com.sun.tools.javac.code.Types in project error-prone by google.
the class IncompatibleArgumentType method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
// example:
// class Foo<A> {
// <B> void bar(@CompatibleWith("A") Object o, @CompatibleWith("B") Object o2) {}
// }
// new Foo<Integer>().<String>bar(1, "a');
// A Type substitution capturing <Integer> on Foo and <String> on bar(Object,Object);
Type calledMethodType = ASTHelpers.getType(methodInvocationTree.getMethodSelect());
// A Type substitution capturing <Integer> on Foo
Type calledClazzType = ASTHelpers.getReceiverType(methodInvocationTree);
List<? extends ExpressionTree> arguments = methodInvocationTree.getArguments();
// The unbound MethodSymbol for bar(), with type parameters <A> and <B>
MethodSymbol declaredMethod = ASTHelpers.getSymbol(methodInvocationTree);
if (arguments.isEmpty() || declaredMethod == null) {
return null;
}
List<RequiredType> requiredTypesAtCallSite = new ArrayList<>(Collections.nCopies(arguments.size(), null));
Types types = state.getTypes();
if (!populateTypesToEnforce(declaredMethod, calledMethodType, calledClazzType, requiredTypesAtCallSite, state)) {
// No annotations on this method, try the supers;
for (MethodSymbol method : ASTHelpers.findSuperMethods(declaredMethod, types)) {
if (populateTypesToEnforce(method, calledMethodType, calledClazzType, requiredTypesAtCallSite, state)) {
break;
}
}
}
reportAnyViolations(arguments, requiredTypesAtCallSite, state);
// We manually report ourselves, so we don't pass any errors up the chain.
return Description.NO_MATCH;
}
use of com.sun.tools.javac.code.Types in project error-prone by google.
the class IncompatibleArgumentType method reportAnyViolations.
private void reportAnyViolations(List<? extends ExpressionTree> arguments, List<RequiredType> requiredArgumentTypes, VisitorState state) {
Types types = state.getTypes();
for (int i = 0; i < requiredArgumentTypes.size(); i++) {
RequiredType requiredType = requiredArgumentTypes.get(i);
if (requiredType == null) {
continue;
}
ExpressionTree argument = arguments.get(i);
Type argType = ASTHelpers.getType(argument);
if (requiredType.type() != null) {
// Report a violation for this type
EqualsIncompatibleType.TypeCompatibilityReport report = EqualsIncompatibleType.compatibilityOfTypes(requiredType.type(), argType, state);
if (!report.compatible()) {
state.reportMatch(describeViolation(argument, argType, requiredType.type(), types));
}
}
}
}
use of com.sun.tools.javac.code.Types in project error-prone by google.
the class FormatStringValidation method getInstance.
private static Object getInstance(Type type, VisitorState state) {
Types types = state.getTypes();
if (type.getKind() == TypeKind.NULL) {
return null;
}
// normalize boxed primitives
Type unboxedType = types.unboxedTypeOrType(types.erasure(type));
if (unboxedType.isPrimitive()) {
type = unboxedType;
switch(type.getKind()) {
case BOOLEAN:
return false;
case BYTE:
return Byte.valueOf((byte) 1);
case SHORT:
return Short.valueOf((short) 2);
case INT:
return Integer.valueOf(3);
case LONG:
return Long.valueOf(4);
case CHAR:
return Character.valueOf('c');
case FLOAT:
return Float.valueOf(5.0f);
case DOUBLE:
return Double.valueOf(6.0d);
case VOID:
case NONE:
case NULL:
case ERROR:
return null;
case ARRAY:
return new Object[0];
default:
throw new AssertionError(type.getKind());
}
}
if (types.isSubtype(type, state.getSymtab().stringType)) {
return String.valueOf("string");
}
if (types.isSubtype(type, state.getTypeFromString(BigDecimal.class.getName()))) {
return BigDecimal.valueOf(42.0d);
}
if (types.isSubtype(type, state.getTypeFromString(BigInteger.class.getName()))) {
return BigInteger.valueOf(43L);
}
if (types.isSubtype(type, state.getTypeFromString(Date.class.getName()))) {
return new Date();
}
if (types.isSubtype(type, state.getTypeFromString(Calendar.class.getName()))) {
return new GregorianCalendar();
}
if (types.isSubtype(type, state.getTypeFromString(TemporalAccessor.class.getName()))) {
return LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
}
return new Object();
}
Aggregations