Search in sources :

Example 21 with DPMethod

use of com.creditease.uav.monitorframework.dproxy.bytecode.DPMethod in project uavstack by uavorg.

the class DynamicProxyInstaller method installProxy.

/**
 * install the proxy code
 *
 * @param className
 *            the target class to be hacked
 * @param importPackages
 *            sometimes need import some new class package for the code
 * @param p
 *            the processor to finsih hacking
 */
public void installProxy(String className, String[] importPackages, DynamicProxyProcessor p, boolean toSystemClassloader) {
    /**
     * source code:
     *
     * try {
     *
     * CtClass cc = pool.get(className);
     *
     * if (importPackages != null) {
     *
     * for (String ip : importPackages) {
     *
     * pool.importPackage(ip); } }
     *
     * CtMethod[] ml = cc.getDeclaredMethods();
     *
     * for (CtMethod m : ml) {
     *
     * try {
     *
     * p.process(m); } catch (Exception e) {
     *
     * this.logger.error( "Do DynamicProxyInstall FAIL for class[" + className + "] method[" + m.getName() + "].",
     * e); } }
     *
     * if (this.cl == null || toSystemClassloader == true) {
     *
     * cc.toClass(); } else {
     *
     * cc.toClass(this.cl, null); }
     *
     * cc.detach(); } catch (NotFoundException e) {
     *
     * // ignore // this.logger.warn("Do DynamicProxyInstall FAIL for class[" + className + "].", e); } catch
     * (CannotCompileException e) {
     *
     * this.logger.warn("Do DynamicProxyInstall FAIL for class[" + className + "] as Can't Compile.", e); }
     */
    Object cc = null;
    try {
        cc = ReflectionHelper.invoke("javassist.ClassPool", pool, "get", new Class[] { String.class }, new Object[] { className }, mofExtClassLoader);
    } catch (RuntimeException e) {
        // ignore;
        return;
    }
    try {
        if (importPackages != null) {
            for (String ip : importPackages) {
                ReflectionHelper.invoke("javassist.ClassPool", pool, "importPackage", new Class[] { String.class }, new Object[] { ip }, mofExtClassLoader);
            }
        }
        Object[] methodArray = (Object[]) ReflectionHelper.invoke("javassist.CtClass", cc, "getDeclaredMethods", null, null, mofExtClassLoader);
        for (Object method : methodArray) {
            DPMethod m = new DPMethod(method);
            try {
                p.process(m);
            } catch (Exception e) {
                this.logger.error("Do DynamicProxyInstall FAIL for class[" + className + "] method[" + m.getName() + "].", e);
            }
        }
        if (this.cl == null || toSystemClassloader == true) {
            ReflectionHelper.invoke("javassist.CtClass", cc, "toClass", null, null, mofExtClassLoader);
        } else {
            ReflectionHelper.invoke("javassist.CtClass", cc, "toClass", new Class[] { ClassLoader.class, ProtectionDomain.class }, new Object[] { this.cl, null }, mofExtClassLoader);
        }
        ReflectionHelper.invoke("javassist.CtClass", cc, "detach", null, null, mofExtClassLoader);
    } catch (RuntimeException e) {
        this.logger.warn("Do DynamicProxyInstall FAIL for class[" + className + "].", e.getCause());
    }
}
Also used : DPMethod(com.creditease.uav.monitorframework.dproxy.bytecode.DPMethod) DPClass(com.creditease.uav.monitorframework.dproxy.bytecode.DPClass)

Aggregations

DPMethod (com.creditease.uav.monitorframework.dproxy.bytecode.DPMethod)21 DynamicProxyProcessor (com.creditease.uav.monitorframework.dproxy.DynamicProxyProcessor)20 JdbcDriverIT (com.creditease.uav.hook.jdbc.interceptors.JdbcDriverIT)4 InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)3 ServerVendor (com.creditease.monitor.UAVServer.ServerVendor)2 DubboIT (com.creditease.uav.hook.dubbo.interceptors.DubboIT)1 ApacheAsyncHttpClientIT (com.creditease.uav.hook.httpclients.async.interceptors.ApacheAsyncHttpClientIT)1 JaxWSHookIT (com.creditease.uav.hook.jaxws.interceptors.JaxWSHookIT)1 DBCPIT (com.creditease.uav.hook.jdbc.pools.dbcp.interceptors.DBCPIT)1 MybatisIT (com.creditease.uav.hook.jdbc.pools.mybatis.interceptors.MybatisIT)1 MongoClientIT (com.creditease.uav.hook.mongoclients.interceptors.MongoClientIT)1 RabbitmqIT (com.creditease.uav.hook.rabbitmq.interceptors.RabbitmqIT)1 AredisCommandObjectIT (com.creditease.uav.hook.redis.aredis.interceptors.AredisCommandObjectIT)1 DPClass (com.creditease.uav.monitorframework.dproxy.bytecode.DPClass)1 WebServiceListenerIT (com.creditease.uav.monitorframework.webservice.interceptors.WebServiceListenerIT)1 FastClasspathScanner (io.github.lukehutch.fastclasspathscanner.FastClasspathScanner)1