寻找可靠的阶级注解装载的URLClassLoader [英] Reliably finding annotations on class loaded with URLClassLoader

查看:223
本文介绍了寻找可靠的阶级注解装载的URLClassLoader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用的URLClassLoader使用上的字段/方法的反思,产生的DTO加载类的指定路径上,扫描他们的注释,然后code发生器。

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.

当我把它改成了Maven的MOJO,我突然失去上看到类javax.persistence.Entity注释的能力。它加载它们,就可以看到所有的字段,但实体注释不再可见。

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.

我假定这是值得做的Classpath的问题 - 是什么呢?无论是测试应用程序或MOJO(在插件本身就是一个main()函数)是该项目的扫描类是从的一部分。但一期工程和其他没有。

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.

我有调试code打印出所有的时候检查它们的类注释的一点点,而在非运行的版本,它发现从字面上没有。

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?

推荐答案

原来,这个问题是pretty简单,虽然我不知道为什么它在一种情况下,而不是在另一个工作得很好。

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

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

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());

对于类加载器,这一切都开始工作就好了。我是pretty无知当它涉及到的类加载器的来龙去脉,所以这不明显。特别是因为这个例子,我下面没有任何指定的父。

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.

这篇关于寻找可靠的阶级注解装载的URLClassLoader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆