Search in sources :

Example 1 with CtBehavior

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

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);
        else {
            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 : CtBehavior(org.hotswap.agent.javassist.CtBehavior) CtClass(org.hotswap.agent.javassist.CtClass) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode)

Example 2 with CtBehavior

use of org.hotswap.agent.javassist.CtBehavior 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

CtBehavior (org.hotswap.agent.javassist.CtBehavior)2 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)1 CtClass (org.hotswap.agent.javassist.CtClass)1 CtConstructor (org.hotswap.agent.javassist.CtConstructor)1 BadBytecode (org.hotswap.agent.javassist.bytecode.BadBytecode)1 MethodInfo (org.hotswap.agent.javassist.bytecode.MethodInfo)1