Search in sources :

Example 16 with CannotCompileException

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

the class ExportedObject method exportObject.

/**
 * Exports an object.
 * This method produces the bytecode of the proxy class used
 * to access the exported object.  A remote applet can load
 * the proxy class and call a method on the exported object.
 *
 * @param name      the name used for looking the object up.
 * @param obj       the exported object.
 * @return          the object identifier
 *
 * @see javassist.tools.rmi.ObjectImporter#lookupObject(String)
 */
public synchronized int exportObject(String name, Object obj) throws CannotCompileException {
    Class clazz = obj.getClass();
    ExportedObject eo = new ExportedObject();
    eo.object = obj;
    eo.methods = clazz.getMethods();
    exportedObjects.addElement(eo);
    eo.identifier = exportedObjects.size() - 1;
    if (name != null)
        exportedNames.put(name, eo);
    try {
        stubGen.makeProxyClass(clazz);
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    }
    return eo.identifier;
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException)

Example 17 with CannotCompileException

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

the class StackMapTable method removeNew.

/**
 * Undocumented method.  Do not use; internal-use only.
 *
 * <p>This method is for javassist.convert.TransformNew.
 * It is called to update the stack map table when
 * the NEW opcode (and the following DUP) is removed.
 *
 * @param where     the position of the removed NEW opcode.
 */
public void removeNew(int where) throws CannotCompileException {
    try {
        byte[] data = new NewRemover(this.get(), where).doit();
        this.set(data);
    } catch (BadBytecode e) {
        throw new CannotCompileException("bad stack map table", e);
    }
}
Also used : CannotCompileException(org.hotswap.agent.javassist.CannotCompileException)

Example 18 with CannotCompileException

use of org.hotswap.agent.javassist.CannotCompileException 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 19 with CannotCompileException

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

the class ExprEditor method doit.

/**
 * Undocumented method.  Do not use; internal-use only.
 */
public boolean doit(CtClass clazz, MethodInfo minfo) throws CannotCompileException {
    CodeAttribute codeAttr = minfo.getCodeAttribute();
    if (codeAttr == null)
        return false;
    CodeIterator iterator = codeAttr.iterator();
    boolean edited = false;
    LoopContext context = new LoopContext(codeAttr.getMaxLocals());
    while (iterator.hasNext()) if (loopBody(iterator, clazz, minfo, context))
        edited = true;
    ExceptionTable et = codeAttr.getExceptionTable();
    int n = et.size();
    for (int i = 0; i < n; ++i) {
        Handler h = new Handler(et, i, iterator, clazz, minfo);
        edit(h);
        if (h.edited()) {
            edited = true;
            context.updateMax(h.locals(), h.stack());
        }
    }
    // so I check the current value of max-locals.
    if (codeAttr.getMaxLocals() < context.maxLocals)
        codeAttr.setMaxLocals(context.maxLocals);
    codeAttr.setMaxStack(codeAttr.getMaxStack() + context.maxStack);
    try {
        if (edited)
            minfo.rebuildStackMapIf6(clazz.getClassPool(), clazz.getClassFile2());
    } catch (BadBytecode b) {
        throw new CannotCompileException(b.getMessage(), b);
    }
    return edited;
}
Also used : CannotCompileException(org.hotswap.agent.javassist.CannotCompileException)

Example 20 with CannotCompileException

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

the class ExprEditor method loopBody.

final boolean loopBody(CodeIterator iterator, CtClass clazz, MethodInfo minfo, LoopContext context) throws CannotCompileException {
    try {
        Expr expr = null;
        int pos = iterator.next();
        int c = iterator.byteAt(pos);
        if (// c < 178
        c < Opcode.GETSTATIC)
            /* skip */
            ;
        else if (c < Opcode.NEWARRAY) {
            // c < 188
            if (c == Opcode.INVOKESTATIC || c == Opcode.INVOKEINTERFACE || c == Opcode.INVOKEVIRTUAL) {
                expr = new MethodCall(pos, iterator, clazz, minfo);
                edit((MethodCall) expr);
            } else if (c == Opcode.GETFIELD || c == Opcode.GETSTATIC || c == Opcode.PUTFIELD || c == Opcode.PUTSTATIC) {
                expr = new FieldAccess(pos, iterator, clazz, minfo, c);
                edit((FieldAccess) expr);
            } else if (c == Opcode.NEW) {
                int index = iterator.u16bitAt(pos + 1);
                context.newList = new NewOp(context.newList, pos, minfo.getConstPool().getClassInfo(index));
            } else if (c == Opcode.INVOKESPECIAL) {
                NewOp newList = context.newList;
                if (newList != null && minfo.getConstPool().isConstructor(newList.type, iterator.u16bitAt(pos + 1)) > 0) {
                    expr = new NewExpr(pos, iterator, clazz, minfo, newList.type, newList.pos);
                    edit((NewExpr) expr);
                    context.newList = newList.next;
                } else {
                    MethodCall mcall = new MethodCall(pos, iterator, clazz, minfo);
                    if (mcall.getMethodName().equals(MethodInfo.nameInit)) {
                        ConstructorCall ccall = new ConstructorCall(pos, iterator, clazz, minfo);
                        expr = ccall;
                        edit(ccall);
                    } else {
                        expr = mcall;
                        edit(mcall);
                    }
                }
            }
        } else {
            // c >= 188
            if (c == Opcode.NEWARRAY || c == Opcode.ANEWARRAY || c == Opcode.MULTIANEWARRAY) {
                expr = new NewArray(pos, iterator, clazz, minfo, c);
                edit((NewArray) expr);
            } else if (c == Opcode.INSTANCEOF) {
                expr = new Instanceof(pos, iterator, clazz, minfo);
                edit((Instanceof) expr);
            } else if (c == Opcode.CHECKCAST) {
                expr = new Cast(pos, iterator, clazz, minfo);
                edit((Cast) expr);
            }
        }
        if (expr != null && expr.edited()) {
            context.updateMax(expr.locals(), expr.stack());
            return true;
        } else
            return false;
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CannotCompileException(org.hotswap.agent.javassist.CannotCompileException)

Aggregations

CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)23 CtMethod (org.hotswap.agent.javassist.CtMethod)11 NotFoundException (org.hotswap.agent.javassist.NotFoundException)9 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)8 CtClass (org.hotswap.agent.javassist.CtClass)7 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)6 CtField (org.hotswap.agent.javassist.CtField)4 MethodCall (org.hotswap.agent.javassist.expr.MethodCall)4 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)2 ClassPool (org.hotswap.agent.javassist.ClassPool)2 CtConstructor (org.hotswap.agent.javassist.CtConstructor)2 LoaderClassPath (org.hotswap.agent.javassist.LoaderClassPath)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ProtectionDomain (java.security.ProtectionDomain)1 LoadEvent (org.hotswap.agent.annotation.LoadEvent)1