6
6
package io .opentelemetry .javaagent .instrumentation .internal .classloader ;
7
7
8
8
import static io .opentelemetry .javaagent .extension .matcher .AgentElementMatchers .extendsClass ;
9
- import static java .util .logging .Level .WARNING ;
10
9
import static net .bytebuddy .matcher .ElementMatchers .isMethod ;
11
10
import static net .bytebuddy .matcher .ElementMatchers .isProtected ;
12
11
import static net .bytebuddy .matcher .ElementMatchers .isPublic ;
17
16
import static net .bytebuddy .matcher .ElementMatchers .takesArgument ;
18
17
import static net .bytebuddy .matcher .ElementMatchers .takesArguments ;
19
18
20
- import io .opentelemetry .javaagent .bootstrap .BootstrapPackagePrefixesHolder ;
21
19
import io .opentelemetry .javaagent .bootstrap .CallDepth ;
22
20
import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
23
21
import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
24
- import io .opentelemetry .javaagent .tooling .Constants ;
25
- import java .lang .invoke .MethodHandle ;
26
- import java .lang .invoke .MethodHandles ;
27
- import java .lang .invoke .MethodType ;
28
- import java .util .List ;
29
- import java .util .logging .Logger ;
22
+ import io .opentelemetry .javaagent .tooling .Utils ;
23
+ import io .opentelemetry .javaagent .tooling .bytebuddy .ExceptionHandlers ;
24
+ import net .bytebuddy .agent .builder .AgentBuilder ;
30
25
import net .bytebuddy .asm .Advice ;
26
+ import net .bytebuddy .description .method .MethodDescription ;
31
27
import net .bytebuddy .description .type .TypeDescription ;
32
28
import net .bytebuddy .matcher .ElementMatcher ;
33
29
@@ -53,7 +49,7 @@ public ElementMatcher<TypeDescription> typeMatcher() {
53
49
54
50
@ Override
55
51
public void transform (TypeTransformer transformer ) {
56
- transformer . applyAdviceToMethod (
52
+ ElementMatcher . Junction < MethodDescription > methodMatcher =
57
53
isMethod ()
58
54
.and (named ("loadClass" ))
59
55
.and (
@@ -64,37 +60,14 @@ public void transform(TypeTransformer transformer) {
64
60
.and (takesArgument (0 , String .class ))
65
61
.and (takesArgument (1 , boolean .class ))))
66
62
.and (isPublic ().or (isProtected ()))
67
- .and (not (isStatic ())),
68
- BootDelegationInstrumentation .class .getName () + "$LoadClassAdvice" );
69
- }
70
-
71
- public static class Holder {
72
-
73
- public static final List <String > bootstrapPackagesPrefixes = findBootstrapPackagePrefixes ();
74
-
75
- /**
76
- * We have to make sure that {@link BootstrapPackagePrefixesHolder} is loaded from bootstrap
77
- * class loader. After that we can use in {@link LoadClassAdvice}.
78
- */
79
- private static List <String > findBootstrapPackagePrefixes () {
80
- try {
81
- Class <?> holderClass =
82
- Class .forName (
83
- "io.opentelemetry.javaagent.bootstrap.BootstrapPackagePrefixesHolder" , true , null );
84
- MethodHandle methodHandle =
85
- MethodHandles .publicLookup ()
86
- .findStatic (
87
- holderClass , "getBoostrapPackagePrefixes" , MethodType .methodType (List .class ));
88
- //noinspection unchecked
89
- return (List <String >) methodHandle .invokeExact ();
90
- } catch (Throwable e ) {
91
- Logger .getLogger (Holder .class .getName ())
92
- .log (WARNING , "Unable to load bootstrap package prefixes from the bootstrap CL" , e );
93
- return Constants .BOOTSTRAP_PACKAGE_PREFIXES ;
94
- }
95
- }
96
-
97
- private Holder () {}
63
+ .and (not (isStatic ()));
64
+ // Inline instrumentation to prevent problems with invokedynamic-recursion
65
+ transformer .applyTransformer (
66
+ new AgentBuilder .Transformer .ForAdvice ()
67
+ .include (Utils .getBootstrapProxy (), Utils .getAgentClassLoader ())
68
+ .withExceptionHandler (ExceptionHandlers .defaultExceptionHandler ())
69
+ .advice (
70
+ methodMatcher , BootDelegationInstrumentation .class .getName () + "$LoadClassAdvice" ));
98
71
}
99
72
100
73
@ SuppressWarnings ("unused" )
@@ -113,7 +86,7 @@ public static Class<?> onEnter(@Advice.Argument(0) String name) {
113
86
}
114
87
115
88
try {
116
- for (String prefix : Holder .bootstrapPackagesPrefixes ) {
89
+ for (String prefix : BootstrapPackagesHelper .bootstrapPackagesPrefixes ) {
117
90
if (name .startsWith (prefix )) {
118
91
try {
119
92
return Class .forName (name , false , null );
@@ -134,13 +107,12 @@ public static Class<?> onEnter(@Advice.Argument(0) String name) {
134
107
}
135
108
136
109
@ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
137
- @ Advice . AssignReturned . ToReturned
138
- public static Class <?> onExit (
139
- @ Advice .Return Class <?> result , @ Advice . Enter Class <?> resultFromBootstrapLoader ) {
110
+ public static void onExit (
111
+ @ Advice . Return ( readOnly = false ) Class <?> result ,
112
+ @ Advice .Enter Class <?> resultFromBootstrapLoader ) {
140
113
if (resultFromBootstrapLoader != null ) {
141
- return resultFromBootstrapLoader ;
114
+ result = resultFromBootstrapLoader ;
142
115
}
143
- return result ;
144
116
}
145
117
}
146
118
}
0 commit comments