问题
is there a list of de-facto immutable classes in the jdk?
technically Immutable classes include the obvious Integer, Double etc..
de-facto immutable will include for example java.lang.String - it might technically be mutable but de-facto it is not.
Also, are there Interfaces/Abstract classes which are required (as stated in the javadoc) to be immutable?
if you cannot provide a complete List, i would already be happy if you know a bunch of classes which state immutability in its javadoc..
回答1:
I try to compile the list as much as I can:
- java.lang.String The wrapper classes for the primitive types:
- java.lang.Integer
- java.lang.Byte
- java.lang.Character
- java.lang.Short
- java.lang.Boolean
- java.lang.Long
- java.lang.Double
- java.lang.Float
- java.lang.StackTraceElement (used in building exception stacktraces)
- Most of the enum classes
- java.math.BigInteger
- java.math.BigDecimal
- java.io.File
- java.awt.Font
- java.awt.BasicStroke
- java.awt.Color
- java.awt.GradientPaint,
- java.awt.LinearGradientPaint
- java.awt.RadialGradientPaint,
- java.awt.Cursor
- java.util.Locale
- java.util.UUID
- java.util.Collections
- java.net.URL
- java.net.URI
- java.net.Inet4Address
- java.net.Inet6Address
- java.net.InetSocketAddress
- most subclasses of java.security.Permission
回答2:
You can use MutabilityDetector and feed it JARs from the JDK to enumerate most immutable classes. It's "most" because it's so strict that even the tiny bit of state change in java.lang.String is enough to make it considered mutable, but the goal is to account for this by 1.0 release.
You can download the latest JAR here: https://github.com/MutabilityDetector/MutabilityDetector/releases
Here's an example of it's use. This is what I used to get most of the immutable JDK 1.7 classes on OSX:
java -jar MutabilityDetector-0.9.5.jar -r IMMUTABLE -cp /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/rt.jar
Here's the output (slightly cleaned up): https://gist.github.com/CalebFenton/85fc87edf64033afe110
I needed to do this for Android framework classes. The tricky part was finding a JAR with Android classes, and not just stubs which is the one included in the SDK. The good people at Robolectric make one, which you can download here: http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22android-all%22
You can see the list of immutable Android classes I use, which includes many JDK classes here: https://github.com/CalebFenton/simplify/blob/master/smalivm/src/main/resources/immutable_classes.cfg
回答3:
Classes whose object contents cannot be modified is called immutable classes. All primitive data types(Wrapper classes only) are immutable. For any class to be immutable the following needs to be done.
- Make all fields private
- Don't provide mutators
- Ensure that methods can't be overridden by either making the class final (Strong Immutability) or making your methods final (Weak Immutability)
- If a field isn't primitive or immutable, make a deep clone on the way in and the way out.
Thank you
来源:https://stackoverflow.com/questions/5998031/complete-list-of-immutable-jdk-classes