|
10 | 10 | import com.tdunning.math.stats.Dist; |
11 | 11 | import com.tdunning.math.stats.MergingDigest; |
12 | 12 | import com.tdunning.math.stats.ScaleFunction; |
| 13 | +import com.wenshuo.agent.ConfigUtils; |
| 14 | +import com.wenshuo.agent.javassist.*; |
13 | 15 | import org.junit.Assert; |
14 | 16 | import org.junit.Ignore; |
15 | 17 | import org.junit.Test; |
16 | 18 |
|
17 | | -import com.wenshuo.agent.javassist.ClassPool; |
18 | | -import com.wenshuo.agent.javassist.CtClass; |
19 | | -import com.wenshuo.agent.javassist.CtMethod; |
20 | | -import com.wenshuo.agent.javassist.Modifier; |
21 | | -import com.wenshuo.agent.javassist.NotFoundException; |
22 | | - |
23 | 19 | public class TestCtMethod { |
24 | 20 |
|
25 | 21 | private static List<String> staticMethodNames = Arrays.asList("privateStatic","protectedStatic","publicStatic","defaultStatic"); |
@@ -53,6 +49,47 @@ static void defaultStatic(){ |
53 | 49 | System.out.println("I'm default and static"); |
54 | 50 | } |
55 | 51 |
|
| 52 | + public static Object publicStaticWithResult(){ |
| 53 | + String result = null; |
| 54 | + System.out.println("I'm public and static"); |
| 55 | + Random random = new Random(); |
| 56 | + if(random.nextInt(100) < 50){ |
| 57 | + result = new String("hello world:random.nextInt(100) < 50"); |
| 58 | + return result; |
| 59 | + } |
| 60 | + result = new String("hello world:random.nextInt(100) >= 50"); |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @Ignore |
| 66 | + public void asFinallyTest() throws Exception { |
| 67 | + String className = "com.wenshuo.agent.test.TestCtMethod"; |
| 68 | + ClassPool pool = ClassPool.getDefault(); |
| 69 | + CtClass ctClass = pool.get(className); |
| 70 | + |
| 71 | + transformerMethod(className, ctClass.getDeclaredMethod("publicStatic")); |
| 72 | + transformerMethod(className, ctClass.getDeclaredMethod("publicStaticWithResult")); |
| 73 | + |
| 74 | + ctClass.writeFile("/home/code/github/javaagent/agent/target"); |
| 75 | + } |
| 76 | + |
| 77 | + private static void transformerMethod(String className, CtMethod m) throws CannotCompileException { |
| 78 | + boolean isMethodStatic = Modifier.isStatic(m.getModifiers()); |
| 79 | + String aopClassName = isMethodStatic ? "\"" + className + "\"" : "this.getClass().getName()"; |
| 80 | + final String timeMethodStr |
| 81 | + = ConfigUtils.isUsingNanoTime() ? "java.lang.System.nanoTime()" : "java.lang.System.currentTimeMillis" |
| 82 | + + "()"; |
| 83 | + String LOG_UTILS = "com.wenshuo.agent.log.ExecuteLogUtils"; |
| 84 | + |
| 85 | + // 避免变量名重复 |
| 86 | + m.addLocalVariable("dingjsh_javaagent_elapsedTime", CtClass.longType); |
| 87 | + m.insertAfter("dingjsh_javaagent_elapsedTime = " + timeMethodStr + " - dingjsh_javaagent_elapsedTime;" |
| 88 | + + LOG_UTILS + ".log(" + aopClassName + ",\"" + m.getName() |
| 89 | + + "\",(long)dingjsh_javaagent_elapsedTime" + ");", true); |
| 90 | + m.insertBefore("dingjsh_javaagent_elapsedTime = " + timeMethodStr + ";"); |
| 91 | + } |
| 92 | + |
56 | 93 |
|
57 | 94 | /** |
58 | 95 | * 100个线程并发测试,每个线程往100个不同的Digest中添加5000000个随机数据[0, 100],并计算耗时<br/> |
|
0 commit comments