[유치수] [NOBTS] formatting, modifying, remove unnecessary codes.

git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-tomcat-profiler/trunk@562 84d0f5b1-2673-498c-a247-62c4ff18d310
This commit is contained in:
Chisu Yu
2012-08-30 04:36:09 +00:00
parent 9aef6b89cf
commit b60e8ec186
16 changed files with 405 additions and 416 deletions
@@ -46,7 +46,7 @@ public class DefaultTracer implements Tracer {
private void logSpan(Span span) {
// TODO: send span to server
System.out.println("Write span hash=" + span.hashCode() + ", value=" + span + ", spanMap.size=" + spanMap.size() + ", threadid=" + Thread.currentThread().getId());
System.out.println("\n\nWrite span hash=" + span.hashCode() + ", value=" + span + ", spanMap.size=" + spanMap.size() + ", threadid=" + Thread.currentThread().getId() + "\n\n");
}
@Override
@@ -1,5 +1,5 @@
package com.profiler.interceptor;
public interface AfterInterceptor extends Interceptor {
void after(InterceptorContext ctx);
void after(InterceptorContext ctx);
}
@@ -1,9 +1,9 @@
package com.profiler.interceptor;
public interface AroundInterceptor extends BeforeInterceptor, AfterInterceptor {
@Override
void before(InterceptorContext ctx);
@Override
void before(InterceptorContext ctx);
@Override
void after(InterceptorContext ctx);
@Override
void after(InterceptorContext ctx);
}
@@ -3,71 +3,64 @@ package com.profiler.interceptor;
import java.util.Arrays;
public class InterceptorContext {
private Object target;
private String className;
private String methodName;
private Object[] parameter;
private Object value;
private Object target;
private String className;
private String methodName;
private Object[] parameter;
private Object value;
private Object result;
private Object result;
public Object getTarget() {
return target;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public void setTarget(Object target) {
this.target = target;
}
public String getClassName() {
return className;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public void setClassName(String className) {
this.className = className;
}
public String getMethodName() {
return methodName;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Object[] getParameter() {
return parameter;
}
public Object[] getParameter() {
return parameter;
}
public void setParameter(Object[] parameter) {
this.parameter = parameter;
}
public void setParameter(Object[] parameter) {
this.parameter = parameter;
}
public Object getResult() {
return result;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
public void setResult(Object result) {
this.result = result;
}
public Object getValue() {
return value;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public String toString() {
return "InterceptorContext{" +
"target=" + target +
", className='" + className + '\'' +
", methodName='" + methodName + '\'' +
", parameter=" + (parameter == null ? null : Arrays.asList(parameter)) +
", value=" + value +
", result=" + result +
'}';
}
@Override
public String toString() {
return "InterceptorContext{" + "target=" + target + ", className='" + className + '\'' + ", methodName='" + methodName + '\'' + ", parameter=" + (parameter == null ? null : Arrays.asList(parameter)) + ", value=" + value + ", result=" + result + '}';
}
}
@@ -1,53 +1,50 @@
package com.profiler.interceptor;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
public class InterceptorRegistry {
private final static int DEFAULT_MAX = 1024;
private final int max;
private final static int DEFAULT_MAX = 1024;
private final int max;
private final AtomicInteger id = new AtomicInteger(0);
private final Interceptor[] index;
// private final ConcurrentMap<String, List<Integer>> nameToIndex = new ConcurrentHashMap<String, List<Integer>>();
private final AtomicInteger id = new AtomicInteger(0);
private final Interceptor[] index;
// private final ConcurrentMap<String, List<Integer>> nameToIndex = new
// ConcurrentHashMap<String, List<Integer>>();
public static final InterceptorRegistry REGISTRY = new InterceptorRegistry();
public static final InterceptorRegistry REGISTRY = new InterceptorRegistry();
InterceptorRegistry() {
this(DEFAULT_MAX);
}
InterceptorRegistry() {
this(DEFAULT_MAX);
}
InterceptorRegistry(int max) {
this.max = max;
this.index = new Interceptor[max];
}
InterceptorRegistry(int max) {
this.max = max;
this.index = new Interceptor[max];
}
int addInterceptor0(Interceptor interceptor) {
if (interceptor == null) {
return -1;
}
int newId = id.getAndIncrement();
if (newId > max) {
throw new IllegalArgumentException("id" + id);
}
int addInterceptor0(Interceptor interceptor) {
if (interceptor == null) {
return -1;
}
int newId = id.getAndIncrement();
if (newId > max) {
throw new IllegalArgumentException("id" + id);
}
this.index[newId] = interceptor;
return newId;
}
this.index[newId] = interceptor;
return newId;
}
Interceptor getInterceptor0(int key) {
return index[key];
}
Interceptor getInterceptor0(int key) {
return index[key];
}
public static int addInterceptor(Interceptor interceptor) {
return REGISTRY.addInterceptor0(interceptor);
}
public static int addInterceptor(Interceptor interceptor) {
return REGISTRY.addInterceptor0(interceptor);
}
public static Interceptor getInterceptor(int key) {
return REGISTRY.getInterceptor0(key);
}
public static Interceptor getInterceptor(int key) {
return REGISTRY.getInterceptor0(key);
}
}
@@ -6,25 +6,23 @@ import java.util.logging.Logger;
public class LoggingInterceptor implements StaticAroundInterceptor {
private final Logger logger;
private final Logger logger;
public LoggingInterceptor(String loggerName) {
this.logger = Logger.getLogger(loggerName);
}
public LoggingInterceptor(String loggerName) {
this.logger = Logger.getLogger(loggerName);
}
@Override
public void before(Object target, String className, String methodName, Object[] args) {
if(logger.isLoggable(Level.INFO)) {
logger.info("before target:" + target.toString() + " " + className + "." + methodName + " args:" + Arrays.toString(args));
}
}
@Override
public void after(Object target, String className, String methodName, Object[] args, Object result) {
if (logger.isLoggable(Level.INFO)) {
logger.info("after target:" + target.toString() + " " + className + "." + methodName
+ " args:" + Arrays.toString(args) + " result:" + result);
}
}
@Override
public void before(Object target, String className, String methodName, Object[] args) {
if (logger.isLoggable(Level.INFO)) {
logger.info("before target:" + target.toString() + " " + className + "." + methodName + " args:" + Arrays.toString(args));
}
}
@Override
public void after(Object target, String className, String methodName, Object[] args, Object result) {
if (logger.isLoggable(Level.INFO)) {
logger.info("after target:" + target.toString() + " " + className + "." + methodName + " args:" + Arrays.toString(args) + " result:" + result);
}
}
}
@@ -1,7 +1,5 @@
package com.profiler.interceptor;
public interface StaticAfterInterceptor extends Interceptor {
void after(Object target, String className, String methodName, Object[] args, Object result);
void after(Object target, String className, String methodName, Object[] args, Object result);
}
@@ -2,5 +2,4 @@ package com.profiler.interceptor;
public interface StaticAroundInterceptor extends StaticBeforeInterceptor, StaticAfterInterceptor {
}
@@ -1,5 +1,5 @@
package com.profiler.interceptor;
public interface StaticBeforeInterceptor extends Interceptor {
void before(Object target, String className, String methodName, Object[] args);
void before(Object target, String className, String methodName, Object[] args);
}
@@ -1,18 +1,17 @@
package com.profiler.interceptor.bci;
import javassist.ClassPool;
import java.security.ProtectionDomain;
import javassist.ClassPool;
public interface ByteCodeInstrumentor {
// 임시로 만들자.
ClassPool getClassPool();
// 임시로 만들자.
ClassPool getClassPool();
void checkLibrary(ClassLoader classLoader, String javassistClassName);
void checkLibrary(ClassLoader classLoader, String javassistClassName);
InstrumentClass getClass(String javassistClassName);
InstrumentClass getClass(String javassistClassName);
Class defineClass(ClassLoader classLoader, String defineClass, ProtectionDomain protectedDomain);
Class<?> defineClass(ClassLoader classLoader, String defineClass, ProtectionDomain protectedDomain);
}
@@ -3,13 +3,13 @@ package com.profiler.interceptor.bci;
import com.profiler.interceptor.Interceptor;
public interface InstrumentClass {
boolean addInterceptor(String methodName, String[] args, Interceptor interceptor);
boolean addInterceptor(String methodName, String[] args, Interceptor interceptor);
boolean addDebugLogBeforeAfterMethod();
boolean addDebugLogBeforeAfterMethod();
boolean addDebugLogBeforeAfterConstructor();
boolean addDebugLogBeforeAfterConstructor();
byte[] toBytecode();
byte[] toBytecode();
Class toClass();
Class<?> toClass();
}
@@ -1,59 +1,57 @@
package com.profiler.interceptor.bci;
import com.profiler.interceptor.Interceptor;
import com.profiler.interceptor.StaticBeforeInterceptor;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.ProtectionDomain;
import java.util.logging.Level;
import java.util.logging.Logger;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
public class JavaAssistByteCodeInstrumentor implements ByteCodeInstrumentor {
private final Logger logger = Logger.getLogger(this.getClass().getName());
private final Logger logger = Logger.getLogger(this.getClass().getName());
private ClassPool classPool;
private ClassPool classPool;
public JavaAssistByteCodeInstrumentor() {
this.classPool = createClassPool(null);
}
public JavaAssistByteCodeInstrumentor() {
this.classPool = createClassPool(null);
}
public JavaAssistByteCodeInstrumentor(String[] pathNames) {
this.classPool = createClassPool(pathNames);
}
public JavaAssistByteCodeInstrumentor(String[] pathNames) {
this.classPool = createClassPool(pathNames);
}
public ClassPool getClassPool() {
return this.classPool;
}
public ClassPool getClassPool() {
return this.classPool;
}
private ClassPool createClassPool(String[] pathNames) {
ClassPool classPool = new ClassPool(null);
classPool.appendSystemPath();
if (pathNames != null) {
for(String path: pathNames) {
appendClassPath(classPool, path);
}
}
private ClassPool createClassPool(String[] pathNames) {
ClassPool classPool = new ClassPool(null);
classPool.appendSystemPath();
if (pathNames != null) {
for (String path : pathNames) {
appendClassPath(classPool, path);
}
}
return classPool;
}
return classPool;
}
private void appendClassPath(ClassPool classPool, String pathName) {
try {
classPool.appendClassPath(pathName);
} catch (NotFoundException e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "appendClassPath fail. lib not found. " + e.getMessage(), e);
}
}
}
private void appendClassPath(ClassPool classPool, String pathName) {
try {
classPool.appendClassPath(pathName);
} catch (NotFoundException e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "appendClassPath fail. lib not found. " + e.getMessage(), e);
}
}
}
public void checkLibrary(ClassLoader classLoader, String javassistClassName) {
public void checkLibrary(ClassLoader classLoader, String javassistClassName) {
// TODO Util로 뽑을까?
boolean findClass = findClass(javassistClassName);
if (findClass) {
@@ -62,38 +60,38 @@ public class JavaAssistByteCodeInstrumentor implements ByteCodeInstrumentor {
loadClassLoaderLibraries(classLoader);
}
@Override
public InstrumentClass getClass(String javassistClassName) {
try {
CtClass cc = classPool.get(javassistClassName);
return new JavaAssistClass(this, cc);
} catch (NotFoundException e) {
// TODO 실패시 더미 객체를 반환해서 잘 에러를 숨길수 있도록 수정필요.
return null;
}
}
@Override
public InstrumentClass getClass(String javassistClassName) {
try {
CtClass cc = classPool.get(javassistClassName);
return new JavaAssistClass(this, cc);
} catch (NotFoundException e) {
// TODO 실패시 더미 객체를 반환해서 잘 에러를 숨길수 있도록 수정필요.
return null;
}
}
@Override
public Class defineClass(ClassLoader classLoader, String defineClass, ProtectionDomain protectedDomain) {
try {
if(logger.isLoggable(Level.INFO)) {
logger.info("defineClass classLoader:" + classLoader + " class:" + defineClass);
}
CtClass clazz = classPool.get(defineClass);
return clazz.toClass(classLoader, protectedDomain);
} catch (NotFoundException e) {
if(logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "defineClass classLoader:" + classLoader + " " + e.getMessage(), e);
}
} catch (CannotCompileException e) {
if(logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "defineClass classLoader:" + classLoader + " "+ e.getMessage(), e);
}
}
return null;
}
@Override
public Class<?> defineClass(ClassLoader classLoader, String defineClass, ProtectionDomain protectedDomain) {
try {
if (logger.isLoggable(Level.INFO)) {
logger.info("defineClass classLoader:" + classLoader + " class:" + defineClass);
}
CtClass clazz = classPool.get(defineClass);
return clazz.toClass(classLoader, protectedDomain);
} catch (NotFoundException e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "defineClass classLoader:" + classLoader + " " + e.getMessage(), e);
}
} catch (CannotCompileException e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "defineClass classLoader:" + classLoader + " " + e.getMessage(), e);
}
}
return null;
}
public boolean findClass(String javassistClassName) {
public boolean findClass(String javassistClassName) {
// TODO 원래는 get인데. find는 ctclas를 생성하지 않아 변경. 어차피 아래서 생성하기는 함. 유효성 여부 확인
// 필요
URL url = classPool.find(javassistClassName);
@@ -116,9 +114,9 @@ public class JavaAssistByteCodeInstrumentor implements ByteCodeInstrumentor {
// TODO 여기서 로그로 class로더를 찍어보면 어떤 clasdLoader에서 로딩되는지 알수 있을거
// 것같음.
// 만약 한개만 로딩해도 된다면. return true 할것
if(logger.isLoggable(Level.FINE)) {
logger.info("Loaded "+filePath+" library.");
}
if (logger.isLoggable(Level.FINE)) {
logger.info("Loaded " + filePath + " library.");
}
} catch (NotFoundException e) {
}
}
@@ -1,234 +1,243 @@
package com.profiler.interceptor.bci;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.profiler.interceptor.*;
import javassist.*;
import javassist.CannotCompileException;
import javassist.CtBehavior;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtMethod;
import javassist.NotFoundException;
import com.profiler.interceptor.Interceptor;
import com.profiler.interceptor.InterceptorRegistry;
import com.profiler.interceptor.LoggingInterceptor;
import com.profiler.interceptor.StaticAfterInterceptor;
import com.profiler.interceptor.StaticAroundInterceptor;
import com.profiler.interceptor.StaticBeforeInterceptor;
public class JavaAssistClass implements InstrumentClass {
private final Logger logger = Logger.getLogger(this.getClass().getName());
private final Logger logger = Logger.getLogger(this.getClass().getName());
private JavaAssistByteCodeInstrumentor instrumentor;
private CtClass ctClass;
private JavaAssistByteCodeInstrumentor instrumentor;
private CtClass ctClass;
public JavaAssistClass(JavaAssistByteCodeInstrumentor instrumentor, CtClass ctClass) {
this.instrumentor = instrumentor;
this.ctClass = ctClass;
}
public JavaAssistClass(JavaAssistByteCodeInstrumentor instrumentor, CtClass ctClass) {
this.instrumentor = instrumentor;
this.ctClass = ctClass;
}
@Override
public boolean addInterceptor(String methodName, String[] args, Interceptor interceptor) {
int id = InterceptorRegistry.addInterceptor(interceptor);
try {
CtMethod method = getMethod(methodName, args);
if (interceptor instanceof StaticAroundInterceptor) {
addAroundInterceptor(methodName, id, method);
} else if (interceptor instanceof StaticBeforeInterceptor) {
addStaticBeforeInterceptor(methodName, id, method);
} else if (interceptor instanceof StaticAfterInterceptor) {
addStaticAfterInterceptor(methodName, id, method);
}
return true;
} catch (NotFoundException e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, e.getMessage(), e);
}
} catch (CannotCompileException e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, e.getMessage(), e);
}
}
return false;
}
@Override
public boolean addInterceptor(String methodName, String[] args, Interceptor interceptor) {
int id = InterceptorRegistry.addInterceptor(interceptor);
try {
CtMethod method = getMethod(methodName, args);
if (interceptor instanceof StaticAroundInterceptor) {
addAroundInterceptor(methodName, id, method);
} else if (interceptor instanceof StaticBeforeInterceptor) {
addStaticBeforeInterceptor(methodName, id, method);
} else if (interceptor instanceof StaticAfterInterceptor) {
addStaticAfterInterceptor(methodName, id, method);
}
return true;
} catch (NotFoundException e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, e.getMessage(), e);
}
} catch (CannotCompileException e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, e.getMessage(), e);
}
}
return false;
}
private void addAroundInterceptor(String methodName, int id, CtBehavior method) throws NotFoundException, CannotCompileException {
addStaticBeforeInterceptor(methodName, id, method);
addStaticAfterInterceptor(methodName, id, method);
}
private void addAroundInterceptor(String methodName, int id, CtBehavior method) throws NotFoundException, CannotCompileException {
addStaticBeforeInterceptor(methodName, id, method);
addStaticAfterInterceptor(methodName, id, method);
}
private void addStaticAfterInterceptor(String methodName, int id, CtBehavior behavior) throws NotFoundException, CannotCompileException {
StringBuilder after = new StringBuilder(1024);
after.append("{");
addGetStaticAfterInterceptor(after, id);
after.append(" interceptor.after(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args, ($w)$_);");
after.append("}");
String buildAfter = after.toString();
if (logger.isLoggable(Level.INFO)) {
logger.info("addStaticAfterInterceptor after behavior:" + behavior.getLongName() + " code:" + buildAfter);
}
behavior.insertAfter(buildAfter);
private void addStaticAfterInterceptor(String methodName, int id, CtBehavior behavior) throws NotFoundException, CannotCompileException {
StringBuilder after = new StringBuilder(1024);
after.append("{");
addGetStaticAfterInterceptor(after, id);
after.append(" interceptor.after(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args, ($w)$_);");
after.append("}");
String buildAfter = after.toString();
if (logger.isLoggable(Level.INFO)) {
logger.info("addStaticAfterInterceptor after behavior:" + behavior.getLongName() + " code:" + buildAfter);
}
behavior.insertAfter(buildAfter);
StringBuilder catchCode = new StringBuilder(1024);
catchCode.append("{");
addGetStaticAfterInterceptor(catchCode, id);
catchCode.append(" interceptor.after(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args, $e);");
catchCode.append(" throw $e;");
catchCode.append("}");
String buildCatch = catchCode.toString();
if (logger.isLoggable(Level.INFO)) {
logger.info("addStaticAfterInterceptor catch behavior:" + behavior.getLongName() + " code:" + buildCatch);
}
CtClass th = instrumentor.getClassPool().get("java.lang.Throwable");
behavior.addCatch(buildCatch, th);
StringBuilder catchCode = new StringBuilder(1024);
catchCode.append("{");
addGetStaticAfterInterceptor(catchCode, id);
catchCode.append(" interceptor.after(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args, $e);");
catchCode.append(" throw $e;");
catchCode.append("}");
String buildCatch = catchCode.toString();
if (logger.isLoggable(Level.INFO)) {
logger.info("addStaticAfterInterceptor catch behavior:" + behavior.getLongName() + " code:" + buildCatch);
}
CtClass th = instrumentor.getClassPool().get("java.lang.Throwable");
behavior.addCatch(buildCatch, th);
}
}
private void addGetStaticAfterInterceptor(StringBuilder after, int id) {
after.append(" com.profiler.interceptor.StaticAfterInterceptor interceptor = "
+ "(com.profiler.interceptor.StaticAfterInterceptor) com.profiler.interceptor.InterceptorRegistry.getInterceptor(");
after.append(id);
after.append(");");
}
private void addGetStaticAfterInterceptor(StringBuilder after, int id) {
after.append(" com.profiler.interceptor.StaticAfterInterceptor interceptor = " + "(com.profiler.interceptor.StaticAfterInterceptor) com.profiler.interceptor.InterceptorRegistry.getInterceptor(");
after.append(id);
after.append(");");
}
private void addStaticBeforeInterceptor(String methodName, int id, CtBehavior behavior) throws CannotCompileException {
StringBuilder code = new StringBuilder(1024);
code.append("{");
addGetBeforeInterceptor(id, code);
code.append(" interceptor.before(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args);");
code.append("}");
String buildBefore = code.toString();
if (logger.isLoggable(Level.INFO)) {
logger.info("addStaticBeforeInterceptor catch behavior:" + behavior.getLongName() + " code:" + buildBefore);
}
private void addStaticBeforeInterceptor(String methodName, int id, CtBehavior behavior) throws CannotCompileException {
StringBuilder code = new StringBuilder(1024);
code.append("{");
addGetBeforeInterceptor(id, code);
code.append(" interceptor.before(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args);");
code.append("}");
String buildBefore = code.toString();
if (logger.isLoggable(Level.INFO)) {
logger.info("addStaticBeforeInterceptor catch behavior:" + behavior.getLongName() + " code:" + buildBefore);
}
if(behavior instanceof CtConstructor) {
((CtConstructor) behavior).insertBeforeBody(buildBefore);
} else {
behavior.insertBefore(buildBefore);
}
}
if (behavior instanceof CtConstructor) {
((CtConstructor) behavior).insertBeforeBody(buildBefore);
} else {
behavior.insertBefore(buildBefore);
}
}
private void addGetBeforeInterceptor(int id, StringBuilder code) {
code.append(" com.profiler.interceptor.StaticBeforeInterceptor interceptor = "
+ "(com.profiler.interceptor.StaticBeforeInterceptor)com.profiler.interceptor.InterceptorRegistry.getInterceptor(");
code.append(id);
code.append(");");
}
private void addGetBeforeInterceptor(int id, StringBuilder code) {
code.append(" com.profiler.interceptor.StaticBeforeInterceptor interceptor = " + "(com.profiler.interceptor.StaticBeforeInterceptor)com.profiler.interceptor.InterceptorRegistry.getInterceptor(");
code.append(id);
code.append(");");
}
public boolean addDebugLogBeforeAfterMethod() {
String className = this.ctClass.getName();
LoggingInterceptor loggingInterceptor = new LoggingInterceptor(className);
int id = InterceptorRegistry.addInterceptor(loggingInterceptor);
try {
CtClass cc = this.instrumentor.getClassPool().get(className);
CtMethod[] methods = cc.getDeclaredMethods();
public boolean addDebugLogBeforeAfterMethod() {
String className = this.ctClass.getName();
LoggingInterceptor loggingInterceptor = new LoggingInterceptor(className);
int id = InterceptorRegistry.addInterceptor(loggingInterceptor);
try {
CtClass cc = this.instrumentor.getClassPool().get(className);
CtMethod[] methods = cc.getDeclaredMethods();
for (CtMethod method : methods) {
if (method.isEmpty()) {
if (logger.isLoggable(Level.FINE)) {
logger.fine(method.getLongName() + " is empty.");
}
continue;
}
String methodName = method.getName();
for (CtMethod method : methods) {
if (method.isEmpty()) {
if (logger.isLoggable(Level.FINE)) {
logger.fine(method.getLongName() + " is empty.");
}
continue;
}
String methodName = method.getName();
// TODO method의 prameter type을 interceptor에 별도 추가해야 될것으로 보임.
String params = getParamsToString(method.getParameterTypes());
addAroundInterceptor(methodName, id, method);
}
return true;
} catch (Exception e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, e.getMessage(), e);
}
}
return false;
}
// TODO method의 prameter type을 interceptor에 별도 추가해야 될것으로 보임.
String params = getParamsToString(method.getParameterTypes());
addAroundInterceptor(methodName, id, method);
}
return true;
} catch (Exception e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, e.getMessage(), e);
}
}
return false;
}
/**
* 제대로 동작안함 다시 봐야 될것 같음.
* 생성자일경우의 bytecode 수정시 에러가 남.
* @return
*/
@Deprecated
public boolean addDebugLogBeforeAfterConstructor() {
String className = this.ctClass.getName();
LoggingInterceptor loggingInterceptor = new LoggingInterceptor(className);
int id = InterceptorRegistry.addInterceptor(loggingInterceptor);
try {
CtClass cc = this.instrumentor.getClassPool().get(className);
CtConstructor[] constructors = cc.getConstructors();
/**
* 제대로 동작안함 다시 봐야 될것 같음. 생성자일경우의 bytecode 수정시 에러가 남.
*
* @return
*/
@Deprecated
public boolean addDebugLogBeforeAfterConstructor() {
String className = this.ctClass.getName();
LoggingInterceptor loggingInterceptor = new LoggingInterceptor(className);
int id = InterceptorRegistry.addInterceptor(loggingInterceptor);
try {
CtClass cc = this.instrumentor.getClassPool().get(className);
CtConstructor[] constructors = cc.getConstructors();
for (CtConstructor constructor : constructors) {
for (CtConstructor constructor : constructors) {
if (constructor.isEmpty()) {
if (logger.isLoggable(Level.FINE)) {
logger.fine(constructor.getLongName() + " is empty.");
}
continue;
}
String constructorName = constructor.getName();
String params = getParamsToString(constructor.getParameterTypes());
if (constructor.isEmpty()) {
if (logger.isLoggable(Level.FINE)) {
logger.fine(constructor.getLongName() + " is empty.");
}
continue;
}
String constructorName = constructor.getName();
String params = getParamsToString(constructor.getParameterTypes());
// constructor.insertAfter("{System.out.println(\"*****" +
// constructorName + " Constructor:Param=(" + params +
// ") is finished. \" + $args);}");
// constructor.addCatch("{System.out.println(\"*****" +
// constructorName + " Constructor:Param=(" + params +
// ") is finished.\"); throw $e; }"
// , instrumentor.getClassPool().get("java.lang.Throwable"));
addAroundInterceptor(constructorName, id, constructor);
}
return true;
} catch (Exception e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, e.getMessage(), e);
}
}
return false;
}
// constructor.insertAfter("{System.out.println(\"*****" + constructorName + " Constructor:Param=(" + params + ") is finished. \" + $args);}");
// constructor.addCatch("{System.out.println(\"*****" + constructorName + " Constructor:Param=(" + params + ") is finished.\"); throw $e; }"
// , instrumentor.getClassPool().get("java.lang.Throwable"));
addAroundInterceptor(constructorName, id, constructor);
}
return true;
} catch (Exception e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, e.getMessage(), e);
}
}
return false;
}
private String getParamsToString(CtClass[] params) throws NotFoundException {
StringBuilder sb = new StringBuilder(512);
if (params.length != 0) {
int paramsLength = params.length;
for (int loop = paramsLength - 1; loop > 0; loop--) {
sb.append(params[loop].getName()).append(",");
}
}
String paramsStr = sb.toString();
if (logger.isLoggable(Level.FINE)) {
logger.fine("params type:" + paramsStr);
}
return paramsStr;
}
private String getParamsToString(CtClass[] params) throws NotFoundException {
StringBuilder sb = new StringBuilder(512);
if (params.length != 0) {
int paramsLength = params.length;
for (int loop = paramsLength - 1; loop > 0; loop--) {
sb.append(params[loop].getName()).append(",");
}
}
String paramsStr = sb.toString();
if (logger.isLoggable(Level.FINE)) {
logger.fine("params type:" + paramsStr);
}
return paramsStr;
}
private CtMethod getMethod(String methodName, String[] args) throws NotFoundException {
CtClass[] params = getCtParameter(args);
return ctClass.getDeclaredMethod(methodName, params);
}
private CtClass[] getCtParameter(String[] args) throws NotFoundException {
if (args == null) {
return null;
}
CtClass[] params = new CtClass[args.length];
for (int i = 0; i < args.length; i++) {
params[i] = instrumentor.getClassPool().getCtClass(args[i]);
}
return params;
}
private CtMethod getMethod(String methodName, String[] args) throws NotFoundException {
CtClass[] params = getCtParameter(args);
return ctClass.getDeclaredMethod(methodName, params);
}
private CtClass[] getCtParameter(String[] args) throws NotFoundException {
if (args == null) {
return null;
}
CtClass[] params = new CtClass[args.length];
for (int i = 0; i < args.length; i++) {
params[i] = instrumentor.getClassPool().getCtClass(args[i]);
}
return params;
}
@Override
public byte[] toBytecode() {
try {
return ctClass.toBytecode();
} catch (IOException e) {
logger.log(Level.INFO, "IoException class:" + ctClass.getName() + " " + e.getMessage(), e);
} catch (CannotCompileException e) {
logger.log(Level.INFO, "CannotCompileException class:" + ctClass.getName() + " " + e.getMessage(), e);
}
return null;
}
public Class toClass() {
try {
return ctClass.toClass();
} catch (CannotCompileException e) {
logger.log(Level.INFO, "CannotCompileException class:" + ctClass.getName() + " " + e.getMessage(), e);
}
return null;
}
@Override
public byte[] toBytecode() {
try {
return ctClass.toBytecode();
} catch (IOException e) {
logger.log(Level.INFO, "IoException class:" + ctClass.getName() + " " + e.getMessage(), e);
} catch (CannotCompileException e) {
logger.log(Level.INFO, "CannotCompileException class:" + ctClass.getName() + " " + e.getMessage(), e);
}
return null;
}
public Class<?> toClass() {
try {
return ctClass.toClass();
} catch (CannotCompileException e) {
logger.log(Level.INFO, "CannotCompileException class:" + ctClass.getName() + " " + e.getMessage(), e);
}
return null;
}
}
@@ -97,6 +97,6 @@ public class MySQLPreparedStatementModifier extends AbstractModifier {
private void updateExecuteQueryMethod(CtClass cc) throws Exception {
CtMethod method = cc.getDeclaredMethod("executeQuery", null);
method.insertAfter("{System.out.println(\"AAAAAAA\"); " + DatabaseRequestTracer.FQCN + ".put(" + TomcatProfilerConstant.REQ_DATA_TYPE_DB_EXECUTE_QUERY + "); }");
method.insertAfter("{" + DatabaseRequestTracer.FQCN + ".put(" + TomcatProfilerConstant.REQ_DATA_TYPE_DB_EXECUTE_QUERY + "); }");
}
}
@@ -15,8 +15,6 @@ public class ExecuteQueryMethodInterceptor implements StaticAroundInterceptor {
@Override
public void before(Object target, String className, String methodName, Object[] args) {
try {
System.out.println("ThreadID=" + Thread.currentThread().getId());
Trace.recordRpcName("mysql", "");
if (args.length > 0) {
@@ -1,18 +1,18 @@
package com.profiler.interceptor.bci;
import com.profiler.interceptor.TestAfterInterceptor;
import com.profiler.interceptor.TestBeforeInterceptor;
import javassist.bytecode.Descriptor;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javassist.bytecode.Descriptor;
import org.junit.Assert;
import org.junit.Test;
import com.profiler.interceptor.TestAfterInterceptor;
import com.profiler.interceptor.TestBeforeInterceptor;
public class JavaAssistClassTest {
private Logger logger = Logger.getLogger(JavaAssistByteCodeInstrumentor.class.getName());