添加/修改注释的Java项目 [英] Adding/modifying annotations in a java project

查看:143
本文介绍了添加/修改注释的Java项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们拥有的Java code库,我们打算在项目中使用。其中的一些项目都需要有添加到这个库中的Java对象注释(即一个项目,这些对象将在JAX-RS Servlet实现应用,因此它们需要与JAXB,JSON等注解来注释)。我遇到的问题是,我无法弄清楚如何在不改变原有的库添加这些注解。

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.

考虑这个例子:

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;
}

起初,我想过使用界面或单独注明,但无法弄清楚如何做到这一点(或者是否有可能或没有)派生类。我也发现有关此主题的了Javassist建议(即Java字节code操作方法),但问题是,这需要在Android的客户端工作,以及因此它不是我的选择。在这一点上,我的想法。

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.

我会AP preciate如果有人可以帮助任何关系。也许我失去了一些东西,或者什么,我试图做的是不是正确的方式。在任何情况下,我现在需要为了进行一些指导。

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.

非常感谢提前。

推荐答案

我看着这个越来越作为一个总结,这里就是我发现:

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

主要由于JAXB不是在Android(和其它原因)present我们切换到JSON和使用杰克逊作为实现启动。这种变化的一个好处是,用什么杰克逊所谓的混合式注解这正是我一直在寻找的能力。

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.

下面的解释更加的链接: HTTP: //www.cowtown$c$cr.com/blog/archives/2009/08/entry_305.html

Here's the link that explains it more: http://www.cowtowncoder.com/blog/archives/2009/08/entry_305.html

这篇关于添加/修改注释的Java项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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