程序如下:
import java.lang.annotation.Annotation;public @interface MyCommField
{ int length(); String type();
}////////////////////////////////
public class Goods
    implements Serializable
{ @XinoCommField(length = 20,type="0030")
        private String goods_id;
}
//////////////////////////////////////////
在另外一个类中。取得Goods的实例。
Class class1 = obj.getClass();
取所有的字段,其实就一个goods_id
Field afield[] = class1.getDeclaredFields();
但是下面这里有问题。
afield[0].isAnnotationPresent(MyCommField.class)总是为false。我想应该要为true才对。
请教高手帮忙解答。

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【quzhenok】截止到2008-07-01 09:56:01的历史汇总数据(不包括此帖):
    发帖数:1                  发帖分:50                 
    结贴数:1                  结贴分:50                 
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   

    @XinoCommField(length = 20,type="0030") 
    这里改一下改为
    @MyCommField(length = 20,type="0030") 
      

  3.   

    更改过的MyCommField,需要增加Retention定义,标识在源码还是运行时有效。如果不定义@Retention(RetentionPolicy.RUNTIME),Annotation会在编译时被丢弃。import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface MyCommField {
        int length();
        String type();
    }