Search in sources :

Example 1 with Bytecode

use of org.hotswap.agent.javassist.bytecode.Bytecode in project HotswapAgent by HotswapProjects.

the class FieldInitLink method modifyConstructors.

private void modifyConstructors(ClassFile cf) throws CannotCompileException, NotFoundException {
    if (fieldInitializers == null)
        return;
    ConstPool cp = cf.getConstPool();
    List list = cf.getMethods();
    int n = list.size();
    for (int i = 0; i < n; ++i) {
        MethodInfo minfo = (MethodInfo) list.get(i);
        if (minfo.isConstructor()) {
            CodeAttribute codeAttr = minfo.getCodeAttribute();
            if (codeAttr != null)
                try {
                    Bytecode init = new Bytecode(cp, 0, codeAttr.getMaxLocals());
                    CtClass[] params = Descriptor.getParameterTypes(minfo.getDescriptor(), classPool);
                    int stacksize = makeFieldInitializer(init, params);
                    insertAuxInitializer(codeAttr, init, stacksize);
                    minfo.rebuildStackMapIf6(classPool, cf);
                } catch (BadBytecode e) {
                    throw new CannotCompileException(e);
                }
        }
    }
}
Also used : ConstPool(org.hotswap.agent.javassist.bytecode.ConstPool) CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute) ArrayList(java.util.ArrayList) List(java.util.List) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode) Bytecode(org.hotswap.agent.javassist.bytecode.Bytecode) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode)

Example 2 with Bytecode

use of org.hotswap.agent.javassist.bytecode.Bytecode in project HotswapAgent by HotswapProjects.

the class Javac method recordStaticProceed.

/**
 * Prepares to use $proceed() representing a static method.
 * If the return type of $proceed() is void, null is pushed on the
 * stack.
 *
 * @param targetClass    the fully-qualified dot-separated name
 *				of the class declaring the method.
 * @param method         the method name.
 */
public void recordStaticProceed(String targetClass, String method) throws CompileError {
    final String c = targetClass;
    final String m = method;
    ProceedHandler h = new ProceedHandler() {

        public void doit(JvstCodeGen gen, Bytecode b, ASTList args) throws CompileError {
            Expr expr = Expr.make(TokenId.MEMBER, new Symbol(c), new Member(m));
            expr = CallExpr.makeCall(expr, args);
            gen.compileExpr(expr);
            gen.addNullIfVoid();
        }

        public void setReturnType(JvstTypeChecker check, ASTList args) throws CompileError {
            Expr expr = Expr.make(TokenId.MEMBER, new Symbol(c), new Member(m));
            expr = CallExpr.makeCall(expr, args);
            expr.accept(check);
            check.addNullIfVoid();
        }
    };
    gen.setProceedHandler(h, proceedName);
}
Also used : BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode) Bytecode(org.hotswap.agent.javassist.bytecode.Bytecode) CtMember(org.hotswap.agent.javassist.CtMember)

Example 3 with Bytecode

use of org.hotswap.agent.javassist.bytecode.Bytecode 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)

Example 4 with Bytecode

use of org.hotswap.agent.javassist.bytecode.Bytecode in project HotswapAgent by HotswapProjects.

the class FieldInitLink method makeClassInitializer.

public CtConstructor makeClassInitializer() throws CannotCompileException {
    CtConstructor clinit = getClassInitializer();
    if (clinit != null)
        return clinit;
    checkModify();
    ClassFile cf = getClassFile2();
    Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
    modifyClassConstructor(cf, code, 0, 0);
    return getClassInitializer();
}
Also used : ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode) Bytecode(org.hotswap.agent.javassist.bytecode.Bytecode)

Aggregations

BadBytecode (org.hotswap.agent.javassist.bytecode.BadBytecode)4 Bytecode (org.hotswap.agent.javassist.bytecode.Bytecode)4 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CtMember (org.hotswap.agent.javassist.CtMember)1 ClassFile (org.hotswap.agent.javassist.bytecode.ClassFile)1 CodeAttribute (org.hotswap.agent.javassist.bytecode.CodeAttribute)1 ConstPool (org.hotswap.agent.javassist.bytecode.ConstPool)1 MethodInfo (org.hotswap.agent.javassist.bytecode.MethodInfo)1 Javac (org.hotswap.agent.javassist.compiler.Javac)1