java Java注解
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/738065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java annotations
提问by Jakub Arnold
I've created simple annotation in Java
我在 Java 中创建了简单的注释
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Column {
String columnName();
}
and class
和班级
public class Table {
@Column(columnName = "id")
private int colId;
@Column(columnName = "name")
private String colName;
private int noAnnotationHere;
public Table(int colId, String colName, int noAnnotationHere) {
this.colId = colId;
this.colName = colName;
this.noAnnotationHere = noAnnotationHere;
}
}
I need to iterate over all fields, that are annotated with Columnand get nameand valueof field and annotation. But I've got problem with getting valueof each field, since all of them are of different data type.
我需要迭代所有字段,这些字段被注释Column并获取字段和注释的名称和值。但是我在获取每个字段的值时遇到了问题,因为它们都是不同的数据类型。
Is there anything that would return collection of fields that have certain annotation? I managed to do it with this code, but I don't think that reflection is good way to solve it.
有什么可以返回具有特定注释的字段集合吗?我设法用这段代码做到了,但我认为反射不是解决它的好方法。
Table table = new Table(1, "test", 2);
for (Field field : table.getClass().getDeclaredFields()) {
Column col;
// check if field has annotation
if ((col = field.getAnnotation(Column.class)) != null) {
String log = "colname: " + col.columnName() + "\n";
log += "field name: " + field.getName() + "\n\n";
// here i don't know how to get value of field, since all get methods
// are type specific
System.out.println(log);
}
}
Do I have to wrap every field in object, which would implement method like getValue(), or is there some better way around this? Basicly all I need is string representation of each field that is annotated.
我是否必须将每个字段都包装在对象中,这将实现像 那样的方法getValue(),还是有更好的方法来解决这个问题?基本上,我需要的是每个注释字段的字符串表示。
edit: yep field.get(table)works, but only for publicfields, is there any way how to do this even for privatefields? Or do I have to make getter and somehowinvoke it?
编辑:是的field.get(table),但仅适用于public字段,即使对于private字段,有没有什么方法可以做到这一点?还是我必须制作吸气剂并以某种方式调用它?
采纳答案by kenj0418
Every object should has toString() defined. (And you can override this for each class to get a more meaningful representation).
每个对象都应该定义 toString()。(并且您可以为每个类覆盖它以获得更有意义的表示)。
So you where your "// here I don't know" comment is, you could have:
所以你的“//这里我不知道”评论在哪里,你可以:
Object value = field.get(table);
// gets the value of this field for the instance 'table'
log += "value: " + value + "\n";
// implicitly uses toString for you
// or will put 'null' if the object is null
回答by Jon Skeet
Reflection is exactlythe way to solve it. Finding out things about types and their members at execution time is pretty much the definition of reflection! The way you've done it looks fine to me.
反射正是解决它的方法。在执行时找出有关类型及其成员的信息几乎就是反射的定义!你这样做的方式在我看来很好。
To find the value of the field, use field.get(table)
要查找字段的值,请使用 field.get(table)
回答by Uri
Reflection is exactly the way to look at annotations. They are a form of "metadata" attached to the class or method, and Java annotations were designed to be examined that way.
反射正是查看注释的方式。它们是附加到类或方法的“元数据”形式,Java 注释旨在以这种方式进行检查。
回答by McDowell
Reflection is one way to process the object (probably the only way if the fields are private and don't have any kind of accessor method). You'll need to look at Field.setAccessibleand perhaps Field.getType.
反射是处理对象的一种方法(如果字段是私有的并且没有任何类型的访问器方法,则可能是唯一的方法)。你需要看看Field.setAccessible也许Field.getType。
Another approach is to generate another class for enumerating the annotated fields using a compile-time annotation processor. This requires a com.sunAPI in Java 5, but support is better in the Java 6 JDK (IDEs like Eclipse may require special project configuration).
另一种方法是使用编译时注释处理器生成另一个用于枚举注释字段的类。这需要Java 5 中的com.sunAPI,但 Java 6 JDK 中的支持更好(Eclipse 等 IDE 可能需要特殊的项目配置)。

