Search in sources :

Example 36 with BadBytecode

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

the class Analyzer method analyzeNextEntry.

private void analyzeNextEntry(MethodInfo method, CodeIterator iter, IntQueue queue, Executor executor) throws BadBytecode {
    int pos = queue.take();
    iter.move(pos);
    iter.next();
    Frame frame = frames[pos].copy();
    Subroutine subroutine = subroutines[pos];
    try {
        executor.execute(method, pos, iter, frame, subroutine);
    } catch (RuntimeException e) {
        throw new BadBytecode(e.getMessage() + "[pos = " + pos + "]", e);
    }
    int opcode = iter.byteAt(pos);
    if (opcode == TABLESWITCH) {
        mergeTableSwitch(queue, pos, iter, frame);
    } else if (opcode == LOOKUPSWITCH) {
        mergeLookupSwitch(queue, pos, iter, frame);
    } else if (opcode == RET) {
        mergeRet(queue, iter, pos, frame, subroutine);
    } else if (Util.isJumpInstruction(opcode)) {
        int target = Util.getJumpTarget(pos, iter);
        if (Util.isJsr(opcode)) {
            // Merge the state before the jsr into the next instruction
            mergeJsr(queue, frames[pos], subroutines[target], pos, lookAhead(iter, pos));
        } else if (!Util.isGoto(opcode)) {
            merge(queue, frame, lookAhead(iter, pos));
        }
        merge(queue, frame, target);
    } else if (opcode != ATHROW && !Util.isReturn(opcode)) {
        // Can advance to next instruction
        merge(queue, frame, lookAhead(iter, pos));
    }
    // Merge all exceptions that are reachable from this instruction.
    // The redundancy is intentional, since the state must be based
    // on the current instruction frame.
    mergeExceptionHandlers(queue, method, pos, frame);
}
Also used : BadBytecode(javassist.bytecode.BadBytecode)

Example 37 with BadBytecode

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

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

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

the class Executor method paramTypesFromDesc.

private Type[] paramTypesFromDesc(String desc) throws BadBytecode {
    CtClass[] classes = null;
    try {
        classes = Descriptor.getParameterTypes(desc, classPool);
    } catch (NotFoundException e) {
        throw new BadBytecode("Could not find class in descriptor [pos = " + lastPos + "]: " + e.getMessage());
    }
    if (classes == null)
        throw new BadBytecode("Could not obtain parameters for descriptor [pos = " + lastPos + "]: " + desc);
    Type[] types = new Type[classes.length];
    for (int i = 0; i < types.length; i++) types[i] = Type.get(classes[i]);
    return types;
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode)

Example 39 with BadBytecode

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

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

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

the class Instanceof method replace.

/**
 * Replaces the instanceof operator with the bytecode derived from
 * the given source text.
 *
 * <p>$0 is available but the value is <code>null</code>.
 *
 * @param statement         a Java statement except try-catch.
 */
@Override
public void replace(String statement) throws CannotCompileException {
    // to call checkModify().
    thisClass.getClassFile();
    @SuppressWarnings("unused") ConstPool constPool = getConstPool();
    int pos = currentPos;
    int index = iterator.u16bitAt(pos + 1);
    Javac jc = new Javac(thisClass);
    ClassPool cp = thisClass.getClassPool();
    CodeAttribute ca = iterator.get();
    try {
        CtClass[] params = new CtClass[] { cp.get(javaLangObject) };
        CtClass retType = CtClass.booleanType;
        int paramVar = ca.getMaxLocals();
        jc.recordParams(javaLangObject, params, true, paramVar, withinStatic());
        int retVar = jc.recordReturnType(retType, true);
        jc.recordProceed(new ProceedForInstanceof(index));
        // because $type is not the return type...
        jc.recordType(getType());
        /* Is $_ included in the source code?
             */
        checkResultValue(retType, statement);
        Bytecode bytecode = jc.getBytecode();
        storeStack(params, true, paramVar, bytecode);
        jc.recordLocalVariables(ca, pos);
        bytecode.addConstZero(retType);
        // initialize $_
        bytecode.addStore(retVar, retType);
        jc.compileStmnt(statement);
        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) 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)

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