3

次のようなメソッド内のステートメントからフィールドタイプをプログラムで取得するにはどうすればよいですか?

Foo foo = getSomeFoo();

フィールドであれば、要素の種類がわかります。

4

2 に答える 2

3

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;
    }
});
于 2010-10-15T11:05:36.890 に答える
0

fooを呼び出すと、オブジェクトのクラスを取得できますfoo.getClass()

クラス (またはオブジェクト) があり、そのクラスの特定のメソッドの戻り値の型をプログラムで取得したい場合は、次のようにしてください。

  • Classクラス/オブジェクトのオブジェクトを取得します
  • メソッドを呼び出してgetMethod()Method オブジェクトを取得する
  • getReturnType()Method オブジェクトでメソッドを呼び出す
于 2010-10-15T00:21:24.627 に答える