use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class InlinabilityResult method forMethod.
static InlinabilityResult forMethod(MethodTree tree, VisitorState state) {
if (tree.getBody() == null) {
return fromError(InlineValidationErrorReason.NO_BODY);
}
if (tree.getBody().getStatements().size() != 1) {
return fromError(InlineValidationErrorReason.NOT_EXACTLY_ONE_STATEMENT);
}
MethodSymbol methSymbol = getSymbol(tree);
if (methSymbol.getModifiers().contains(Modifier.PRIVATE)) {
return fromError(InlineValidationErrorReason.API_IS_PRIVATE);
}
StatementTree statement = tree.getBody().getStatements().get(0);
if (state.getSourceForNode(statement) == null) {
return fromError(InlineValidationErrorReason.NO_BODY);
}
// we can only inline either a ExpressionStatementTree or a ReturnTree
ExpressionTree body;
// InlinabilityResult.forMethod
switch(statement.getKind()) {
case EXPRESSION_STATEMENT:
body = ((ExpressionStatementTree) statement).getExpression();
break;
case RETURN:
body = ((ReturnTree) statement).getExpression();
if (body == null) {
return fromError(InlineValidationErrorReason.EMPTY_VOID);
}
break;
default:
return fromError(InlineValidationErrorReason.COMPLEX_STATEMENT);
}
if (methSymbol.isVarArgs() && usesVarargsParamPoorly(body, methSymbol.params().last(), state)) {
return fromError(InlineValidationErrorReason.VARARGS_USED_UNSAFELY, body);
}
// contains
if (body.toString().contains("{")) {
return fromError(InlineValidationErrorReason.COMPLEX_STATEMENT, body);
}
Symbol usedMultipliedTimes = usesVariablesMultipleTimes(body, methSymbol.params(), state);
if (usedMultipliedTimes != null) {
return fromError(InlineValidationErrorReason.REUSE_OF_ARGUMENTS, body, usedMultipliedTimes.toString());
}
Tree privateOrDeprecatedApi = usesPrivateOrDeprecatedApis(body, state, getVisibility(methSymbol));
if (privateOrDeprecatedApi != null) {
return fromError(InlineValidationErrorReason.CALLS_DEPRECATED_OR_PRIVATE_APIS, body, state.getSourceForNode(privateOrDeprecatedApi));
}
if (hasLambdaCapturingParameters(tree, body)) {
return fromError(InlineValidationErrorReason.LAMBDA_CAPTURES_PARAMETER, body);
}
if (ASTHelpers.methodCanBeOverridden(methSymbol)) {
// overridden due to having no publicly-accessible constructors.
return fromError(methSymbol.isDefault() ? InlineValidationErrorReason.METHOD_CAN_BE_OVERIDDEN_AND_CANT_BE_FIXED : InlineValidationErrorReason.METHOD_CAN_BE_OVERIDDEN_BUT_CAN_BE_FIXED, body);
}
return inlinable(body);
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class ImmutableSetForContains method convertListToSetInit.
private static SuggestedFix convertListToSetInit(VariableTree var, VisitorState state) {
SuggestedFix.Builder fix = SuggestedFix.builder().addImport(ImmutableSet.class.getName()).replace(stripParameters(var.getType()), "ImmutableSet");
if (IMMUTABLE_LIST_FACTORIES.matches(var.getInitializer(), state)) {
fix.replace(getReceiver(var.getInitializer()), "ImmutableSet");
return fix.build();
}
if (IMMUTABLE_COLLECTION.matches(var.getInitializer(), state)) {
fix.addStaticImport("com.google.common.collect.ImmutableSet.toImmutableSet").replace(getOnlyElement(((MethodInvocationTree) var.getInitializer()).getArguments()), "toImmutableSet()");
return fix.build();
}
if (IMMUTABLE_LIST_BUILD.matches(var.getInitializer(), state)) {
Optional<ExpressionTree> rootExpr = getRootMethod((MethodInvocationTree) var.getInitializer(), state);
if (rootExpr.isPresent()) {
if (rootExpr.get().getKind().equals(Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodTree = (MethodInvocationTree) rootExpr.get();
fix.replace(getReceiver(methodTree), "ImmutableSet");
return fix.build();
}
if (rootExpr.get().getKind().equals(Kind.NEW_CLASS)) {
NewClassTree ctorTree = (NewClassTree) rootExpr.get();
fix.replace(stripParameters(ctorTree.getIdentifier()), "ImmutableSet.Builder");
}
return fix.build();
}
}
return fix.replace(var.getInitializer(), "ImmutableSet.copyOf(" + state.getSourceForNode(var.getInitializer()) + ")").build();
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class ForEachIterable method isNext.
private static boolean isNext(VariableTree tree, VisitorState state, TreePath p) {
Tree parentTree = p.getParentPath().getLeaf();
if (!(parentTree instanceof ExpressionTree)) {
return false;
}
ExpressionTree parent = (ExpressionTree) parentTree;
return NEXT.matches(parent, state) && getSymbol(tree).equals(getSymbol(getReceiver(parent)));
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class ForEachIterable method match.
private Description match(VariableTree tree, VisitorState state, int startPosition, ExpressionTree condition, StatementTree body) {
if (tree.getInitializer() == null || !ITERATOR.matches(tree.getInitializer(), state)) {
return NO_MATCH;
}
VarSymbol iterator = getSymbol(tree);
if (!isHasNext(iterator, stripParentheses(condition), state)) {
return NO_MATCH;
}
ImmutableList<TreePath> uses = findUses(state, body, iterator);
if (uses.size() != 1 || !uses.stream().allMatch(p -> isNext(tree, state, p))) {
return NO_MATCH;
}
Type iteratorType = state.getTypes().asSuper(getType(tree.getType()), state.getSymtab().iteratorType.tsym);
if (iteratorType == null || iteratorType.getTypeArguments().isEmpty()) {
return NO_MATCH;
}
SuggestedFix.Builder fix = SuggestedFix.builder();
VariableTree existingVariable = existingVariable(iterator, body, state);
String replacement;
if (existingVariable != null) {
replacement = existingVariable.getName().toString();
fix.delete(existingVariable);
} else {
replacement = "element";
uses.forEach(p -> {
TreePath path = p.getParentPath().getParentPath();
switch(path.getParentPath().getLeaf().getKind()) {
case EXPRESSION_STATEMENT:
fix.delete(path.getParentPath().getLeaf());
break;
default:
fix.replace(path.getLeaf(), replacement);
break;
}
});
}
Type elementType = getOnlyElement(iteratorType.getTypeArguments());
if (elementType.hasTag(TypeTag.WILDCARD)) {
elementType = getUpperBound(elementType, state.getTypes());
}
Tree iterableExprNode = getReceiver(tree.getInitializer());
String iterableExpr = iterableExprNode != null ? state.getSourceForNode(iterableExprNode) : "this";
fix.replace(startPosition, getStartPosition(body), String.format("for (%s %s : %s) ", SuggestedFixes.prettyType(state, fix, elementType), replacement, iterableExpr));
return describeMatch(tree, fix.build());
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class IsInstanceIncompatibleType method classTypeArgument.
private static Type classTypeArgument(ExpressionTree tree) {
ExpressionTree receiver = getReceiver(tree);
if (receiver == null) {
return null;
}
List<Type> receiverTypeArguments = getType(receiver).getTypeArguments();
return !receiverTypeArguments.isEmpty() ? getOnlyElement(receiverTypeArguments) : null;
}
Aggregations