Search in sources :

Example 31 with BadBytecode

use of javassist.bytecode.BadBytecode in project blaze-persistence by Blazebit.

the class ProxyFactory method compileUnsafe.

private void compileUnsafe(CtConstructor ctConstructor, String src) throws CannotCompileException {
    // This is essentially what javassist.compiler.Javac.compileBody does, except for an unsafe part
    CtClass cc = ctConstructor.getDeclaringClass();
    MethodInfo methodInfo = ctConstructor.getMethodInfo();
    try {
        Bytecode b = new Bytecode(cc.getClassFile2().getConstPool(), 0, 0);
        JvstCodeGen gen = new JvstCodeGen(b, cc, cc.getClassPool());
        SymbolTable stable = new SymbolTable();
        int mod = ctConstructor.getModifiers();
        gen.recordParams(ctConstructor.getParameterTypes(), Modifier.isStatic(mod), "$", "$args", "$$", stable);
        gen.recordType(CtClass.voidType);
        gen.recordReturnType(CtClass.voidType, "$r", null, stable);
        Parser p = new Parser(new Lex(src));
        SymbolTable stb = new SymbolTable(stable);
        Stmnt s = p.parseStatement(stb);
        if (p.hasMore()) {
            throw new CompileError("the method/constructor body must be surrounded by {}");
        }
        // setting callSuper to false is the unsafe part...
        boolean callSuper = false;
        gen.atMethodBody(s, callSuper, true);
        methodInfo.setCodeAttribute(b.toCodeAttribute());
        methodInfo.setAccessFlags(methodInfo.getAccessFlags() & ~AccessFlag.ABSTRACT);
        methodInfo.rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
        cc.rebuildClassFile();
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    }
}
Also used : CompileError(javassist.compiler.CompileError) JvstCodeGen(javassist.compiler.JvstCodeGen) SymbolTable(javassist.compiler.SymbolTable) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode) Parser(javassist.compiler.Parser) CtClass(javassist.CtClass) MethodInfo(javassist.bytecode.MethodInfo) Bytecode(javassist.bytecode.Bytecode) BadBytecode(javassist.bytecode.BadBytecode) Stmnt(javassist.compiler.ast.Stmnt) Lex(javassist.compiler.Lex)

Example 32 with BadBytecode

use of javassist.bytecode.BadBytecode in project JavaAgentTools by ethushiroha.

the class FieldInitLink method modifyConstructors.

private void modifyConstructors(ClassFile cf) throws CannotCompileException, NotFoundException {
    if (fieldInitializers == null)
        return;
    ConstPool cp = cf.getConstPool();
    List list = cf.getMethods();
    int n = list.size();
    for (int i = 0; i < n; ++i) {
        MethodInfo minfo = (MethodInfo) list.get(i);
        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) ArrayList(java.util.ArrayList) List(java.util.List) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) BadBytecode(javassist.bytecode.BadBytecode)

Example 33 with BadBytecode

use of javassist.bytecode.BadBytecode in project JavaAgentTools by ethushiroha.

the class Executor method evalArrayStore.

private void evalArrayStore(Type expectedComponent, Frame frame) throws BadBytecode {
    Type value = simplePop(frame);
    Type index = frame.pop();
    Type array = frame.pop();
    if (array == Type.UNINIT) {
        verifyAssignable(Type.INTEGER, index);
        return;
    }
    Type component = array.getComponent();
    if (component == null)
        throw new BadBytecode("Not an array! [pos = " + lastPos + "]: " + component);
    component = zeroExtend(component);
    verifyAssignable(expectedComponent, component);
    verifyAssignable(Type.INTEGER, index);
    // // but will throw arraystoreexception
    if (expectedComponent == Type.OBJECT) {
        verifyAssignable(expectedComponent, value);
    } else {
        verifyAssignable(component, value);
    }
}
Also used : BadBytecode(javassist.bytecode.BadBytecode)

Example 34 with BadBytecode

use of javassist.bytecode.BadBytecode in project JavaAgentTools by ethushiroha.

the class Analyzer method mergeJsr.

private void mergeJsr(IntQueue queue, Frame frame, Subroutine sub, int pos, int next) throws BadBytecode {
    if (sub == null)
        throw new BadBytecode("No subroutine at jsr target! [pos = " + pos + "]");
    Frame old = frames[next];
    boolean changed = false;
    if (old == null) {
        old = frames[next] = frame.copy();
        changed = true;
    } else {
        for (int i = 0; i < frame.localsLength(); i++) {
            // Skip everything accessed by a subroutine, mergeRet must handle this
            if (!sub.isAccessed(i)) {
                Type oldType = old.getLocal(i);
                Type newType = frame.getLocal(i);
                if (oldType == null) {
                    old.setLocal(i, newType);
                    changed = true;
                    continue;
                }
                newType = oldType.merge(newType);
                // Always set the type, in case a multi-type switched to a standard type.
                old.setLocal(i, newType);
                if (!newType.equals(oldType) || newType.popChanged())
                    changed = true;
            }
        }
    }
    if (!old.isJsrMerged()) {
        old.setJsrMerged(true);
        changed = true;
    }
    if (changed && old.isRetMerged())
        queue.add(next);
}
Also used : BadBytecode(javassist.bytecode.BadBytecode)

Example 35 with BadBytecode

use of javassist.bytecode.BadBytecode in project UniverseCore by EB-wilson.

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)

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