Getting field type in method in eclipse

How to programmatically obtain a field type from an instruction inside a method as follows:

Foo foo = getSomeFoo();

If this is a field, I can find out the type of element.

+3
source share
2 answers

You need to use 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;
    }
});
+3
source

You can get the class of the object fooby calling foo.getClass().

If you have a class (or object) and want to programmatically get the return type for a specific method in this class, try the following:

  • Get object Classfor class / object
  • Call a method getMethod()and return a method object
  • Call the method getReturnType()of the Method object
0
source

Source: https://habr.com/ru/post/1769658/


All Articles