Search in sources :

Example 1 with NotFoundException

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

the class ResteasyRegistryPlugin method patchFilterDispatcher.

/**
 * @param ctClass
 * @param classPool
 */
@OnClassLoadEvent(classNameRegexp = "org.jboss.resteasy.plugins.server.servlet.FilterDispatcher")
public static void patchFilterDispatcher(CtClass ctClass, ClassPool classPool) {
    try {
        CtMethod init = ctClass.getDeclaredMethod("init");
        init.insertAfter(// 
        "" + // 
        "java.lang.ClassLoader $$cl = Thread.currentThread().getContextClassLoader();" + // 
        "java.lang.Object $$servletContext = servletConfig.getServletContext();" + // 
        PluginManagerInvoker.buildInitializePlugin(ResteasyRegistryPlugin.class, "$$cl") + PluginManagerInvoker.buildCallPluginMethod("$$cl", ResteasyRegistryPlugin.class, "registerContext", "$$servletContext", // 
        "java.lang.Object") + PluginManagerInvoker.buildCallPluginMethod("$$cl", ResteasyRegistryPlugin.class, "registerServletContainerDispatcher", "servletContainerDispatcher", // 
        "java.lang.Object"));
    } catch (NotFoundException | CannotCompileException e) {
        LOGGER.error("Error patching FilterDispatcher", e);
    }
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 2 with NotFoundException

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

the class ResteasyRegistryPlugin method patchServletDispatcher.

@OnClassLoadEvent(classNameRegexp = "org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher")
public static void patchServletDispatcher(CtClass ctClass, ClassPool classPool) {
    try {
        CtMethod init = ctClass.getDeclaredMethod("init");
        init.insertAfter(// 
        "" + // 
        "java.lang.Object $$servletContext = servletConfig.getServletContext();" + // 
        "java.lang.ClassLoader $$cl = Thread.currentThread().getContextClassLoader();" + // 
        PluginManagerInvoker.buildInitializePlugin(ResteasyRegistryPlugin.class, "$$cl") + PluginManagerInvoker.buildCallPluginMethod("$$cl", ResteasyRegistryPlugin.class, "registerContext", "$$servletContext", // 
        "java.lang.Object") + PluginManagerInvoker.buildCallPluginMethod("$$cl", ResteasyRegistryPlugin.class, "registerServletContainerDispatcher", "servletContainerDispatcher", // 
        "java.lang.Object"));
    } catch (NotFoundException | CannotCompileException e) {
        LOGGER.error("Error patching HttpServletDispatcher", e);
    }
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 3 with NotFoundException

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

the class CtClassJavaProxyGenerator method addProxyMethod.

/**
 * Add another method to be proxied, either by creating a new ProxyMethod object or augmenting an old one for a
 * duplicate method.
 *
 * "fromClass" indicates the proxy interface that the method was found through, which may be different from (a
 * subinterface of) the method's "declaring class". Note that the first Method object passed for a given name and
 * descriptor identifies the Method object (and thus the declaring class) that will be passed to the invocation
 * handler's "invoke" method for a given set of duplicate methods.
 */
private void addProxyMethod(CtMethod m, CtClass fromClass) {
    String name = m.getName();
    CtClass[] parameterTypes;
    CtClass returnType;
    CtClass[] exceptionTypes;
    try {
        parameterTypes = m.getParameterTypes();
        returnType = m.getReturnType();
        exceptionTypes = m.getExceptionTypes();
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    String sig = name + getParameterDescriptors(parameterTypes);
    List<ProxyMethod> sigmethods = proxyMethods.get(sig);
    if (sigmethods != null) {
        for (ProxyMethod pm : sigmethods) {
            if (returnType == pm.returnType || returnType.getName().equals(pm.returnType.getName())) {
                /*
					 * Found a match: reduce exception types to the greatest set of exceptions that can thrown
					 * compatibly with the throws clauses of both overridden methods.
					 */
                List<CtClass> legalExceptions = new ArrayList<>();
                collectCompatibleTypes(exceptionTypes, pm.exceptionTypes, legalExceptions);
                collectCompatibleTypes(pm.exceptionTypes, exceptionTypes, legalExceptions);
                pm.exceptionTypes = new CtClass[legalExceptions.size()];
                pm.exceptionTypes = legalExceptions.toArray(pm.exceptionTypes);
                return;
            }
        }
    } else {
        sigmethods = new ArrayList<>(3);
        proxyMethods.put(sig, sigmethods);
    }
    sigmethods.add(new ProxyMethod(name, parameterTypes, returnType, exceptionTypes, fromClass));
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ArrayList(java.util.ArrayList) NotFoundException(org.hotswap.agent.javassist.NotFoundException)

Example 4 with NotFoundException

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

the class CtClassJavaProxyGenerator method getFriendlyMethodSignature.

/**
 * Returns a human-readable string representing the signature of a method with the given name and parameter types.
 */
private static String getFriendlyMethodSignature(String name, CtClass[] parameterTypes) {
    StringBuilder sig = new StringBuilder(name);
    sig.append('(');
    for (int i = 0; i < parameterTypes.length; i++) {
        if (i > 0) {
            sig.append(',');
        }
        CtClass parameterType = parameterTypes[i];
        int dimensions = 0;
        while (parameterType.isArray()) {
            try {
                parameterType = parameterType.getComponentType();
            } catch (NotFoundException e) {
                throw new RuntimeException(e);
            }
            dimensions++;
        }
        sig.append(parameterType.getName());
        while (dimensions-- > 0) {
            sig.append("[]");
        }
    }
    sig.append(')');
    return sig.toString();
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException)

Example 5 with NotFoundException

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

the class DeltaSpikeProxyTransformer method instrumentTryToLoadClassForName.

private static void instrumentTryToLoadClassForName(CtClass ctClass, String methodName) throws CannotCompileException {
    try {
        CtMethod getProxyClassMethod = ctClass.getDeclaredMethod(methodName);
        getProxyClassMethod.instrument(new ExprEditor() {

            public void edit(MethodCall m) throws CannotCompileException {
                if (m.getClassName().equals("org.apache.deltaspike.core.util.ClassUtils") && m.getMethodName().equals("tryToLoadClassForName"))
                    m.replace("{ $_ = org.hotswap.agent.plugin.deltaspike.proxy.ProxyClassLoadingDelegate.tryToLoadClassForName($$); }");
            }
        });
    } catch (NotFoundException e) {
        LOGGER.debug("Method '{}' not found in '{}'.", methodName, ctClass.getName());
    }
}
Also used : ExprEditor(org.hotswap.agent.javassist.expr.ExprEditor) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) MethodCall(org.hotswap.agent.javassist.expr.MethodCall) 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