java 注释 - 从 Vaadin 项目调用定位器类的问题

发布于 2024-08-28 03:27:18 字数 1649 浏览 3 评论 0原文

我不知道如何在不写几页的情况下解释这一点,所以我希望实际的代码更具表现力。

我制作了一个包含多个注释声明的 jar,类似于以下内容:

@Target(ElementType.PACKAGE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MarkedPackage {
}

然后我制作了一个测试 jar,其中包含多个包中的多个类,并使用上述注释(使用 package-info.java)仅标记一个包,如下所示:

@myPackage.MarkedPackage
package package.test.jar;

此 jar在其构建路径中有包含注释的 jar。

然后我创建了一个静态类,它有一个方法(LoadPlugins),该方法检索包含目录中所有 jar 的列表。然后它在 jar 中搜索“package-info”类并检查该类包是否包含 MarkedPackage 注释。通过调用:

if (checkPackageAnnotation(thisClass.getPackage()))

其中 thisClass 是通过类加载器检索的包信息类。并且:

public static boolean checkPackageAnnotation(AnnotatedElement elem) {
  System.out.println(elem.getAnnotations().length);
  if (elem == null || !elem.isAnnotationPresent(MarkedPackage.class))
   return false;
return true;
}

elem.getAnnotatios().length 用于调试目的。

当我从静态类调用该方法时,问题就出现了:

如果我从主函数调用它:

public class MyMain {
 public static void main(String[] args){
  PluginUtils.LoadPlugins();
 }
}

一切正常,它会显示 System.out.println(elem.getAnnotations().length); 中的“1”;

但是,如果我从我的 Vaadin 项目中的按钮调用它:

header.addComponent(new Button("CallThat",
 new Button.ClickListener() {
 public void buttonClick(ClickEvent event) {
 PluginUtils.LoadPlugins();
 }
}));

它会显示 System.out.println(elem.getAnnotations().length); 中的“0”

另外我应该提到,我在 Vaadin 项目中创建了 main,因此它将具有完全相同的构建路径和资源。

Web 应用程序和“@Retention(RetentionPolicy.RUNTIME)”是否存在问题?

希望我说得足够清楚...希望有人能为我提供解决方案...如果您需要更多信息 - 让我知道。

谢谢。

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 annotation. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2024-09-04 03:27:18

是的,

因为一个包中只有一个 package-info 类。

yes,

because there is only one package-info class in one package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文