代码之家  ›  专栏  ›  技术社区  ›  fastcodejava

在Eclipse中获取方法中的字段类型

  •  3
  • fastcodejava  · 技术社区  · 15 年前

    如何以编程方式从如下方法中的语句获取字段类型:

    Foo foo = getSomeFoo();
    

    如果是字段,我可以知道元素的类型。

    2 回复  |  直到 15 年前
        1
  •  3
  •   IAdapter    15 年前

    你需要使用Eclipse的AST

    ICompilationUnit icu = ...
    
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setResolveBindings(true);
    parser.setSource(icu);
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    cu.accept(new ASTVisitor() {
        @Override
        public boolean visit(VariableDeclarationStatement node) {
            System.out.println("node=" + node);
            System.out.println("node.getType()=" + node.getType());
            return true;
        }
    });
        2
  •  0
  •   Jim Tough    15 年前

    你可以得到 foo 通过调用 foo.getClass() .

    如果您有一个类(或对象),并且希望以编程方式获取该类上特定方法的返回类型,请尝试以下操作: