Search in sources :

Example 26 with Types

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();
}
Also used : Types(com.sun.tools.javac.code.Types) Type(com.sun.tools.javac.code.Type) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Example 27 with Types

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;
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) Types(com.sun.tools.javac.code.Types)

Example 28 with Types

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;
}
Also used : Types(com.sun.tools.javac.code.Types) EqualsIncompatibleType(com.google.errorprone.bugpatterns.EqualsIncompatibleType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ArrayList(java.util.ArrayList)

Example 29 with Types

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));
            }
        }
    }
}
Also used : Types(com.sun.tools.javac.code.Types) EqualsIncompatibleType(com.google.errorprone.bugpatterns.EqualsIncompatibleType) Type(com.sun.tools.javac.code.Type) ExpressionTree(com.sun.source.tree.ExpressionTree) EqualsIncompatibleType(com.google.errorprone.bugpatterns.EqualsIncompatibleType)

Example 30 with 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();
}
Also used : Types(com.sun.tools.javac.code.Types) Type(com.sun.tools.javac.code.Type) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date)

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