use of com.sun.tools.javac.tree.JCTree in project error-prone by google.
the class SuggestedFixes method renameVariable.
/**
* Renames the given {@link VariableTree} and its usages in the current compilation unit to {@code
* replacement}.
*/
public static SuggestedFix renameVariable(VariableTree tree, final String replacement, VisitorState state) {
String name = tree.getName().toString();
int typeLength = state.getSourceForNode(tree.getType()).length();
int pos = ((JCTree) tree).getStartPosition() + state.getSourceForNode(tree).indexOf(name, typeLength);
final SuggestedFix.Builder fix = SuggestedFix.builder().replace(pos, pos + name.length(), replacement);
final Symbol.VarSymbol sym = getSymbol(tree);
((JCTree) state.getPath().getCompilationUnit()).accept(new TreeScanner() {
@Override
public void visitIdent(JCTree.JCIdent tree) {
if (sym.equals(getSymbol(tree))) {
fix.replace(tree, replacement);
}
}
});
return fix.build();
}
use of com.sun.tools.javac.tree.JCTree in project error-prone by google.
the class UTemplaterTest method testTemplateWithoutTypes.
/**
* Parses a Java expression, templates it using a {@link UTemplater}, and tests that the template
* matches the original parsed expression.
*/
private void testTemplateWithoutTypes(String expression) {
JCTree ast = parseExpression(expression);
UTree<?> template = (UTree<?>) new UTemplater(context).template(ast);
assertNotNull(template.unify(ast, unifier));
}
use of com.sun.tools.javac.tree.JCTree 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.sun.tools.javac.tree.JCTree in project error-prone by google.
the class StaticGuardedByInstance method matchSynchronized.
@Override
public Description matchSynchronized(SynchronizedTree tree, VisitorState state) {
Symbol lock = ASTHelpers.getSymbol(TreeInfo.skipParens((JCTree) tree.getExpression()));
if (!(lock instanceof VarSymbol)) {
return Description.NO_MATCH;
}
if (lock.isStatic()) {
return Description.NO_MATCH;
}
Multimap<VarSymbol, Tree> writes = WriteVisitor.scan(tree.getBlock());
for (Entry<VarSymbol, Tree> write : writes.entries()) {
if (!write.getKey().isStatic()) {
continue;
}
state.reportMatch(buildDescription(write.getValue()).setMessage(String.format(MESSAGE, lock)).build());
}
return Description.NO_MATCH;
}
use of com.sun.tools.javac.tree.JCTree in project error-prone by google.
the class BlockTemplateMatch method getRange.
@Override
public String getRange(JCCompilationUnit unit) {
try {
CharSequence sequence = unit.getSourceFile().getCharContent(true);
JCTree firstStatement = statements.get(0);
JCTree lastStatement = statements.get(statements.size() - 1);
return sequence.subSequence(firstStatement.getStartPosition(), lastStatement.getEndPosition(unit.endPositions)).toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations