Search in sources :

Example 6 with MethodInfo

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

the class FieldInitLink method modifyClassConstructor.

private void modifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize) throws CannotCompileException {
    MethodInfo m = cf.getStaticInitializer();
    if (m == null) {
        code.add(Bytecode.RETURN);
        code.setMaxStack(stacksize);
        code.setMaxLocals(localsize);
        m = new MethodInfo(cf.getConstPool(), "<clinit>", "()V");
        m.setAccessFlags(AccessFlag.STATIC);
        m.setCodeAttribute(code.toCodeAttribute());
        cf.addMethod(m);
        CtMember.Cache cache = hasMemberCache();
        if (cache != null)
            cache.addConstructor(new CtConstructor(m, this));
    } else {
        CodeAttribute codeAttr = m.getCodeAttribute();
        if (codeAttr == null)
            throw new CannotCompileException("empty <clinit>");
        try {
            CodeIterator it = codeAttr.iterator();
            int pos = it.insertEx(code.get());
            it.insert(code.getExceptionTable(), pos);
            int maxstack = codeAttr.getMaxStack();
            if (maxstack < stacksize)
                codeAttr.setMaxStack(stacksize);
            int maxlocals = codeAttr.getMaxLocals();
            if (maxlocals < localsize)
                codeAttr.setMaxLocals(localsize);
        } catch (BadBytecode e) {
            throw new CannotCompileException(e);
        }
    }
    try {
        m.rebuildStackMapIf6(classPool, cf);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute) CodeIterator(org.hotswap.agent.javassist.bytecode.CodeIterator) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode)

Example 7 with MethodInfo

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

the class Reflection method rebuildClassFile.

public void rebuildClassFile(ClassFile cf) throws BadBytecode {
    if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_6)
        return;
    Iterator methods = cf.getMethods().iterator();
    while (methods.hasNext()) {
        MethodInfo mi = (MethodInfo) methods.next();
        mi.rebuildStackMap(classPool);
    }
}
Also used : Iterator(java.util.Iterator) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo)

Example 8 with MethodInfo

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

the class FramePrinter method print.

/**
 * Prints the instructions and the frame states of the given method.
 */
public void print(CtMethod method) {
    stream.println("\n" + getMethodString(method));
    MethodInfo info = method.getMethodInfo2();
    ConstPool pool = info.getConstPool();
    CodeAttribute code = info.getCodeAttribute();
    if (code == null)
        return;
    Frame[] frames;
    try {
        frames = (new Analyzer()).analyze(method.getDeclaringClass(), info);
    } catch (BadBytecode e) {
        throw new RuntimeException(e);
    }
    int spacing = String.valueOf(code.getCodeLength()).length();
    CodeIterator iterator = code.iterator();
    while (iterator.hasNext()) {
        int pos;
        try {
            pos = iterator.next();
        } catch (BadBytecode e) {
            throw new RuntimeException(e);
        }
        stream.println(pos + ": " + InstructionPrinter.instructionString(iterator, pos, pool));
        addSpacing(spacing + 3);
        Frame frame = frames[pos];
        if (frame == null) {
            stream.println("--DEAD CODE--");
            continue;
        }
        printStack(frame);
        addSpacing(spacing + 3);
        printLocals(frame);
    }
}
Also used : ConstPool(org.hotswap.agent.javassist.bytecode.ConstPool) CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute) CodeIterator(org.hotswap.agent.javassist.bytecode.CodeIterator) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode)

Example 9 with MethodInfo

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

the class AnnotationImpl method getDefault.

private Object getDefault(String name, Method method) throws ClassNotFoundException, RuntimeException {
    String classname = annotation.getTypeName();
    if (pool != null) {
        try {
            CtClass cc = pool.get(classname);
            ClassFile cf = cc.getClassFile2();
            MethodInfo minfo = cf.getMethod(name);
            if (minfo != null) {
                AnnotationDefaultAttribute ainfo = (AnnotationDefaultAttribute) minfo.getAttribute(AnnotationDefaultAttribute.tag);
                if (ainfo != null) {
                    MemberValue mv = ainfo.getDefaultValue();
                    return mv.getValue(classLoader, pool, method);
                }
            }
        } catch (NotFoundException e) {
            throw new RuntimeException("cannot find a class file: " + classname);
        }
    }
    throw new RuntimeException("no default value: " + classname + "." + name + "()");
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) AnnotationDefaultAttribute(org.hotswap.agent.javassist.bytecode.AnnotationDefaultAttribute) NotFoundException(org.hotswap.agent.javassist.NotFoundException) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo)

Example 10 with MethodInfo

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

the class Expr method where.

/**
 * Returns the constructor or method containing the expression.
 */
public CtBehavior where() {
    MethodInfo mi = thisMethod;
    CtBehavior[] cb = thisClass.getDeclaredBehaviors();
    for (int i = cb.length - 1; i >= 0; --i) if (cb[i].getMethodInfo2() == mi)
        return cb[i];
    CtConstructor init = thisClass.getClassInitializer();
    if (init != null && init.getMethodInfo2() == mi)
        return init;
    /* getDeclaredBehaviors() returns a list of methods/constructors.
         * Although the list is cached in a CtClass object, it might be
         * recreated for some reason.  Thus, the member name and the signature
         * must be also checked.
         */
    for (int i = cb.length - 1; i >= 0; --i) {
        if (thisMethod.getName().equals(cb[i].getMethodInfo2().getName()) && thisMethod.getDescriptor().equals(cb[i].getMethodInfo2().getDescriptor())) {
            return cb[i];
        }
    }
    throw new RuntimeException("fatal: not found");
}
Also used : CtBehavior(org.hotswap.agent.javassist.CtBehavior) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo) CtConstructor(org.hotswap.agent.javassist.CtConstructor)

Aggregations

MethodInfo (org.hotswap.agent.javassist.bytecode.MethodInfo)12 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ClassFile (org.hotswap.agent.javassist.bytecode.ClassFile)5 BadBytecode (org.hotswap.agent.javassist.bytecode.BadBytecode)3 CodeAttribute (org.hotswap.agent.javassist.bytecode.CodeAttribute)3 ConstPool (org.hotswap.agent.javassist.bytecode.ConstPool)3 CodeIterator (org.hotswap.agent.javassist.bytecode.CodeIterator)2 Iterator (java.util.Iterator)1 CtBehavior (org.hotswap.agent.javassist.CtBehavior)1 CtClass (org.hotswap.agent.javassist.CtClass)1 CtConstructor (org.hotswap.agent.javassist.CtConstructor)1 NotFoundException (org.hotswap.agent.javassist.NotFoundException)1 AnnotationDefaultAttribute (org.hotswap.agent.javassist.bytecode.AnnotationDefaultAttribute)1 Bytecode (org.hotswap.agent.javassist.bytecode.Bytecode)1 FieldInfo (org.hotswap.agent.javassist.bytecode.FieldInfo)1