Search in sources :

Example 1 with TreeScanner

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();
}
Also used : ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) MethodTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher) MethodTree(com.sun.source.tree.MethodTree) TreeScanner(com.sun.tools.javac.tree.TreeScanner) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ArrayList(java.util.ArrayList) VisitorState(com.google.errorprone.VisitorState) Matcher(java.util.regex.Matcher) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Objects(com.google.common.base.Objects) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) Name(javax.lang.model.element.Name) MoreCollectors(com.google.common.collect.MoreCollectors) Symbol(com.sun.tools.javac.code.Symbol) Set(java.util.Set) Streams(com.google.common.collect.Streams) JCTree(com.sun.tools.javac.tree.JCTree) Collectors(java.util.stream.Collectors) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) List(java.util.List) Description(com.google.errorprone.matchers.Description) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Pattern(java.util.regex.Pattern) ASTHelpers(com.google.errorprone.util.ASTHelpers) TypeParameterTree(com.sun.source.tree.TypeParameterTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) TreeScanner(com.sun.tools.javac.tree.TreeScanner) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) JCTree(com.sun.tools.javac.tree.JCTree)

Example 2 with TreeScanner

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();
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) TreeScanner(com.sun.tools.javac.tree.TreeScanner) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with TreeScanner

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();
}
Also used : TreeScanner(com.sun.tools.javac.tree.TreeScanner) ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree) Builder(com.google.errorprone.fixes.SuggestedFix.Builder)

Example 4 with TreeScanner

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();
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) TreeScanner(com.sun.tools.javac.tree.TreeScanner) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) JCTree(com.sun.tools.javac.tree.JCTree)

Example 5 with TreeScanner

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");
}
Also used : Context(com.sun.tools.javac.util.Context) Options(com.sun.tools.javac.util.Options) Log(com.sun.tools.javac.util.Log) JCTree(com.sun.tools.javac.tree.JCTree) ParserFactory(com.sun.tools.javac.parser.ParserFactory) Parser(com.sun.tools.javac.parser.Parser) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) TreeScanner(com.sun.tools.javac.tree.TreeScanner)

Aggregations

TreeScanner (com.sun.tools.javac.tree.TreeScanner)8 JCTree (com.sun.tools.javac.tree.JCTree)7 Symbol (com.sun.tools.javac.code.Symbol)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)2 MethodTree (com.sun.source.tree.MethodTree)2 Tree (com.sun.source.tree.Tree)2 TypeVariableSymbol (com.sun.tools.javac.code.Symbol.TypeVariableSymbol)2 Parser (com.sun.tools.javac.parser.Parser)2 ParserFactory (com.sun.tools.javac.parser.ParserFactory)2 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 Context (com.sun.tools.javac.util.Context)2 Log (com.sun.tools.javac.util.Log)2 Options (com.sun.tools.javac.util.Options)2 Set (java.util.Set)2 JavaFileObject (javax.tools.JavaFileObject)2 Objects (com.google.common.base.Objects)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1