@@ -17,54 +17,65 @@ import java.util.concurrent.atomic.AtomicBoolean
1717 */
1818class SourceProbePluginDefine : ClassInstanceMethodsEnhancePluginDefine () {
1919
20+ companion object {
21+ private val probeStarted = AtomicBoolean ()
22+ private val cache = HashMap <String , ClassMatch >()
23+ }
24+
2025 override fun enhanceClass (): ClassMatch {
21- val defaultClass = Class .forName(
22- " org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$ Default"
23- )
24- val dispatcherField = defaultClass.getDeclaredField(" DISPATCHER" )
25- makeAccessible(dispatcherField)
26- val realDispatcher = dispatcherField.get(null )
27- dispatcherField.set(null , null )
26+ if (SourceProbe .isAgentInitialized) {
27+ if (ProbeConfiguration .isNotQuite) println (" SourceProbe is already initialized" )
28+ return NameMatch .byName(" " ) // ignore
29+ }
2830
29- val dispatcherClass = Class .forName(
30- " org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$ Default\$ Dispatcher"
31- )
32- val addTransformerMethod = dispatcherClass.getDeclaredMethod(
33- " addTransformer" ,
34- Instrumentation ::class .java, ClassFileTransformer ::class .java, Boolean ::class .java
35- )
36- val setNativeMethodPrefixMethod = dispatcherClass.getDeclaredMethod(
37- " setNativeMethodPrefix" ,
38- Instrumentation ::class .java, ClassFileTransformer ::class .java, String ::class .java
39- )
40- val isNativeMethodPrefixSupportedMethod = dispatcherClass.getDeclaredMethod(
41- " isNativeMethodPrefixSupported" , Instrumentation ::class .java
42- )
31+ return cache.computeIfAbsent(" cache" ) {
32+ val defaultClass = Class .forName(
33+ " org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$ Default"
34+ )
35+ val dispatcherField = defaultClass.getDeclaredField(" DISPATCHER" )
36+ makeAccessible(dispatcherField)
37+ val realDispatcher = dispatcherField.get(null )
38+ dispatcherField.set(null , null )
4339
44- val probeStarted = AtomicBoolean ()
45- val proxyDispatcher = Proxy .newProxyInstance(
46- dispatcherClass.classLoader,
47- arrayOf(dispatcherClass)
48- ) { _, method, args ->
49- return @newProxyInstance when (method.name) {
50- " addTransformer" -> {
51- if (probeStarted.compareAndSet(false , true )) {
52- SourceProbe .bootAsPlugin(args?.get(0 ) as Instrumentation )
40+ val dispatcherClass = Class .forName(
41+ " org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$ Default\$ Dispatcher"
42+ )
43+ val addTransformerMethod = dispatcherClass.getDeclaredMethod(
44+ " addTransformer" ,
45+ Instrumentation ::class .java, ClassFileTransformer ::class .java, Boolean ::class .java
46+ )
47+ val setNativeMethodPrefixMethod = dispatcherClass.getDeclaredMethod(
48+ " setNativeMethodPrefix" ,
49+ Instrumentation ::class .java, ClassFileTransformer ::class .java, String ::class .java
50+ )
51+ val isNativeMethodPrefixSupportedMethod = dispatcherClass.getDeclaredMethod(
52+ " isNativeMethodPrefixSupported" , Instrumentation ::class .java
53+ )
54+
55+ val proxyDispatcher = Proxy .newProxyInstance(
56+ dispatcherClass.classLoader,
57+ arrayOf(dispatcherClass)
58+ ) { _, method, args ->
59+ return @newProxyInstance when (method.name) {
60+ " addTransformer" -> {
61+ if (probeStarted.compareAndSet(false , true )) {
62+ SourceProbe .bootAsPlugin(args?.get(0 ) as Instrumentation )
63+ }
64+ addTransformerMethod.invoke(realDispatcher, args[0 ], args[1 ], args[2 ])
5365 }
54- addTransformerMethod.invoke(realDispatcher, args[ 0 ], args[ 1 ], args[ 2 ])
55- }
56- " setNativeMethodPrefix " -> {
57- setNativeMethodPrefixMethod.invoke(realDispatcher, args[ 0 ], args[ 1 ], args[ 2 ])
58- }
59- " isNativeMethodPrefixSupported " -> {
60- isNativeMethodPrefixSupportedMethod.invoke(realDispatcher, args[ 0 ] )
66+ " setNativeMethodPrefix " -> {
67+ setNativeMethodPrefixMethod.invoke(realDispatcher, args[ 0 ], args[ 1 ], args[ 2 ])
68+ }
69+ " isNativeMethodPrefixSupported " -> {
70+ isNativeMethodPrefixSupportedMethod.invoke(realDispatcher, args[ 0 ])
71+ }
72+ else -> throw IllegalStateException ( " Unknown method: ${method.name} " )
6173 }
62- else -> throw IllegalStateException (" Unknown method: ${method.name} " )
6374 }
64- }
65- dispatcherField.set(null , proxyDispatcher)
75+ dispatcherField.set(null , proxyDispatcher)
6676
67- return NameMatch .byName(" " ) // ignore
77+ return @computeIfAbsent NameMatch .byName(" " ) // ignore
78+ }
6879 }
6980
7081 @Throws(Exception ::class )
0 commit comments