访问父项的属性注释

时间:2022-08-14 15:56:27

Say I have two below classes

说我有两个以下的课程

class Parent extends MyBase {
    @Annot(key="some.key", ref=Child.class)
    public List<Child> children = new List<Child>();
}

class Child extends MyBase {
    @Annot(key="another.key")
    public String id;
}

Now say I have

现在说我有

  • a Parent class object => parent and
  • 父类对象=>父和

  • containing 3 Child class objects in children list.
  • 在子列表中包含3个子类对象。

It means that parent.children.get(0).id can be accessed. Now i need to form the key sequences of the attribute id. Here Key Sequence is the concatenated string of all the key values of @Annot annotation. For example, in this case the key sequence is some.key/another.key

这意味着可以访问parent.children.get(0).id。现在我需要形成属性id的键序列。这里的Key Sequence是@Annot注释的所有键值的连接字符串。例如,在这种情况下,键序列是some.key / another.key

Any way to get it done through java reflection?

有没有办法通过java反射完成它?

1 个解决方案

#1


0  

This is a possible way which doesn't use objects in children. It inspects generic type of children and scans this class to find an annotation.

这是一种不在儿童中使用对象的可能方法。它检查子类的泛型类型并扫描此类以查找注释。

    Field childrenField = Parent.class.getField("children");
    Annotation[] annotations = childrenField.getDeclaredAnnotations();

    String key = null;
    for (Annotation annotation : annotations) {
        if (annotation instanceof Annot) {
            Annot a = (Annot) annotation;
            key = a.key();
            break;
        }
    }

    ParameterizedType type = (ParameterizedType) childrenField.getGenericType();
    Class<?> c = (Class<?>) type.getActualTypeArguments()[0];
    annotations = c.getDeclaredField("id").getAnnotations();
    for (Annotation annotation : annotations) {
        if (annotation instanceof Annot) {
            Annot a = (Annot) annotation;
            key += "/" + a.key();
            break;
        }
    }

    System.out.println(key);

See this guide for more information about annotations.

有关注释的详细信息,请参阅本指南。

#1


0  

This is a possible way which doesn't use objects in children. It inspects generic type of children and scans this class to find an annotation.

这是一种不在儿童中使用对象的可能方法。它检查子类的泛型类型并扫描此类以查找注释。

    Field childrenField = Parent.class.getField("children");
    Annotation[] annotations = childrenField.getDeclaredAnnotations();

    String key = null;
    for (Annotation annotation : annotations) {
        if (annotation instanceof Annot) {
            Annot a = (Annot) annotation;
            key = a.key();
            break;
        }
    }

    ParameterizedType type = (ParameterizedType) childrenField.getGenericType();
    Class<?> c = (Class<?>) type.getActualTypeArguments()[0];
    annotations = c.getDeclaredField("id").getAnnotations();
    for (Annotation annotation : annotations) {
        if (annotation instanceof Annot) {
            Annot a = (Annot) annotation;
            key += "/" + a.key();
            break;
        }
    }

    System.out.println(key);

See this guide for more information about annotations.

有关注释的详细信息,请参阅本指南。