Search in sources :

Example 16 with Type

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

the class JUnit3FloatingPointComparisonWithoutDelta method canBeConvertedToJUnit4.

/**
   * Determines if the invocation can be safely converted to JUnit 4 based on its argument types.
   */
private boolean canBeConvertedToJUnit4(VisitorState state, List<Type> argumentTypes) {
    // Delta argument is used.
    if (argumentTypes.size() > 2) {
        return true;
    }
    Type firstType = argumentTypes.get(0);
    Type secondType = argumentTypes.get(1);
    // Neither argument is floating-point.
    if (!isFloatingPoint(state, firstType) && !isFloatingPoint(state, secondType)) {
        return true;
    }
    // One argument is not numeric.
    if (!isNumeric(state, firstType) || !isNumeric(state, secondType)) {
        return true;
    }
    // Neither argument is primitive.
    if (!firstType.isPrimitive() && !secondType.isPrimitive()) {
        return true;
    }
    return false;
}
Also used : Type(com.sun.tools.javac.code.Type)

Example 17 with Type

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

the class MissingDefault method matchSwitch.

@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
    Type switchType = ASTHelpers.getType(tree.getExpression());
    if (switchType.asElement().getKind() == ElementKind.ENUM) {
        // by MissingCasesInEnumSwitch
        return NO_MATCH;
    }
    Optional<? extends CaseTree> maybeDefault = tree.getCases().stream().filter(c -> c.getExpression() == null).findFirst();
    if (!maybeDefault.isPresent()) {
        Description.Builder description = buildDescription(tree);
        if (!tree.getCases().isEmpty()) {
            // Inserting the default after the last case is easier than finding the closing brace
            // for the switch statement. Hopefully we don't often see switches with zero cases.
            description.addFix(SuggestedFix.postfixWith(getLast(tree.getCases()), "\ndefault: // fall out\n"));
        }
        return description.build();
    }
    CaseTree defaultCase = maybeDefault.get();
    if (!defaultCase.getStatements().isEmpty()) {
        return NO_MATCH;
    }
    int idx = tree.getCases().indexOf(defaultCase);
    // The default case may appear before a non-default case, in which case the documentation
    // should say "fall through" instead of "fall out".
    boolean isLast = idx == tree.getCases().size() - 1;
    int end = isLast ? state.getEndPosition(tree) : ((JCTree) tree.getCases().get(idx + 1)).getStartPosition();
    if (ErrorProneTokens.getTokens(state.getSourceCode().subSequence(state.getEndPosition(defaultCase), end).toString(), state.context).stream().anyMatch(t -> !t.comments().isEmpty())) {
        return NO_MATCH;
    }
    return buildDescription(defaultCase).setMessage("Default case should be documented with a comment").addFix(SuggestedFix.postfixWith(defaultCase, isLast ? " // fall out" : " // fall through")).build();
}
Also used : ElementKind(javax.lang.model.element.ElementKind) Iterables.getLast(com.google.common.collect.Iterables.getLast) NO_MATCH(com.google.errorprone.matchers.Description.NO_MATCH) CaseTree(com.sun.source.tree.CaseTree) JCTree(com.sun.tools.javac.tree.JCTree) SwitchTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher) SwitchTree(com.sun.source.tree.SwitchTree) VisitorState(com.google.errorprone.VisitorState) ErrorProneTokens(com.google.errorprone.util.ErrorProneTokens) Description(com.google.errorprone.matchers.Description) BugPattern(com.google.errorprone.BugPattern) Optional(java.util.Optional) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) JDK(com.google.errorprone.BugPattern.Category.JDK) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) CaseTree(com.sun.source.tree.CaseTree) Type(com.sun.tools.javac.code.Type) Description(com.google.errorprone.matchers.Description)

Example 18 with Type

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

the class JUnit3FloatingPointComparisonWithoutDelta method getArgumentTypesWithoutMessage.

/**
   * Gets the argument types, excluding the message argument if present.
   */
private List<Type> getArgumentTypesWithoutMessage(MethodInvocationTree methodInvocationTree, VisitorState state) {
    List<Type> argumentTypes = new ArrayList<>();
    for (ExpressionTree argument : methodInvocationTree.getArguments()) {
        JCTree tree = (JCTree) argument;
        argumentTypes.add(tree.type);
    }
    removeMessageArgumentIfPresent(state, argumentTypes);
    return argumentTypes;
}
Also used : Type(com.sun.tools.javac.code.Type) ArrayList(java.util.ArrayList) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 19 with Type

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

the class RedundantThrows method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    List<? extends ExpressionTree> thrown = tree.getThrows();
    if (thrown.isEmpty()) {
        return NO_MATCH;
    }
    SetMultimap<Symbol, ExpressionTree> exceptionsBySuper = LinkedHashMultimap.create();
    for (ExpressionTree exception : thrown) {
        Type type = getType(exception);
        do {
            type = state.getTypes().supertype(type);
            exceptionsBySuper.put(type.tsym, exception);
        } while (!state.getTypes().isSameType(type, state.getSymtab().objectType));
    }
    Set<ExpressionTree> toRemove = new HashSet<>();
    List<String> messages = new ArrayList<>();
    for (ExpressionTree exception : thrown) {
        Symbol sym = getSymbol(exception);
        if (exceptionsBySuper.containsKey(sym)) {
            Set<ExpressionTree> sub = exceptionsBySuper.get(sym);
            messages.add(String.format("%s %s of %s", oxfordJoin(", ", sub), sub.size() == 1 ? "is a subtype" : "are subtypes", sym.getSimpleName()));
            toRemove.addAll(sub);
        }
    }
    if (toRemove.isEmpty()) {
        return NO_MATCH;
    }
    // sort by order in input
    List<ExpressionTree> delete = ImmutableList.<ExpressionTree>copyOf(Iterables.filter(tree.getThrows(), Predicates.in(toRemove)));
    return buildDescription(delete.get(0)).setMessage("Redundant throws clause: " + oxfordJoin("; ", messages)).addFix(SuggestedFixes.deleteExceptions(tree, state, delete)).build();
}
Also used : ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Type(com.sun.tools.javac.code.Type) ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) ArrayList(java.util.ArrayList) ExpressionTree(com.sun.source.tree.ExpressionTree) HashSet(java.util.HashSet)

Example 20 with Type

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

the class ForOverrideChecker method getOutermostClass.

/** Get the outermost class/interface/enum of an element, or null if none. */
private Type getOutermostClass(VisitorState state) {
    TreePath path = state.getPath();
    Type type = null;
    while (path != null) {
        if (path.getLeaf().getKind() == Kind.CLASS || path.getLeaf().getKind() == Kind.INTERFACE || path.getLeaf().getKind() == Kind.ENUM) {
            type = ASTHelpers.getSymbol(path.getLeaf()).type;
        }
        path = path.getParentPath();
    }
    return type;
}
Also used : Type(com.sun.tools.javac.code.Type) TreePath(com.sun.source.util.TreePath)

Aggregations

Type (com.sun.tools.javac.code.Type)253 Symbol (com.sun.tools.javac.code.Symbol)64 ClassType (com.sun.tools.javac.code.Type.ClassType)55 ArrayType (com.sun.tools.javac.code.Type.ArrayType)47 MethodType (com.sun.tools.javac.code.Type.MethodType)42 WildcardType (com.sun.tools.javac.code.Type.WildcardType)42 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)36 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)33 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)32 JCTree (com.sun.tools.javac.tree.JCTree)27 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)24 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)23 Types (com.sun.tools.javac.code.Types)22 ExpressionTree (com.sun.source.tree.ExpressionTree)21 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)17 ArrayList (java.util.ArrayList)17 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)16 Tree (com.sun.source.tree.Tree)14 Name (com.sun.tools.javac.util.Name)14 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)13