use of com.sun.tools.javac.tree.TreeScanner 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.TreeScanner in project error-prone by google.
the class CompilerBasedTest method compile.
protected void compile(JavaFileObject fileObject) {
final ImmutableMap.Builder<String, JCMethodDecl> methodsBuilder = ImmutableMap.builder();
final ImmutableList.Builder<JCCompilationUnit> compilationUnitsBuilder = ImmutableList.builder();
compile(new TreeScanner() {
@Override
public void visitMethodDef(JCMethodDecl tree) {
if (!TreeInfo.isConstructor(tree)) {
methodsBuilder.put(tree.getName().toString(), tree);
}
}
@Override
public void visitTopLevel(JCCompilationUnit tree) {
compilationUnitsBuilder.add(tree);
super.visitTopLevel(tree);
}
}, fileObject);
this.methods = methodsBuilder.build();
this.compilationUnits = compilationUnitsBuilder.build();
}
use of com.sun.tools.javac.tree.TreeScanner 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();
// For a lambda parameter without explicit type, it will return null.
String source = state.getSourceForNode(tree.getType());
int typeLength = source == null ? 0 : source.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.TreeScanner in project error-prone by google.
the class TypeParameterShadowing method renameTypeVariable.
static SuggestedFix renameTypeVariable(TypeParameterTree typeParameter, Tree owningTree, String typeVarReplacement, VisitorState state) {
Symbol typeVariableSymbol = ASTHelpers.getSymbol(typeParameter);
// replace only the type parameter name (and not any upper bounds)
String name = typeParameter.getName().toString();
int pos = ((JCTree) typeParameter).getStartPosition();
SuggestedFix.Builder fixBuilder = SuggestedFix.builder().replace(pos, pos + name.length(), typeVarReplacement);
((JCTree) owningTree).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.TreeScanner in project ceylon-compiler by ceylon.
the class TestLog method test.
static void test(boolean genEndPos) throws IOException {
Context context = new Context();
Options options = Options.instance(context);
options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
Log log = Log.instance(context);
log.multipleErrors = true;
JavacFileManager.preRegister(context);
ParserFactory pfac = ParserFactory.instance(context);
final String text = "public class Foo {\n" + " public static void main(String[] args) {\n" + " if (args.length == 0)\n" + " System.out.println(\"no args\");\n" + " else\n" + " System.out.println(args.length + \" args\");\n" + " }\n" + "}\n";
JavaFileObject fo = new StringJavaFileObject("Foo", text);
log.useSource(fo);
CharSequence cs = fo.getCharContent(true);
Parser parser = pfac.newParser(cs, false, genEndPos, false);
JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
log.setEndPosTable(fo, tree.endPositions);
TreeScanner ts = new LogTester(log, tree.endPositions);
ts.scan(tree);
check(log.nerrors, 4, "errors");
check(log.nwarnings, 4, "warnings");
}
Aggregations