可靠地查找使用 URLClassLoader 加载的类上的注释

发布于 2024-08-31 22:14:42 字数 353 浏览 2 评论 0原文

我有一个代码生成器,它使用 URLClassLoader 在指定路径上加载类,扫描它们的注释,然后在字段/方法上使用反射,生成 DTO。

在测试应用程序中效果很好。

当我将其放入 Maven MOJO 中时,我突然无法看到类上的 javax.persistence.Entity 注释。它加载它们,它可以看到所有字段,但实体注释不再可见。

我假设这与类路径问题有关 - 是吗?测试应用程序(插件本身的 main() 函数)或 MOJO 都不是扫描类所在项目的一部分。但其中一个有效,另一个无效。

我有一些调试代码,在检查类时打印出类上的所有注释,而在非运行版本中它几乎找不到。

有什么想法如何调试问题/解决它吗?

I have a code generator that uses URLClassLoader to load classes on a specified path, scan them for annotations, and then using Reflection on the fields/methods, generate DTOs.

It works great, in the test app.

When I put it into the Maven MOJO, I suddenly lose the ability to see the javax.persistence.Entity annotations on the classes. It loads them, it can see all the fields, but the Entity annotation is no longer visible.

I am assuming this is something to do with Classpath issues - is it? Neither the test app (a main() function in the plugin itself) or the MOJO are part of the project that the scanned classes are from. But one works and the other doesn't.

I have a little bit of debug code that prints out all of the annotations on the class when it examines them, and in the non-running version it finds literally none.

Any ideas how I debug the problem/solve it?

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

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

发布评论

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

评论(4

岁月流歌 2024-09-07 22:14:42

事实证明问题非常简单,尽管我不确定为什么它在一种情况下运行良好而在另一种情况下却不起作用。

我的 URLClassLoader 创建没有指定父类加载器。所以,我认为它找不到任何东西。当我使用

loader = new URLClassLoader(classUrls, Thread.currentThread().getContextClassLoader());

类加载器时,一切都开始正常工作。当谈到类加载器的来龙去脉时,我非常无知,所以这并不明显。特别是因为我下面的示例也没有指定父级。

The problem turned out to be pretty simple, although I'm not sure why it worked fine in one case and not in another.

My URLClassLoader creation didn't specify a parent classloader. So, I assume it couldn't find anything. As soon as I used

loader = new URLClassLoader(classUrls, Thread.currentThread().getContextClassLoader());

for the classloader, it all started working just fine. I'm pretty ignorant when it comes to the ins and outs of classloaders, so this wasn't obvious. Especially since the example I was following didn't specify a parent either.

你曾走过我的故事 2024-09-07 22:14:42

当您扫描加载的类中的注释时,您看不到在类路径中找不到的注释。也就是说,要读取 JPA 的 Entity 注释,您的代码生成器应该在类路径中具有 JPA API(Maven 中的 javax.persistence:persistence-api:1.0)。

但是,如果您使用类加载器加载外部类并扫描它们以查找注释,则可能会面临缺少依赖项和静态初始化程序执行的其他问题。也许更好的方法是使用字节码操作库,例如 ASM 来扫描类而不加载它们。

When you scan loaded class for annotations, you can't see the annotations which can't be found in classpath. That is, to read JPA's Entity annotation your code generator should have a JPA API in classpath (javax.persistence:persistence-api:1.0 in Maven).

However, if you use classloader to load external classes and scan them for annotations, you may face other problems with missed dependencies and execution of static initializers. May be, the better approach is to use bytecode manipulation libraries, such as ASM, to scan classes without loading them.

羁〃客ぐ 2024-09-07 22:14:42

注释具有与其关联的持久性级别。有些无法在编译时生存(即它们不会放入.class 文件中。)检查您引用的文件是否不是这种类型。

Annotations have a persistence level associated with them. Some don't survive compile time (i.e. they are not put into the .class files.) Check to see that the ones you are referencing are not of this type.

等待圉鍢 2024-09-07 22:14:42

我假设这与类路径问题有关

我敢打赌,persistence-api 工件是使用提供的范围声明的,并且未在传递给插件的类路径中列出。你能否证实这一点?

I am assuming this is something to do with Classpath issues

My bet is that the persistence-api artifact is declared with a provided scope and isn't listed in the class path passed to the plugin. Can you confirm this?

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