use of javassist.CtClass in project jvm-profiler by uber-common.
the class JavaAgentFileTransformer method transformImpl.
private byte[] transformImpl(ClassLoader loader, String className, byte[] classfileBuffer) {
if (durationProfilingFilter.isEmpty() && argumentFilterProfilingFilter.isEmpty()) {
return null;
}
String normalizedClassName = className.replaceAll("/", ".");
logger.debug("Checking class for transform: " + normalizedClassName);
if (!durationProfilingFilter.matchClass(normalizedClassName) && !argumentFilterProfilingFilter.matchClass(normalizedClassName)) {
return null;
}
byte[] byteCode;
logger.info("Transforming class: " + normalizedClassName);
try {
ClassPool classPool = new ClassPool();
classPool.appendClassPath(new LoaderClassPath(loader));
final CtClass ctClass;
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(classfileBuffer)) {
ctClass = classPool.makeClass(byteArrayInputStream);
}
CtMethod[] ctMethods = ctClass.getDeclaredMethods();
for (CtMethod ctMethod : ctMethods) {
boolean enableDurationProfiling = durationProfilingFilter.matchMethod(ctClass.getName(), ctMethod.getName());
List<Integer> enableArgumentProfiler = argumentFilterProfilingFilter.matchMethod(ctClass.getName(), ctMethod.getName());
transformMethod(normalizedClassName, ctMethod, enableDurationProfiling, enableArgumentProfiler);
}
byteCode = ctClass.toBytecode();
ctClass.detach();
} catch (Throwable ex) {
ex.printStackTrace();
logger.warn("Failed to transform class: " + normalizedClassName, ex);
byteCode = null;
}
return byteCode;
}
use of javassist.CtClass in project uavstack by uavorg.
the class AbstractAdaptor method inject.
protected byte[] inject(String className, String[] importPackages, AdaptorProcessor... p) {
try {
CtClass cc = pool.get(className);
if (importPackages != null) {
for (String ip : importPackages) {
pool.importPackage(ip);
}
}
for (AdaptorProcessor ap : p) {
CtMethod mthd = null;
try {
if (ap.getParamNum() == -1) {
mthd = cc.getDeclaredMethod(ap.getMethodName());
} else {
CtMethod[] mlist = cc.getDeclaredMethods();
for (CtMethod m : mlist) {
if (m.getParameterTypes().length == ap.getParamNum() && m.getName().equals(ap.getMethodName())) {
mthd = m;
break;
}
}
}
} catch (Exception e) {
continue;
}
if (mthd == null) {
continue;
}
ap.process(mthd);
}
byte[] code = cc.toBytecode();
cc.detach();
return code;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of javassist.CtClass in project uavstack by uavorg.
the class AbstractAdaptor method addLocalVar.
protected void addLocalVar(CtMethod m, String varName, String varClassName) {
try {
CtClass cc = pool.get(varClassName);
m.addLocalVariable(varName, cc);
} catch (Exception e) {
e.printStackTrace();
}
}
use of javassist.CtClass in project uavstack by uavorg.
the class TtlAdaptor method getCtClass.
private CtClass getCtClass(byte[] classFileBuffer, ClassLoader classLoader) throws IOException {
ClassPool classPool = new ClassPool(true);
if (null != classLoader) {
classPool.appendClassPath(new LoaderClassPath(classLoader));
}
CtClass clazz = classPool.makeClass(new ByteArrayInputStream(classFileBuffer), false);
clazz.defrost();
return clazz;
}
use of javassist.CtClass in project uavstack by uavorg.
the class TtlAdaptor method updateMethod.
static void updateMethod(CtClass clazz, CtMethod method) throws NotFoundException, CannotCompileException {
if (!updateMethodNames.contains(method.getName())) {
return;
}
if (method.getDeclaringClass() != clazz) {
return;
}
final int modifiers = method.getModifiers();
if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) {
return;
}
CtClass[] parameterTypes = method.getParameterTypes();
StringBuilder insertCode = new StringBuilder();
for (int i = 0; i < parameterTypes.length; i++) {
CtClass paraType = parameterTypes[i];
if (RUNNABLE_CLASS_NAME.equals(paraType.getName())) {
String code = String.format("$%d = %s.get($%d, false, true);", i + 1, TTL_RUNNABLE_CLASS_NAME, i + 1);
insertCode.append(code);
} else if (CALLABLE_CLASS_NAME.equals(paraType.getName())) {
String code = String.format("$%d = %s.get($%d, false, true);", i + 1, TTL_CALLABLE_CLASS_NAME, i + 1);
insertCode.append(code);
}
}
if (insertCode.length() > 0) {
method.insertBefore(insertCode.toString());
}
}
Aggregations