Home | History | Annotate | Download | only in dexmaker
      1 --- src/mockito/java/com/google/dexmaker/mockito/UnsafeAllocator.java
      2 +++ src/mockito/java/com/google/dexmaker/mockito/UnsafeAllocator.java
      3 @@ -92,6 +92,29 @@ abstract class UnsafeAllocator {
      4          } catch (Exception ignored) {
      5          }
      6  
      7 +        // try dalvikvm, with change https://android-review.googlesource.com/#/c/52331/
      8 +        // public class ObjectStreamClass {
      9 +        //   private static native long getConstructorId(Class<?> c);
     10 +        //   private static native Object newInstance(Class<?> instantiationClass, long methodId);
     11 +        // }
     12 +        try {
     13 +            Method getConstructorId = ObjectStreamClass.class
     14 +                    .getDeclaredMethod("getConstructorId", Class.class);
     15 +            getConstructorId.setAccessible(true);
     16 +            final long constructorId = (Long) getConstructorId.invoke(null, Object.class);
     17 +            final Method newInstance = ObjectStreamClass.class
     18 +                    .getDeclaredMethod("newInstance", Class.class, long.class);
     19 +            newInstance.setAccessible(true);
     20 +            return new UnsafeAllocator() {
     21 +                @Override
     22 +                @SuppressWarnings("unchecked")
     23 +                public <T> T newInstance(Class<T> c) throws Exception {
     24 +                    return (T) newInstance.invoke(null, c, constructorId);
     25 +                }
     26 +            };
     27 +        } catch (Exception ignored) {
     28 +        }
     29 +
     30          // give up
     31          return new UnsafeAllocator() {
     32              @Override
     33 @@ -100,4 +123,4 @@ abstract class UnsafeAllocator {
     34              }
     35          };
     36      }
     37 -}
     38 \ No newline at end of file
     39 +}
     40