java 不可变 JDK 类的完整列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5998031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Complete List of immutable JDK classes?
提问by Andreas Petersson
is there a list of de-factoimmutable classes in the jdk?
jdk 中是否有事实上的不可变类列表?
technically Immutable classes include the obvious Integer, Double etc..
从技术上讲,不可变类包括明显的 Integer、Double 等。
de-facto immutable will include for example java.lang.String - it might technically be mutable but de-facto it is not.
事实上的不可变将包括例如 java.lang.String - 它在技术上可能是可变的,但事实上它不是。
Also, are there Interfaces/Abstract classes which are required (as stated in the javadoc) to be immutable?
另外,是否有接口/抽象类需要(如 javadoc 中所述)是不可变的?
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..
如果你不能提供一个完整的列表,如果你知道一堆在其 javadoc 中声明不可变性的类,我会很高兴。
回答by kavi temre
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
- java.lang.String 原始类型的包装类:
- java.lang.Integer
- java.lang.Byte
- java.lang.Character
- java.lang.Short
- 布尔值
- java.lang.Long
- java.lang.Double
- java.lang.Float
- java.lang.StackTraceElement(用于构建异常堆栈跟踪)
- 大多数枚举类
- java.math.BigInteger
- java.math.BigDecimal
- java.io.文件
- java.awt.Font
- java.awt.BasicStroke
- java.awt.Color
- java.awt.GradientPaint,
- java.awt.LinearGradientPaint
- java.awt.RadialGradientPaint,
- java.awt.Cursor
- java.util.Locale
- 实用程序UUID
- java.util.Collections
- 网址
- java.net.URI
- java.net.Inet4地址
- java.net.Inet6地址
- java.net.InetSocket地址
- java.security.Permission 的大多数子类
回答by Caleb Fenton
You can use MutabilityDetectorand 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.Stringis enough to make it considered mutable, but the goal is to account for this by 1.0 release.
您可以使用MutabilityDetector并从 JDK 提供 JAR 以枚举大多数不可变类。它是“最”的,因为它非常严格,即使java.lang.String 中的微小状态变化也足以使它被认为是可变的,但目标是在 1.0 版本中解决这个问题。
You can download the latest JAR here: https://github.com/MutabilityDetector/MutabilityDetector/releases
你可以在这里下载最新的 JAR: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:
这是它的使用示例。这是我用来在 OSX 上获得大部分不可变 JDK 1.7 类的方法:
java -jar MutabilityDetector-0.9.5.jar -r IMMUTABLE -cp /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/rt.jar
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
这是输出(稍微清理):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 Robolectricmake one, which you can download here: http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22android-all%22
我需要为 Android 框架类执行此操作。棘手的部分是找到一个带有 Android 类的 JAR,而不仅仅是 SDK 中包含的存根。善良的人们在Robolectric做一个,你可以在这里下载: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
您可以在此处查看我使用的不可变 Android 类列表,其中包括许多 JDK 类:https: //github.com/CalebFenton/simplify/blob/master/smalivm/src/main/resources/immutable_classes.cfg
回答by Anuj Balan
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
谢谢