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());
}
}
Aggregations