当前位置: 首页>>代码示例>>Java>>正文


Java TreeScanner类代码示例

本文整理汇总了Java中com.sun.source.util.TreeScanner的典型用法代码示例。如果您正苦于以下问题:Java TreeScanner类的具体用法?Java TreeScanner怎么用?Java TreeScanner使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


TreeScanner类属于com.sun.source.util包,在下文中一共展示了TreeScanner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testVarPos

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testVarPos() throws IOException {

    final String code = "package t; class Test { " +
            "{ java.io.InputStream in = null; } }";

    CompilationUnitTree cut = getCompilationUnitTree(code);

    new TreeScanner<Void, Void>() {

        @Override
        public Void visitVariable(VariableTree node, Void p) {
            if ("in".contentEquals(node.getName())) {
                JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
                assertEquals("testVarPos","in = null; } }",
                        code.substring(var.pos));
            }
            return super.visitVariable(node, p);
        }
    }.scan(cut, null);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:JavacParserTest.java

示例2: testTryResourcePos

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testTryResourcePos() throws IOException {

    final String code = "package t; class Test { " +
            "{ try (java.io.InputStream in = null) { } } }";

    CompilationUnitTree cut = getCompilationUnitTree(code);

    new TreeScanner<Void, Void>() {
        @Override
        public Void visitVariable(VariableTree node, Void p) {
            if ("in".contentEquals(node.getName())) {
                JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
                assertEquals("testTryResourcePos", "in = null) { } } }",
                        code.substring(var.pos));
            }
            return super.visitVariable(node, p);
        }
    }.scan(cut, null);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:JavacParserTest.java

示例3: testOperatorMissingError

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testOperatorMissingError() throws IOException {

    String code = "package test; public class ErrorTest { "
            + "void method() { int x = y  z } }";
    CompilationUnitTree cut = getCompilationUnitTree(code);
    final List<String> values = new ArrayList<>();
    final List<String> expectedValues =
            new ArrayList<>(Arrays.asList("[z]"));

    new TreeScanner<Void, Void>() {
        @Override
        public Void visitErroneous(ErroneousTree node, Void p) {
            values.add(getErroneousTreeValues(node).toString());
            return null;

        }
    }.scan(cut, null);

    assertEquals("testOperatorMissingError: The Erroneous tree "
            + "error values: " + values
            + " do not match expected error values: "
            + expectedValues, values, expectedValues);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:JavacParserTest.java

示例4: testMissingParenthesisError

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testMissingParenthesisError() throws IOException {

    String code = "package test; public class ErrorTest { "
            + "void f() {String s = new String; } }";
    CompilationUnitTree cut = getCompilationUnitTree(code);
    final List<String> values = new ArrayList<>();
    final List<String> expectedValues =
            new ArrayList<>(Arrays.asList("[new String()]"));

    new TreeScanner<Void, Void>() {
        @Override
        public Void visitErroneous(ErroneousTree node, Void p) {
            values.add(getErroneousTreeValues(node).toString());
            return null;
        }
    }.scan(cut, null);

    assertEquals("testMissingParenthesisError: The Erroneous tree "
            + "error values: " + values
            + " do not match expected error values: "
            + expectedValues, values, expectedValues);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:JavacParserTest.java

示例5: testMissingClassError

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testMissingClassError() throws IOException {

    String code = "package Test; clas ErrorTest {  "
            + "void f() {String s = new String(); } }";
    CompilationUnitTree cut = getCompilationUnitTree(code);
    final List<String> values = new ArrayList<>();
    final List<String> expectedValues =
            new ArrayList<>(Arrays.asList("[, clas]", "[]"));

    new TreeScanner<Void, Void>() {
        @Override
        public Void visitErroneous(ErroneousTree node, Void p) {
            values.add(getErroneousTreeValues(node).toString());
            return null;
        }
    }.scan(cut, null);

    assertEquals("testMissingClassError: The Erroneous tree "
            + "error values: " + values
            + " do not match expected error values: "
            + expectedValues, values, expectedValues);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:JavacParserTest.java

示例6: testSwitchError

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testSwitchError() throws IOException {

    String code = "package test; public class ErrorTest { "
            + "int numDays; void m1(int i) { switchh {i} { case 1: "
            + "numDays = 31; break; } } }";
    CompilationUnitTree cut = getCompilationUnitTree(code);
    final List<String> values = new ArrayList<>();
    final List<String> expectedValues =
            new ArrayList<>(Arrays.asList("[switchh]", "[i]"));

    new TreeScanner<Void, Void>() {
        @Override
        public Void visitErroneous(ErroneousTree node, Void p) {
            values.add(getErroneousTreeValues(node).toString());
            return null;
        }
    }.scan(cut, null);

    assertEquals("testSwitchError: The Erroneous tree "
            + "error values: " + values
            + " do not match expected error values: "
            + expectedValues, values, expectedValues);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:JavacParserTest.java

示例7: testMethodError

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testMethodError() throws IOException {

    String code = "package Test; class ErrorTest {  "
            + "static final void f) {String s = new String(); } }";
    CompilationUnitTree cut = cut = getCompilationUnitTree(code);

    final List<String> values = new ArrayList<>();
    final List<String> expectedValues =
            new ArrayList<>(Arrays.asList("[\nstatic final void f();]"));

    new TreeScanner<Void, Void>() {
        @Override
        public Void visitErroneous(ErroneousTree node, Void p) {
            values.add(normalize(getErroneousTreeValues(node).toString()));
            return null;
        }
    }.scan(cut, null);

    assertEquals("testMethodError: The Erroneous tree "
            + "error value: " + values
            + " does not match expected error values: "
            + expectedValues, values, expectedValues);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:JavacParserTest.java

示例8: testOperatorMissingError

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testOperatorMissingError() throws IOException {

    String code = "package test; public class ErrorTest { "
            + "void method() { int x = y  z } }";
    CompilationUnitTree cut = getCompilationUnitTree(code);
    final List<String> values = new ArrayList<>();
    final List<String> expectedValues =
            new ArrayList<>(Arrays.asList("[z]"));

    new TreeScanner<Void, Void>() {
        @Override
        public Void visitErroneous(ErroneousTree node, Void p) {
            values.add(getErroneousTreeValues(node).toString());
            return null;

        }
    }.scan(cut, null);

    assertEquals("testSwitchError: The Erroneous tree "
            + "error values: " + values
            + " do not match expected error values: "
            + expectedValues, values, expectedValues);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:25,代码来源:JavacParserTest.java

示例9: testMissingParenthesisError

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testMissingParenthesisError() throws IOException {

    String code = "package test; public class ErrorTest { "
            + "void f() {String s = new String; } }";
    CompilationUnitTree cut = getCompilationUnitTree(code);
    final List<String> values = new ArrayList<>();
    final List<String> expectedValues =
            new ArrayList<>(Arrays.asList("[new String()]"));

    new TreeScanner<Void, Void>() {
        @Override
        public Void visitErroneous(ErroneousTree node, Void p) {
            values.add(getErroneousTreeValues(node).toString());
            return null;
        }
    }.scan(cut, null);

    assertEquals("testSwitchError: The Erroneous tree "
            + "error values: " + values
            + " do not match expected error values: "
            + expectedValues, values, expectedValues);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:24,代码来源:JavacParserTest.java

示例10: testMissingClassError

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
@Test
void testMissingClassError() throws IOException {

    String code = "package Test; clas ErrorTest {  "
            + "void f() {String s = new String(); } }";
    CompilationUnitTree cut = getCompilationUnitTree(code);
    final List<String> values = new ArrayList<>();
    final List<String> expectedValues =
            new ArrayList<>(Arrays.asList("[, clas]", "[]"));

    new TreeScanner<Void, Void>() {
        @Override
        public Void visitErroneous(ErroneousTree node, Void p) {
            values.add(getErroneousTreeValues(node).toString());
            return null;
        }
    }.scan(cut, null);

    assertEquals("testSwitchError: The Erroneous tree "
            + "error values: " + values
            + " do not match expected error values: "
            + expectedValues, values, expectedValues);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:24,代码来源:JavacParserTest.java

示例11: containsTestMethod

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
/** Returns true if the tree contains a method invocation that looks like a test assertion. */
public static boolean containsTestMethod(Tree tree) {
  return firstNonNull(
      tree.accept(
          new TreeScanner<Boolean, Void>() {
            @Override
            public Boolean visitMethodInvocation(MethodInvocationTree node, Void unused) {
              String name = ASTHelpers.getSymbol(node).getSimpleName().toString();
              return name.contains("assert")
                  || name.contains("verify")
                  || name.contains("check")
                  || name.contains("fail")
                  || name.contains("expect")
                  || firstNonNull(super.visitMethodInvocation(node, null), false);
            }

            @Override
            public Boolean reduce(Boolean a, Boolean b) {
              return firstNonNull(a, false) || firstNonNull(b, false);
            }
          },
          null),
      false);
}
 
开发者ID:google,项目名称:error-prone,代码行数:25,代码来源:JUnitMatchers.java

示例12: mockingObsoleteMethod

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
/** Allow mocking APIs that return obsolete types. */
private boolean mockingObsoleteMethod(MethodTree enclosingMethod, VisitorState state, Type type) {
  // mutable boolean to return result from visitor
  boolean[] found = {false};
  enclosingMethod.accept(
      new TreeScanner<Void, Void>() {
        @Override
        public Void visitMethodInvocation(MethodInvocationTree node, Void unused) {
          if (found[0]) {
            return null;
          }
          if (MOCKITO_MATCHER.matches(node, state)) {
            Type stubber = ASTHelpers.getReturnType(node);
            if (!stubber.getTypeArguments().isEmpty()
                && ASTHelpers.isSameType(
                    getOnlyElement(stubber.getTypeArguments()), type, state)) {
              found[0] = true;
            }
          }
          return super.visitMethodInvocation(node, null);
        }
      },
      null);
  return found[0];
}
 
开发者ID:google,项目名称:error-prone,代码行数:26,代码来源:JdkObsolete.java

示例13: catchVariableIsUsed

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
private boolean catchVariableIsUsed(CatchTree c) {
  VarSymbol sym = ASTHelpers.getSymbol(c.getParameter());
  boolean[] found = {false};
  c.getBlock()
      .accept(
          new TreeScanner<Void, Void>() {
            @Override
            public Void visitIdentifier(IdentifierTree node, Void aVoid) {
              if (Objects.equals(sym, ASTHelpers.getSymbol(node))) {
                found[0] = true;
              }
              return super.visitIdentifier(node, aVoid);
            }
          },
          null);
  return found[0];
}
 
开发者ID:google,项目名称:error-prone,代码行数:18,代码来源:CatchFail.java

示例14: getMethodReturnTypes

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
private static ImmutableSet<ClassType> getMethodReturnTypes(MethodTree methodTree) {
  ImmutableSet.Builder<ClassType> returnTypes = ImmutableSet.builder();
  methodTree.accept(
      new TreeScanner<Void, Void>() {
        @Override
        public Void visitReturn(ReturnTree node, Void unused) {
          Type type = ASTHelpers.getType(node.getExpression());
          if (type instanceof ClassType) {
            returnTypes.add((ClassType) type);
          }

          return null;
        }
      },
      null /* unused */);
  return returnTypes.build();
}
 
开发者ID:google,项目名称:error-prone,代码行数:18,代码来源:MutableMethodReturnType.java

示例15: refactorInternalApplyMethods

import com.sun.source.util.TreeScanner; //导入依赖的package包/类
private void refactorInternalApplyMethods(
    MethodTree tree, SuggestedFix.Builder fixBuilder, Tree param, String mappedFunction) {
  getMappingForApply(mappedFunction)
      .ifPresent(
          apply -> {
            tree.accept(
                new TreeScanner<Void, Void>() {
                  @Override
                  public Void visitMethodInvocation(MethodInvocationTree callTree, Void unused) {
                    if (getSymbol(callTree).name.contentEquals("apply")) {
                      Symbol receiverSym = getSymbol(getReceiver(callTree));
                      if (receiverSym != null
                          && receiverSym.equals(ASTHelpers.getSymbol(param))) {
                        fixBuilder.replace(
                            callTree.getMethodSelect(), receiverSym.name + "." + apply);
                      }
                    }
                    return super.visitMethodInvocation(callTree, unused);
                  }
                },
                null);
          });
}
 
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:LambdaFunctionalInterface.java


注:本文中的com.sun.source.util.TreeScanner类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。