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,因此它就不会被引用着并且很容易被垃圾收集器给回收掉。但是从你将引用设为NULL到被垃圾收集器收集的这个时间段之内(原文:But from the time, you made the reference null to the time garbage collector kicks in),它是处于字符串池中的,并且在你系统中进行一个复杂的攻击(原文:And a sophisticated attack on your system),也是可以读取到你的内存区域并且获得密码,虽然机会很小,但是总是存在的。
这就是为什么建议使用char[]数组存放密码,当使用完过后,你可以迭代处理当前数组,修改/清空这些字符。
另外一个方式就是使用魔术类Unsafe。你可以创建另外一个和当前密码字符串具有相同长度的临时字符串,将临时密码中的每个字符都设值为"?"或者"*"(任何字符都可以),当你完成密码的逻辑后,你只需要简单的将临时密码中的字节数组拷贝到原始的密码串中,这就是使用临时密码覆盖真实的密码。
示例代码可能会是这样:
- 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); // ????????????
我们可以在运行时运态的创建类,例如通过编译后的.class文件,操作方式就是将.class文件读取到字节数据组中,并将其传到defineClass方法中。
- //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;
- }
4)、超大数组
从所周知,常量Integer.MAX_VALUE是JAVA中数组长度的最大值,如果你想创建一个非常大的数组(虽然在通常的应用中不可能会用上),可以通过对内存进行直接分配实现。
下面这个示例将会创建分配一段连续的内存(数组),它的容易是允许最大容量的两倍。
- 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;
- }
- }
但请注意这可能会导致JVM挂掉。
结束语
sun.misc.Unsafe provides almost unlimited capabilities for exploring and modification of VM’s runtime data structures. Despite the fact that these capabilities are almost inapplicable in Java development itself, Unsafe is a great tool for anyone who want to study HotSpot VM without C++ code debugging or need to create ad hoc profiling instruments.
sun.misc.Unsafe提供了可以随意查看及修改JVM中运行时的数据结构,尽管这些功能在JAVA开发本身是不适用的,Unsafe是一个用于研究学习HotSpot虚拟机非常棒的工具,因为它不需要调用C++代码,或者需要创建即时分析的工具。
参考连接:http://blog.csdn.net/fenglibing/article/details/17138079