I'm not sure how to explain this without writing several pages so I hope the actual code is more expressive.
I've made a jar containing multiple annotation declaration similar to the following:
@Target(ElementType.PACKAGE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MarkedPackage {
}
then I have made a test jar containing several classes in several packages and marked just one package with the above annotation (with package-info.java) like below:
@myPackage.MarkedPackage
package package.test.jar;
this jar had in its build path the jar containing the annotations.
then I made a static class that has a method (LoadPlugins) that retrieves a list with all the jars of a directory. Then it searches through the jars for the 'package-info' class and checks if that classes package contains the MarkedPackage a开发者_如何学JAVAnnotation. by calling this:
if (checkPackageAnnotation(thisClass.getPackage()))
where thisClass is the package-info class retrieved via a classloader. and:
public static boolean checkPackageAnnotation(AnnotatedElement elem) {
System.out.println(elem.getAnnotations().length);
if (elem == null || !elem.isAnnotationPresent(MarkedPackage.class))
return false;
return true;
}
the elem.getAnnotatios().length is there for debug purposes.
And the problem appears when I call the method from the static class:
if I call it from a main function:
public class MyMain {
public static void main(String[] args){
PluginUtils.LoadPlugins();
}
}
everything works perfectly it displays '1' from that System.out.println(elem.getAnnotations().length);
But if I call it from a button from my Vaadin project:
header.addComponent(new Button("CallThat",
new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
PluginUtils.LoadPlugins();
}
}));
It displays '0' from that System.out.println(elem.getAnnotations().length);
Also I should mention that I created the main inside my Vaadin project so it would have the exact same build path and resources.
Is there a problem with web applications and that "@Retention(RetentionPolicy.RUNTIME)" ?
hope I was clear enough... Hope someone has a solution for me... If you need more information - let me know.
Thank you.
yes,
because there is only one package-info class in one package.
精彩评论