Skip to content

Commit 996100f

Browse files
author
dingjs
committed
fix the web project javassist NotFoundException
1 parent e66f623 commit 996100f

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/com/thunisoft/agent/transformer/ThunisoftClassFileTransformer.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import javassist.CtClass;
1919
import javassist.CtMethod;
2020
import javassist.LoaderClassPath;
21+
import javassist.NotFoundException;
2122

2223
import com.thunisoft.agent.ConfigUtils;
2324

@@ -37,14 +38,6 @@ public class ThunisoftClassFileTransformer implements ClassFileTransformer {
3738

3839
private static byte[] logUtilsOutputRunnableClassBytes = null;
3940

40-
// private Set<ClassLoader> loadedClassLoaders = new HashSet<ClassLoader>();
41-
// private WeakHashMap<ClassLoader, Object> loadedClassLoaders = new
42-
// WeakHashMap<ClassLoader, Object>();
43-
44-
// private static final Object EXISTS = new Object();
45-
46-
// private boolean classLoaderInited;
47-
4841
public static void setLogUtilsClassBytes(byte[] logUtilsClassBytes,
4942
byte[] runnableBytes) {
5043
ThunisoftClassFileTransformer.logUtilsClassBytes = logUtilsClassBytes;
@@ -71,7 +64,7 @@ public byte[] transform(ClassLoader loader, String className,
7164
loader = Thread.currentThread().getContextClassLoader();
7265
}
7366
loadLogUtilsClass(loader);
74-
byteCode = aopLog(className, byteCode);
67+
byteCode = aopLog(loader, className, byteCode);
7568
return byteCode;
7669
}
7770

@@ -81,7 +74,7 @@ private void loadLogUtilsClass(ClassLoader loader) {
8174
try {
8275
logUtilsClass = loader.loadClass(LOG_UTILS);
8376
} catch (ClassNotFoundException e1) {
84-
e1.printStackTrace();
77+
System.err.println(e1);
8578
}
8679
if (null == logUtilsClass) {
8780
ClassPool cp = ClassPool.getDefault();
@@ -110,23 +103,29 @@ private void loadLogUtilsClass(ClassLoader loader) {
110103
e.printStackTrace();
111104
}
112105
}
113-
if(null != logUtilsClass){
114-
try{
106+
if (null != logUtilsClass) {
107+
try {
115108
Method initMethod = logUtilsClass.getDeclaredMethod("init",
116109
String.class, int.class, boolean.class);
117110
initMethod.invoke(logUtilsClass, ConfigUtils.getLogFileName(),
118111
ConfigUtils.getLogInterval(),
119112
ConfigUtils.isLogAvgExecuteTime());
120-
}catch(Exception e){
113+
} catch (Exception e) {
121114
e.printStackTrace();
122115
}
123116
}
124117
}
125118

126-
private byte[] aopLog(String className, byte[] byteCode) {
119+
private byte[] aopLog(ClassLoader loader, String className, byte[] byteCode) {
127120
try {
128121
ClassPool cp = ClassPool.getDefault();
129-
CtClass cc = cp.get(className);
122+
CtClass cc = null;
123+
try {
124+
cc = cp.get(className);
125+
} catch (NotFoundException e) {
126+
cp.insertClassPath(new LoaderClassPath(loader));
127+
cc = cp.get(className);
128+
}
130129
if (null != cc) {
131130
if (!cc.isInterface()) {
132131
CtMethod[] methods = cc.getDeclaredMethods();

0 commit comments

Comments
 (0)