samedi 7 juillet 2018

In Java is it possible to change or modify an enum it self and thus to corrupt an enum singleton?

Is it possible to change an enum itself at run-time somehow? E.g. using reflection. The question is not about to change the state of an enum constant. It's about to change the set of constants of an enum or to remove any constants.

Regarding the following enum is it possible to add a color WHITE or remove color RED or to change their order?

public enum Color {

  RED, GREEN, BLUE;

}

Why I ask?

I know this question is kind of malicious. But even Joshua Bloch mentioned a "cleverly crafted attack" when talking (1) about implementing a singleton and recommended the enum singleton pattern. If we can modify an enum is then an attack on this pattern feasible?

I tried to work it out and managed it partially. I will post my results as an answer - following this advice.


(1) See What is an efficient way to implement a singleton pattern in Java? There follow the link to effective_java_reloaded.pdf, page 31.





Aucun commentaire:

Enregistrer un commentaire