Search in sources :

Example 91 with ClassReader

use of org.objectweb.asm.ClassReader 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 92 with ClassReader

use of org.objectweb.asm.ClassReader in project pinpoint by naver.

the class ASMClassWriter method get.

private String get(final String type1ClassInternalName, final String type2ClassInternalName) {
    if (type1ClassInternalName == null || type1ClassInternalName.equals(OBJECT_CLASS_INTERNAL_NAME) || type2ClassInternalName == null || type2ClassInternalName.equals(OBJECT_CLASS_INTERNAL_NAME)) {
        // object is the root of the class hierarchy.
        return OBJECT_CLASS_INTERNAL_NAME;
    }
    if (type1ClassInternalName.equals(type2ClassInternalName)) {
        // two equal.
        return type1ClassInternalName;
    }
    // current class.
    if (type1ClassInternalName.equals(classInternalName)) {
        return getCommonSuperClass(superClassInternalName, type2ClassInternalName);
    } else if (type2ClassInternalName.equals(classInternalName)) {
        return getCommonSuperClass(type1ClassInternalName, superClassInternalName);
    }
    ClassReader type1ClassReader = getClassReader(type1ClassInternalName);
    ClassReader type2ClassReader = getClassReader(type2ClassInternalName);
    if (type1ClassReader == null || type2ClassReader == null) {
        logger.warn("Skip get common super class. not found class {type1ClassInternalName={}, reader={}}, {type2ClassInternalName={}, reader={}}", type1ClassInternalName, type1ClassReader, type2ClassInternalName, type2ClassReader);
        return OBJECT_CLASS_INTERNAL_NAME;
    }
    // interface.
    if (isInterface(type1ClassReader)) {
        String interfaceInternalName = type1ClassInternalName;
        if (isImplements(interfaceInternalName, type2ClassReader)) {
            return interfaceInternalName;
        }
        if (isInterface(type2ClassReader)) {
            interfaceInternalName = type2ClassInternalName;
            if (isImplements(interfaceInternalName, type1ClassReader)) {
                return interfaceInternalName;
            }
        }
        return OBJECT_CLASS_INTERNAL_NAME;
    }
    // interface.
    if (isInterface(type2ClassReader)) {
        String interfaceName = type2ClassInternalName;
        if (isImplements(interfaceName, type1ClassReader)) {
            return interfaceName;
        }
        return OBJECT_CLASS_INTERNAL_NAME;
    }
    // class.
    final Set<String> superClassNames = new HashSet<String>();
    superClassNames.add(type1ClassInternalName);
    superClassNames.add(type2ClassInternalName);
    String type1SuperClassName = type1ClassReader.getSuperName();
    if (!superClassNames.add(type1SuperClassName)) {
        // find common superClass.
        return type1SuperClassName;
    }
    String type2SuperClassName = type2ClassReader.getSuperName();
    if (!superClassNames.add(type2SuperClassName)) {
        // find common superClass.
        return type2SuperClassName;
    }
    while (type1SuperClassName != null || type2SuperClassName != null) {
        if (type1SuperClassName != null) {
            type1SuperClassName = getSuperClassInternalName(type1SuperClassName);
            if (type1SuperClassName != null) {
                if (!superClassNames.add(type1SuperClassName)) {
                    return type1SuperClassName;
                }
            }
        }
        if (type2SuperClassName != null) {
            type2SuperClassName = getSuperClassInternalName(type2SuperClassName);
            if (type2SuperClassName != null) {
                if (!superClassNames.add(type2SuperClassName)) {
                    return type2SuperClassName;
                }
            }
        }
    }
    return OBJECT_CLASS_INTERNAL_NAME;
}
Also used : ClassReader(org.objectweb.asm.ClassReader) HashSet(java.util.HashSet)

Example 93 with ClassReader

use of org.objectweb.asm.ClassReader 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 94 with ClassReader

use of org.objectweb.asm.ClassReader in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class ValkyrienWarfareTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] classData) {
    classData = transformer.transform(classData);
    TransformAdapter adapter = new TransformAdapter(ASM5, ValkyrienWarfarePlugin.isObfuscatedEnvironment, transformedName);
    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    adapter.setCV(classWriter);
    try {
        new ClassReader(classData).accept(adapter, ClassReader.EXPAND_FRAMES);
        classData = classWriter.toByteArray();
    //Performs sanity checks and frame stack recalculations before pushing the new bytecode
    //			ClassReader cr = new ClassReader(byteArray);
    //			ClassWriter checkedWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    //			CheckClassAdapter adapterChecker = new CheckClassAdapter(checkedWriter, true);
    //			cr.accept(adapterChecker, ClassReader.EXPAND_FRAMES);
    //			return checkedWriter.toByteArray();
    } catch (Exception e) {
        //			System.out.println(transformedName);
        e.printStackTrace();
    }
    return classData;
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 95 with ClassReader

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

the class ASMHelper method createClassNode.

public static ClassNode createClassNode(byte[] bytes, int flags) {
    ClassNode cnode = new ClassNode();
    ClassReader reader = new ClassReader(bytes);
    reader.accept(cnode, flags);
    return cnode;
}
Also used : ClassReader(org.objectweb.asm.ClassReader)

Aggregations

ClassReader (org.objectweb.asm.ClassReader)449 ClassWriter (org.objectweb.asm.ClassWriter)187 Test (org.junit.Test)134 IOException (java.io.IOException)78 InputStream (java.io.InputStream)76 TreeMap (java.util.TreeMap)59 ClassNode (org.objectweb.asm.tree.ClassNode)58 ClassVisitor (org.objectweb.asm.ClassVisitor)54 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)53 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