在java项目中添加/修改注释

发布于 2024-12-03 01:07:44 字数 760 浏览 0 评论 0原文

我们有一个 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 技术交流群。

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

发布评论

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

评论(2

浪荡不羁 2024-12-10 01:07:44

我对此进行了更多研究,作为总结,我发现以下内容:

主要是由于 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:

  • Java language does not allow changing annotations during runtime.
  • One can use Javassist to modify the java bytecode but it does not work on platforms like Android.
  • One can use ASM but that involves dealing with Java assembly (an example is here: http://community.jboss.org/thread/150002).

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

淡忘如思 2024-12-10 01:07:44

如果没有在运行时破解 .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.

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