Reputation: 3105
Is there any way by using JDT ASTParser, by which we can identify if the method declaration is inside the Anonymous inner class?
I am creating a Eclipse plug-in to find unused public methods in a project. I am using a ASTVisitor on the MethodDeclaration node and then using the JDT search. The problem is that while traversing through each Java Class i am visiting MethodDeclaration of anonymous inner classes like Listeners. I need to avoid these methods.
Thanks in advance.
Upvotes: 2
Views: 1159
Reputation: 3105
At last i found a solution. There is a AnonymousClassDeclaration ASTNode in ASTParser which denotes the anonymous inner classes in the Java file. When visiting such nodes we can specify that these nodes need not be visited entirely (by returning false).
public boolean visit(AnonymousClassDeclaration classDeclarationStatement) {
return false;
}
Upvotes: 1
Reputation: 95430
If CLASS_INSTANCE_CREATION is the type of node for an anonymous class, that should work. I'm not a Java expert; seems to me there are several ways to create an anonymous class, so you need to check that this node type covers them all.
... doesn't your anonymous listener(?) class have to inherit/implement a Listener interface? If that's the case, you should be able climb over to the part of the tree near the CLASS_INSTANCE_CREATION that is the inherits/implements clause, and check that it really does implement/inherit what you expect. To do this right, you need full name and type resolution; just because an interface name is spelled "Listener" doesn't mean it is the one you intended
Upvotes: 1