Pointer-Spielereien in Java

Thomas Darimont

Erfahrenes Mitglied
Hallo,

Java:
package de.tutorials;

import java.lang.reflect.Field;

import  sun.misc.Unsafe;

public class UnsafeExample {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
        Field theUnsafeField =Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafeField.setAccessible(true);
        Unsafe unsafe = (Unsafe)theUnsafeField.get(null);
        
        String[] data = {"AAA","BBB","CCC"};
        
        
        long arrayBaseOffset = unsafe.arrayBaseOffset(String[].class);
        long arrayIndexWidth = unsafe.arrayIndexScale(String[].class);
        
        long index = 2; //"CCC"
        
        Object o = unsafe.getObject(data, arrayBaseOffset + index * arrayIndexWidth);
        System.out.println(o);
    }

}

Gruß Tom
 

Neue Beiträge

Zurück