简体   繁体   中英

Accessing java files from Eclipse source-bundle

How do I access source code from an Eclipse/OSGI source-bundle?

I use Maven/Tycho to produce the source bundles using the source-feature packaging. This works well for attaching the source for 3rd party development. They are referenced on the JAR as 'External location' for 'Java Source Attachement'.

But how do I programmatically reach the source-bundle? Source bundles are not accessible as regular bundles, ie Platform.getBundle('com.myplugin.source') , and the source files are not accessible through the master bundle, ie 'com.myplugin' There's no reference in the plugin manifest headers either. Is there an Eclipse OSGI service that I should use? Any help would be appreciated.

Not the solution I was hoping for but as a workaround I can go directly to the source bundle JAR file. I would hope there is a better solution as I am not sure this is future proof with such as Oomph.

String bloc = Platform.getBundle(bundlename).getLocation();
String location = bloc.replace(bundlename, bundlename+".source");
String path = new URL(location.replace("reference:", "")).getPath();

// Depending on installation type it may to prefix with Eclipse's installation path
if(!new File(path).exists())
    path=Platform.getInstallLocation().getURL().getFile()+path;

File srcbundle = new File(path);
if(!srcbundle.exists()){
    Logger.IDE.severe("Source bundle '"+path+"' not found!");
    // When in runtime workbench we will get the source files from the bundle
    for (URL url : Collections.list(bundle.findEntries("/", "*.*", true))) {
        try(InputStream input = url.openStream()){
            outputFile(input, url.getFile());
        }
    }
} else {
    // When plugin installation we will unzip the source bundle JAR. 
    try(JarFile jar=new JarFile(srcbundle)){
        for (JarEntry ze : Collections.list(jar.entries())) {
            try(InputStream input = jar.getInputStream(ze)){
                outputFile(input, ze.getName());
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM