从依赖类调用JSONPROPERTY SETETER方法
我的课程如下:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您要实现什么以及如何将值传递给设置,但这是可能的。请查看下面的代码件,以了解您的问题。
如果您可以向我解释全部要求,那么我可以帮助您实施最佳解决方案,但是对于此问题,下面的代码也将起作用 -
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 -