Search in sources :

Example 16 with NotFoundException

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

the class Executor method paramTypesFromDesc.

private Type[] paramTypesFromDesc(String desc) throws BadBytecode {
    CtClass[] classes = null;
    try {
        classes = Descriptor.getParameterTypes(desc, classPool);
    } catch (NotFoundException e) {
        throw new BadBytecode("Could not find class in descriptor [pos = " + lastPos + "]: " + e.getMessage());
    }
    if (classes == null)
        throw new BadBytecode("Could not obtain parameters for descriptor [pos = " + lastPos + "]: " + desc);
    Type[] types = new Type[classes.length];
    for (int i = 0; i < types.length; i++) types[i] = Type.get(classes[i]);
    return types;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode)

Example 17 with NotFoundException

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

the class Javac method compileMethod.

private CtBehavior compileMethod(Parser p, MethodDecl md) throws CompileError {
    int mod = MemberResolver.getModifiers(md.getModifiers());
    CtClass[] plist = gen.makeParamList(md);
    CtClass[] tlist = gen.makeThrowsList(md);
    recordParams(plist, Modifier.isStatic(mod));
    md = p.parseMethod2(stable, md);
    try {
        if (md.isConstructor()) {
            CtConstructor cons = new CtConstructor(plist, gen.getThisClass());
            cons.setModifiers(mod);
            md.accept(gen);
            cons.getMethodInfo().setCodeAttribute(bytecode.toCodeAttribute());
            cons.setExceptionTypes(tlist);
            return cons;
        } else {
            Declarator r = md.getReturn();
            CtClass rtype = gen.resolver.lookupClass(r);
            recordReturnType(rtype, false);
            CtMethod method = new CtMethod(rtype, r.getVariable().get(), plist, gen.getThisClass());
            method.setModifiers(mod);
            gen.setThisMethod(method);
            md.accept(gen);
            if (md.getBody() != null)
                method.getMethodInfo().setCodeAttribute(bytecode.toCodeAttribute());
            else
                method.setModifiers(mod | Modifier.ABSTRACT);
            method.setExceptionTypes(tlist);
            return method;
        }
    } 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 18 with NotFoundException

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

the class ClassInitPlugin method patch.

@OnClassLoadEvent(classNameRegexp = ".*", events = LoadEvent.REDEFINE)
public static void patch(final CtClass ctClass, final ClassLoader classLoader, final Class<?> originalClass) throws IOException, CannotCompileException, NotFoundException {
    if (isSyntheticClass(originalClass)) {
        return;
    }
    final String className = ctClass.getName();
    try {
        CtMethod origMethod = ctClass.getDeclaredMethod(HOTSWAP_AGENT_CLINIT_METHOD);
        ctClass.removeMethod(origMethod);
    } catch (org.hotswap.agent.javassist.NotFoundException ex) {
    // swallow
    }
    CtConstructor clinit = ctClass.getClassInitializer();
    if (clinit != null) {
        LOGGER.debug("Adding __ha_clinit to class: {}", className);
        CtConstructor haClinit = new CtConstructor(clinit, ctClass, null);
        haClinit.getMethodInfo().setName(HOTSWAP_AGENT_CLINIT_METHOD);
        haClinit.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
        ctClass.addConstructor(haClinit);
        final boolean[] reinitializeStatics = new boolean[] { false };
        haClinit.instrument(new ExprEditor() {

            public void edit(FieldAccess f) throws CannotCompileException {
                try {
                    if (f.isStatic() && f.isWriter()) {
                        Field originalField = null;
                        try {
                            originalField = originalClass.getDeclaredField(f.getFieldName());
                        } catch (NoSuchFieldException e) {
                            LOGGER.debug("New field will be initialized {}", f.getFieldName());
                            reinitializeStatics[0] = true;
                        }
                        if (originalField != null) {
                            // ENUM$VALUES is last in enumeration
                            if (originalClass.isEnum() && "ENUM$VALUES".equals(f.getFieldName())) {
                                if (reinitializeStatics[0]) {
                                    LOGGER.debug("New field will be initialized {}", f.getFieldName());
                                } else {
                                    reinitializeStatics[0] = checkOldEnumValues(ctClass, originalClass);
                                }
                            } else {
                                LOGGER.debug("Skipping old field {}", f.getFieldName());
                                f.replace("{}");
                            }
                        }
                    }
                } catch (Exception e) {
                    LOGGER.error("Patching __ha_clinit method failed.", e);
                }
            }
        });
        if (reinitializeStatics[0]) {
            PluginManager.getInstance().getScheduler().scheduleCommand(new Command() {

                @Override
                public void executeCommand() {
                    try {
                        Class<?> clazz = classLoader.loadClass(className);
                        Method m = clazz.getDeclaredMethod(HOTSWAP_AGENT_CLINIT_METHOD, new Class[] {});
                        if (m != null) {
                            m.invoke(null, new Object[] {});
                        }
                    } catch (Exception e) {
                        LOGGER.error("Error initializing redefined class {}", e, className);
                    } finally {
                        reloadFlag = false;
                    }
                }
            }, // Hack : init should be done after dependant class redefinition. Since the class can
            150);
        // be proxied by syntetic proxy, the class init must be scheduled after proxy redefinition.
        // Currently proxy redefinition (in ProxyPlugin) is scheduled with 100ms delay, therefore
        // the class init must be scheduled after it.
        } else {
            reloadFlag = false;
        }
    }
}
Also used : CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) CtMethod(org.hotswap.agent.javassist.CtMethod) Method(java.lang.reflect.Method) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) IOException(java.io.IOException) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtConstructor(org.hotswap.agent.javassist.CtConstructor) Field(java.lang.reflect.Field) CtField(org.hotswap.agent.javassist.CtField) Command(org.hotswap.agent.command.Command) NotFoundException(org.hotswap.agent.javassist.NotFoundException) ExprEditor(org.hotswap.agent.javassist.expr.ExprEditor) CtClass(org.hotswap.agent.javassist.CtClass) FieldAccess(org.hotswap.agent.javassist.expr.FieldAccess) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 19 with NotFoundException

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

the class Expr method mayThrow.

/**
 * Returns the list of exceptions that the expression may throw. This list
 * includes both the exceptions that the try-catch statements including the
 * expression can catch and the exceptions that the throws declaration
 * allows the method to throw.
 */
public CtClass[] mayThrow() {
    ClassPool pool = thisClass.getClassPool();
    ConstPool cp = thisMethod.getConstPool();
    LinkedList list = new LinkedList();
    try {
        CodeAttribute ca = thisMethod.getCodeAttribute();
        ExceptionTable et = ca.getExceptionTable();
        int pos = currentPos;
        int n = et.size();
        for (int i = 0; i < n; ++i) if (et.startPc(i) <= pos && pos < et.endPc(i)) {
            int t = et.catchType(i);
            if (t > 0)
                try {
                    addClass(list, pool.get(cp.getClassInfo(t)));
                } catch (NotFoundException e) {
                }
        }
    } catch (NullPointerException e) {
    }
    ExceptionsAttribute ea = thisMethod.getExceptionsAttribute();
    if (ea != null) {
        String[] exceptions = ea.getExceptions();
        if (exceptions != null) {
            int n = exceptions.length;
            for (int i = 0; i < n; ++i) try {
                addClass(list, pool.get(exceptions[i]));
            } catch (NotFoundException e) {
            }
        }
    }
    return (CtClass[]) list.toArray(new CtClass[list.size()]);
}
Also used : ConstPool(org.hotswap.agent.javassist.bytecode.ConstPool) CtClass(org.hotswap.agent.javassist.CtClass) ExceptionsAttribute(org.hotswap.agent.javassist.bytecode.ExceptionsAttribute) CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute) ClassPool(org.hotswap.agent.javassist.ClassPool) NotFoundException(org.hotswap.agent.javassist.NotFoundException) ExceptionTable(org.hotswap.agent.javassist.bytecode.ExceptionTable) LinkedList(java.util.LinkedList)

Example 20 with NotFoundException

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

the class TransformCall method matchClass.

private boolean matchClass(String name, ClassPool pool) {
    if (classname.equals(name))
        return true;
    try {
        CtClass clazz = pool.get(name);
        CtClass declClazz = pool.get(classname);
        if (clazz.subtypeOf(declClazz))
            try {
                CtMethod m = clazz.getMethod(methodname, methodDescriptor);
                return m.getDeclaringClass().getName().equals(classname);
            } catch (NotFoundException e) {
                // maybe the original method has been removed.
                return true;
            }
    } catch (NotFoundException e) {
        return false;
    }
    return false;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod)

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