这可能是任何人都提出过的最愚蠢的问题,但是只是出于好奇,我们可以将值分配给作为参数传递的对象吗?例如
public class ForDemo3 {
int i = 0;
ArrayList cp = new ArrayList();
public ForDemo3() {
int j = 30;
cp.add(j);
cp.add(i);
i = 5;
j = 12;
System.out.println("i = " + i + " & j= " + j);
assignDvalues(i, j);
System.out
.println("After Restoring the values i = " + i + " & j= " + j);
}
public void assignDvalues(Object... obj) {
for (int n = 0; n <= obj.length - 1; n++) {
obj[n] = cp.get(n);
System.out.println("" + obj[n]);
}
}
public static void main(String[] args) {
new ForDemo3();
}
}
我正在尝试在Java中开发基于检查点的恢复。并且cp
是可以存储值的检查点对象。
上面的输出是
i = 5 & j= 12
30
0
After Restoring the values i = 5 & j= 12
编辑道歉我不是stackoverflow的好用户。我正在研究一个用于并行计算的框架。并且将有一个函数savecheckpoint(List l)
将用于将列表或数组列表存储到DB作为检查点,而另一个函数public object restoreCheckPoint(int i)
将恢复列表。到目前为止,我已经能够保存检查点,然后将其还原。但是,如何在不影响用户代码的情况下分配对象或变量的值?如以上示例所示,我想使用恢复值assignDvalues()
。有什么办法使之成为可能?
这将是一个真正的技巧,因此请明智地使用它。跟随方法使用反射并且具有两个局限性。无法处理原始类型(由于自动装箱),也无法处理初始化为常量的字符串(即String s = "some string"
不支持,但String s = new String("some string")
效果很好)。另外,您应该使用new Integer(X)
而不是int x
不弄乱原始类型(类似于String的故事)
在下面的示例中,我int i
用Integer i
和代替了,j
因为正如我所说,不支持基元。
用户唯一需要的是使用“ Integer”而不是“ int”,“ Long”而不是“ long”等,而且,很痛苦的是,避免将String定义为常量。
public class Demo2 {
Integer i = 0;
ArrayList cp = new ArrayList();
public Demo2() {
Integer j = 30;
String s = "def";
cp.add(j);
cp.add(i);
cp.add(s);
i = new Integer(5); // you need to use "i = new Integer(5)" instead of "i = 5" because you risk with messing the primitive int 5
j = new Integer(12); // here also you need to use new Integer(12)
s = new String("some string"); // NOTE: don't even try this with s ="some string" because this will lead to unexpected behaviour
//s = "some string" - if you pass string initialized this way you will mess up the string "some string" in jvm string pool
System.out.println("i = " + i + " & j= " + j + " & s= " + s);
assignDvalues(i, j, s);
System.out.println("After Restoring the values i = " + i + " & j= " + j + " & s= " + s);
}
public void assignDvalues(Object... obj) {
try {
for (int n = 0; n <= (obj.length - 1); n++) {
if (obj[n] instanceof Integer) {
Integer curInt = (Integer) obj[n];
Field field = curInt.getClass().getDeclaredField("value"); // Integer stores the real value in private field "value"
field.setAccessible(true);
field.set(curInt, cp.get(n));
System.out.println("" + obj[n]);
} else if (obj[n] instanceof String) {
String curS = (String) obj[n];
Field field = curS.getClass().getDeclaredField("value"); // String stores the text as char sequence in private field "value"
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); // need to reset final flag to override value of String
field.set(curS, ((String) cp.get(n)).toCharArray());
System.out.println("" + obj[n]);
} // else if (obj[n] insteanceOf SomeOtherType) { // you should provide implementation specific for each type/class you want to support with assignDvalues method
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static void main(String[] args) throws Exception {
new Demo2();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句