Search in sources :

Example 6 with Javac

use of org.hotswap.agent.javassist.compiler.Javac in project HotswapAgent by HotswapProjects.

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 : CompileError(org.hotswap.agent.javassist.compiler.CompileError) Javac(org.hotswap.agent.javassist.compiler.Javac)

Example 7 with Javac

use of org.hotswap.agent.javassist.compiler.Javac in project HotswapAgent by HotswapProjects.

the class FieldInitLink method makeFieldInitializer.

private int makeFieldInitializer(Bytecode code, CtClass[] parameters) throws CannotCompileException, NotFoundException {
    int stacksize = 0;
    Javac jv = new Javac(code, this);
    try {
        jv.recordParams(parameters, false);
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    }
    for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
        CtField f = fi.field;
        if (!Modifier.isStatic(f.getModifiers())) {
            int s = fi.init.compile(f.getType(), f.getName(), code, parameters, jv);
            if (stacksize < s)
                stacksize = s;
        }
    }
    return stacksize;
}
Also used : CompileError(org.hotswap.agent.javassist.compiler.CompileError) Javac(org.hotswap.agent.javassist.compiler.Javac)

Example 8 with Javac

use of org.hotswap.agent.javassist.compiler.Javac in project HotswapAgent by HotswapProjects.

the class FieldInitLink method modifyClassConstructor.

private void modifyClassConstructor(ClassFile cf) throws CannotCompileException, NotFoundException {
    if (fieldInitializers == null)
        return;
    Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
    Javac jv = new Javac(code, this);
    int stacksize = 0;
    boolean doInit = false;
    for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
        CtField f = fi.field;
        if (Modifier.isStatic(f.getModifiers())) {
            doInit = true;
            int s = fi.init.compileIfStatic(f.getType(), f.getName(), code, jv);
            if (stacksize < s)
                stacksize = s;
        }
    }
    if (// need an initializer for static fileds.
    doInit)
        modifyClassConstructor(cf, code, stacksize, 0);
}
Also used : Javac(org.hotswap.agent.javassist.compiler.Javac) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode) Bytecode(org.hotswap.agent.javassist.bytecode.Bytecode)

Aggregations

Javac (org.hotswap.agent.javassist.compiler.Javac)8 CompileError (org.hotswap.agent.javassist.compiler.CompileError)7 BadBytecode (org.hotswap.agent.javassist.bytecode.BadBytecode)1 Bytecode (org.hotswap.agent.javassist.bytecode.Bytecode)1