JAVA如何获得被注解的成员变量?


下面是我自定义的注解:


 java


 @Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Index {


}

我在哪个成员变量是使用这个注解,就表示哪个成员变量是一个“索引”:


 java


 public class User {
    @Index
    private String scenicId;
    private String pinyin;
    private String title;
    private String cityName; // 城市名字
    private String cityCode;
    private String stateCode;
    private String stateName;
    private String continentCode;
    private String continentName;
    private String countryCode;
    private String countryName;
    private double latitude;
    private double longitude;
    private String geohash;// 景点的GEOHASH编码
    private String location;// 位置
    private String image; // 这个景点所对应的一张照片。相当于封面
    private double  score; // 此score是此地点的平均分
    private List<String> themes; // 此地点最受欢迎的几个主题
    private int commentCount = 0;// 评论数
    private int collectCount = 0; // 收藏数
    private String visitorUpdateInfo; // 用户贡献的信息,一次只能有一个
    private int scoreCount = 0; // 打分的人数
    private long createTime; // 创建时间
    private String type;
    private String introduction; // 描述(几个字描述)
    private String descriptions; // 概述
    private int titleSize;// 记录了标题的长度,可以用来做搜索。按照标题长度来匹配
    public String getScenicId() {
        return scenicId;
    }

    public void setScenicId(String scenicId) {
        this.scenicId = scenicId;
    }

    public String getPinyin() {
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCityCode() {
        return cityCode;
    }

    public void setCityCode(String cityCode) {
        this.cityCode = cityCode;
    }

    public String getStateCode() {
        return stateCode;
    }

    public void setStateCode(String stateCode) {
        this.stateCode = stateCode;
    }

    public String getStateName() {
        return stateName;
    }

    public void setStateName(String stateName) {
        this.stateName = stateName;
    }

    public String getContinentCode() {
        return continentCode;
    }

    public void setContinentCode(String continentCode) {
        this.continentCode = continentCode;
    }

    public String getContinentName() {
        return continentName;
    }

    public void setContinentName(String continentName) {
        this.continentName = continentName;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getGeohash() {
        return geohash;
    }

    public void setGeohash(String geohash) {
        this.geohash = geohash;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public List<String> getThemes() {
        return themes;
    }

    public void setThemes(List<String> themes) {
        this.themes = themes;
    }

    public int getCommentCount() {
        return commentCount;
    }

    public void setCommentCount(int commentCount) {
        this.commentCount = commentCount;
    }

    public int getCollectCount() {
        return collectCount;
    }

    public void setCollectCount(int collectCount) {
        this.collectCount = collectCount;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }


    public int getScoreCount() {
        return scoreCount;
    }

    public void setScoreCount(int scoreCount) {
        this.scoreCount = scoreCount;
    }

    public String getVisitorUpdateInfo() {
        return visitorUpdateInfo;
    }

    public void setVisitorUpdateInfo(String visitorUpdateInfo) {
        this.visitorUpdateInfo = visitorUpdateInfo;
    }
    public int getTitleSize() {
        return titleSize;
    }
    public void setTitleSize(int titleSize) {
        this.titleSize = titleSize;
    }

    public String getDescriptions() {
        return descriptions;
    }

    public void setDescriptions(String descriptions) {
        this.descriptions = descriptions;
    }
}

在scenicId上面使用注解,就表示scenicId是需要建立索引的。
但是我使用如下的方法,没有得到任何索引值:


 java


 
User user=new User(); Annotation[] annotations = user.getClass().getAnnotations(); System.out.println(annotations.length); for (Annotation annotation : annotations) { }

请问我该如何获得被注解的成员变量(在这里就是“scenicId”)?

java 注解

求合体,求交往 9 years, 9 months ago

 public static void main(String[] args) {
        Class<User> clazz = User.class;
        Field[] fields = clazz.getDeclaredFields();
        List<Field> result = new ArrayList<Field>();
        for (Field field:fields){
            if(field.getAnnotation(Index.class)!=null)
                result.add(field);
        }

        for(Field list:result){
            System.out.println("被注解的字段为:"+list.getName());
        }
    }

欲与春哥比爷们 answered 9 years, 9 months ago

Your Answer