Search in sources :

Example 86 with ClassReader

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

the class ParamProfiler method handleClass.

public static byte[] handleClass(byte[] bytes) {
    if (!isActive)
        return bytes;
    final ClassReader reader = new ClassReader(bytes);
    final ClassNode node = new ClassNode();
    reader.accept(node, 0);
    final String className = node.name;
    for (final MethodNode m : node.methods) {
        final String methodName = m.name;
        final String methodDesc = m.desc;
        final boolean isConst = methodName.contains("<") || methodName.contains(">");
        if (isConst)
            continue;
        final long methodId = minMethodId++;
        final List<String> varList = new ArrayList<>();
        if (!methodDesc.startsWith("("))
            throw new UnsupportedOperationException(methodDesc);
        outer: for (int i = 1; i < methodDesc.length(); i++) {
            switch(methodDesc.charAt(i)) {
                case ')':
                    break outer;
                case 'L':
                    int startA = i;
                    while (methodDesc.charAt(i) != ';') i++;
                    varList.add(methodDesc.substring(startA, i + 1));
                    break;
                case '[':
                    int startB = i;
                    while (methodDesc.charAt(i) == '[') i++;
                    if (methodDesc.charAt(i) == 'L') {
                        while (methodDesc.charAt(i) != ';') i++;
                    }
                    varList.add(methodDesc.substring(startB, i + 1));
                    break;
                default:
                    varList.add(String.valueOf(methodDesc.charAt(i)));
            }
        }
        final List<Label> catchStatement = new ArrayList<>();
        MethodNode mv = new MethodNode(Opcodes.ASM4, m.access, m.name, m.desc, m.signature, m.exceptions.toArray(new String[0])) {

            @Override
            public void visitCode() {
                super.visitCode();
                Label l0 = new Label();
                visitLabel(l0);
                visitLdcInsn(new Long(methodId));
                visitLdcInsn(className + "+" + methodName + "+" + methodDesc);
                if ((m.access & Opcodes.ACC_STATIC) != 0) {
                    visitInsn(Opcodes.ACONST_NULL);
                } else {
                    visitVarInsn(Opcodes.ALOAD, 0);
                }
                visitIntInsn(Opcodes.BIPUSH, varList.size());
                visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
                int count = 0;
                int pos = 0;
                if ((m.access & Opcodes.ACC_STATIC) == 0) {
                    pos = 1;
                }
                for (String varNode : varList) {
                    visitInsn(Opcodes.DUP);
                    visitIntInsn(Opcodes.BIPUSH, count++);
                    if (!varNode.startsWith("L") && !varNode.startsWith("[")) {
                        String primitiveType = varNode;
                        switch(primitiveType.charAt(0)) {
                            case 'I':
                                visitVarInsn(Opcodes.ILOAD, pos);
                                pos += 1;
                                visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
                                break;
                            case 'J':
                                visitVarInsn(Opcodes.LLOAD, pos);
                                pos += 2;
                                visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
                                break;
                            case 'Z':
                                visitVarInsn(Opcodes.ILOAD, pos);
                                pos += 1;
                                visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
                                break;
                            case 'B':
                                visitVarInsn(Opcodes.ILOAD, pos);
                                pos += 1;
                                visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
                                break;
                            case 'C':
                                visitVarInsn(Opcodes.ILOAD, pos);
                                pos += 1;
                                visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;", false);
                                break;
                            case 'S':
                                visitVarInsn(Opcodes.ILOAD, pos);
                                pos += 1;
                                visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
                                break;
                            case 'F':
                                visitVarInsn(Opcodes.FLOAD, pos);
                                pos += 1;
                                visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
                                break;
                            case 'D':
                                visitVarInsn(Opcodes.DLOAD, pos);
                                pos += 2;
                                visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
                                break;
                            default:
                                throw new UnsupportedOperationException("'" + primitiveType + "'");
                        }
                    } else {
                        visitVarInsn(Opcodes.ALOAD, pos);
                        pos += 1;
                    }
                    visitInsn(Opcodes.AASTORE);
                }
                visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/ParamProfiler", "methodStart", "(JLjava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)V", false);
            }

            @Override
            public void visitInsn(int opcode) {
                if (opcode == Opcodes.RETURN || opcode == Opcodes.IRETURN || opcode == Opcodes.LRETURN || opcode == Opcodes.FRETURN || opcode == Opcodes.DRETURN || opcode == Opcodes.ARETURN) {
                    visitLdcInsn(new Long(methodId));
                    visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/ParamProfiler", "methodEnd", "(J)V", false);
                }
                super.visitInsn(opcode);
            }

            @Override
            public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
                catchStatement.add(handler);
                super.visitTryCatchBlock(start, end, handler, type);
            }

            boolean watchForHandling = false;

            @Override
            public void visitLabel(Label label) {
                watchForHandling = false;
                super.visitLabel(label);
                if (catchStatement.contains(label)) {
                    watchForHandling = true;
                }
            }

            @Override
            public void visitVarInsn(int opcode, int var) {
                super.visitVarInsn(opcode, var);
                if (watchForHandling) {
                    watchForHandling = false;
                    Label l = new Label();
                    visitLabel(l);
                    visitVarInsn(Opcodes.ALOAD, var);
                    visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/ParamProfiler", "handleException", "(Ljava/lang/Throwable;)V", false);
                }
            }
        };
        m.accept(mv);
        node.methods.set(node.methods.indexOf(m), mv);
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    node.accept(writer);
    return writer.toByteArray();
}
Also used : Label(org.objectweb.asm.Label) ClassWriter(org.objectweb.asm.ClassWriter) ClassReader(org.objectweb.asm.ClassReader)

Example 87 with ClassReader

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

the class JavassistVerifyErrorTest method asm_stackmapframe_check.

@Test
public void asm_stackmapframe_check() throws Exception {
    CustomURLClassLoader classLoader = new CustomURLClassLoader(new URL[] {}, Thread.currentThread().getContextClassLoader());
    final InputStream stream = classLoader.getResourceAsStream(JavaAssistUtils.javaNameToJvmName(INVALID_STACK_MAP_FRAME) + ".class");
    ClassReader cr = new ClassReader(stream);
    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    ClassVisitor cv = new BytecodeVerifyTestClassVisitor(cw);
    cr.accept(cv, ClassReader.EXPAND_FRAMES | ClassReader.SKIP_DEBUG);
    byte[] bytecode = cw.toByteArray();
    classLoader.defineClass0(INVALID_STACK_MAP_FRAME, bytecode);
    final Class<?> aClass = Class.forName(INVALID_STACK_MAP_FRAME, true, classLoader);
    Assert.assertSame(aClass.getClassLoader(), classLoader);
    final ASMBytecodeDisassembler bytecodeDisassembler = new ASMBytecodeDisassembler();
    final String dumpBytecode = bytecodeDisassembler.dumpBytecode(bytecode);
    logger.debug("dumpBytecode:{}", dumpBytecode);
    final String verify = bytecodeDisassembler.dumpVerify(bytecode, classLoader);
    logger.debug("dumpVerify:{}", verify);
//        final String dumpAsm = bytecodeDisassembler.dumpASM(bytecode);
//        logger.debug("dumpAsm :{}", dumpAsm);
}
Also used : BytecodeVerifyTestClassVisitor(bug_regression_jdk7.javassist.asm.BytecodeVerifyTestClassVisitor) InputStream(java.io.InputStream) ASMBytecodeDisassembler(com.navercorp.pinpoint.profiler.instrument.ASMBytecodeDisassembler) ClassReader(org.objectweb.asm.ClassReader) BytecodeVerifyTestClassVisitor(bug_regression_jdk7.javassist.asm.BytecodeVerifyTestClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) Test(org.junit.Test)

Example 88 with ClassReader

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

use of org.objectweb.asm.ClassReader in project retrolambda by orfjackal.

the class LambdaClassesTest method does_not_contain_references_to_JDK_lambda_classes.

@Test
public void does_not_contain_references_to_JDK_lambda_classes() throws IOException {
    ClassReader cr = new ClassReader("net/orfjackal/retrolambda/test/LambdaClassesTest$Dummy1$$Lambda$1");
    // XXX: fix visitConstantPool and assert the constant pool entries instead of this hack
    //        TestUtil.visitConstantPool(cr, (item, constant) -> {
    //        });
    String bytecode = new String(cr.b);
    assertThat(bytecode, not(containsString("java/lang/invoke/LambdaForm")));
}
Also used : ClassReader(org.objectweb.asm.ClassReader) Test(org.junit.Test)

Example 90 with ClassReader

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

the class ASMBytecodeDisassembler method dumpVerify.

public String dumpVerify(byte[] bytecode, ClassLoader classLoader) {
    if (bytecode == null) {
        throw new NullPointerException("bytecode must not be null");
    }
    if (classLoader == null) {
        throw new NullPointerException("classLoader must not be null");
    }
    final StringWriter out = new StringWriter();
    final PrintWriter writer = new PrintWriter(out);
    final ClassReader cr = new ClassReader(bytecode);
    CheckClassAdapter.verify(cr, classLoader, true, writer);
    return out.toString();
}
Also used : StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

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