Search in sources :

Example 21 with BadBytecode

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

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

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

the class Analyzer method mergeRet.

private void mergeRet(IntQueue queue, CodeIterator iter, int pos, Frame frame, Subroutine subroutine) throws BadBytecode {
    if (subroutine == null)
        throw new BadBytecode("Ret on no subroutine! [pos = " + pos + "]");
    for (int caller : subroutine.callers()) {
        int returnLoc = getNext(iter, caller, pos);
        boolean changed = false;
        Frame old = frames[returnLoc];
        if (old == null) {
            old = frames[returnLoc] = frame.copyStack();
            changed = true;
        } else {
            changed = old.mergeStack(frame);
        }
        for (int index : subroutine.accessed()) {
            Type oldType = old.getLocal(index);
            Type newType = frame.getLocal(index);
            if (oldType != newType) {
                old.setLocal(index, newType);
                changed = true;
            }
        }
        if (!old.isRetMerged()) {
            old.setRetMerged(true);
            changed = true;
        }
        if (changed && old.isJsrMerged())
            queue.add(returnLoc);
    }
}
Also used : BadBytecode(javassist.bytecode.BadBytecode)

Example 23 with BadBytecode

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

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

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

the class MapMaker method make.

/*
    public static void main(String[] args) throws Exception {
        boolean useMain2 = args[0].equals("0");
        if (useMain2 && args.length > 1) {
            main2(args);
            return;
        }

        for (int i = 0; i < args.length; i++)
            main1(args[i]);
    }

    public static void main1(String className) throws Exception {
        ClassPool cp = ClassPool.getDefault();
        //javassist.CtClass cc = cp.get(className);
        javassist.CtClass cc = cp.makeClass(new java.io.FileInputStream(className));
        System.out.println(className);
        ClassFile cf = cc.getClassFile();
        java.util.List minfos = cf.getMethods();
        for (int i = 0; i < minfos.size(); i++) {
            MethodInfo minfo = (MethodInfo)minfos.get(i);
            CodeAttribute ca = minfo.getCodeAttribute();
            if (ca != null)
                ca.setAttribute(make(cp, minfo));
        }

        cc.writeFile("tmp");
    }

    public static void main2(String[] args) throws Exception {
        ClassPool cp = ClassPool.getDefault();
        //javassist.CtClass cc = cp.get(args[1]);
        javassist.CtClass cc = cp.makeClass(new java.io.FileInputStream(args[1]));
        MethodInfo minfo;
        if (args[2].equals("_init_"))
            minfo = cc.getDeclaredConstructors()[0].getMethodInfo();
            // minfo = cc.getClassInitializer().getMethodInfo();
        else
            minfo = cc.getDeclaredMethod(args[2]).getMethodInfo();

        CodeAttribute ca = minfo.getCodeAttribute();
        if (ca == null) {
            System.out.println("abstarct method");
            return;
        }

        TypedBlock[] blocks = TypedBlock.makeBlocks(minfo, ca, false);
        MapMaker mm = new MapMaker(cp, minfo, ca);
        mm.make(blocks, ca.getCode());
        for (int i = 0; i < blocks.length; i++)
            System.out.println(blocks[i]);
    }
    */
/**
 * Computes the stack map table of the given method and returns it.
 * It returns null if the given method does not have to have a
 * stack map table or it includes JSR.
 */
public static StackMapTable make(ClassPool classes, MethodInfo minfo) throws BadBytecode {
    CodeAttribute ca = minfo.getCodeAttribute();
    if (ca == null)
        return null;
    TypedBlock[] blocks;
    try {
        blocks = TypedBlock.makeBlocks(minfo, ca, true);
    } catch (BasicBlock.JsrBytecode e) {
        return null;
    }
    if (blocks == null)
        return null;
    MapMaker mm = new MapMaker(classes, minfo, ca);
    try {
        mm.make(blocks, ca.getCode());
    } catch (BadBytecode bb) {
        throw new BadBytecode(minfo, bb);
    }
    return mm.toStackMap(blocks);
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) BadBytecode(javassist.bytecode.BadBytecode)

Example 25 with BadBytecode

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

the class TypeData method aastore.

public static void aastore(TypeData array, TypeData value, ClassPool cp) throws BadBytecode {
    if (array instanceof AbsTypeVar)
        if (!value.isNullType())
            ((AbsTypeVar) array).merge(ArrayType.make(value));
    if (value instanceof AbsTypeVar)
        if (array instanceof AbsTypeVar)
            // should call value.setType() later.
            ArrayElement.make(array);
        else if (array instanceof ClassName) {
            if (!array.isNullType()) {
                String type = ArrayElement.typeName(array.getName());
                value.setType(type, cp);
            }
        } else
            throw new BadBytecode("bad AASTORE: " + array);
}
Also used : 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