从依赖类调用JSONPROPERTY SETETER方法

发布于 2025-02-11 20:20:28 字数 1431 浏览 1 评论 0原文

我的课程如下:

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;
   }
}

我的班级对另一个名为secondclass(通过列表实体)的班级有一个

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

   public getName() {
      return this.name;
   }

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

依赖性> JSONPROPERTY 名称,如下所示:

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);
            }
    }
}

但是,我的问题是,是否有一种方法可以通过 secondClass 通过 myclass 基于jsonproperty使用反射的名称?

作为一个例子,我想基于jsonproperty值调用getList(),然后基于jsonproperty值“ my_name”。

这是使用反射的可能性吗?

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]

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:

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?

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".

Is this a possibility using reflection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄风 2025-02-18 20:20:29

我不确定您要实现什么以及如何将值传递给设置,但这是可能的。请查看下面的代码件,以了解您的问题。
如果您可以向我解释全部要求,那么我可以帮助您实施最佳解决方案,但是对于此问题,下面的代码也将起作用 -

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));
                    }
                }

            }
    }
}
}

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));
                    }
                }

            }
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文