Search in sources :

Example 6 with BadBytecode

use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.

the class FieldInitLink method modifyClassConstructor.

private void modifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize) throws CannotCompileException {
    MethodInfo m = cf.getStaticInitializer();
    if (m == null) {
        code.add(Bytecode.RETURN);
        code.setMaxStack(stacksize);
        code.setMaxLocals(localsize);
        m = new MethodInfo(cf.getConstPool(), "<clinit>", "()V");
        m.setAccessFlags(AccessFlag.STATIC);
        m.setCodeAttribute(code.toCodeAttribute());
        cf.addMethod(m);
        CtMember.Cache cache = hasMemberCache();
        if (cache != null)
            cache.addConstructor(new CtConstructor(m, this));
    } else {
        CodeAttribute codeAttr = m.getCodeAttribute();
        if (codeAttr == null)
            throw new CannotCompileException("empty <clinit>");
        try {
            CodeIterator it = codeAttr.iterator();
            int pos = it.insertEx(code.get());
            it.insert(code.getExceptionTable(), pos);
            int maxstack = codeAttr.getMaxStack();
            if (maxstack < stacksize)
                codeAttr.setMaxStack(stacksize);
            int maxlocals = codeAttr.getMaxLocals();
            if (maxlocals < localsize)
                codeAttr.setMaxLocals(localsize);
        } catch (BadBytecode e) {
            throw new CannotCompileException(e);
        }
    }
    try {
        m.rebuildStackMapIf6(classPool, cf);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode)

Example 7 with BadBytecode

use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.

the class FieldInitLink method modifyConstructors.

private void modifyConstructors(ClassFile cf) throws CannotCompileException, NotFoundException {
    if (fieldInitializers == null)
        return;
    ConstPool cp = cf.getConstPool();
    List<MethodInfo> methods = cf.getMethods();
    for (MethodInfo minfo : methods) {
        if (minfo.isConstructor()) {
            CodeAttribute codeAttr = minfo.getCodeAttribute();
            if (codeAttr != null)
                try {
                    Bytecode init = new Bytecode(cp, 0, codeAttr.getMaxLocals());
                    CtClass[] params = Descriptor.getParameterTypes(minfo.getDescriptor(), classPool);
                    int stacksize = makeFieldInitializer(init, params);
                    insertAuxInitializer(codeAttr, init, stacksize);
                    minfo.rebuildStackMapIf6(classPool, cf);
                } catch (BadBytecode e) {
                    throw new CannotCompileException(e);
                }
        }
    }
}
Also used : ConstPool(javassist.bytecode.ConstPool) CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) BadBytecode(javassist.bytecode.BadBytecode)

Example 8 with BadBytecode

use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.

the class CtConstructor method insertBeforeBody.

/**
 * Inserts bytecode just after another constructor in the super class
 * or this class is called.
 * It does not work if this object represents a class initializer.
 *
 * @param src       the source code representing the inserted bytecode.
 *                  It must be a single statement or block.
 */
public void insertBeforeBody(String src) throws CannotCompileException {
    CtClass cc = declaringClass;
    cc.checkModify();
    if (isClassInitializer())
        throw new CannotCompileException("class initializer");
    CodeAttribute ca = methodInfo.getCodeAttribute();
    CodeIterator iterator = ca.iterator();
    Bytecode b = new Bytecode(methodInfo.getConstPool(), ca.getMaxStack(), ca.getMaxLocals());
    b.setStackDepth(ca.getMaxStack());
    Javac jv = new Javac(b, cc);
    try {
        jv.recordParams(getParameterTypes(), false);
        jv.compileStmnt(src);
        ca.setMaxStack(b.getMaxStack());
        ca.setMaxLocals(b.getMaxLocals());
        iterator.skipConstructor();
        int pos = iterator.insertEx(b.get());
        iterator.insert(b.getExceptionTable(), pos);
        methodInfo.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) Javac(javassist.compiler.Javac) CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) BadBytecode(javassist.bytecode.BadBytecode)

Example 9 with BadBytecode

use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.

the class CtBehavior method setBody.

/**
 * Sets a method/constructor body.
 *
 * @param src       the source code representing the body.
 *                  It must be a single statement or block.
 *                  If it is <code>null</code>, the substituted
 *                  body does nothing except returning zero or null.
 * @param delegateObj       the source text specifying the object
 *                          that is called on by <code>$proceed()</code>.
 * @param delegateMethod    the name of the method
 *                          that is called by <code>$proceed()</code>.
 */
public void setBody(String src, String delegateObj, String delegateMethod) throws CannotCompileException {
    CtClass cc = declaringClass;
    cc.checkModify();
    try {
        Javac jv = new Javac(cc);
        if (delegateMethod != null)
            jv.recordProceed(delegateObj, delegateMethod);
        Bytecode b = jv.compileBody(this, src);
        methodInfo.setCodeAttribute(b.toCodeAttribute());
        methodInfo.setAccessFlags(methodInfo.getAccessFlags() & ~AccessFlag.ABSTRACT);
        methodInfo.rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
        declaringClass.rebuildClassFile();
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CompileError(javassist.compiler.CompileError) Javac(javassist.compiler.Javac) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) BadBytecode(javassist.bytecode.BadBytecode)

Example 10 with BadBytecode

use of javassist.bytecode.BadBytecode in project javassist by jboss-javassist.

the class CtBehavior method insertBefore.

private void insertBefore(String src, boolean rebuild) throws CannotCompileException {
    CtClass cc = declaringClass;
    cc.checkModify();
    CodeAttribute ca = methodInfo.getCodeAttribute();
    if (ca == null)
        throw new CannotCompileException("no method body");
    CodeIterator iterator = ca.iterator();
    Javac jv = new Javac(cc);
    try {
        int nvars = jv.recordParams(getParameterTypes(), Modifier.isStatic(getModifiers()));
        jv.recordParamNames(ca, nvars);
        jv.recordLocalVariables(ca, 0);
        jv.recordReturnType(getReturnType0(), false);
        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);
        int pos = iterator.insertEx(b.get());
        iterator.insert(b.getExceptionTable(), pos);
        if (rebuild)
            methodInfo.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) Javac(javassist.compiler.Javac) CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) BadBytecode(javassist.bytecode.BadBytecode)

Aggregations

BadBytecode (javassist.bytecode.BadBytecode)135 Bytecode (javassist.bytecode.Bytecode)57 CodeAttribute (javassist.bytecode.CodeAttribute)56 CodeIterator (javassist.bytecode.CodeIterator)43 MethodInfo (javassist.bytecode.MethodInfo)38 CtClass (javassist.CtClass)32 NotFoundException (javassist.NotFoundException)32 ConstPool (javassist.bytecode.ConstPool)30 Javac (javassist.compiler.Javac)27 CompileError (javassist.compiler.CompileError)26 CannotCompileException (javassist.CannotCompileException)22 ClassPool (javassist.ClassPool)12 List (java.util.List)11 Type (javassist.bytecode.analysis.Type)8 DuplicateMemberException (javassist.bytecode.DuplicateMemberException)7 IOException (java.io.IOException)6 CtMethod (javassist.CtMethod)5 ClassFile (javassist.bytecode.ClassFile)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 Method (java.lang.reflect.Method)4