Search in sources :

Example 41 with BadBytecode

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

the class NewArray method replace2.

private void replace2(String statement) throws CompileError, NotFoundException, BadBytecode, CannotCompileException {
    // to call checkModify().
    thisClass.getClassFile();
    ConstPool constPool = getConstPool();
    int pos = currentPos;
    CtClass retType;
    int codeLength;
    int index = 0;
    int dim = 1;
    String desc;
    if (opcode == Opcode.NEWARRAY) {
        // atype
        index = iterator.byteAt(currentPos + 1);
        CtPrimitiveType cpt = (CtPrimitiveType) getPrimitiveType(index);
        desc = "[" + cpt.getDescriptor();
        codeLength = 2;
    } else if (opcode == Opcode.ANEWARRAY) {
        index = iterator.u16bitAt(pos + 1);
        desc = constPool.getClassInfo(index);
        if (desc.startsWith("["))
            desc = "[" + desc;
        else
            desc = "[L" + desc + ";";
        codeLength = 3;
    } else if (opcode == Opcode.MULTIANEWARRAY) {
        index = iterator.u16bitAt(currentPos + 1);
        desc = constPool.getClassInfo(index);
        dim = iterator.byteAt(currentPos + 3);
        codeLength = 4;
    } else
        throw new RuntimeException("bad opcode: " + opcode);
    retType = Descriptor.toCtClass(desc, thisClass.getClassPool());
    Javac jc = new Javac(thisClass);
    CodeAttribute ca = iterator.get();
    CtClass[] params = new CtClass[dim];
    for (int i = 0; i < dim; ++i) params[i] = CtClass.intType;
    int paramVar = ca.getMaxLocals();
    jc.recordParams(javaLangObject, params, true, paramVar, withinStatic());
    /* Is $_ included in the source code?
         */
    checkResultValue(retType, statement);
    int retVar = jc.recordReturnType(retType, true);
    jc.recordProceed(new ProceedForArray(retType, opcode, index, dim));
    Bytecode bytecode = jc.getBytecode();
    storeStack(params, true, paramVar, bytecode);
    jc.recordLocalVariables(ca, pos);
    // initialize $_
    bytecode.addOpcode(ACONST_NULL);
    bytecode.addAstore(retVar);
    jc.compileStmnt(statement);
    bytecode.addAload(retVar);
    replace0(pos, bytecode, codeLength);
}
Also used : ConstPool(javassist.bytecode.ConstPool) CtClass(javassist.CtClass) Javac(javassist.compiler.Javac) CodeAttribute(javassist.bytecode.CodeAttribute) CtPrimitiveType(javassist.CtPrimitiveType) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode)

Example 42 with BadBytecode

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

the class ExprEditor method loopBody.

final boolean loopBody(CodeIterator iterator, CtClass clazz, MethodInfo minfo, LoopContext context) throws CannotCompileException {
    try {
        Expr expr = null;
        int pos = iterator.next();
        int c = iterator.byteAt(pos);
        if (// c < 178
        c < Opcode.GETSTATIC)
            /* skip */
            ;
        else if (c < Opcode.NEWARRAY) {
            // c < 188
            if (c == Opcode.INVOKESTATIC || c == Opcode.INVOKEINTERFACE || c == Opcode.INVOKEVIRTUAL) {
                expr = new MethodCall(pos, iterator, clazz, minfo);
                edit((MethodCall) expr);
            } else if (c == Opcode.GETFIELD || c == Opcode.GETSTATIC || c == Opcode.PUTFIELD || c == Opcode.PUTSTATIC) {
                expr = new FieldAccess(pos, iterator, clazz, minfo, c);
                edit((FieldAccess) expr);
            } else if (c == Opcode.NEW) {
                int index = iterator.u16bitAt(pos + 1);
                context.newList = new NewOp(context.newList, pos, minfo.getConstPool().getClassInfo(index));
            } else if (c == Opcode.INVOKESPECIAL) {
                NewOp newList = context.newList;
                if (newList != null && minfo.getConstPool().isConstructor(newList.type, iterator.u16bitAt(pos + 1)) > 0) {
                    expr = new NewExpr(pos, iterator, clazz, minfo, newList.type, newList.pos);
                    edit((NewExpr) expr);
                    context.newList = newList.next;
                } else {
                    MethodCall mcall = new MethodCall(pos, iterator, clazz, minfo);
                    if (mcall.getMethodName().equals(MethodInfo.nameInit)) {
                        ConstructorCall ccall = new ConstructorCall(pos, iterator, clazz, minfo);
                        expr = ccall;
                        edit(ccall);
                    } else {
                        expr = mcall;
                        edit(mcall);
                    }
                }
            }
        } else {
            // c >= 188
            if (c == Opcode.NEWARRAY || c == Opcode.ANEWARRAY || c == Opcode.MULTIANEWARRAY) {
                expr = new NewArray(pos, iterator, clazz, minfo, c);
                edit((NewArray) expr);
            } else if (c == Opcode.INSTANCEOF) {
                expr = new Instanceof(pos, iterator, clazz, minfo);
                edit((Instanceof) expr);
            } else if (c == Opcode.CHECKCAST) {
                expr = new Cast(pos, iterator, clazz, minfo);
                edit((Cast) expr);
            }
        }
        if (expr != null && expr.edited()) {
            context.updateMax(expr.locals(), expr.stack());
            return true;
        }
        return false;
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode)

Example 43 with BadBytecode

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

the class MethodCall method replace.

/*
     * Returns the parameter types of the called method.

    public CtClass[] getParameterTypes() throws NotFoundException {
        return Descriptor.getParameterTypes(getMethodDesc(),
                                            thisClass.getClassPool());
    }
    */
/*
     * Returns the return type of the called method.

    public CtClass getReturnType() throws NotFoundException {
        return Descriptor.getReturnType(getMethodDesc(),
                                        thisClass.getClassPool());
    }
    */
/**
 * Replaces the method call with the bytecode derived from
 * the given source text.
 *
 * <p>$0 is available even if the called method is static.
 *
 * @param statement         a Java statement except try-catch.
 */
@Override
public void replace(String statement) throws CannotCompileException {
    // to call checkModify().
    thisClass.getClassFile();
    ConstPool constPool = getConstPool();
    int pos = currentPos;
    int index = iterator.u16bitAt(pos + 1);
    String classname, methodname, signature;
    int opcodeSize;
    int c = iterator.byteAt(pos);
    if (c == INVOKEINTERFACE) {
        opcodeSize = 5;
        classname = constPool.getInterfaceMethodrefClassName(index);
        methodname = constPool.getInterfaceMethodrefName(index);
        signature = constPool.getInterfaceMethodrefType(index);
    } else if (c == INVOKESTATIC || c == INVOKESPECIAL || c == INVOKEVIRTUAL) {
        opcodeSize = 3;
        classname = constPool.getMethodrefClassName(index);
        methodname = constPool.getMethodrefName(index);
        signature = constPool.getMethodrefType(index);
    } else
        throw new CannotCompileException("not method invocation");
    Javac jc = new Javac(thisClass);
    ClassPool cp = thisClass.getClassPool();
    CodeAttribute ca = iterator.get();
    try {
        CtClass[] params = Descriptor.getParameterTypes(signature, cp);
        CtClass retType = Descriptor.getReturnType(signature, cp);
        int paramVar = ca.getMaxLocals();
        jc.recordParams(classname, params, true, paramVar, withinStatic());
        int retVar = jc.recordReturnType(retType, true);
        if (c == INVOKESTATIC)
            jc.recordStaticProceed(classname, methodname);
        else if (c == INVOKESPECIAL)
            jc.recordSpecialProceed(Javac.param0Name, classname, methodname, signature, index);
        else
            jc.recordProceed(Javac.param0Name, methodname);
        /* Is $_ included in the source code?
             */
        checkResultValue(retType, statement);
        Bytecode bytecode = jc.getBytecode();
        storeStack(params, c == INVOKESTATIC, paramVar, bytecode);
        jc.recordLocalVariables(ca, pos);
        if (retType != CtClass.voidType) {
            bytecode.addConstZero(retType);
            // initialize $_
            bytecode.addStore(retVar, retType);
        }
        jc.compileStmnt(statement);
        if (retType != CtClass.voidType)
            bytecode.addLoad(retVar, retType);
        replace0(pos, bytecode, opcodeSize);
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException("broken method");
    }
}
Also used : CompileError(javassist.compiler.CompileError) ConstPool(javassist.bytecode.ConstPool) Javac(javassist.compiler.Javac) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode) CtClass(javassist.CtClass) CodeAttribute(javassist.bytecode.CodeAttribute) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode)

Example 44 with BadBytecode

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

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 45 with BadBytecode

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

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