Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Finding IType for anonymous inner class
Finding IType for anonymous inner class [message #962183] Sun, 28 October 2012 23:11 Go to next message
ted stockwell is currently offline ted stockwellFriend
Messages: 123
Registered: July 2009
Senior Member
Hi All,

I am having a problem getting an IType for an anonymous inner class.
Given the name of an anonymous inner class how can I get the associated IType?

What I have now (which doesn't work) is something like this...

// className is something that ends with a number, like "myclass.SomeClass$1"
IType getIType4AnonymousClass(String className, IJavaProject project) {
int i= className.lastIndexOf('$');
// for now assume primaryName is NOT another anonymous class
String primaryName= primaryName.substring(0, i);
IType primaryType= project.getType(primaryName);
int occurance= Integer.parseInt(className.substring(i+1));
return primaryType.getType("", occurance);

I've tried this a million different ways, I'm stumped, any help would be appreciated.
I'm using Eclipse version 3.7 (indigo).

Re: Finding IType for anonymous inner class [message #963347 is a reply to message #962183] Mon, 29 October 2012 18:55 Go to previous message
ted stockwell is currently offline ted stockwellFriend
Messages: 123
Registered: July 2009
Senior Member
I have not found a good answer to this question in the forum or on the net so I'm posting my solution to finding an anonymous inner class.
It seems to be working, but I have not yet tested it thoroughly...

 * Return an IType (Source type, not Binary) for the given class name.
 * @return null if no such class can be found.
 * @throws JavaModelException
public static IType findType(IJavaProject javaProject, final String className) 
throws JavaModelException 
    String primaryName= className;
    int i= primaryName.lastIndexOf('$');
    int occurence= 0;
    if (0 < i) {
        try {
            occurence= Integer.parseInt(primaryName.substring(i+1));
            primaryName= primaryName.substring(0, i);
        catch (NumberFormatException x) {
     * IJavaProject.findType works for top level classes and named inner 
     * classes, but not for anonymous inner classes
    IType primaryType= javaProject.findType(primaryName);
    if (!primaryType.exists())
        return null;
    if (occurence <= 0) // if not anonymous then we done
        return primaryType;

     * the following snippet never works, but according to the 
     * docs it should.  
    IType innrType= primaryType.getType("", occurence);
    if (innrType != null) {
        String name= primaryType.getFullyQualifiedName();
        if (name.equals(className)) {
            return innrType;

     * If we're looking for an anonymous inner class then we need to look 
     * through the primary type for it. 
    LinkedList<IJavaElement> todo= new LinkedList<IJavaElement>();
    IType innerType= null;
    while (!todo.isEmpty()) {
        IJavaElement element= todo.removeFirst();

        if (element instanceof IType) {
            IType type= (IType)element;
            String name= type.getFullyQualifiedName();
            if (name.equals(className)) {
                innerType= type;

        if (element instanceof IParent) {
            for (IJavaElement child:((IParent)element).getChildren()) {

    return innerType;
Previous Topic:How do I create an Eclipse project file?
Next Topic:New schema in Android ADT tool
Goto Forum:

Current Time: Wed Dec 02 13:36:59 GMT 2020

Powered by FUDForum. Page generated in 0.05006 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top