あなたはここで非常によく似た質問を見つけることができます:非常に良い説明があります:
ここで:
なぜ最終オブジェクトを変更できるのですか?
ちなみに私は反射メカニズムについて考え始めました…。
理論的には...所有しているクラスインスタンスを取得できると信じています。次に、クラスメンバーを取得し、現在のインスタンスを見つけて、それが最終的なものであることを確認します...。
私はそれをチェックしませんでした、そしてそれが可能であるかどうかさえ知りません-それはただの考えです(多分?)
の
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(); }
}
私はあなたが他に何ができるかを考えていました...再びそれはただの!!!!!!!!!です 擬似コード!!! それがうまくいくかどうかはわかりません:P
おそらく、注釈の知識を持っている人が刺激を受けて、それを機能させるでしょう。残念ながら、今週はこれ以上時間がありません。後で、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();
                }
            }
        }
    }           
}