Search in sources :

Example 46 with BadBytecode

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

the class CtBehavior method copy.

/**
 * @param isCons        true if this is a constructor.
 */
void copy(CtBehavior src, boolean isCons, ClassMap map) throws CannotCompileException {
    CtClass declaring = declaringClass;
    MethodInfo srcInfo = src.methodInfo;
    CtClass srcClass = src.getDeclaringClass();
    ConstPool cp = declaring.getClassFile2().getConstPool();
    map = new ClassMap(map);
    map.put(srcClass.getName(), declaring.getName());
    try {
        boolean patch = false;
        CtClass srcSuper = srcClass.getSuperclass();
        CtClass destSuper = declaring.getSuperclass();
        String destSuperName = null;
        if (srcSuper != null && destSuper != null) {
            String srcSuperName = srcSuper.getName();
            destSuperName = destSuper.getName();
            if (!srcSuperName.equals(destSuperName))
                if (srcSuperName.equals(CtClass.javaLangObject))
                    patch = true;
                else
                    map.putIfNone(srcSuperName, destSuperName);
        }
        // a stack map table is copied from srcInfo.
        methodInfo = new MethodInfo(cp, srcInfo.getName(), srcInfo, map);
        if (isCons && patch)
            methodInfo.setSuperclass(destSuperName);
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : ConstPool(javassist.bytecode.ConstPool) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode)

Example 47 with BadBytecode

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

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

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

the class TypedBlock method initFirstBlock.

/**
 * Initializes the first block by the given method descriptor.
 *
 * @param block             the first basic block that this method initializes.
 * @param className         a dot-separated fully qualified class name.
 *                          For example, <code>javassist.bytecode.stackmap.BasicBlock</code>.
 * @param methodDesc        method descriptor.
 * @param isStatic          true if the method is a static method.
 * @param isConstructor     true if the method is a constructor.
 */
void initFirstBlock(int maxStack, int maxLocals, String className, String methodDesc, boolean isStatic, boolean isConstructor) throws BadBytecode {
    if (methodDesc.charAt(0) != '(')
        throw new BadBytecode("no method descriptor: " + methodDesc);
    stackTop = 0;
    stackTypes = TypeData.make(maxStack);
    TypeData[] locals = TypeData.make(maxLocals);
    if (isConstructor)
        locals[0] = new TypeData.UninitThis(className);
    else if (!isStatic)
        locals[0] = new TypeData.ClassName(className);
    int n = isStatic ? -1 : 0;
    int i = 1;
    try {
        while ((i = descToTag(methodDesc, i, ++n, locals)) > 0) if (locals[n].is2WordType())
            locals[++n] = TypeTag.TOP;
    } catch (StringIndexOutOfBoundsException e) {
        throw new BadBytecode("bad method descriptor: " + methodDesc);
    }
    numLocals = n;
    localsTypes = locals;
}
Also used : BadBytecode(javassist.bytecode.BadBytecode)

Example 49 with BadBytecode

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

the class Javac method compile.

/**
 * Compiles a method, constructor, or field declaration
 * to a class.
 * A field declaration can declare only one field.
 *
 * <p>In a method or constructor body, $0, $1, ... and $_
 * are not available.
 *
 * @return          a <code>CtMethod</code>, <code>CtConstructor</code>,
 *                  or <code>CtField</code> object.
 * @see #recordProceed(String,String)
 */
public CtMember compile(String src) throws CompileError {
    Parser p = new Parser(new Lex(src));
    ASTList mem = p.parseMember1(stable);
    try {
        if (mem instanceof FieldDecl)
            return compileField((FieldDecl) mem);
        CtBehavior cb = compileMethod(p, (MethodDecl) mem);
        CtClass decl = cb.getDeclaringClass();
        cb.getMethodInfo2().rebuildStackMapIf6(decl.getClassPool(), decl.getClassFile2());
        return cb;
    } catch (BadBytecode bb) {
        throw new CompileError(bb.getMessage());
    } catch (CannotCompileException e) {
        throw new CompileError(e.getMessage());
    }
}
Also used : FieldDecl(javassist.compiler.ast.FieldDecl) CtBehavior(javassist.CtBehavior) CtClass(javassist.CtClass) ASTList(javassist.compiler.ast.ASTList) CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode)

Example 50 with BadBytecode

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

the class MapMaker method make2.

/**
 * Computes the stack map table for J2ME.
 * It returns null if the given method does not have to have a
 * stack map table or it includes JSR.
 */
public static StackMap make2(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.toStackMap2(minfo.getConstPool(), blocks);
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) 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