What is Mutability Detector?Mutability Detector is designed to analyse Java classes and report on whether instances of a given class are immutable. It can be used:
- In a unit test, with an assertion like
assertImmutable(MyClass.class). Is your class actually immutable? What about after that change you just made?
- As a FindBugs plugin. Those classes you annotated with
@Immutable, are they actually?
- At runtime. Does your API require being given immutable objects?
- From the command line. Do you want to quickly run Mutability Detector over an entire code base?
Why Try To Detect Mutability?Developing classes to be immutable has several benefits. An immutable object is one which cannot be changed once it is constructed. While writing concurrent programs, using immutable objects can greatly simplify complex systems, as sharing an object across threads is much safer. There are a few rules for what makes an object immutable, and it is easy to break the rules and render the object unsafe. This could lead to subtle, hard-to-detect bugs which could lower the integrity of the system. Using an automated tool to recognise mutability where it's not intended can reduce the complexity of writing immutable classes.
Getting started with Unit TestingGot Mutability Detector jar? Ready to write your first test? Check out the getting started guide!
Check out the latest Mutability Detector blog posts: