Search in sources :

Example 11 with BadBytecode

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

the class CtConstructor method removeConsCall.

private static void removeConsCall(CodeAttribute ca) throws CannotCompileException {
    CodeIterator iterator = ca.iterator();
    try {
        int pos = iterator.skipConstructor();
        if (pos >= 0) {
            int mref = iterator.u16bitAt(pos + 1);
            String desc = ca.getConstPool().getMethodrefType(mref);
            int num = Descriptor.numOfParameters(desc) + 1;
            if (num > 3)
                pos = iterator.insertGapAt(pos, num - 3, false).position;
            // this
            iterator.writeByte(Opcode.POP, pos++);
            iterator.writeByte(Opcode.NOP, pos);
            iterator.writeByte(Opcode.NOP, pos + 1);
            Descriptor.Iterator it = new Descriptor.Iterator(desc);
            while (true) {
                it.next();
                if (it.isParameter())
                    iterator.writeByte(it.is2byte() ? Opcode.POP2 : Opcode.POP, pos++);
                else
                    break;
            }
        }
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CodeIterator(javassist.bytecode.CodeIterator) CodeIterator(javassist.bytecode.CodeIterator) Descriptor(javassist.bytecode.Descriptor) BadBytecode(javassist.bytecode.BadBytecode)

Example 12 with BadBytecode

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

the class CtBehavior method insertAt.

/**
 * Inserts bytecode at the specified line in the body.
 *
 * <p>If there is not
 * a statement at the specified line, the bytecode might be inserted
 * at the line including the first statement after that line specified.
 * For example, if there is only a closing brace at that line, the
 * bytecode would be inserted at another line below.
 * To know exactly where the bytecode will be inserted, call with
 * <code>modify</code> set to <code>false</code>.
 *
 * @param lineNum   the line number.  The bytecode is inserted at the
 *                  beginning of the code at the line specified by this
 *                  line number.
 * @param modify    if false, this method does not insert the bytecode.
 *                  It instead only returns the line number at which
 *                  the bytecode would be inserted.
 * @param src       the source code representing the inserted bytecode.
 *                  It must be a single statement or block.
 *                  If modify is false, the value of src can be null.
 * @return      the line number at which the bytecode has been inserted.
 */
public int insertAt(int lineNum, boolean modify, String src) throws CannotCompileException {
    CodeAttribute ca = methodInfo.getCodeAttribute();
    if (ca == null)
        throw new CannotCompileException("no method body");
    LineNumberAttribute ainfo = (LineNumberAttribute) ca.getAttribute(LineNumberAttribute.tag);
    if (ainfo == null)
        throw new CannotCompileException("no line number info");
    LineNumberAttribute.Pc pc = ainfo.toNearPc(lineNum);
    lineNum = pc.line;
    int index = pc.index;
    if (!modify)
        return lineNum;
    CtClass cc = declaringClass;
    cc.checkModify();
    CodeIterator iterator = ca.iterator();
    Javac jv = new Javac(cc);
    try {
        jv.recordLocalVariables(ca, index);
        jv.recordParams(getParameterTypes(), Modifier.isStatic(getModifiers()));
        jv.setMaxLocals(ca.getMaxLocals());
        jv.compileStmnt(src);
        Bytecode b = jv.getBytecode();
        int locals = b.getMaxLocals();
        int stack = b.getMaxStack();
        ca.setMaxLocals(locals);
        /* We assume that there is no values in the operand stack
             * at the position where the bytecode is inserted.
             */
        if (stack > ca.getMaxStack())
            ca.setMaxStack(stack);
        index = iterator.insertAt(index, b.get());
        iterator.insert(b.getExceptionTable(), index);
        methodInfo.rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
        return lineNum;
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : LineNumberAttribute(javassist.bytecode.LineNumberAttribute) 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 13 with BadBytecode

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

the class CtBehavior method addCatch.

/**
 * Adds a catch clause that handles an exception thrown in the
 * body.  The catch clause must end with a return or throw statement.
 *
 * @param src       the source code representing the catch clause.
 *                  It must be a single statement or block.
 * @param exceptionType     the type of the exception handled by the
 *                          catch clause.
 * @param exceptionName     the name of the variable containing the
 *                          caught exception, for example,
 *                          <code>$e</code>.
 */
public void addCatch(String src, CtClass exceptionType, String exceptionName) throws CannotCompileException {
    CtClass cc = declaringClass;
    cc.checkModify();
    ConstPool cp = methodInfo.getConstPool();
    CodeAttribute ca = methodInfo.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(getParameterTypes(), Modifier.isStatic(getModifiers()));
        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(getStartPosOfBody(ca), len, len, cp.addClassInfo(exceptionType));
        iterator.append(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) ConstPool(javassist.bytecode.ConstPool) 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 14 with BadBytecode

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

the class Reflection method start.

/**
 * Initializes the object.
 */
@Override
public void start(ClassPool pool) throws NotFoundException {
    classPool = pool;
    final String msg = "javassist.tools.reflect.Sample is not found or broken.";
    try {
        CtClass c = classPool.get("javassist.tools.reflect.Sample");
        rebuildClassFile(c.getClassFile());
        trapMethod = c.getDeclaredMethod("trap");
        trapStaticMethod = c.getDeclaredMethod("trapStatic");
        trapRead = c.getDeclaredMethod("trapRead");
        trapWrite = c.getDeclaredMethod("trapWrite");
        readParam = new CtClass[] { classPool.get("java.lang.Object") };
    } catch (NotFoundException e) {
        throw new RuntimeException(msg);
    } catch (BadBytecode e) {
        throw new RuntimeException(msg);
    }
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode)

Example 15 with BadBytecode

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

the class FieldAccess method replace.

/**
 * Replaces the method call with the bytecode derived from
 * the given source text.
 *
 * <p>$0 is available even if the called method is static.
 * If the field access is writing, $_ is available but the value
 * of $_ is ignored.
 *
 * @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);
    Javac jc = new Javac(thisClass);
    CodeAttribute ca = iterator.get();
    try {
        CtClass[] params;
        CtClass retType;
        CtClass fieldType = Descriptor.toCtClass(constPool.getFieldrefType(index), thisClass.getClassPool());
        boolean read = isReader();
        if (read) {
            params = new CtClass[0];
            retType = fieldType;
        } else {
            params = new CtClass[1];
            params[0] = fieldType;
            retType = CtClass.voidType;
        }
        int paramVar = ca.getMaxLocals();
        jc.recordParams(constPool.getFieldrefClassName(index), params, true, paramVar, withinStatic());
        /* Is $_ included in the source code?
             */
        boolean included = checkResultValue(retType, statement);
        if (read)
            included = true;
        int retVar = jc.recordReturnType(retType, included);
        if (read)
            jc.recordProceed(new ProceedForRead(retType, opcode, index, paramVar));
        else {
            // because $type is not the return type...
            jc.recordType(fieldType);
            jc.recordProceed(new ProceedForWrite(params[0], opcode, index, paramVar));
        }
        Bytecode bytecode = jc.getBytecode();
        storeStack(params, isStatic(), paramVar, bytecode);
        jc.recordLocalVariables(ca, pos);
        if (included)
            if (retType == CtClass.voidType) {
                bytecode.addOpcode(ACONST_NULL);
                bytecode.addAstore(retVar);
            } else {
                bytecode.addConstZero(retType);
                // initialize $_
                bytecode.addStore(retVar, retType);
            }
        jc.compileStmnt(statement);
        if (read)
            bytecode.addLoad(retVar, retType);
        replace0(pos, bytecode, 3);
    } 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) 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)

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