title: In Android, how to invoke sun.misc.Unsafe methods using Java reflection?
body: <p>Although there are similar questions (such as <a href="https://stackoverflow.com/questions/8843953/how-to-invoke-method-with-variable-arguments-in-java-using-reflection">A</a>, <a href="https://stackoverflow.com/questions/20925962/how-to-invoke-a-method-using-reflection">B</a> and <a href="https://stackoverflow.com/questions/18274891/invoking-reflection-apis-correctly-android">C</a>), their answers do not solve my problem.</p>
  
  <p>I am using Android Studio 1.5.1 targeting Android API 18 (before Android KitKat 4.4, so I’m dealing with Dalvik, not ART runtime).</p>
  
  <p><strong>My questions are:</strong></p>
  
  <p>(1) When I use the following code, I can print a list of all <a href="https://android.googlesource.com/platform/libcore/+/9edf43dfcc35c761d97eb9156ac4254152ddbc55/libdvm/src/main/java/sun/misc/Unsafe.java" rel="nofollow noreferrer">sun.misc.Unsafe methods available in Android</a>, so I think I have access to them using <strong>reflection</strong> but I do not know how to call them using <strong>reflection</strong>.</p>
  
  <p>(2) If (1) is possible, how to find the magicNumber (in the code below) address using sun.misc.Unsafe methods in Android via reflection?</p>
  
  <p>(3) If (1) is possible but (2) is not possible, how to put an integer number (say int test=123) in any native memory address and print its memory address using sun.misc.Unsafe methods in Android via reflection? </p>
  
  <pre><code>        String ClassName = "sun.misc.Unsafe";
          int magicNumber = 0x23420023 ;
      try {
          Class classToInvestigate = Class.forName(ClassName);
          Constructor[] aClassConstructors = classToInvestigate.getDeclaredConstructors();
          for(Constructor c : aClassConstructors){
              System.out.println("********************* constructor="+c);
          }
  
          Method[] aClassMethods = classToInvestigate.getDeclaredMethods();
          for(Method m : aClassMethods){
              System.out.println("********************* method="+m);
  
          }
          Field theUnsafe = classToInvestigate.getDeclaredField("THE_ONE");
          theUnsafe.setAccessible(true);
          Object unsafe = theUnsafe.get(null);
  
      } catch (ClassNotFoundException e) {
          // Class not found!
      }
      catch (Exception e) {
          // Unknown exception
      }
  </code></pre>