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