Advertisement
TheDemystifier

NULL Value in Java

Jul 9th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. import java.lang.reflect.Field;
  2. import javax.swing.JOptionPane;
  3. import sun.misc.Unsafe;
  4.  
  5. public class Testing {
  6.     public static void main(String[] args) throws Exception {
  7.         Integer varInt = 147147147;
  8.         System.out.println(Long.toHexString(Addresser.addressOf(varInt)));
  9.         JOptionPane.showMessageDialog(null, "1st Change");
  10.  
  11.         varInt = 258258258;
  12.         System.out.println(Long.toHexString(Addresser.addressOf(varInt)));
  13.         JOptionPane.showMessageDialog(null, "2nd Change");
  14.  
  15.         varInt = null;
  16.         System.out.println(Long.toHexString(Addresser.addressOf(varInt)));
  17.         JOptionPane.showMessageDialog(null, "Changed to null");
  18.  
  19.         varInt = 639639639;
  20.         System.out.println(Long.toHexString(Addresser.addressOf(varInt)));
  21.         JOptionPane.showMessageDialog(null, "4th Change");
  22.     }
  23. }
  24.  
  25. class Addresser {
  26.     @SuppressWarnings("restriction")
  27.     private static Unsafe unsafe;
  28.  
  29.     static {
  30.         try {
  31.             Field field = Unsafe.class.getDeclaredField("theUnsafe");
  32.             field.setAccessible(true);
  33.             unsafe = (Unsafe) field.get(null);
  34.         } catch (Exception e) {
  35.             e.printStackTrace();
  36.         }
  37.     }
  38.  
  39.     public static long addressOf(Object o) throws Exception {
  40.         Object[] array = new Object[] { o };
  41.  
  42.         long baseOffset = unsafe.arrayBaseOffset(Object[].class);
  43.         int addressSize = unsafe.addressSize();
  44.         long objectAddress;
  45.         switch (addressSize) {
  46.         case 4:
  47.             objectAddress = unsafe.getInt(array, baseOffset);
  48.             break;
  49.         case 8:
  50.             objectAddress = unsafe.getLong(array, baseOffset);
  51.             break;
  52.         default:
  53.             throw new Error("unsupported address size: " + addressSize);
  54.         }
  55.  
  56.         return (objectAddress);
  57.     }
  58.  
  59.     public static void main(String... args) throws Exception {
  60.         Object mine = "Hi there".toCharArray();
  61.         long address = addressOf(mine);
  62.         System.out.println("Addess: " + address);
  63.  
  64.         // Verify address works - should see the characters in the array in the output
  65.         printBytes(address, 27);
  66.  
  67.     }
  68.  
  69.     public static void printBytes(long objectAddress, int num) {
  70.         for (long i = 0; i < num; i++) {
  71.             int cur = unsafe.getByte(objectAddress + i);
  72.             System.out.print((char) cur);
  73.         }
  74.         System.out.println();
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement