You can find a very similiar question here with quite good explaination:
HERE:
Why can final object be modified?
By the way I started thinking about Reflection mechanism ....
in theory ... I belive you can get owning class instance.. then get class members, find current instance and check it is final ....
I didn't check it and I dont't even kwnow if it's possible - it's just an idea (maybe ?)
in
public class Final {
static final Point p = new Point();
public static void main(String[] args) throws MyImmutableException {
p = new Point(); // Fails
p.setB(10); // OK
p.setA(20); // Fails - throws MyImmutableException
}
}
public class Point() {
int a = 10;
int b = 20;
public setA(int a) {
this.a = a;
}
public setB(int b) throws MyImmutableException {
detectIsFinal()
this.b = b;
}
private void detectIsFinal() throws MyImmutableException {
int mod = this.getClass().getModifiers()
if (Modifier.isFinal(mod)) {
throw new MyImmutableException();
}
}
}
public class MyImmutableException extends Exception {
public MyImmutableException() {super(); }
}
I was thinking what else you can do ... again it's just a !!!!!!!!! PSEUDOCODE !!!
I am not sure if it will even work :P
Maybe someone with annotations knowledge will be inspired and make it work. Unfortunatelly I don't have more time this week. Maybe later I will try to make a POC.
public class Final {
@Immutable
static final Point p = new Point();
public static void main(String[] args) {
p = new Point(); // Fails
p.setB(10); // Fails
p.setA(20); // OK
}
}
public class Point() {
int a = 10;
@ImmutableOnThisInstance
int b = 20;
@DetectIsImmutable
public setA(int a) {
this.a = a;
}
@DetectIsImmutable
public setB(int b) {
this.b = b;
}
}
class Immutable {
?????????????????
}
class DetectIsImmutable {
/**
* Just a pseudocode - I don't know how to work with ANNOTATIONS :) :) :)
* SORRY :)
* @throws MyImmutableException
*/
private void detectMethod() throws MyImmutableException {
Immutable instance = CLASS_THAT_IS_ANNOTATED.getClass().getAnnotation(Immutable.class)
if (instance != null) {
// get parent Method invocation name (from stacktrace list)
String methodName = .............;
if (methodName.startsWith("set")) {
// check id we have variable with this settername
String fieldName = ...; // cut "set" get rest of it, make first letterSmall
// find this field in object fields
Field f = this.getClass().getDeclaredField(fieldName);
if (f.getAnnotation(ImmutableOnThisInstance.class) != null) {
throw MyImmutableException();
}
}
}
}
}