gpt4 book ai didi

java - 使用java反射覆盖复杂对象实例中的值

转载 作者:行者123 更新时间:2023-12-01 18:41:41 28 4
gpt4 key购买 nike

我正在尝试编写一种方法来更改对象的一个​​嵌套值。我认为 java 反射是最好的方法,但我在开发解决方案时遇到了麻烦。我想做的是:

package abcClass;

import overwrite.ValueOverwriter;

public class main {
public static void main(String[] args) {
A obj1 = new A();
B bClass = new B();
C cClass = new C();
cClass.setStringValue("XXX");
bClass.setcClass(cClass);
obj1.setbClass(bClass);

E obj2 = new E();
D dClass = new D();
dClass.setSomeInt(10);
obj2.setdClass(dClass);

A newObj1 = (A) ValueOverwriter.overwiteValue(obj1, "bClass.cClass.someString", "YYY");
E newObj2 = (E) ValueOverwriter.overwiteValue(obj2, "dClass.someInt", "20");
}
}

带有我尝试实现的方法的类:

package overwrite;

import abcClass.Abcdef;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class ValueOverwriter {

// String path is path to value that is to be changed like: " aClass.bClass.cClass.stringValue"
public static Abcdef overwiteValue(Abcdef obj, String path, String newValue) {
List<String> propertyAddress = Arrays.asList(path.split("\\."));
StringBuilder methodToInvokeName = new StringBuilder("obj");

for (int i = 0; i < propertyAddress.size() - 1; i++) {
methodToInvokeName.append("." + returnMethodName("get", propertyAddress.get(i)));
}
String setterMethodName = methodToInvokeName.toString() + "." + returnMethodName("set", propertyAddress.get(propertyAddress.size() - 1));
String getterMethodName = methodToInvokeName.toString() + "." + returnMethodName("get", propertyAddress.get(propertyAddress.size() - 1));

try {
Method setterMethod = obj.getClass().getMethod(setterMethodName);
setterMethod.invoke(obj, newValue);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return obj;
}


private static String returnMethodName(String method, String field) {
return method + field.substring(0, 1)
.toUpperCase() + field.substring(1);
}
}

还有一些简单的类:

Abcdef 接口(interface):

    package abcClass;

public interface Abcdef {
}

一个类:

    package abcClass;

public class A implements Abcdef{
private B bClass;

public B getbClass() {
return bClass;
}

public void setbClass(B bClass) {
this.bClass = bClass;
}
}

B级:

package abcClass;

public class B{
private C cClass;

public C getcClass() {
return cClass;
}

public void setcClass(C cClass) {
this.cClass = cClass;
}
}

C类:

package abcClass;

public class C{
private String stringValue = "some String";

public String getStringValue() {
return stringValue;
}

public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}
}

D级:

package abcClass;

public class D {
private int someInt = 10;

public int getSomeInt() {
return someInt;
}

public void setSomeInt(int someInt) {
this.someInt = someInt;
}
}

E级:

package abcClass;

public class E implements Abcdef{
private D dClass;

public D getdClass() {
return dClass;
}

public void setdClass(D bClass) {
this.dClass = bClass;
}
}

我需要这个方法能够处理不同的值类型,也许我应该为此与 OverRider 进行接口(interface)?

现在我遇到异常:

"C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.2\lib\idea_rt.jar=64108:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_191\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\rt.jar;C:\Users\karolina.kosmala\batchWorkspace\spring-batch-master\field_overwrite\target\classes;C:\Users\karolina.kosmala\.m2\repository\org\springframework\spring-beans\5.2.3.RELEASE\spring-beans-5.2.3.RELEASE.jar;C:\Users\karolina.kosmala\.m2\repository\org\springframework\spring-core\5.2.3.RELEASE\spring-core-5.2.3.RELEASE.jar;C:\Users\karolina.kosmala\.m2\repository\org\springframework\spring-jcl\5.2.3.RELEASE\spring-jcl-5.2.3.RELEASE.jar" abcClass.main
java.lang.NoSuchMethodException: abcClass.A.obj.getBClass.getCClass.setSomeString()
at java.lang.Class.getMethod(Class.java:1786)
at overwrite.ValueOverwriter.overwiteValue(ValueOverwriter.java:25)
at abcClass.main.main(main.java:19)
java.lang.NoSuchMethodException: abcClass.E.obj.getDClass.setSomeInt()
at java.lang.Class.getMethod(Class.java:1786)
at overwrite.ValueOverwriter.overwiteValue(ValueOverwriter.java:25)
at abcClass.main.main(main.java:20)

Process finished with exit code 0

最佳答案

abcClass.A.obj.getBClass.getCClass.setSomeString() 显示您缺少 getter 方法的括号。那是对的吗?我认为这应该是生成的字符串:abcClass.A.obj.getBClass().getCClass().setSomeString()

可能需要对 returnMethodName() 进行此更改:

private static String returnMethodName(String method, String field) {
return method + field.substring(0, 1)
.toUpperCase() + field.substring(1) + "()"; //parantheses
}

但是,总而言之,我不确定您所采取的方法是否良好 - 使用反射的方法。已经有Java Beans API可用于此类目的。你检查了吗?

关于java - 使用java反射覆盖复杂对象实例中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59922233/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com