Search code examples
javaeclipseeclipse-jdt

Find related types that can be instantiated


In Eclipse (Mars, FWIW), I'm trying to construct a list of types that are related to another type. By "related" I mean that I might at one time want the supertypes of type X, and at another time I might want the subtypes of type X. So far, so good. This sounds a lot like Eclipse "Open Type" dialog restricted to a particular interface. But...

I only want types that can actually be used at a particular point in code. Say, for example, that I wanted to replace the null in the below:

myFoo.setBar(null);

where setBar() is declared as follows:

public class Foo {
    // ...
    public void setBar(Bar theNewBar) {
        this.myBar = theNewBar;
    }
    // ...
}

I don't care about references to existing instances of Bar or its subtypes, I just care about Bar itself and subtypes thereof that I could instantiate on the spot, as if I were about to write:

myFoo.setBar(new SubBar());

So, specifically, types that are declared private would be excluded. Local types would be excluded (unless I can determine that they are declared within the method that I'm working with at the time. Anonymous inner classes declared elsewhere are out. And so is...wait for it...any type that is not visible based on access rules defined in its declaring plug-in (i.e. its plug-in doesn't export it). (This is Eclipse after all, and I care about conforming to the plug-in model.) There are Code Assist options that will exclude those types (CODEASSIST_DISCOURAGED_REFERENCE_CHECK and CODEASSIST_FORBIDDEN_REFERENCE_CHECK do what I want, I think) but Code Assist isn't able to discriminate by subtype or supertype.

So basically, if I can't instantiate it directly, and I can't write an anonymous inner class for it, I don't want to see it.

I've tried using ITypeHierarchy, but I can't see any methods either in creating it or in the ITypes that it returns that can be used to discriminate on the criteria that I've mentioned above.

IType.isLocal() and IType.isAnonymous() help. I can use them to rule out the local and anonymous types. But the others? For example, I don't care whether a type is protected or package default, I care whether I can instantiate it. Some protected or package default types can be instantiated at any given point in code, and others cannot.

I do, however, want to see types that could be instantiated if their declaring plug-in was added to my manifest.mf dependencies.

How can I go about either finding the types that I want (either using ITypeHierarchy or any other mechanism), or excluding the types that I don't want from ITypeHierarchy?


Solution

  • IType is an IMember, which has the int getFlags() method returning flags defined by Flags, which has good helper methods, e.g.:

    Flags.isAbstract(iType.getFlags())
    Flags.isInterface(iType.getFlags())
    Flags.isPackageDefault(iType.getFlags()) // You then compare package yourself
    Flags.isProtected(iType.getFlags()) // You then compare package yourself
    Flags.isPublic(iType.getFlags())