Search in sources :

Example 31 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project neo4j by neo4j.

the class ByteCodeVerifier method classNode.

private static ClassNode classNode(ByteBuffer bytecode) {
    byte[] bytes;
    if (bytecode.hasArray()) {
        bytes = bytecode.array();
    } else {
        bytes = new byte[bytecode.limit()];
        bytecode.get(bytes);
    }
    ClassNode classNode = new ClassNode();
    new ClassReader(bytes).accept(new CheckClassAdapter(classNode, false), SKIP_DEBUG);
    return classNode;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader)

Example 32 with ClassNode

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

the class ASMClassNodeAdapter method get.

public static ASMClassNodeAdapter get(final InstrumentContext pluginContext, final ClassLoader classLoader, final String classInternalName, final boolean skipCode) {
    if (pluginContext == null || classInternalName == null) {
        throw new IllegalArgumentException("plugin context or class name must not be null.");
    }
    InputStream in = null;
    try {
        in = pluginContext.getResourceAsStream(classLoader, classInternalName + ".class");
        if (in != null) {
            final ClassReader classReader = new ClassReader(in);
            final ClassNode classNode = new ClassNode();
            if (skipCode) {
                classReader.accept(classNode, ClassReader.SKIP_CODE);
            } else {
                classReader.accept(classNode, 0);
            }
            return new ASMClassNodeAdapter(pluginContext, classLoader, classNode, skipCode);
        }
    } catch (IOException ignored) {
    // not found class.
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
    return null;
}
Also used : InnerClassNode(org.objectweb.asm.tree.InnerClassNode) ClassNode(org.objectweb.asm.tree.ClassNode) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException)

Example 33 with ClassNode

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

the class ASMEngine method getClass.

@Override
public InstrumentClass getClass(InstrumentContext instrumentContext, ClassLoader classLoader, String className, byte[] classFileBuffer) throws NotFoundInstrumentException {
    if (className == null) {
        throw new NullPointerException("class name must not be null.");
    }
    try {
        if (classFileBuffer == null) {
            ASMClassNodeAdapter classNode = ASMClassNodeAdapter.get(instrumentContext, classLoader, JavaAssistUtils.javaNameToJvmName(className));
            if (classNode == null) {
                return null;
            }
            ApiMetaDataService apiMetaDataService = this.apiMetaDataService.get();
            return new ASMClass(objectBinderFactory, instrumentContext, interceptorRegistryBinder, apiMetaDataService, classLoader, classNode);
        }
        // Use ASM tree api.
        final ClassReader classReader = new ClassReader(classFileBuffer);
        final ClassNode classNode = new ClassNode();
        classReader.accept(classNode, 0);
        ApiMetaDataService apiMetaDataService = this.apiMetaDataService.get();
        return new ASMClass(objectBinderFactory, instrumentContext, interceptorRegistryBinder, apiMetaDataService, classLoader, classNode);
    } catch (Exception e) {
        throw new NotFoundInstrumentException(e);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException) ClassReader(org.objectweb.asm.ClassReader) ApiMetaDataService(com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException)

Example 34 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project Engine by VoltzEngine-Project.

the class ClassTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (EngineCoreMod.devMode && enableSuperDetails) {
        EngineCoreMod.logger.info("com.builtbroken.mc.core.asm.template.transform(name='" + name + "'  transformedName='" + transformedName + "',  bytes);");
    }
    try {
        //Only functions on our classes
        if (!shouldProcess(transformedName) || TemplateManager.templates.isEmpty()) {
            if (EngineCoreMod.devMode && enableSuperDetails) {
                EngineCoreMod.logger.info("\tClass is not in processing list so is ignored");
            }
            return bytes;
        }
        boolean changed = false;
        ClassNode cnode = ASMHelper.createClassNode(bytes);
        if (cnode != null && cnode.visibleAnnotations != null) {
            for (AnnotationNode nodes : cnode.visibleAnnotations) {
                if (nodes.desc.equals("Lcom/builtbroken/mc/api/InjectTemplate;")) {
                    if (EngineCoreMod.devMode && enableSuperDetails) {
                        EngineCoreMod.logger.info("\t Class contains annotation, injecting code");
                    }
                    /*
                         * The 2nd value in UniversalClass is the annotation we're looking for to filter
                         * out which mod to deal with.
                         */
                    String flags = null;
                    if (nodes.values != null && nodes.values.size() >= 2) {
                        flags = (String) nodes.values.get(1);
                    }
                    changed |= injectTemplate(cnode, flags);
                    break;
                }
            }
        }
        if (changed) {
            byte[] data = ASMHelper.createBytes(cnode, ClassWriter.COMPUTE_FRAMES);
            if (EngineCoreMod.devMode) {
                try {
                    File file = new File(System.getProperty("user.dir"), "asmTestFolder/" + name + ".class");
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(data);
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return data;
        }
    } catch (Exception e) {
        throw new RuntimeException("TemplateClassTransformer: Failed to process class " + transformedName, e);
    }
    return bytes;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 35 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project Engine by VoltzEngine-Project.

the class InjectionTemplate method init.

//Called late to avoid loading class data in the constructor
private boolean init() {
    if (!init) {
        init = true;
        try {
            final ClassNode cnode = ASMHelper.createClassNode(((LaunchClassLoader) InjectionTemplate.class.getClassLoader()).getClassBytes(className.replace('/', '.')));
            for (MethodNode method : cnode.methods) {
                this.methodImplementations.add(method);
                method.desc = new ObfMapping(cnode.name, method.name, method.desc).toRuntime().s_desc;
            }
        } catch (IOException e) {
            //TODO error out in dev mode
            //TODO make notation to users that this injector failed
            e.printStackTrace();
            failedToLoadClass = true;
        }
    }
    return failedToLoadClass;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ObfMapping(com.builtbroken.mc.lib.asm.ObfMapping) MethodNode(org.objectweb.asm.tree.MethodNode) IOException(java.io.IOException)

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