Search in sources :

Example 21 with ClassReader

use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.

the class AsmAnalyzerTest method testFindGlobs.

@Test
public void testFindGlobs() throws IOException, LogAbortException {
    Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
    TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();
    // this matches classes, a package match returns nothing
    found.clear();
    mAa.findGlobs("mock_android.view", zipClasses, found);
    assertArrayEquals(new String[] {}, found.keySet().toArray());
    // a complex glob search. * is a search pattern that matches names, not dots
    mAa.findGlobs("mock_android.*.*Group$*Layout*", zipClasses, found);
    assertArrayEquals(new String[] { "mock_android.view.ViewGroup$LayoutParams", "mock_android.view.ViewGroup$MarginLayoutParams" }, found.keySet().toArray());
    // a complex glob search. ** is a search pattern that matches names including dots
    mAa.findGlobs("mock_android.**Group*", zipClasses, found);
    assertArrayEquals(new String[] { "mock_android.view.ViewGroup", "mock_android.view.ViewGroup$LayoutParams", "mock_android.view.ViewGroup$MarginLayoutParams" }, found.keySet().toArray());
    // matches a single class
    found.clear();
    mAa.findGlobs("mock_android.view.View", zipClasses, found);
    assertArrayEquals(new String[] { "mock_android.view.View" }, found.keySet().toArray());
    // matches everyting inside the given package but not sub-packages
    found.clear();
    mAa.findGlobs("mock_android.view.*", zipClasses, found);
    assertArrayEquals(new String[] { "mock_android.view.View", "mock_android.view.ViewGroup", "mock_android.view.ViewGroup$LayoutParams", "mock_android.view.ViewGroup$MarginLayoutParams" }, found.keySet().toArray());
    for (String key : found.keySet()) {
        ClassReader value = found.get(key);
        assertNotNull("No value for " + key, value);
        assertEquals(key, AsmAnalyzer.classReaderToClassName(value));
    }
}
Also used : ClassReader(org.objectweb.asm.ClassReader) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 22 with ClassReader

use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.

the class AsmAnalyzerTest method testFindClass.

@Test
public void testFindClass() throws IOException, LogAbortException {
    Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
    TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();
    ClassReader cr = mAa.findClass("mock_android.view.ViewGroup$LayoutParams", zipClasses, found);
    assertNotNull(cr);
    assertEquals("mock_android/view/ViewGroup$LayoutParams", cr.getClassName());
    assertArrayEquals(new String[] { "mock_android.view.ViewGroup$LayoutParams" }, found.keySet().toArray());
    assertArrayEquals(new ClassReader[] { cr }, found.values().toArray());
}
Also used : ClassReader(org.objectweb.asm.ClassReader) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 23 with ClassReader

use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.

the class AsmAnalyzerTest method testFindClassesDerivingFrom.

@Test
public void testFindClassesDerivingFrom() throws LogAbortException, IOException {
    Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
    TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();
    mAa.findClassesDerivingFrom("mock_android.view.View", zipClasses, found);
    assertArrayEquals(new String[] { "mock_android.view.View", "mock_android.view.ViewGroup", "mock_android.widget.LinearLayout", "mock_android.widget.TableLayout" }, found.keySet().toArray());
    for (String key : found.keySet()) {
        ClassReader value = found.get(key);
        assertNotNull("No value for " + key, value);
        assertEquals(key, AsmAnalyzer.classReaderToClassName(value));
    }
}
Also used : ClassReader(org.objectweb.asm.ClassReader) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 24 with ClassReader

use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.

the class ClassHasNativeVisitorTest method testHasNative.

@Test
public void testHasNative() throws IOException {
    MockClassHasNativeVisitor cv = new MockClassHasNativeVisitor();
    String className = this.getClass().getCanonicalName() + "$" + ClassWithNative.class.getSimpleName();
    ClassReader cr = new ClassReader(className);
    cr.accept(cv, 0);
    assertArrayEquals(new String[] { "native_method" }, cv.getMethodsFound());
    assertTrue(cv.hasNativeMethods());
}
Also used : ClassReader(org.objectweb.asm.ClassReader) Test(org.junit.Test)

Example 25 with ClassReader

use of org.objectweb.asm.ClassReader in project LogisticsPipes by RS485.

the class LogisticsWrapperHandler method getWrappedProxy.

@SuppressWarnings("unchecked")
public static <T> T getWrappedProxy(String modId, Class<T> interfaze, Class<? extends T> proxyClazz, T dummyProxy, Class<?>... wrapperInterfaces) throws NoSuchFieldException, SecurityException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
    String proxyName = interfaze.getSimpleName().substring(1);
    if (!proxyName.endsWith("Proxy")) {
        throw new RuntimeException("UnuportedProxyName: " + proxyName);
    }
    proxyName = proxyName.substring(0, proxyName.length() - 5);
    String className = "logisticspipes/asm/wrapper/generated/" + proxyName + "ProxyWrapper";
    boolean ignoreModLoaded = false;
    if (modId.startsWith("!")) {
        ignoreModLoaded = true;
        modId = modId.substring(1);
    }
    List<Class<?>> wrapperInterfacesList = Arrays.asList(wrapperInterfaces);
    Class<?> clazz = null;
    synchronized (lookupMap) {
        clazz = LogisticsWrapperHandler.lookupMap.get(className);
        if (clazz == null) {
            String fieldName = interfaze.getName().replace('.', '/');
            //String classFile = interfaze.getSimpleName().substring(1) + "Wrapper.java";
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, className, null, "logisticspipes/asm/wrapper/AbstractWrapper", new String[] { fieldName });
            cw.visitSource(".LP|ASM.dynamic", null);
            {
                FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "proxy", "L" + fieldName + ";", null, null);
                fv.visitEnd();
            }
            {
                FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "dummyProxy", "L" + fieldName + ";", null, null);
                fv.visitEnd();
            }
            {
                MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(L" + fieldName + ";L" + fieldName + ";)V", null, null);
                mv.visitCode();
                Label l0 = new Label();
                mv.visitLabel(l0);
                mv.visitLineNumber(11, l0);
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "logisticspipes/asm/wrapper/AbstractWrapper", "<init>", "()V");
                Label l1 = new Label();
                mv.visitLabel(l1);
                mv.visitLineNumber(12, l1);
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                mv.visitVarInsn(Opcodes.ALOAD, 1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, className, "dummyProxy", "L" + fieldName + ";");
                Label l2 = new Label();
                mv.visitLabel(l2);
                mv.visitLineNumber(13, l2);
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                mv.visitVarInsn(Opcodes.ALOAD, 2);
                mv.visitFieldInsn(Opcodes.PUTFIELD, className, "proxy", "L" + fieldName + ";");
                Label l3 = new Label();
                mv.visitLabel(l3);
                mv.visitLineNumber(14, l3);
                mv.visitInsn(Opcodes.RETURN);
                Label l4 = new Label();
                mv.visitLabel(l4);
                mv.visitLocalVariable("this", "L" + className + ";", null, l0, l4, 0);
                mv.visitLocalVariable("dProxy", "L" + fieldName + ";", null, l0, l4, 1);
                mv.visitLocalVariable("iProxy", "L" + fieldName + ";", null, l0, l4, 2);
                mv.visitMaxs(2, 3);
                mv.visitEnd();
            }
            int lineAddition = 100;
            for (Method method : interfaze.getMethods()) {
                LogisticsWrapperHandler.addProxyMethod(cw, method, fieldName, className, lineAddition, !wrapperInterfacesList.contains(method.getReturnType()));
                lineAddition += 10;
            }
            LogisticsWrapperHandler.addGetName(cw, className, proxyName);
            LogisticsWrapperHandler.addGetTypeName(cw, className, "Proxy");
            cw.visitEnd();
            String lookfor = className.replace('/', '.');
            byte[] bytes = cw.toByteArray();
            if (LPConstants.DEBUG) {
                if (LogisticsWrapperHandler.DUMP) {
                    LogisticsWrapperHandler.saveGeneratedClass(bytes, lookfor, "LP_WRAPPER_CLASSES");
                }
                ClassReader cr = new ClassReader(bytes);
                org.objectweb.asm.util.CheckClassAdapter.verify(cr, Launch.classLoader, false, new PrintWriter(System.err));
            }
            try {
                clazz = LogisticsWrapperHandler.loadClass(bytes, lookfor);
            } catch (LinkageError e) {
                try {
                    if (e.getMessage().contains("attempted") && e.getMessage().contains("duplicate class definition")) {
                        Class<?> prev = Class.forName(className);
                        System.err.println(e.getMessage());
                        System.err.println("Already loaded: " + String.valueOf(prev));
                        String resourcePath = className.replace('.', '/').concat(".class");
                        URL classResource = Launch.classLoader.findResource(resourcePath);
                        if (classResource != null) {
                            String path = classResource.getPath().toString();
                            System.err.println("Class source: " + path);
                        } else {
                            System.err.println("Class source: Null");
                        }
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                throw e;
            }
            LogisticsWrapperHandler.lookupMap.put(className, clazz);
        }
    }
    T proxy = null;
    Throwable e = null;
    if (ModStatusHelper.areModsLoaded(modId) || ignoreModLoaded) {
        try {
            proxy = proxyClazz.newInstance();
        } catch (Exception e1) {
            if (e1 instanceof VersionNotSupportedException) {
                throw (VersionNotSupportedException) e1;
            }
            if (!(e1 instanceof DontLoadProxy)) {
                e1.printStackTrace();
                e = e1;
            }
        } catch (NoClassDefFoundError e1) {
            if (!ignoreModLoaded) {
                e1.printStackTrace();
                e = e1;
            }
        }
    }
    T instance = (T) clazz.getConstructor(new Class<?>[] { interfaze, interfaze }).newInstance(dummyProxy, proxy);
    if (proxy != null) {
        LogisticsPipes.log.info("Loaded " + proxyName + "Proxy");
    } else {
        LogisticsPipes.log.info("Loaded " + proxyName + " DummyProxy");
        if (e != null) {
            ((AbstractWrapper) instance).setState(WrapperState.Exception);
            ((AbstractWrapper) instance).setReason(e);
        } else {
            ((AbstractWrapper) instance).setState(WrapperState.ModMissing);
        }
    }
    ((AbstractWrapper) instance).setModId(modId);
    ((AbstractWrapper) instance).setWrapperInterfaces(Collections.unmodifiableList(wrapperInterfacesList));
    LogisticsWrapperHandler.wrapperController.add((AbstractWrapper) instance);
    return instance;
}
Also used : DontLoadProxy(logisticspipes.proxy.DontLoadProxy) Label(org.objectweb.asm.Label) Method(java.lang.reflect.Method) FieldVisitor(org.objectweb.asm.FieldVisitor) VersionNotSupportedException(logisticspipes.proxy.VersionNotSupportedException) ClassWriter(org.objectweb.asm.ClassWriter) URL(java.net.URL) VersionNotSupportedException(logisticspipes.proxy.VersionNotSupportedException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodVisitor(org.objectweb.asm.MethodVisitor) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Aggregations

ClassReader (org.objectweb.asm.ClassReader)437 ClassWriter (org.objectweb.asm.ClassWriter)182 Test (org.junit.Test)134 IOException (java.io.IOException)77 InputStream (java.io.InputStream)75 TreeMap (java.util.TreeMap)59 ClassNode (org.objectweb.asm.tree.ClassNode)58 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)53 ClassVisitor (org.objectweb.asm.ClassVisitor)48 HashSet (java.util.HashSet)39 ZipEntry (java.util.zip.ZipEntry)34 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 ZipFile (java.util.zip.ZipFile)29 InvocationTargetException (java.lang.reflect.InvocationTargetException)26 Method (java.lang.reflect.Method)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23 InnerClass (com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass)23 PrintWriter (java.io.PrintWriter)23 MethodVisitor (org.objectweb.asm.MethodVisitor)23 MethodNode (org.objectweb.asm.tree.MethodNode)21