Make sure the class is immutable

In this, the answers to the 6-year answers to the questions say that it is impossible to verify immutability. On the other hand, there are two fairly recent answers below that suggest you can use:

To determine if a class is immutable.

My questions are: are these tools useful in real life? Is there any other (better) solution? Has something changed in the last few years?

+6
source share
2 answers

Although I am sure that there are many tools for direct testing, if the class is immutable, the real problem is testing immutable classes, which allow the use of fields containing an interface or declaration of an abstract class. In principle, only private immutable classes can be resolved as child fields, since inheritance often violates immutability.

It is difficult to make complex immutable objects without using the Java collection API (i.e. do not use Map , List or Set , etc.

Another problem is that just because the final field does not really make it β€œfinal”. Many serialization tools, such as Hibernate, will happily create immutable objects and set final fields through reflection.

The only way I can see is to detect true immutability is by analyzing the runtime, but it may take a long time before your code really gets into a specific use case where the variability happens, and I would suggest that these kinds of devices quite expensive.

Another interesting view of the alleged consequence of immutable objects is that they should not change things outside when they are built. That is, the creation of an immutable object must be free from side effects. This is where the tools will have a hard time. You can easily create an immutable object with a constructor that does all kinds of external muck, for example, discarding threads or setting system properties.

+1
source

As to whether the tool is useful in real life, it depends on why you need immutability (as indicated in this article, you check at runtime or through verification or protection against malicious code, or simply guarantee that your own code is immutable ?). The simplest assurance that a class is immutable, if you manually view it, is that all field entries occur only in the constructor, or that all fields are final (or both). A.

But this is still limited, because "final Object x" ensures that your link does not change, but does not mean that the link object does not change. As Adam evades, if this list can change the list, which can cause thread problems in your (otherwise immutable) class. And if you used the value in the list to determine your hash code (you use x.hashCode () as part of generating your own hash code), then your hash code will change, violating other rules (therefore the immutable representation of the list isn’t even enough).

If your immutable class is not affected by the changes to the held object, then this may be good for your purposes.

A run-time check must be limited in time / depth / limit, therefore it cannot be perfect. Those tools (or any similar execution approach) have some use, but it really depends on why you need to define immutability. If you can specify immutability as part of an API requirement, detect violations of this rule (for example, hashCode of object changes) and throw an exception when they change, then you do not need to detect it yourself. Or trust your subscribers. If you are trying to write an immutable class, these tools do not help at all. If you are really looking for thread safety, immutability is not an adequate answer.

This is not a satisfactory answer to this question (which is good), but I think that in most cases this becomes the wrong question if you consider the problem more closely.

0
source

Source: https://habr.com/ru/post/980716/


All Articles