在java项目中添加/修改注解 [英] Adding/modifying annotations in a java project

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

问题描述

我们有一个打算跨项目使用的 Java 代码库.其中一些项目需要在该库中的 Java 对象中添加注释(即,在一个项目中,这些对象将用于 JAX-RS servlet 实现,因此它们需要使用 JAXB、JSON 等注释进行注释).我遇到的问题是我无法弄清楚如何在不更改原始库的情况下添加这些注释.

考虑这个例子:

公共类MyClass{私人字符串字段1;私有 int field2;}

在某些项目中,我希望类的行为就像它一样

公共类MyClass{@注解1私人字符串字段1;@注解2私有 int field2;}

最初我想使用单独注释的接口或派生类,但不知道如何去做(或者是否可能).我还发现了关于 Javassist 建议 在这个线程(即 Java 字节码操作方法)但是问题是这也需要在 Android 客户端上运行,所以它不是我的选择.在这一点上,我没有想法.

如果有人能以任何方式提供帮助,我将不胜感激.也许我错过了一些东西,或者我试图做的不是正确的方法.无论如何,我现在需要一些指导才能继续.

非常感谢.

解决方案

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

主要是由于 Android 上没有 JAXB(以及其他原因),我们切换到 JSON 并开始使用 Jackson 作为实现.此更改的一个好处是能够使用 Jackson 所说的混合注释",这正是我所寻找的.

这里有更多解释的链接:http://www.cowtowncoder.com/blog/archives/2009/08/entry_305.html

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.

解决方案

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

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

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