eclipsercp

Eclipse , How to get the URI of a class


Class<?> clazz = IWorkbench.class ;

I wonder if there is any API that can help me get the URI of class , This URI is used to open an editor

tried to make my own URL and succeeded, but I still want to use the eclipse API

public static IEditorPart openClassInEditorFromURI(URI uri) {
    IWorkbenchPage page = BitFanUI.getLastActivateWindow().getActivePage() ;
    if( page == null ) {
        return null ;
    }
    try {
        IFileStore fileStore = EFS.getStore( uri ) ;
        return IDE.openEditorOnFileStore(page, fileStore);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null ;
}

Solution

  • In general, a (runtime) class doesn't have a URI. (You can define a class on the fly from an array of bytes ...)

    Having said that, most classes loaded in the normal way will have have a resource URI. If you know the fully qualified class name and the classloader that loaded it, you should be able to do the following:

    1. Transform the classes fully qualified name to its resource path. For example "com.example.MyClass" would typically map to "/com/example/MyClass.class".

    2. Use classloader.getResource(resourcePath) to get the URI.

    However note that the URI may not be resolvable in another context.


    I don't think that Eclipse provides an API that does the above. (I can't find one.)


    UPDATE - If you are trying to programmatically find a source URI for a class from its full name, then this might provide the answer:

    Note that if you really have a Class object in the context of an Eclipse plugin (as implied by your Class<?> clazz = IWorkbench.class snippet) then you can extract the full class name from the Class object. But this is sounding a bit strange ...