com.sun.tools.javac.tree.TreeScanner Java Examples

The following examples show how to use com.sun.tools.javac.tree.TreeScanner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: TwrAvoidNullCheck.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
    List<JCTree> result = super.translateTopLevelClass(env, cdef, make);

    new TreeScanner() {
        @Override
        public void visitBinary(JCBinary tree) {
            hasNullCheck |= tree.operator.getSimpleName().contentEquals("!=") &&
                            "resource".equals(String.valueOf(TreeInfo.name(tree.lhs))) &&
                            TreeInfo.isNull(tree.rhs);
            super.visitBinary(tree);
        }
    }.scan(result);

    return result;
}
 
Example #2
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: FieldOverloadKindNotAssignedTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    Context context = new Context();
    JavacFileManager.preRegister(context);
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));
    Iterable<? extends CompilationUnitTree> elements = ct.parse();
    ct.analyze();
    Assert.check(elements.iterator().hasNext());
    JCTree topLevel = (JCTree)elements.iterator().next();
    new TreeScanner() {
        @Override
        public void visitReference(JCMemberReference tree) {
            Assert.check(tree.getOverloadKind() != null);
        }
    }.scan(topLevel);
}
 
Example #3
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: BoxingAndSuper.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
    List<JCTree> result = super.translateTopLevelClass(env, cdef, make);
    Map<Symbol, JCMethodDecl> declarations = new HashMap<>();
    Set<Symbol> toDump = new TreeSet<>(symbolComparator);

    new TreeScanner() {
        @Override
        public void visitMethodDef(JCMethodDecl tree) {
            if (tree.name.toString().startsWith("dump")) {
                toDump.add(tree.sym);
            }
            declarations.put(tree.sym, tree);
            super.visitMethodDef(tree);
        }
    }.scan(result);

    for (Symbol d : toDump) {
        dump(d, declarations, new HashSet<>());
    }

    return result;
}
 
Example #4
Source Project: Refaster   Author: google   File: CompilerBasedTest.java    License: Apache License 2.0 6 votes vote down vote up
protected void compile(TreeScanner scanner, JavaFileObject fileObject) {
  JavaCompiler compiler = JavacTool.create();
  DiagnosticCollector<JavaFileObject> diagnosticsCollector =
      new DiagnosticCollector<JavaFileObject>();
  StandardJavaFileManager fileManager = 
      compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, UTF_8);
  JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(CharStreams.nullWriter(), 
      fileManager,
      diagnosticsCollector,
      ImmutableList.<String>of(),
      null,
      ImmutableList.of(fileObject));
  try {
    this.sourceFile = SourceFile.create(fileObject);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    task.analyze();
    for (CompilationUnitTree tree : trees) {
      scanner.scan((JCCompilationUnit) tree);
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  this.context = task.getContext();
}
 
Example #5
Source Project: TencentKona-8   Author: Tencent   File: TestLog.java    License: GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example #6
Source Project: jdk8u60   Author: chenghanpeng   File: TestLog.java    License: GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example #7
Source Project: java-n-IDE-for-Android   Author: shenghuntianlang   File: JavaInputAstVisitor.java    License: Apache License 2.0 5 votes vote down vote up
private boolean isStringConcat(ExpressionTree first) {
    final boolean[] stringLiteral = {true};
    final boolean[] formatString = {false};
    new TreeScanner() {
        @Override
        public void scan(JCTree tree) {
            if (tree == null) {
                return;
            }
            switch (tree.getKind()) {
                case STRING_LITERAL:
                    break;
                case PLUS:
                    super.scan(tree);
                    break;
                default:
                    stringLiteral[0] = false;
                    break;
            }
            if (tree.getKind() == STRING_LITERAL) {
                Object value = ((LiteralTree) tree).getValue();
                if (value instanceof String && FORMAT_SPECIFIER.matcher(value.toString()).find()) {
                    formatString[0] = true;
                }
            }
        }
    }.scan((JCTree) first);
    return stringLiteral[0] && formatString[0];
}
 
Example #8
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: TestLog.java    License: GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example #9
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: TestLog.java    License: GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example #10
Source Project: javaide   Author: tranleduy2000   File: JavaInputAstVisitor.java    License: GNU General Public License v3.0 5 votes vote down vote up
private boolean isStringConcat(ExpressionTree first) {
    final boolean[] stringLiteral = {true};
    final boolean[] formatString = {false};
    new TreeScanner() {
        @Override
        public void scan(JCTree tree) {
            if (tree == null) {
                return;
            }
            switch (tree.getKind()) {
                case STRING_LITERAL:
                    break;
                case PLUS:
                    super.scan(tree);
                    break;
                default:
                    stringLiteral[0] = false;
                    break;
            }
            if (tree.getKind() == STRING_LITERAL) {
                Object value = ((LiteralTree) tree).getValue();
                if (value instanceof String && FORMAT_SPECIFIER.matcher(value.toString()).find()) {
                    formatString[0] = true;
                }
            }
        }
    }.scan((JCTree) first);
    return stringLiteral[0] && formatString[0];
}
 
Example #11
Source Project: hottub   Author: dsrg-uoft   File: TestLog.java    License: GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example #12
Source Project: openjdk-8-source   Author: keerath   File: TestLog.java    License: GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example #13
Source Project: openjdk-8   Author: bpupadhyaya   File: TestLog.java    License: GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example #14
Source Project: google-java-format   Author: google   File: JavaInputAstVisitor.java    License: Apache License 2.0 5 votes vote down vote up
private boolean isStringConcat(ExpressionTree first) {
  final boolean[] stringLiteral = {true};
  final boolean[] formatString = {false};
  new TreeScanner() {
    @Override
    public void scan(JCTree tree) {
      if (tree == null) {
        return;
      }
      switch (tree.getKind()) {
        case STRING_LITERAL:
          break;
        case PLUS:
          super.scan(tree);
          break;
        default:
          stringLiteral[0] = false;
          break;
      }
      if (tree.getKind() == STRING_LITERAL) {
        Object value = ((LiteralTree) tree).getValue();
        if (value instanceof String && FORMAT_SPECIFIER.matcher(value.toString()).find()) {
          formatString[0] = true;
        }
      }
    }
  }.scan((JCTree) first);
  return stringLiteral[0] && formatString[0];
}
 
Example #15
Source Project: bazel   Author: bazelbuild   File: JavacTransitive.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Records the super type closure of all class declarations in the given compilation unit. Called
 * after attribute is complete for a compilation unit.
 */
public void postAttribute(Env<AttrContext> result) {
  result.toplevel.accept(
      new TreeScanner() {
        @Override
        public void visitClassDef(JCClassDecl tree) {
          recordSuperClosure(tree.sym);
          super.visitClassDef(tree);
        }
      });
}
 
Example #16
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: TestLog.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void test(boolean genEndPos) throws Exception {
    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);
    Factory diagnosticFactory = JCDiagnostic.Factory.instance(context);
    Field defaultErrorFlagsField =
            JCDiagnostic.Factory.class.getDeclaredField("defaultErrorFlags");

    defaultErrorFlagsField.setAccessible(true);

    Set<DiagnosticFlag> defaultErrorFlags =
            (Set<DiagnosticFlag>) defaultErrorFlagsField.get(diagnosticFactory);

    defaultErrorFlags.add(DiagnosticFlag.MULTIPLE);

    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");
}
 
Example #17
Source Project: Refaster   Author: google   File: CompilerBasedTest.java    License: Apache License 2.0 4 votes vote down vote up
protected void compile(TreeScanner scanner, String... lines) {
  compile(scanner, JavaFileObjects.forSourceLines("CompilerBasedTestInput", lines));
}