Search in sources :

Example 26 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project LogisticsPipes by RS485.

the class LogisticsPipesClassInjector method transform.

@Override
@SuppressWarnings("unchecked")
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes != null) {
        if (name.startsWith("logisticspipes.")) {
            final ClassReader reader = new ClassReader(bytes);
            final ClassNode node = new ClassNode();
            reader.accept(node, 0);
            if (node.visibleAnnotations != null) {
                for (AnnotationNode a : node.visibleAnnotations) {
                    if (a.desc.equals("Llogisticspipes/asm/ModVersionedClass;")) {
                        if (a.values.size() == 8 && a.values.get(0).equals("modId") && a.values.get(2).equals("version") && a.values.get(4).equals("classData") && a.values.get(6).equals("classDataDev")) {
                            String modId = a.values.get(1).toString();
                            String version = a.values.get(3).toString();
                            String classData = a.values.get(5).toString();
                            String classDataDev = a.values.get(7).toString();
                            if (ModStatusHelper.isModLoaded(modId) && !ModStatusHelper.isModVersionEqualsOrHigher(modId, version)) {
                                if (isObfEnv == null) {
                                    try {
                                        isObfEnv = (Class.forName("net.minecraft.world.World").getDeclaredField("chunkProvider") == null);
                                    } catch (Throwable e) {
                                        isObfEnv = true;
                                    }
                                }
                                bytes = transform(name, transformedName, DatatypeConverter.parseBase64Binary(isObfEnv ? classData : classDataDev));
                            }
                        } else {
                            throw new UnsupportedOperationException("Can't parse the annotations correctly");
                        }
                    }
                }
            }
        }
        return bytes;
    }
    try {
        if (name.startsWith("logisticspipes.proxy.opencomputers.asm.BaseWrapperClass$") && name.endsWith("$OpenComputersWrapper")) {
            String correctName = name.substring(56, name.length() - 21);
            Class<?> clazz = Launch.classLoader.findClass(correctName);
            bytes = ClassCreator.getWrappedClassAsBytes(CCObjectWrapper.getWrapperInformation(clazz), clazz.getName());
            Set<String> set = new TreeSet<>();
            set.add(name);
            Launch.classLoader.clearNegativeEntries(set);
            Map<String, byte[]> map = (Map<String, byte[]>) fResourceCache.get(Launch.classLoader);
            map.put(name, bytes);
            return bytes;
        }
    } catch (Exception e) {
        if (LPConstants.DEBUG) {
            // For better Debugging
            e.printStackTrace();
            return bytes;
        }
        throw new RuntimeException(e);
    }
    return bytes;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) TreeSet(java.util.TreeSet) ClassReader(org.objectweb.asm.ClassReader) Map(java.util.Map)

Example 27 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project pinpoint by naver.

the class ASMMethodNodeAdapterRenameTest method rename.

@Test
public void rename() throws Exception {
    final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.NormalClass";
    classLoader.setTrace(false);
    classLoader.setTargetClassName(targetClassName);
    classLoader.setCallbackHandler(new ASMClassNodeLoader.CallbackHandler() {

        @Override
        public void handle(ClassNode classNode) {
            List<MethodNode> methodNodes = classNode.methods;
            for (MethodNode methodNode : methodNodes) {
                ASMMethodNodeAdapter adapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
                if (!adapter.isConstructor()) {
                    adapter.rename(adapter.getName() + "_rename");
                }
            }
        }
    });
    Class<?> clazz = classLoader.loadClass(targetClassName);
    Method method = clazz.getDeclaredMethod("sum_rename", int.class);
    method.invoke(clazz.newInstance(), 10);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) List(java.util.List) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 28 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project pinpoint by naver.

the class ASMMethodNodeTest method addInterceptor.

@Test
public void addInterceptor() throws Exception {
    final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new ArgsArrayInterceptor());
    final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.NormalClass";
    final ASMClass declaringClass = mock(ASMClass.class);
    when(declaringClass.getName()).thenReturn(targetClassName);
    final ObjectBinderFactory objectBinderFactory = mock(ObjectBinderFactory.class);
    ASMClassNodeLoader.TestClassLoader classLoader = ASMClassNodeLoader.getClassLoader();
    classLoader.setTrace(false);
    classLoader.setVerify(false);
    classLoader.setTargetClassName(targetClassName);
    classLoader.setCallbackHandler(new ASMClassNodeLoader.CallbackHandler() {

        @Override
        public void handle(ClassNode classNode) {
            List<MethodNode> methodNodes = classNode.methods;
            for (MethodNode methodNode : methodNodes) {
                ASMMethod method = new ASMMethod(objectBinderFactory, pluginContext, apiMetaDataService, interceptorRegistryBinder, declaringClass, methodNode);
                try {
                    method.addInterceptor(interceptorId);
                } catch (InstrumentException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    Class<?> clazz = classLoader.loadClass(targetClassName);
    Method method = clazz.getDeclaredMethod("sum", int.class);
    assertEquals(55, method.invoke(clazz.newInstance(), 10));
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) ArgsArrayInterceptor(com.navercorp.pinpoint.profiler.instrument.mock.ArgsArrayInterceptor) ObjectBinderFactory(com.navercorp.pinpoint.profiler.objectfactory.ObjectBinderFactory) Method(java.lang.reflect.Method) MethodNode(org.objectweb.asm.tree.MethodNode) List(java.util.List) Test(org.junit.Test)

Example 29 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project pinpoint by naver.

the class ASMClassWriterTest method accept.

@Test
public void accept() throws Exception {
    final String className = "com.navercorp.pinpoint.profiler.instrument.mock.SampleClass";
    ClassNode classNode = ASMClassNodeLoader.get(JavaAssistUtils.javaNameToJvmName(className));
    ASMClassWriter cw = new ASMClassWriter(pluginContext, classNode.name, classNode.superName, 0, null);
    TraceClassVisitor tcv = new TraceClassVisitor(cw, new PrintWriter(System.out));
    classNode.accept(tcv);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 30 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project pinpoint by naver.

the class ASMFieldNodeAdapterTest method getName.

@Test
public void getName() throws Exception {
    ClassNode classNode = ASMClassNodeLoader.get("com.navercorp.pinpoint.profiler.instrument.mock.FieldClass");
    List<FieldNode> fieldNodes = classNode.fields;
    for (FieldNode fieldNode : fieldNodes) {
        ASMFieldNodeAdapter adapter = new ASMFieldNodeAdapter(fieldNode);
        assertNotNull(adapter.getName());
        assertNotNull(adapter.getClassName());
        assertNotNull(adapter.getDesc());
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) Test(org.junit.Test)

Aggregations

ClassNode (org.objectweb.asm.tree.ClassNode)117 ClassReader (org.objectweb.asm.ClassReader)58 MethodNode (org.objectweb.asm.tree.MethodNode)42 ClassWriter (org.objectweb.asm.ClassWriter)28 IOException (java.io.IOException)14 FieldNode (org.objectweb.asm.tree.FieldNode)12 ArrayList (java.util.ArrayList)11 List (java.util.List)11 Test (org.junit.Test)10 ZipEntry (java.util.zip.ZipEntry)8 Method (java.lang.reflect.Method)7 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)6 AnnotationNode (org.objectweb.asm.tree.AnnotationNode)6 InnerClassNode (org.objectweb.asm.tree.InnerClassNode)6 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)6 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 Label (org.objectweb.asm.Label)5