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


Java TreeVisitor类代码示例

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


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

示例1: parse

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
public static void parse(Path moduleInfoPath, ModuleClassVisitor moduleClassVisitor) throws IOException {
  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  try(StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(moduleInfoPath);
    CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
    JavacTask javacTask = (JavacTask)task;
    Iterable<? extends CompilationUnitTree> units= javacTask.parse();
    CompilationUnitTree unit = units.iterator().next();

    ModuleHandler moduleHandler = new ModuleHandler(moduleClassVisitor);
    TreeVisitor<?,?> visitor = (TreeVisitor<?,?>)Proxy.newProxyInstance(TreeVisitor.class.getClassLoader(), new Class<?>[]{ TreeVisitor.class},
        (proxy, method, args) -> {
          ModuleHandler.METHOD_MAP
          .getOrDefault(method.getName(), (handler, node, v) -> { 
            throw new AssertionError("invalid node " + node.getClass());
          })
          .visit(moduleHandler, (Tree)args[0], (TreeVisitor<?,?>)proxy);
          return null;
        });

    unit.accept(visitor, null);
  }
}
 
开发者ID:forax,项目名称:moduletools,代码行数:24,代码来源:JavacModuleParser.java

示例2: matches

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
@Test
public void matches() {
  // TODO(b/67738557): consolidate helpers for creating fake trees
  LiteralTree tree =
      new LiteralTree() {

        @Override
        public Kind getKind() {
          throw new UnsupportedOperationException();
        }

        @Override
        public <R, D> R accept(TreeVisitor<R, D> visitor, D data) {
          throw new UnsupportedOperationException();
        }

        @Override
        public Object getValue() {
          return "a string literal";
        }
      };
  assertTrue(new StringLiteral("a string literal").matches(tree, null));
}
 
开发者ID:google,项目名称:error-prone,代码行数:24,代码来源:StringLiteralTest.java

示例3: accept

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
public <R, D> R accept(TreeVisitor<R, D> arg0, D arg1) {
    R ret = null;
    for (JCVariableDecl v : vars) {
        ret = v.accept(arg0, arg1);
    }
    return ret;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:8,代码来源:FieldGroupTree.java

示例4: visitCompilationUnit

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
@SuppressWarnings("static-method")
public void visitCompilationUnit(CompilationUnitTree node, TreeVisitor<?, ?> visitor) {
  for(Tree decl: node.getTypeDecls()) {
    if (!(decl instanceof ModuleTree)) {  // skip unnecessary nodes: imports, etc
      continue;
    }
    accept(visitor, decl);
  }
}
 
开发者ID:forax,项目名称:moduletools,代码行数:10,代码来源:JavacModuleParser.java

示例5: visitModule

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
public void visitModule(ModuleTree node, TreeVisitor<?, ?> visitor) {
  String name = qualifiedString(node.getName());
  int flags = node.getModuleType() == ModuleKind.OPEN? ACC_OPEN: 0;
  
  mv = moduleClassVisitor.visitModule(name, flags, null);
  
  node.getDirectives().forEach(n -> accept(visitor, n));
}
 
开发者ID:forax,项目名称:moduletools,代码行数:9,代码来源:JavacModuleParser.java

示例6: accept

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
@Override
public <R, D> R accept(TreeVisitor<R, D> tv, D d) {
    for (T current : children) {
        if (current != null) {
            current.accept(tv, d);
        }
    }
    return null;
}
 
开发者ID:fundacionjala,项目名称:oblivion-netbeans-plugin,代码行数:10,代码来源:CompoundTree.java

示例7: accept

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
@Override
public <R, D> R accept(TreeVisitor<R, D> tv, D d) {
    tv.visitMethod(this, d);
    for (VariableTree variable : parameters) {
        variable.accept(tv, d);
    }
    return super.accept(tv, d);
}
 
开发者ID:fundacionjala,项目名称:oblivion-netbeans-plugin,代码行数:9,代码来源:MethodTreeImpl.java

示例8: accept

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
@Override
public <R, D> R accept(TreeVisitor<R, D> visitor, D data) {
    return visitor.visitOther(this, data);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:5,代码来源:WorkingCopy.java

示例9: createInstance

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
public static <T> T createInstance(Context ctx, Class<T> clazz, Name ident, JCIdent jcIdent, Class<?>[] requiredConstructor, Object[] params) {
    try {
        Class<?> fake = baseClass2Impl.get(clazz);

        if (fake == null) {
            Method visitIdent = Visitor.class.getDeclaredMethod("visitIdent", JCIdent.class);
            Method visitIdentifier = TreeVisitor.class.getDeclaredMethod("visitIdentifier", IdentifierTree.class, Object.class);
            Method toString = Object.class.getDeclaredMethod("toString");
            fake = new ByteBuddy()
                    .subclass(clazz)
                    .implement(IdentifierTree.class)
                    .defineField("ident", Name.class, Visibility.PUBLIC)
                    .defineField("jcIdent", JCIdent.class, Visibility.PUBLIC)
                    .method(ElementMatchers.named("getName")).intercept(FieldAccessor.ofField("ident"))
                    .method(ElementMatchers.named("getKind")).intercept(FixedValue.value(Kind.IDENTIFIER))
                    .method(ElementMatchers.named("accept").and(ElementMatchers.takesArguments(Visitor.class))).intercept(MethodCall.invoke(visitIdent).onArgument(0).withField("jcIdent"))
                    .method(ElementMatchers.named("accept").and(ElementMatchers.takesArgument(0, TreeVisitor.class))).intercept(MethodCall.invoke(visitIdentifier).onArgument(0).withThis().withArgument(1))
                    .method(ElementMatchers.named("toString")).intercept(MethodCall.invoke(toString).onField("ident"))
                    .make()
                    .load(JackpotTrees.class.getClassLoader())
                    .getLoaded();
            baseClass2Impl.put(clazz, fake);
        }

        NEXT: for (Constructor c : fake.getDeclaredConstructors()) {
            if (c.getParameterCount() < requiredConstructor.length)
                continue;
            for (int e = 0; e < requiredConstructor.length; e++) {
                if (!c.getParameterTypes()[e].equals(requiredConstructor[e])) {
                    continue NEXT;
                }
            }
            java.util.List<Object> instances = new ArrayList<>();
            instances.addAll(Arrays.asList(params));
            for (int i = instances.size(); i < c.getParameterCount(); i++) {
                instances.add(null);
            }

            JCTree tree = (JCTree) c.newInstance(instances.toArray(new Object[0]));

            Field identField = fake.getDeclaredField("ident");

            identField.set(tree, ident);

            Field jcIdentField = fake.getDeclaredField("jcIdent");

            jcIdentField.set(tree, jcIdent);

            return clazz.cast(tree);
        }

        throw new IllegalStateException();
    } catch (IllegalAccessException | IllegalArgumentException | IllegalStateException | InstantiationException | NoSuchFieldException | NoSuchMethodException | SecurityException | InvocationTargetException ex) {
        throw new IllegalStateException(ex);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:57,代码来源:JackpotTrees.java

示例10: accept

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
@Override
public <R, D> R accept(TreeVisitor<R, D> v, D d) {
    return v.visitIdentifier(this, d);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:5,代码来源:JackpotTrees.java

示例11: accept

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
private static void accept(TreeVisitor<?, ?> visitor, Tree node) {
  node.accept(visitor, null);
}
 
开发者ID:forax,项目名称:moduletools,代码行数:4,代码来源:JavacModuleParser.java

示例12: visitRequires

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
public void visitRequires(RequiresTree node, @SuppressWarnings("unused") TreeVisitor<?, ?> __) {
  int modifiers = (node.isStatic()? ACC_STATIC: 0) | (node.isTransitive()? ACC_TRANSITIVE: 0);
  mv.visitRequire(qualifiedString(node.getModuleName()), modifiers, null);
}
 
开发者ID:forax,项目名称:moduletools,代码行数:5,代码来源:JavacModuleParser.java

示例13: visitExports

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
public void visitExports(ExportsTree node, @SuppressWarnings("unused") TreeVisitor<?, ?> __) {
  mv.visitExport(qualifiedString(node.getPackageName()), 0, toArray(node.getModuleNames()));
}
 
开发者ID:forax,项目名称:moduletools,代码行数:4,代码来源:JavacModuleParser.java

示例14: visitOpens

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
public void visitOpens(OpensTree node, @SuppressWarnings("unused") TreeVisitor<?, ?> __) {
  mv.visitOpen(qualifiedString(node.getPackageName()), 0, toArray(node.getModuleNames()));
}
 
开发者ID:forax,项目名称:moduletools,代码行数:4,代码来源:JavacModuleParser.java

示例15: visitUses

import com.sun.source.tree.TreeVisitor; //导入依赖的package包/类
public void visitUses(UsesTree node, @SuppressWarnings("unused") TreeVisitor<?, ?> __) {
  mv.visitUse(qualifiedString(node.getServiceName()));
}
 
开发者ID:forax,项目名称:moduletools,代码行数:4,代码来源:JavacModuleParser.java


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