Search in sources :

Example 61 with CtClass

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

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

the class Descriptor method toCtClass.

private static int toCtClass(ClassPool cp, String desc, int i, CtClass[] args, int n) throws NotFoundException {
    int i2;
    String name;
    int arrayDim = 0;
    char c = desc.charAt(i);
    while (c == '[') {
        ++arrayDim;
        c = desc.charAt(++i);
    }
    if (c == 'L') {
        i2 = desc.indexOf(';', ++i);
        name = desc.substring(i, i2++).replace('/', '.');
    } else {
        CtClass type = toPrimitiveClass(c);
        if (type == null)
            // error
            return -1;
        i2 = i + 1;
        if (arrayDim == 0) {
            args[n] = type;
            // neither an array type or a class type
            return i2;
        } else
            name = type.getName();
    }
    if (arrayDim > 0) {
        StringBuffer sbuf = new StringBuffer(name);
        while (arrayDim-- > 0) sbuf.append("[]");
        name = sbuf.toString();
    }
    args[n] = cp.get(name);
    return i2;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass)

Example 63 with CtClass

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

the class TypeChecker method atNewExpr.

public void atNewExpr(NewExpr expr) throws CompileError {
    if (expr.isArray())
        atNewArrayExpr(expr);
    else {
        CtClass clazz = resolver.lookupClassByName(expr.getClassName());
        String cname = clazz.getName();
        ASTList args = expr.getArguments();
        atMethodCallCore(clazz, MethodInfo.nameInit, args);
        exprType = CLASS;
        arrayDim = 0;
        className = MemberResolver.javaToJvmName(cname);
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass)

Example 64 with CtClass

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

the class TypeChecker method atCallExpr.

public void atCallExpr(CallExpr expr) throws CompileError {
    String mname = null;
    CtClass targetClass = null;
    ASTree method = expr.oprand1();
    ASTList args = (ASTList) expr.oprand2();
    if (method instanceof Member) {
        mname = ((Member) method).get();
        targetClass = thisClass;
    } else if (method instanceof Keyword) {
        // constructor
        // <init>
        mname = MethodInfo.nameInit;
        if (((Keyword) method).get() == SUPER)
            targetClass = MemberResolver.getSuperclass(thisClass);
        else
            targetClass = thisClass;
    } else if (method instanceof Expr) {
        Expr e = (Expr) method;
        mname = ((Symbol) e.oprand2()).get();
        int op = e.getOperator();
        if (// static method
        op == MEMBER)
            targetClass = resolver.lookupClass(((Symbol) e.oprand1()).get(), false);
        else if (op == '.') {
            ASTree target = e.oprand1();
            String classFollowedByDotSuper = isDotSuper(target);
            if (classFollowedByDotSuper != null)
                targetClass = MemberResolver.getSuperInterface(thisClass, classFollowedByDotSuper);
            else {
                try {
                    target.accept(this);
                } catch (NoFieldException nfe) {
                    if (nfe.getExpr() != target)
                        throw nfe;
                    // it should be a static method.
                    exprType = CLASS;
                    arrayDim = 0;
                    // JVM-internal
                    className = nfe.getField();
                    e.setOperator(MEMBER);
                    e.setOprand1(new Symbol(MemberResolver.jvmToJavaName(className)));
                }
                if (arrayDim > 0)
                    targetClass = resolver.lookupClass(javaLangObject, true);
                else if (exprType == CLASS)
                    /* && arrayDim == 0 */
                    targetClass = resolver.lookupClassByJvmName(className);
                else
                    badMethod();
            }
        } else
            badMethod();
    } else
        fatal();
    MemberResolver.Method minfo = atMethodCallCore(targetClass, mname, args);
    expr.setMethod(minfo);
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass)

Example 65 with CtClass

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

the class TransformAccessArrayField method getTopType.

private String getTopType(int pos) throws BadBytecode {
    Frame frame = getFrame(pos);
    if (frame == null)
        return null;
    CtClass clazz = frame.peek().getCtClass();
    return clazz != null ? Descriptor.toJvmName(clazz) : null;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) Frame(org.hotswap.agent.javassist.bytecode.analysis.Frame)

Aggregations

CtClass (org.hotswap.agent.javassist.CtClass)69 NotFoundException (org.hotswap.agent.javassist.NotFoundException)20 CtMethod (org.hotswap.agent.javassist.CtMethod)18 ClassPool (org.hotswap.agent.javassist.ClassPool)14 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)13 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)8 CtConstructor (org.hotswap.agent.javassist.CtConstructor)8 CtField (org.hotswap.agent.javassist.CtField)8 HashMap (java.util.HashMap)6 LoaderClassPath (org.hotswap.agent.javassist.LoaderClassPath)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Test (org.junit.Test)5 Iterator (java.util.Iterator)4 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)4 Method (java.lang.reflect.Method)3 IdentityHashMap (java.util.IdentityHashMap)3 BadBytecode (org.hotswap.agent.javassist.bytecode.BadBytecode)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2