use of com.google.errorprone.VisitorState in project error-prone by google.
the class ASTHelpersTest method testHasDirectAnnotationWithSimpleName.
@Test
public void testHasDirectAnnotationWithSimpleName() {
writeFile(//
"A.java", "public class A {", " @Deprecated public void doIt() {}", "}");
TestScanner scanner = new TestScanner() {
@Override
public Void visitMethod(MethodTree tree, VisitorState state) {
if (tree.getName().contentEquals("doIt")) {
setAssertionsComplete();
Symbol sym = ASTHelpers.getSymbol(tree);
assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Deprecated")).isTrue();
assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Nullable")).isFalse();
}
return super.visitMethod(tree, state);
}
};
tests.add(scanner);
assertCompiles(scanner);
}
use of com.google.errorprone.VisitorState in project error-prone by google.
the class ASTHelpersTest method getUpperBoundScanner.
/* Tests for ASTHelpers#getUpperBound */
private TestScanner getUpperBoundScanner(final String expectedBound) {
return new TestScanner() {
@Override
public Void visitVariable(VariableTree tree, VisitorState state) {
setAssertionsComplete();
Type varType = ASTHelpers.getType(tree.getType());
assertThat(ASTHelpers.getUpperBound(varType.getTypeArguments().get(0), state.getTypes()).toString()).isEqualTo(expectedBound);
return super.visitVariable(tree, state);
}
};
}
use of com.google.errorprone.VisitorState in project error-prone by google.
the class ASTHelpersTest method testGetUpperBoundCapturedTypeVariable.
@Test
public void testGetUpperBoundCapturedTypeVariable() {
writeFile("A.java", "import java.lang.Number;", "import java.util.List;", "public class A {", " public void doSomething(List<? extends Number> list) {", " list.get(0);", " }", "}");
TestScanner scanner = new TestScanner() {
@Override
public Void visitMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!"super()".equals(tree.toString())) {
// ignore synthetic super call
setAssertionsComplete();
Type type = ASTHelpers.getType(tree);
assertThat(type instanceof TypeVar).isTrue();
assertThat(((TypeVar) type).isCaptured()).isTrue();
assertThat(ASTHelpers.getUpperBound(type, state.getTypes()).toString()).isEqualTo("java.lang.Number");
}
return super.visitMethodInvocation(tree, state);
}
};
tests.add(scanner);
assertCompiles(scanner);
}
use of com.google.errorprone.VisitorState in project error-prone by google.
the class TypeParameterShadowing method renameTypeVariable.
private static SuggestedFix renameTypeVariable(Tree sourceTree, List<? extends TypeParameterTree> typeParameters, Name typeVariable, String typeVarReplacement, VisitorState state) {
TypeParameterTree matchingTypeParam = typeParameters.stream().filter(t -> t.getName().contentEquals(typeVariable)).collect(MoreCollectors.onlyElement());
Symbol typeVariableSymbol = ASTHelpers.getSymbol(matchingTypeParam);
// replace only the type parameter name (and not any upper bounds)
String name = matchingTypeParam.getName().toString();
int pos = ((JCTree) matchingTypeParam).getStartPosition();
SuggestedFix.Builder fixBuilder = SuggestedFix.builder().replace(pos, pos + name.length(), typeVarReplacement);
((JCTree) sourceTree).accept(new TreeScanner() {
@Override
public void visitIdent(JCTree.JCIdent tree) {
Symbol identSym = ASTHelpers.getSymbol(tree);
if (Objects.equal(identSym, typeVariableSymbol)) {
// }
if (Objects.equal(state.getSourceForNode(tree), name)) {
fixBuilder.replace(tree, typeVarReplacement);
}
}
}
});
return fixBuilder.build();
}
use of com.google.errorprone.VisitorState in project error-prone by google.
the class UTemplater method visitMethodInvocation.
@Override
public UExpression visitMethodInvocation(MethodInvocationTree tree, Void v) {
if (anyMatch(ANY_OF, tree.getMethodSelect(), new Unifier(context))) {
return UAnyOf.create(templateExpressions(tree.getArguments()));
} else if (anyMatch(IS_INSTANCE, tree.getMethodSelect(), new Unifier(context))) {
return UInstanceOf.create(template(Iterables.getOnlyElement(tree.getArguments())), templateType(getSingleExplicitTypeArgument(tree)));
} else if (anyMatch(CLAZZ, tree.getMethodSelect(), new Unifier(context))) {
Tree typeArg = getSingleExplicitTypeArgument(tree);
return UMemberSelect.create(templateType(typeArg), "class", UClassType.create("java.lang.Class", template(((JCTree) typeArg).type)));
} else if (anyMatch(NEW_ARRAY, tree.getMethodSelect(), new Unifier(context))) {
Tree typeArg = getSingleExplicitTypeArgument(tree);
ExpressionTree lengthArg = Iterables.getOnlyElement(tree.getArguments());
return UNewArray.create(templateType(typeArg), ImmutableList.of(template(lengthArg)), null);
} else if (anyMatch(ENUM_VALUE_OF, tree.getMethodSelect(), new Unifier(context))) {
Tree typeArg = getSingleExplicitTypeArgument(tree);
ExpressionTree strArg = Iterables.getOnlyElement(tree.getArguments());
return UMethodInvocation.create(UMemberSelect.create(templateType(typeArg), "valueOf", UMethodType.create(template(((JCTree) typeArg).type), UClassType.create("java.lang.String"))), template(strArg));
} else if (anyMatch(AS_VARARGS, tree.getMethodSelect(), new Unifier(context))) {
ExpressionTree arg = Iterables.getOnlyElement(tree.getArguments());
checkArgument(ASTHelpers.hasAnnotation(arg, Repeated.class, new VisitorState(context)));
return template(arg);
}
Map<MethodSymbol, PlaceholderMethod> placeholderMethods = context.get(RefasterRuleBuilderScanner.PLACEHOLDER_METHODS_KEY);
if (placeholderMethods != null && placeholderMethods.containsKey(ASTHelpers.getSymbol(tree))) {
return UPlaceholderExpression.create(placeholderMethods.get(ASTHelpers.getSymbol(tree)), templateExpressions(tree.getArguments()));
} else {
return UMethodInvocation.create(template(tree.getMethodSelect()), templateExpressions(tree.getArguments()));
}
}
Aggregations