Search in sources :

Example 1 with ClassWithNative

use of com.android.tools.layoutlib.create.dataclass.ClassWithNative in project android_frameworks_base by ParanoidAndroid.

the class DelegateClassAdapterTest method testNoOp.

/**
     * Tests that a class not being modified still works.
     */
@SuppressWarnings("unchecked")
@Test
public void testNoOp() throws Throwable {
    // create an instance of the class that will be modified
    // (load the class in a distinct class loader so that we can trash its definition later)
    ClassLoader cl1 = new ClassLoader(this.getClass().getClassLoader()) {
    };
    Class<ClassWithNative> clazz1 = (Class<ClassWithNative>) cl1.loadClass(NATIVE_CLASS_NAME);
    ClassWithNative instance1 = clazz1.newInstance();
    assertEquals(42, instance1.add(20, 22));
    try {
        instance1.callNativeInstance(10, 3.1415, new Object[0]);
        fail("Test should have failed to invoke callTheNativeMethod [1]");
    } catch (UnsatisfiedLinkError e) {
    // This is expected to fail since the native method is not implemented.
    }
    // Now process it but tell the delegate to not modify any method
    ClassWriter cw = new ClassWriter(0);
    HashSet<String> delegateMethods = new HashSet<String>();
    String internalClassName = NATIVE_CLASS_NAME.replace('.', '/');
    DelegateClassAdapter cv = new DelegateClassAdapter(mLog, cw, internalClassName, delegateMethods);
    ClassReader cr = new ClassReader(NATIVE_CLASS_NAME);
    cr.accept(cv, 0);
    // Load the generated class in a different class loader and try it again
    ClassLoader2 cl2 = null;
    try {
        cl2 = new ClassLoader2() {

            @Override
            public void testModifiedInstance() throws Exception {
                Class<?> clazz2 = loadClass(NATIVE_CLASS_NAME);
                Object i2 = clazz2.newInstance();
                assertNotNull(i2);
                assertEquals(42, callAdd(i2, 20, 22));
                try {
                    callCallNativeInstance(i2, 10, 3.1415, new Object[0]);
                    fail("Test should have failed to invoke callTheNativeMethod [2]");
                } catch (InvocationTargetException e) {
                    // This is expected to fail since the native method has NOT been
                    // overridden here.
                    assertEquals(UnsatisfiedLinkError.class, e.getCause().getClass());
                }
                // Check that the native method does NOT have the new annotation
                Method[] m = clazz2.getDeclaredMethods();
                assertEquals("native_instance", m[2].getName());
                assertTrue(Modifier.isNative(m[2].getModifiers()));
                Annotation[] a = m[2].getAnnotations();
                assertEquals(0, a.length);
            }
        };
        cl2.add(NATIVE_CLASS_NAME, cw);
        cl2.testModifiedInstance();
    } catch (Throwable t) {
        throw dumpGeneratedClass(t, cl2);
    }
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClassReader(org.objectweb.asm.ClassReader) OuterClass(com.android.tools.layoutlib.create.dataclass.OuterClass) InnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass) ClassWithNative(com.android.tools.layoutlib.create.dataclass.ClassWithNative) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with ClassWithNative

use of com.android.tools.layoutlib.create.dataclass.ClassWithNative in project android_frameworks_base by AOSPA.

the class DelegateClassAdapterTest method testNoOp.

/**
     * Tests that a class not being modified still works.
     */
@Test
public void testNoOp() throws Throwable {
    // create an instance of the class that will be modified
    // (load the class in a distinct class loader so that we can trash its definition later)
    ClassLoader cl1 = new ClassLoader(this.getClass().getClassLoader()) {
    };
    @SuppressWarnings("unchecked") Class<ClassWithNative> clazz1 = (Class<ClassWithNative>) cl1.loadClass(NATIVE_CLASS_NAME);
    ClassWithNative instance1 = clazz1.newInstance();
    assertEquals(42, instance1.add(20, 22));
    try {
        instance1.callNativeInstance(10, 3.1415, new Object[0]);
        fail("Test should have failed to invoke callTheNativeMethod [1]");
    } catch (UnsatisfiedLinkError e) {
    // This is expected to fail since the native method is not implemented.
    }
    // Now process it but tell the delegate to not modify any method
    ClassWriter cw = new ClassWriter(0);
    HashSet<String> delegateMethods = new HashSet<>();
    String internalClassName = NATIVE_CLASS_NAME.replace('.', '/');
    DelegateClassAdapter cv = new DelegateClassAdapter(mLog, cw, internalClassName, delegateMethods);
    ClassReader cr = new ClassReader(NATIVE_CLASS_NAME);
    cr.accept(cv, 0);
    // Load the generated class in a different class loader and try it again
    ClassLoader2 cl2 = null;
    try {
        cl2 = new ClassLoader2() {

            @Override
            public void testModifiedInstance() throws Exception {
                Class<?> clazz2 = loadClass(NATIVE_CLASS_NAME);
                Object i2 = clazz2.newInstance();
                assertNotNull(i2);
                assertEquals(42, callAdd(i2, 20, 22));
                try {
                    callCallNativeInstance(i2, 10, 3.1415, new Object[0]);
                    fail("Test should have failed to invoke callTheNativeMethod [2]");
                } catch (InvocationTargetException e) {
                    // This is expected to fail since the native method has NOT been
                    // overridden here.
                    assertEquals(UnsatisfiedLinkError.class, e.getCause().getClass());
                }
                // Check that the native method does NOT have the new annotation
                Method[] m = clazz2.getDeclaredMethods();
                Method nativeInstanceMethod = null;
                for (Method method : m) {
                    if ("native_instance".equals(method.getName())) {
                        nativeInstanceMethod = method;
                        break;
                    }
                }
                assertNotNull(nativeInstanceMethod);
                assertTrue(Modifier.isNative(nativeInstanceMethod.getModifiers()));
                Annotation[] a = nativeInstanceMethod.getAnnotations();
                assertEquals(0, a.length);
            }
        };
        cl2.add(NATIVE_CLASS_NAME, cw);
        cl2.testModifiedInstance();
    } catch (Throwable t) {
        throw dumpGeneratedClass(t, cl2);
    }
}
Also used : Method(java.lang.reflect.Method) ClassWriter(org.objectweb.asm.ClassWriter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClassReader(org.objectweb.asm.ClassReader) OuterClass(com.android.tools.layoutlib.create.dataclass.OuterClass) StaticInnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.StaticInnerClass) InnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass) ClassWithNative(com.android.tools.layoutlib.create.dataclass.ClassWithNative) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ClassWithNative

use of com.android.tools.layoutlib.create.dataclass.ClassWithNative in project platform_frameworks_base by android.

the class DelegateClassAdapterTest method testNoOp.

/**
     * Tests that a class not being modified still works.
     */
@Test
public void testNoOp() throws Throwable {
    // create an instance of the class that will be modified
    // (load the class in a distinct class loader so that we can trash its definition later)
    ClassLoader cl1 = new ClassLoader(this.getClass().getClassLoader()) {
    };
    @SuppressWarnings("unchecked") Class<ClassWithNative> clazz1 = (Class<ClassWithNative>) cl1.loadClass(NATIVE_CLASS_NAME);
    ClassWithNative instance1 = clazz1.newInstance();
    assertEquals(42, instance1.add(20, 22));
    try {
        instance1.callNativeInstance(10, 3.1415, new Object[0]);
        fail("Test should have failed to invoke callTheNativeMethod [1]");
    } catch (UnsatisfiedLinkError e) {
    // This is expected to fail since the native method is not implemented.
    }
    // Now process it but tell the delegate to not modify any method
    ClassWriter cw = new ClassWriter(0);
    HashSet<String> delegateMethods = new HashSet<>();
    String internalClassName = NATIVE_CLASS_NAME.replace('.', '/');
    DelegateClassAdapter cv = new DelegateClassAdapter(mLog, cw, internalClassName, delegateMethods);
    ClassReader cr = new ClassReader(NATIVE_CLASS_NAME);
    cr.accept(cv, 0);
    // Load the generated class in a different class loader and try it again
    ClassLoader2 cl2 = null;
    try {
        cl2 = new ClassLoader2() {

            @Override
            public void testModifiedInstance() throws Exception {
                Class<?> clazz2 = loadClass(NATIVE_CLASS_NAME);
                Object i2 = clazz2.newInstance();
                assertNotNull(i2);
                assertEquals(42, callAdd(i2, 20, 22));
                try {
                    callCallNativeInstance(i2, 10, 3.1415, new Object[0]);
                    fail("Test should have failed to invoke callTheNativeMethod [2]");
                } catch (InvocationTargetException e) {
                    // This is expected to fail since the native method has NOT been
                    // overridden here.
                    assertEquals(UnsatisfiedLinkError.class, e.getCause().getClass());
                }
                // Check that the native method does NOT have the new annotation
                Method[] m = clazz2.getDeclaredMethods();
                Method nativeInstanceMethod = null;
                for (Method method : m) {
                    if ("native_instance".equals(method.getName())) {
                        nativeInstanceMethod = method;
                        break;
                    }
                }
                assertNotNull(nativeInstanceMethod);
                assertTrue(Modifier.isNative(nativeInstanceMethod.getModifiers()));
                Annotation[] a = nativeInstanceMethod.getAnnotations();
                assertEquals(0, a.length);
            }
        };
        cl2.add(NATIVE_CLASS_NAME, cw);
        cl2.testModifiedInstance();
    } catch (Throwable t) {
        throw dumpGeneratedClass(t, cl2);
    }
}
Also used : Method(java.lang.reflect.Method) ClassWriter(org.objectweb.asm.ClassWriter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClassReader(org.objectweb.asm.ClassReader) OuterClass(com.android.tools.layoutlib.create.dataclass.OuterClass) StaticInnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.StaticInnerClass) InnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass) ClassWithNative(com.android.tools.layoutlib.create.dataclass.ClassWithNative) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ClassWithNative

use of com.android.tools.layoutlib.create.dataclass.ClassWithNative in project android_frameworks_base by DirtyUnicorns.

the class DelegateClassAdapterTest method testNoOp.

/**
     * Tests that a class not being modified still works.
     */
@Test
public void testNoOp() throws Throwable {
    // create an instance of the class that will be modified
    // (load the class in a distinct class loader so that we can trash its definition later)
    ClassLoader cl1 = new ClassLoader(this.getClass().getClassLoader()) {
    };
    @SuppressWarnings("unchecked") Class<ClassWithNative> clazz1 = (Class<ClassWithNative>) cl1.loadClass(NATIVE_CLASS_NAME);
    ClassWithNative instance1 = clazz1.newInstance();
    assertEquals(42, instance1.add(20, 22));
    try {
        instance1.callNativeInstance(10, 3.1415, new Object[0]);
        fail("Test should have failed to invoke callTheNativeMethod [1]");
    } catch (UnsatisfiedLinkError e) {
    // This is expected to fail since the native method is not implemented.
    }
    // Now process it but tell the delegate to not modify any method
    ClassWriter cw = new ClassWriter(0);
    HashSet<String> delegateMethods = new HashSet<>();
    String internalClassName = NATIVE_CLASS_NAME.replace('.', '/');
    DelegateClassAdapter cv = new DelegateClassAdapter(mLog, cw, internalClassName, delegateMethods);
    ClassReader cr = new ClassReader(NATIVE_CLASS_NAME);
    cr.accept(cv, 0);
    // Load the generated class in a different class loader and try it again
    ClassLoader2 cl2 = null;
    try {
        cl2 = new ClassLoader2() {

            @Override
            public void testModifiedInstance() throws Exception {
                Class<?> clazz2 = loadClass(NATIVE_CLASS_NAME);
                Object i2 = clazz2.newInstance();
                assertNotNull(i2);
                assertEquals(42, callAdd(i2, 20, 22));
                try {
                    callCallNativeInstance(i2, 10, 3.1415, new Object[0]);
                    fail("Test should have failed to invoke callTheNativeMethod [2]");
                } catch (InvocationTargetException e) {
                    // This is expected to fail since the native method has NOT been
                    // overridden here.
                    assertEquals(UnsatisfiedLinkError.class, e.getCause().getClass());
                }
                // Check that the native method does NOT have the new annotation
                Method[] m = clazz2.getDeclaredMethods();
                Method nativeInstanceMethod = null;
                for (Method method : m) {
                    if ("native_instance".equals(method.getName())) {
                        nativeInstanceMethod = method;
                        break;
                    }
                }
                assertNotNull(nativeInstanceMethod);
                assertTrue(Modifier.isNative(nativeInstanceMethod.getModifiers()));
                Annotation[] a = nativeInstanceMethod.getAnnotations();
                assertEquals(0, a.length);
            }
        };
        cl2.add(NATIVE_CLASS_NAME, cw);
        cl2.testModifiedInstance();
    } catch (Throwable t) {
        throw dumpGeneratedClass(t, cl2);
    }
}
Also used : Method(java.lang.reflect.Method) ClassWriter(org.objectweb.asm.ClassWriter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClassReader(org.objectweb.asm.ClassReader) OuterClass(com.android.tools.layoutlib.create.dataclass.OuterClass) StaticInnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.StaticInnerClass) InnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass) ClassWithNative(com.android.tools.layoutlib.create.dataclass.ClassWithNative) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with ClassWithNative

use of com.android.tools.layoutlib.create.dataclass.ClassWithNative in project android_frameworks_base by ResurrectionRemix.

the class DelegateClassAdapterTest method testNoOp.

/**
     * Tests that a class not being modified still works.
     */
@Test
public void testNoOp() throws Throwable {
    // create an instance of the class that will be modified
    // (load the class in a distinct class loader so that we can trash its definition later)
    ClassLoader cl1 = new ClassLoader(this.getClass().getClassLoader()) {
    };
    @SuppressWarnings("unchecked") Class<ClassWithNative> clazz1 = (Class<ClassWithNative>) cl1.loadClass(NATIVE_CLASS_NAME);
    ClassWithNative instance1 = clazz1.newInstance();
    assertEquals(42, instance1.add(20, 22));
    try {
        instance1.callNativeInstance(10, 3.1415, new Object[0]);
        fail("Test should have failed to invoke callTheNativeMethod [1]");
    } catch (UnsatisfiedLinkError e) {
    // This is expected to fail since the native method is not implemented.
    }
    // Now process it but tell the delegate to not modify any method
    ClassWriter cw = new ClassWriter(0);
    HashSet<String> delegateMethods = new HashSet<>();
    String internalClassName = NATIVE_CLASS_NAME.replace('.', '/');
    DelegateClassAdapter cv = new DelegateClassAdapter(mLog, cw, internalClassName, delegateMethods);
    ClassReader cr = new ClassReader(NATIVE_CLASS_NAME);
    cr.accept(cv, 0);
    // Load the generated class in a different class loader and try it again
    ClassLoader2 cl2 = null;
    try {
        cl2 = new ClassLoader2() {

            @Override
            public void testModifiedInstance() throws Exception {
                Class<?> clazz2 = loadClass(NATIVE_CLASS_NAME);
                Object i2 = clazz2.newInstance();
                assertNotNull(i2);
                assertEquals(42, callAdd(i2, 20, 22));
                try {
                    callCallNativeInstance(i2, 10, 3.1415, new Object[0]);
                    fail("Test should have failed to invoke callTheNativeMethod [2]");
                } catch (InvocationTargetException e) {
                    // This is expected to fail since the native method has NOT been
                    // overridden here.
                    assertEquals(UnsatisfiedLinkError.class, e.getCause().getClass());
                }
                // Check that the native method does NOT have the new annotation
                Method[] m = clazz2.getDeclaredMethods();
                Method nativeInstanceMethod = null;
                for (Method method : m) {
                    if ("native_instance".equals(method.getName())) {
                        nativeInstanceMethod = method;
                        break;
                    }
                }
                assertNotNull(nativeInstanceMethod);
                assertTrue(Modifier.isNative(nativeInstanceMethod.getModifiers()));
                Annotation[] a = nativeInstanceMethod.getAnnotations();
                assertEquals(0, a.length);
            }
        };
        cl2.add(NATIVE_CLASS_NAME, cw);
        cl2.testModifiedInstance();
    } catch (Throwable t) {
        throw dumpGeneratedClass(t, cl2);
    }
}
Also used : Method(java.lang.reflect.Method) ClassWriter(org.objectweb.asm.ClassWriter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClassReader(org.objectweb.asm.ClassReader) OuterClass(com.android.tools.layoutlib.create.dataclass.OuterClass) StaticInnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.StaticInnerClass) InnerClass(com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass) ClassWithNative(com.android.tools.layoutlib.create.dataclass.ClassWithNative) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ClassWithNative (com.android.tools.layoutlib.create.dataclass.ClassWithNative)6 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)6 InnerClass (com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass)6 IOException (java.io.IOException)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 HashSet (java.util.HashSet)6 Test (org.junit.Test)6 ClassReader (org.objectweb.asm.ClassReader)6 ClassWriter (org.objectweb.asm.ClassWriter)6 StaticInnerClass (com.android.tools.layoutlib.create.dataclass.OuterClass.StaticInnerClass)5 Method (java.lang.reflect.Method)5