Search in sources :

Example 26 with NotFoundException

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

the class Type method getComponent.

/**
 * Returns the array component if this type is an array. If the type
 * is not an array null is returned.
 *
 * @return the array component if an array, otherwise null
 */
public Type getComponent() {
    if (this.clazz == null || !this.clazz.isArray())
        return null;
    CtClass component;
    try {
        component = this.clazz.getComponentType();
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    Type type = (Type) prims.get(component);
    return (type != null) ? type : new Type(component);
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException)

Example 27 with NotFoundException

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

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

the class Javac method compileBody.

/**
 * Compiles a method (or constructor) body.
 *
 * @param src	a single statement or a block.
 *              If null, this method produces a body returning zero or null.
 */
public Bytecode compileBody(CtBehavior method, String src) throws CompileError {
    try {
        int mod = method.getModifiers();
        recordParams(method.getParameterTypes(), Modifier.isStatic(mod));
        CtClass rtype;
        if (method instanceof CtMethod) {
            gen.setThisMethod((CtMethod) method);
            rtype = ((CtMethod) method).getReturnType();
        } else
            rtype = CtClass.voidType;
        recordReturnType(rtype, false);
        boolean isVoid = rtype == CtClass.voidType;
        if (src == null)
            makeDefaultBody(bytecode, rtype);
        else {
            Parser p = new Parser(new Lex(src));
            SymbolTable stb = new SymbolTable(stable);
            Stmnt s = p.parseStatement(stb);
            if (p.hasMore())
                throw new CompileError("the method/constructor body must be surrounded by {}");
            boolean callSuper = false;
            if (method instanceof CtConstructor)
                callSuper = !((CtConstructor) method).isClassInitializer();
            gen.atMethodBody(s, callSuper, isVoid);
        }
        return bytecode;
    } catch (NotFoundException e) {
        throw new CompileError(e.toString());
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod) CtConstructor(org.hotswap.agent.javassist.CtConstructor)

Example 29 with NotFoundException

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

the class TypeChecker method fieldAccess.

/* if EXPR is to access a static field, fieldAccess() translates EXPR
     * into an expression using '#' (MEMBER).  For example, it translates
     * java.lang.Integer.TYPE into java.lang.Integer#TYPE.  This translation
     * speeds up type resolution by MemberCodeGen.
     */
protected CtField fieldAccess(ASTree expr) throws CompileError {
    if (expr instanceof Member) {
        Member mem = (Member) expr;
        String name = mem.get();
        try {
            CtField f = thisClass.getField(name);
            if (Modifier.isStatic(f.getModifiers()))
                mem.setField(f);
            return f;
        } catch (NotFoundException e) {
            // EXPR might be part of a static member access?
            throw new NoFieldException(name, expr);
        }
    } else if (expr instanceof Expr) {
        Expr e = (Expr) expr;
        int op = e.getOperator();
        if (op == MEMBER) {
            Member mem = (Member) e.oprand2();
            CtField f = resolver.lookupField(((Symbol) e.oprand1()).get(), mem);
            mem.setField(f);
            return f;
        } else if (op == '.') {
            try {
                e.oprand1().accept(this);
            } catch (NoFieldException nfe) {
                if (nfe.getExpr() != e.oprand1())
                    throw nfe;
                /* EXPR should be a static field.
                     * If EXPR might be part of a qualified class name,
                     * lookupFieldByJvmName2() throws NoFieldException.
                     */
                return fieldAccess2(e, nfe.getField());
            }
            CompileError err = null;
            try {
                if (exprType == CLASS && arrayDim == 0)
                    return resolver.lookupFieldByJvmName(className, (Symbol) e.oprand2());
            } catch (CompileError ce) {
                err = ce;
            }
            /* If a filed name is the same name as a package's,
                 * a static member of a class in that package is not
                 * visible.  For example,
                 *
                 * class Foo {
                 *   int javassist;
                 * }
                 *
                 * It is impossible to add the following method:
                 *
                 * String m() { return javassist.CtClass.intType.toString(); }
                 *
                 * because javassist is a field name.  However, this is
                 * often inconvenient, this compiler allows it.  The following
                 * code is for that.
                 */
            ASTree oprnd1 = e.oprand1();
            if (oprnd1 instanceof Symbol)
                return fieldAccess2(e, ((Symbol) oprnd1).get());
            if (err != null)
                throw err;
        }
    }
    throw new CompileError("bad filed access");
}
Also used : CtField(org.hotswap.agent.javassist.CtField) NotFoundException(org.hotswap.agent.javassist.NotFoundException)

Example 30 with NotFoundException

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

the class JettyPlugin method patchWebXmlConfiguration.

/**
 * Plugin initialization step needs to be fine tuned. It can be intialized only AFTER the classloader
 * already knows about hotswap-agent.properties file (i.e. after webapp basic path is added to the classloader),
 * but BEFORE first servlet is initialized.
 *
 * WebXmlConfiguration seems to be good place which should work in most setups. The plugin is intialized before
 * web.xml file is processed - basic paths should be known, but nothing is processed yet.
 *
 * Application classloader is processed during plugin initialization. It means that other plugins triggered
 * on plugin init should fire as well - for jetty is important core watchResources plugin, which will handle
 * extraClassPath and watchResources configuration properties (jetty fortunately depends only on basic
 * URLClassLoader behaviour which is handled by that plugin).
 */
@OnClassLoadEvent(classNameRegexp = "org.eclipse.jetty.webapp.WebXmlConfiguration")
public static void patchWebXmlConfiguration(CtClass ctClass) throws NotFoundException, CannotCompileException, ClassNotFoundException {
    try {
        // after application context initialized, but before processing started
        CtMethod doStart = ctClass.getDeclaredMethod("configure");
        // init the plugin
        String src = PluginManagerInvoker.buildInitializePlugin(JettyPlugin.class, "context.getClassLoader()");
        src += PluginManagerInvoker.buildCallPluginMethod("context.getClassLoader()", JettyPlugin.class, "init", "context", "java.lang.Object");
        doStart.insertBefore(src);
    } catch (NotFoundException e) {
        LOGGER.warning("org.eclipse.jetty.webapp.WebAppContext does not contain startContext method. Jetty plugin will be disabled.\n" + "*** This is Ok, Jetty plugin handles only special properties ***");
        return;
    }
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Aggregations

NotFoundException (org.hotswap.agent.javassist.NotFoundException)33 CtClass (org.hotswap.agent.javassist.CtClass)19 CtMethod (org.hotswap.agent.javassist.CtMethod)15 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)8 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)8 CtField (org.hotswap.agent.javassist.CtField)7 CtConstructor (org.hotswap.agent.javassist.CtConstructor)5 ClassPool (org.hotswap.agent.javassist.ClassPool)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 BadBytecode (org.hotswap.agent.javassist.bytecode.BadBytecode)2 ConstPool (org.hotswap.agent.javassist.bytecode.ConstPool)2 ExceptionTable (org.hotswap.agent.javassist.bytecode.ExceptionTable)2 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1