这个帖子是关于 JAVA 中鲜为人知的特性 http://howtodoinjava.com/tag/java-hidden-features/ 的后续更新, 如果想得到下次在线讨论的更新, 请通过邮件订阅, 并且不要忘了在评论区留下你的意见和建议.
Java 是一个安全的开发工具, 它阻止开发人员犯很多低级的错误, 而大部份的错误都是基于内存管理方面的. 如果你想搞破坏, 可以使用 Unsafe 这个类. 这个类是属于 sun.* API 中的类, 并且它不是 J2SE 中真正的一部份, 因此你可能找不到任何的官方文档, 更可悲的是, 它也没有比较好的代码文档.
实例化 sun.misc.Unsafe
如果你尝试创建 Unsafe 类的实例, 基于以下两种原因是不被允许的.
1),Unsafe 类的构造函数是私有的;
2), 虽然它有静态的 getUnsafe() 方法, 但是如果你尝试调用 Unsafe.getUnsafe(), 会得到一个 SecutiryException. 这个类只有被 JDK 信任的类实例化.
但是这总会是有变通的解决办法的, 一个简单的方式就是使用反射进行实例化:
- Field f = Unsafe.class.getDeclaredField("theUnsafe"); //Internal reference
- f.setAccessible(true);
- Unsafe unsafe = (Unsafe) f.get(null);
注: IDE 如 Eclipse 对会这样的使用报错, 不过不用担心, 直接运行代码就行, 可以正常运行的.
(译者注: 还有一种解决方案, 就是将 Eclipse 中这种限制获取由错误, 修改为警告, 具体操作为将 Windows->Preference...->Java->Compiler->Errors/Warnings 中的 "Deprecated and restricted API", 级别由 Error 修改为 Warning 就可以了)
现在进入主题, 使用这个对象我们可以做如下 "有趣的" 事情.
使用 sun.misc.Unsafe
1), 突破限制创建实例
通过 allocateInstance() 方法, 你可以创建一个类的实例, 但是却不需要调用它的构造函数, 初使化代码, 各种 JVM 安全检查以及其它的一些底层的东西. 即使构造函数是私有, 我们也可以通过这个方法创建它的实例.
(这个对单例模式情有独钟的程序员来说将会是一个噩梦, 它们没有办法阻止这种方式调用 )
看下面一个实例 (注: 为了配合这个主题, 译者将原实例中的 public 构造函数修改为了私有的):
- public class UnsafeDemo {
- public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException {
- Field f = Unsafe.class.getDeclaredField("theUnsafe"); // Internal reference
- f.setAccessible(true);
- Unsafe unsafe = (Unsafe) f.get(null);
- // This creates an instance of player class without any initialization
- Player p = (Player) unsafe.allocateInstance(Player.class);
- System.out.println(p.getAge()); // Print 0
- p.setAge(45); // Let's now set age 45 to un-initialized object
- System.out.println(p.getAge()); // Print 45
- }
- }
- class Player {
- private int age = 12;
- private Player() {
- this.age = 50;
- }
- public int getAge() {
- return this.age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
2), 使用直接获取内存的方式实现浅克隆
如何实现浅克隆? 在 clone(){...} 方法中调用 super.clone(), 对吗? 这里存在的问题是首先你必须继续 Cloneable 接口, 并且在所有你需要做浅克隆的对象中实现 clone() 方法, 对于一个懒懒的程序员来说, 这个工作量太大了.
我不推荐上面的做法而是直接使用 Unsafe, 我们可以仅使用几行代码就实现浅克隆, 并且它可以像某些工具类一样用于任意类的克隆.
这个戏法就是把一个对象的字节码拷贝到内存的另外一个地方, 然后再将这个对象转换为被克隆的对象类型.
3), 来自黑客的密码安全
这个好似很有趣吧? 实事就是这样的. 开发人员创建密码或者是保证密码到字符串中, 然后在应用程序的代码中使用这些密码, 使用过后, 聪明的程序员会把字符串的引用设为 NULL, 因此它就不会被引用着并且很容易被垃圾收集器给回收掉.
- String password = new String("l00k@myHor$e");
- String fake = new String(password.replaceAll(".", "?"));
- System.out.println(password); // l00k@myHor$e
- System.out.println(fake); // ????????????
- getUnsafe().copyMemory(fake, 0L, null, toAddress(password), sizeOf(password));
- System.out.println(password); // ????????????
- System.out.println(fake); // ????????????
- //Sample code to craeet classes
- byte[] classContents = getClassContent();
- Class c = getUnsafe().defineClass(null, classContents, 0, classContents.length);
- c.getMethod("a").invoke(c.newInstance(), null);
- //Method to read .class file
- private static byte[] getClassContent() throws Exception {
- File f = new File("/home/mishadoff/tmp/A.class");
- FileInputStream input = new FileInputStream(f);
- byte[] content = new byte[(int)f.length()];
- input.read(content);
- input.close();
- return content;
- }
- class SuperArray {
- private final static int BYTE = 1;
- private long size;
- private long address;
- public SuperArray(long size) {
- this.size = size;
- // 得到分配内存的起始地址
- address = getUnsafe().allocateMemory(size * BYTE);
- }
- public void set(long i, byte value) {
- getUnsafe().putByte(address + i * BYTE, value);
- }
- public int get(long idx) {
- return getUnsafe().getByte(address + idx * BYTE);
- }
- public long size() {
- return size;
- }
- }
- long SUPER_SIZE = (long)Integer.MAX_VALUE * 2;
- SuperArray array = new SuperArray(SUPER_SIZE);
- System.out.println("Array size:" + array.size()); // 4294967294
- for (int i = 0; i < 100; i++) {
- array.set((long)Integer.MAX_VALUE + i, (byte)3);
- sum += array.get((long)Integer.MAX_VALUE + i);
- }
- System.out.println("Sum of 100 elements:" + sum); // 300
来源: http://www.bubuko.com/infodetail-2928119.html