hi, i am developing a plugin in which i search for a particular method.Now i want to display all the variable declared and used in it along with their types.How can i do that?method name is of IMethod type.Help
+1
A:
What you need is the Java reflection API. Have a look at this: link text
Matthias
2010-02-19 08:41:22
No, I'm pretty sure he's talking about the `IMethod` interface of Eclipse: http://www.jarvana.com/jarvana/view/org/eclipse/jdt/doc/isv/3.2.1-r321_v20060907/isv-3.2.1-r321_v20060907.jar!/reference/api/org/eclipse/jdt/core/IMethod.html And regardless, reflection doesn't tell you about variables declared inside a method.
T.J. Crowder
2010-02-19 08:49:00
A:
You can use reflection to get the types of all the parameters required by the method.
First reflect the method using Class, then use `Method.getParameterTypes()'.
Software Monkey
2010-02-19 08:43:22
+1
A:
If you have the CompilationUnit of that IMethod, you could use a ASTParser
as illustrated here):
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(compilationUnit);
parser.setSourceRange(method.getSourceRange().getOffset(), method.getSourceRange().getLength());
parser.setResolveBindings(true);
CompilationUnit cu = (CompilationUnit)parser.createAST(null);
cu.accept(new ASTMethodVisitor());
Then you can use an ASTVisitor
cu.accept(new ASTVisitor() {
public boolean visit(SimpleName node) {
System.out.println(node); // print all simple names in compilation unit. in our example it would be A, i, j (class name, and then variables)
// filter the variables here
return true;
}
});
VonC
2010-02-24 09:46:45