Search in sources :

Example 6 with ConstPool

use of javassist.bytecode.ConstPool in project hibernate-orm by hibernate.

the class BulkAccessorFactory method addDefaultConstructor.

/**
	 * Declares a constructor that takes no parameter.
	 *
	 * @param classfile The class descriptor
	 *
	 * @throws CannotCompileException Indicates trouble with the underlying Javassist calls
	 */
private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
    final ConstPool constPool = classfile.getConstPool();
    final String constructorSignature = "()V";
    final MethodInfo constructorMethodInfo = new MethodInfo(constPool, MethodInfo.nameInit, constructorSignature);
    final Bytecode code = new Bytecode(constPool, 0, 1);
    // aload_0
    code.addAload(0);
    // invokespecial
    code.addInvokespecial(BulkAccessor.class.getName(), MethodInfo.nameInit, constructorSignature);
    // return
    code.addOpcode(Opcode.RETURN);
    constructorMethodInfo.setCodeAttribute(code.toCodeAttribute());
    constructorMethodInfo.setAccessFlags(AccessFlag.PUBLIC);
    classfile.addMethod(constructorMethodInfo);
}
Also used : ConstPool(javassist.bytecode.ConstPool) MethodInfo(javassist.bytecode.MethodInfo) Bytecode(javassist.bytecode.Bytecode)

Example 7 with ConstPool

use of javassist.bytecode.ConstPool 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 8 with ConstPool

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

the class JavassistEngineTest method getTransformByteCode.

public byte[] getTransformByteCode() {
    try {
        final ClassPool pool = new ClassPool(true);
        final CtClass ctClass = pool.get(mock);
        final ConstPool constPool = ctClass.getClassFile2().getConstPool();
        MethodInfo info = new MethodInfo(constPool, "transformMethod", "()V");
        final CtMethod newMethod = CtMethod.make(info, ctClass);
        ctClass.addMethod(newMethod);
        return ctClass.toBytecode();
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}
Also used : CtClass(javassist.CtClass) ConstPool(javassist.bytecode.ConstPool) ClassPool(javassist.ClassPool) MethodInfo(javassist.bytecode.MethodInfo) CtMethod(javassist.CtMethod)

Aggregations

ConstPool (javassist.bytecode.ConstPool)8 MethodInfo (javassist.bytecode.MethodInfo)6 CtClass (javassist.CtClass)4 Bytecode (javassist.bytecode.Bytecode)4 ClassPool (javassist.ClassPool)3 BadBytecode (javassist.bytecode.BadBytecode)3 CodeIterator (javassist.bytecode.CodeIterator)3 NotFoundException (javassist.NotFoundException)2 CodeAttribute (javassist.bytecode.CodeAttribute)2 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)2 Method (java.lang.reflect.Method)1 CannotCompileException (javassist.CannotCompileException)1 CtMethod (javassist.CtMethod)1 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 StackMapTable (javassist.bytecode.StackMapTable)1 Annotation (javassist.bytecode.annotation.Annotation)1 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)1 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)1 CompileError (javassist.compiler.CompileError)1 Javac (javassist.compiler.Javac)1