Search in sources :

Example 11 with Bytecode

use of javassist.bytecode.Bytecode in project pinpoint by naver.

the class JavassistMethod method insertCatch.

private void insertCatch(int from, String src, CtClass exceptionType, String exceptionName) throws CannotCompileException {
    CtClass cc = behavior.getDeclaringClass();
    ConstPool cp = behavior.getMethodInfo().getConstPool();
    CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
    CodeIterator iterator = ca.iterator();
    Bytecode b = new Bytecode(cp, ca.getMaxStack(), ca.getMaxLocals());
    b.setStackDepth(1);
    Javac jv = new Javac(b, cc);
    try {
        jv.recordParams(behavior.getParameterTypes(), Modifier.isStatic(getModifiers()));
        jv.recordLocalVariables(ca, from);
        int var = jv.recordVariable(exceptionType, exceptionName);
        b.addAstore(var);
        jv.compileStmnt(src);
        int stack = b.getMaxStack();
        int locals = b.getMaxLocals();
        if (stack > ca.getMaxStack())
            ca.setMaxStack(stack);
        if (locals > ca.getMaxLocals())
            ca.setMaxLocals(locals);
        int len = iterator.getCodeLength();
        int pos = iterator.append(b.get());
        ca.getExceptionTable().add(from, len, len, cp.addClassInfo(exceptionType));
        iterator.append(b.getExceptionTable(), pos);
        behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CompileError(javassist.compiler.CompileError) CtClass(javassist.CtClass) ConstPool(javassist.bytecode.ConstPool) Javac(javassist.compiler.Javac) CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode)

Example 12 with Bytecode

use of javassist.bytecode.Bytecode in project pinpoint by naver.

the class JavassistMethod method insertBeforeConstructor.

private int insertBeforeConstructor(int pos, String src) throws CannotCompileException {
    CtClass cc = behavior.getDeclaringClass();
    CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
    CodeIterator iterator = ca.iterator();
    Bytecode b = new Bytecode(behavior.getMethodInfo().getConstPool(), ca.getMaxStack(), ca.getMaxLocals());
    b.setStackDepth(ca.getMaxStack());
    Javac jv = new Javac(b, cc);
    try {
        jv.recordParams(behavior.getParameterTypes(), false);
        jv.recordLocalVariables(ca, 0);
        jv.compileStmnt(src);
        ca.setMaxStack(b.getMaxStack());
        ca.setMaxLocals(b.getMaxLocals());
        iterator.skipConstructor();
        if (pos != -1) {
            iterator.insertEx(pos, b.get());
        } else {
            pos = iterator.insertEx(b.get());
        }
        iterator.insert(b.getExceptionTable(), pos);
        behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
        return pos + b.length();
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CompileError(javassist.compiler.CompileError) CtClass(javassist.CtClass) Javac(javassist.compiler.Javac) CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode)

Example 13 with Bytecode

use of javassist.bytecode.Bytecode in project pinpoint by naver.

the class JavassistMethod method insertBeforeMethod.

private int insertBeforeMethod(int pos, String src) throws CannotCompileException {
    CtClass cc = behavior.getDeclaringClass();
    CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
    if (ca == null)
        throw new CannotCompileException("no method body");
    CodeIterator iterator = ca.iterator();
    Javac jv = new Javac(cc);
    try {
        int nvars = jv.recordParams(behavior.getParameterTypes(), Modifier.isStatic(getModifiers()));
        jv.recordParamNames(ca, nvars);
        jv.recordLocalVariables(ca, 0);
        jv.recordType(getReturnType0());
        jv.compileStmnt(src);
        Bytecode b = jv.getBytecode();
        int stack = b.getMaxStack();
        int locals = b.getMaxLocals();
        if (stack > ca.getMaxStack())
            ca.setMaxStack(stack);
        if (locals > ca.getMaxLocals())
            ca.setMaxLocals(locals);
        if (pos != -1) {
            iterator.insertEx(pos, b.get());
        } else {
            pos = iterator.insertEx(b.get());
        }
        iterator.insert(b.getExceptionTable(), pos);
        behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
        return pos + b.length();
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CompileError(javassist.compiler.CompileError) CtClass(javassist.CtClass) Javac(javassist.compiler.Javac) CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode)

Example 14 with Bytecode

use of javassist.bytecode.Bytecode in project fakereplace by fakereplace.

the class Transformer method addConstructorForInstrumentation.

private void addConstructorForInstrumentation(ClassFile file) {
    MethodInfo ret = new MethodInfo(file.getConstPool(), "<init>", Constants.ADDED_CONSTRUCTOR_DESCRIPTOR);
    Bytecode code = new Bytecode(file.getConstPool());
    // if the class does not have a constructor return
    if (!ManipulationUtils.addBogusConstructorCall(file, code)) {
        return;
    }
    CodeAttribute ca = code.toCodeAttribute();
    ca.setMaxLocals(4);
    ret.setCodeAttribute(ca);
    ret.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.SYNTHETIC);
    try {
        ca.computeMaxStack();
        file.addMethod(ret);
    } catch (DuplicateMemberException e) {
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DuplicateMemberException(javassist.bytecode.DuplicateMemberException) CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) DuplicateMemberException(javassist.bytecode.DuplicateMemberException)

Example 15 with Bytecode

use of javassist.bytecode.Bytecode in project fakereplace by fakereplace.

the class Transformer method addMethodForInstrumentation.

/**
 * Adds a method to a class that re can redefine when the class is reloaded
 */
private void addMethodForInstrumentation(ClassFile file) {
    try {
        MethodInfo m = new MethodInfo(file.getConstPool(), Constants.ADDED_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
        m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.SYNTHETIC);
        Bytecode b = new Bytecode(file.getConstPool(), 5, 3);
        if (BuiltinClassData.skipInstrumentation(file.getSuperclass())) {
            b.addNew(NoSuchMethodError.class.getName());
            b.add(Opcode.DUP);
            b.addInvokespecial(NoSuchMethodError.class.getName(), "<init>", "()V");
            b.add(Opcode.ATHROW);
        } else {
            // delegate to the parent class
            b.add(Bytecode.ALOAD_0);
            b.add(Bytecode.ILOAD_1);
            b.add(Bytecode.ALOAD_2);
            b.addInvokespecial(file.getSuperclass(), Constants.ADDED_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
            b.add(Bytecode.ARETURN);
        }
        CodeAttribute ca = b.toCodeAttribute();
        m.setCodeAttribute(ca);
        file.addMethod(m);
    } catch (DuplicateMemberException e) {
    // e.printStackTrace();
    }
    try {
        MethodInfo m = new MethodInfo(file.getConstPool(), Constants.ADDED_STATIC_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
        m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC | AccessFlag.SYNTHETIC);
        Bytecode b = new Bytecode(file.getConstPool(), 5, 3);
        b.addNew(NoSuchMethodError.class.getName());
        b.add(Opcode.DUP);
        b.addInvokespecial(NoSuchMethodError.class.getName(), "<init>", "()V");
        b.add(Opcode.ATHROW);
        CodeAttribute ca = b.toCodeAttribute();
        m.setCodeAttribute(ca);
        file.addMethod(m);
    } catch (DuplicateMemberException e) {
    // e.printStackTrace();
    }
}
Also used : DuplicateMemberException(javassist.bytecode.DuplicateMemberException) CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode)

Aggregations

Bytecode (javassist.bytecode.Bytecode)36 MethodInfo (javassist.bytecode.MethodInfo)28 BadBytecode (javassist.bytecode.BadBytecode)24 CodeIterator (javassist.bytecode.CodeIterator)15 CodeAttribute (javassist.bytecode.CodeAttribute)11 ConstPool (javassist.bytecode.ConstPool)11 DuplicateMemberException (javassist.bytecode.DuplicateMemberException)8 IOException (java.io.IOException)6 List (java.util.List)6 ClassFile (javassist.bytecode.ClassFile)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DataOutputStream (java.io.DataOutputStream)4 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 Method (java.lang.reflect.Method)4 HashSet (java.util.HashSet)4 NotFoundException (javassist.NotFoundException)4 JumpMarker (org.fakereplace.util.JumpMarker)4 HashMap (java.util.HashMap)3 Set (java.util.Set)3 CannotCompileException (javassist.CannotCompileException)3