Thomas Darimont
Erfahrenes Mitglied
Hallo!
let's do some black magic
... geht witzigerweise auch per Reflection:
aber nur für finale Instanz member. Klassen member können damit nicht ersetzt werden.
Gruss Tom
let's do some black magic
Code:
package de.tutorials;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class VeryDirtyFinalFieldChangeExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
SomeObject o = new SomeObject("Hallo");
System.out.println(o);
Field field = SomeObject.class.getDeclaredField("str");
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
long fieldOffset = unsafe.fieldOffset(field);
unsafe.putObject(o, fieldOffset, "FOO");
System.out.println(o);
}
static class SomeObject {
final String str;
public SomeObject(String str) {
this.str = str;
}
public String toString() {
return super.toString() + " " + this.str;
}
}
}
... geht witzigerweise auch per Reflection:
Code:
/**
*
*/
package de.tutorials;
import java.lang.reflect.Field;
/**
* @author Thomas.Darimont
*
*/
public class FinalFieldSetExample {
private final Object OBJECT = new Object();
/**
* @param args
*/
public static void main(String[] args) throws Throwable{
FinalFieldSetExample finalFieldSetExample = new FinalFieldSetExample();
System.out.println(finalFieldSetExample.OBJECT);
Field field = FinalFieldSetExample.class.getDeclaredField("OBJECT");
field.setAccessible(true);
field.set(finalFieldSetExample, "BUBU");
System.out.println(finalFieldSetExample.OBJECT);
}
}
aber nur für finale Instanz member. Klassen member können damit nicht ersetzt werden.
Gruss Tom