2

My first attempt at this was to simply include the Jars as part of my main application. (Eclipse 3.7) I know this is frowned upon, but I'm writing a standalone specialised app and I wasn't too worried about trying to fathom out bundles.

Having attempted to run this:

 Reflections r = new Reflections("mypackage");

 Set<Class<?>> activeActions = r
                .getTypesAnnotatedWith(MyAnnotation.class);

In my stand alone test app this worked fine, but when trying it in RCP I got the following output:

org.reflections.ReflectionsException: could not create Vfs.Dir from url, 
no matching UrlType was found [bundleresource://1009.fwk651584550/]

either use fromURL(final URL url, final List<UrlType> urlTypes) 
or use the static setDefaultURLTypes(final List<UrlType> urlTypes) 
or addDefaultURLTypes(UrlType urlType) with your specialized UrlType.

So I looked around for an answer and found This Question which covers my issue but doesn't give me enough detail to solve it. I moved the reflection packages out in to a bundle as a first step, but now when I run it I get no output at all, I don't know if this is because of bad log4j paths or what, but I am now getting no feedback from reflections package.

my activeActions list is empty, so the scanning has not worked (and I didn't expect it to) since I hadn't implemented the second part of the solution, which is to create the custom UrlType. However I have now implemented the custom URL type and still nothing is being found, I don't know why because I can;t see any feedback from the reflections package, it's just not finding anything.

I'mm hoping someone can help me to get Reflections logging working, and then find out why the UrlType solution is not working for me.

EDIT

Here's a joke, If I don't bother with the Bundle and I just shove the reflections jars in to my classpath the scanning works provided the custom UrlType is registered. And the logging all works. Why would I bother with Bundles?

Community
  • 1
  • 1
Link19
  • 586
  • 1
  • 18
  • 47
  • So I'm finally migrating to E4 and this has stopped working again. I guess the custom url type doesn't work for E4 so I'm back to square 1. – Link19 Sep 26 '14 at 14:48
  • I don't use E4 anymore. Last time I saw it it was full of undocumented classes and it had no useful tutorials either. – Adam Arold Sep 26 '14 at 22:22

1 Answers1

0

It looks like an issue with class loading under the OSGI framework. You have to remember that each bundle/plugin maintains its own class loader so I think what is happening you are using the wrong class loaders to load your classes and resources are not getting resolved. First off to load you classes in OSGI, this is the suggested approach:

Get the Bundle/Plugin that contains your class by doing this Platform.getBundle("com.mybundle.id"); On the bundle instance there is a method called .loadClass(String class);

Try loading your MyAnnotation.class with the approach I just mentioned and see what happens. The eclipse buddy-register feature in the post your referenced is useful for things like using hibernate under OSGI where you mapped data classes are in your bundle and by default hibernate uses Class.forname() to load the class and it runs into an error because that mapped class is not on the class path of the hibernate lib bundle. It makes no sense to add a dependency on the hibernate plugin to your bundle so in the manifest of the hibernate bundle you set eclipse-buddy-policy:registered (not correct on exact syntax) then in your bundle with mapped classes you register hibernate as a buddy and in runtime the hibernate class loader will have all classes in your bundle on its class path.

Hopefully this gives you an idea of class loading, that seems to be the issue. Try loading your classes from a bundle instance not = new().

Duncan Krebs
  • 3,366
  • 2
  • 33
  • 53