在java项目中添加/修改注释
我们有一个 Java 代码库,打算跨项目使用。其中一些项目需要向该库中的 Java 对象添加注释(即,在一个项目中,这些对象将在 JAX-RS servlet 实现中使用,因此需要使用 JAXB、JSON 等注释对它们进行注释)。我遇到的问题是,我无法弄清楚如何在不更改原始库的情况下添加这些注释。
考虑这个例子:
public class MyClass
{
private String field1;
private int field2;
}
在一些项目中,我希望类的行为就像它一样
public class MyClass
{
@Annotation1
private String field1;
@Annotation2
private int field2;
}
最初我考虑使用单独注释的接口或派生类,但无法弄清楚如何做到这一点(或者是否可能) 。我还发现了关于Javassist建议在此线程中(即Java字节码操作方法),但是问题是这也需要在 Android 客户端上运行,所以它不是我的选择。此时我已经没有主意了。
如果有人能以任何方式提供帮助,我将不胜感激。也许我错过了一些东西,或者也许我想做的不是正确的方法。无论如何,我现在需要一些指导才能继续。
预先非常感谢。
We have a library of Java code that we intend to use across projects. Some of these projects will require having annotations added to the Java objects in this library (i.e. in one project, these objects will be used in a JAX-RS servlet implementation so they need to be annotated with JAXB, JSON etc annotations). The issue I am having is that I could not figure out how to add these annotations without changing the original library.
Consider this example:
public class MyClass
{
private String field1;
private int field2;
}
In some projects, I would like the class to behave as if it was
public class MyClass
{
@Annotation1
private String field1;
@Annotation2
private int field2;
}
Initially I thought about using interfaces or derived classes that are separately annotated but could not figure out how to do it (or whether it is possible or not). I also found about the Javassist suggestion in this thread (i.e. Java bytecode manipulation approach) but the issue is that this needs to work on Android clients as well so it is not an option for me. At this point I am out of ideas.
I would appreciate if someone could help in any way. Maybe I am missing something, or maybe what I am trying to do is not the right way. In any case, I need some guidance now in order to proceed.
Thanks very much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对此进行了更多研究,作为总结,我发现以下内容:
主要是由于 Android 上不存在 JAXB(以及其他原因),我们切换到 JSON 并开始使用 Jackson 作为实现。这一更改的好处之一是能够使用 Jackson 所说的“混合注释”,这正是我所寻找的。
这是更多解释它的链接: http://www.cowtowncoder.com /blog/archives/2009/08/entry_305.html
I looked into this more and as a summary, here's what I found:
Primarily due to JAXB not being present on Android (and other reasons) we switched to JSON and starting using Jackson as the implementation. One benefit of this change was the ability to use what Jackson calls "mixed-in annotations" which is exactly what I was looking for.
Here's the link that explains it more: http://www.cowtowncoder.com/blog/archives/2009/08/entry_305.html
如果没有在运行时破解 .class 字节码,这是不可能完成的。如果您确实想在运行时动态更改库类的字节码,则可以使用 Javassist,但从更大的角度来看,这可能最终会导致严重后果。
我建议您只需注释该库并重新编译(如果它是您的代码库)。如果不是,您仍然可以通过继承将库类/接口包装在您自己的类/接口中,并在那里进行注释。不管怎样,我认为在代码中注释比通过运行时字节码黑客更好。额外的好处是,IDE 可以看到源代码中的注释,从而使 IDE 能够更好地帮助您。
Short of hacking the .class bytecode during runtime, this can't be done. You can use Javassist if you really want to dynamically change the bytecode of the library classes during runtime but in the bigger picture this will probably end up blowing up badly.
I would suggest that you just annotate the library and recompile if its your codebase. If it isn't, you can still wrap the library classes/interfaces in your own classes/interfaces through inheritance and annotate there. Either way, I think it would be better to annotate in the code than through runtime bytecode hacking. Added bonus, annotations in the source will be seen by IDEs, allowing the IDE to better assist you.