简体   繁体   English

从依赖类调用 JsonProperty setter 方法

[英]Call JsonProperty setter method from a dependent class

I have a class as follows:我有一堂课如下:

public class MyClass {
   @JsonProperty("my_id")
   private String id;

   @JsonProperty("my_list")
    private List<SecondClass> myList;

   public getId() {
      return this.id;
   }

   public setId(String id) {
      this.id = id;
   }

   public getMyList() {
      return this.myList;
   }

   public setMyList(List<SecondClass> myList) {
      this.myList = myList;
   }
}

My class has a dependency on another class called SecondClass [through the List entity]我的班级依赖于另一个名为 SecondClass 的班级 [通过 List 实体]

public class SecondClass {
   @JsonProperty("my_name")
   private String name;

   public getName() {
      return this.name;
   }

   public setName(String name) {
      this.name = name;
   }
}

I know how to access the getters and setters of "MyClass" using Reflection based on the JsonProperty name, as shown below:我知道如何使用基于JsonProperty名称的反射来访问“MyClass”的 getter 和 setter,如下所示:

public void myMethod(MyClass myClass, String jsonProperty, String newId) throws IllegalAccessException {
    for (Field field : MyClass.class.getDeclaredFields()) {
        JsonProperty jsonPropAnnotation = field.getAnnotation(JsonProperty.class);
        if (jsonPropAnnotation != null)
            if (jsonPropAnnotation.value().equals(jsonProperty)) {
                field.setAccessible(true);
                field.set(myClass, newId);
            }
    }
}

But, my question is, is there a way to fetch the getter and setter from SecondClass via MyClass based on the JsonProperty name using Reflection?但是,我的问题是,有没有办法通过MyClass基于 JsonProperty 名称使用反射从SecondClass获取 getter 和 setter?

As an example I would like to call getList() based on JsonProperty value "my_list" and then setName() based on the JsonProperty value "my_name".例如,我想根据 JsonProperty 值“my_list”调用 getList(),然后根据 JsonProperty 值“my_name”调用 setName()。

Is this a possibility using reflection?这是使用反射的可能性吗?

I am not sure what you want to achieve and how you will pass the values to set but all this is possible.我不确定您想要实现什么以及如何传递要设置的值,但这一切都是可能的。 Please check below code piece for your problem.请检查下面的代码片段以解决您的问题。 If you can explain me the full requirement then may be I can help you in implementing the best solution but for this ask below code will also work -如果您可以向我解释全部要求,那么我可能可以帮助您实施最佳解决方案,但对于这个问题,下面的代码也可以使用 -

public class Main {
public static void main(String[] args) throws IllegalAccessException {
    MyClass myClass = new MyClass();
    List<SecondClass> secondClasses = new ArrayList<>();
    myClass.setId("1234");
    SecondClass sc1 = new SecondClass();
    sc1.setName("Name1");
    secondClasses.add(sc1);

    myMethod(myClass, "my_id", "1234");
    System.out.println(myClass.getId());
    for (SecondClass secondClass : secondClasses) {
        SecondClass secondClass1 = new SecondClass();
        myMethod(secondClass1, "my_name", secondClass);
        System.out.println(secondClass1.getName());
    }

}

public static void myMethod(Object object, String jsonProperty, Object value) throws IllegalAccessException {
    for (Field field : object.getClass().getDeclaredFields()) {

        JsonProperty jsonPropAnnotation = field.getAnnotation(JsonProperty.class);
        if (jsonPropAnnotation != null)
            if (jsonPropAnnotation.value().equals(jsonProperty)) {
                if (field.getType().equals(String.class) || field.getType().equals(Double.class)) {
                    field.setAccessible(true);
                    if (value.getClass().equals(String.class)) {
                        field.set(object, value);
                    }else{
                        field.set(object, field.get(value));
                    }
                }

            }
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM