@@ -100,75 +100,77 @@ public String toString() {
100
100
101
101
@ Override
102
102
public AdviceTransformer transformer () {
103
- // transformer possible adding extra 3 methods to ServletInputStreamWrapper
104
- return new AdviceTransformer () {
105
- private final Map <ClassLoader , Boolean > injectedClassLoaders =
106
- Collections .synchronizedMap (new WeakHashMap <ClassLoader , Boolean >());
103
+ return new CustomTransformer ();
104
+ }
107
105
108
- @ Override
109
- public DynamicType .Builder <?> transform (
110
- DynamicType .Builder <?> builder ,
111
- TypeDescription typeDescription ,
112
- ClassLoader classLoader ,
113
- JavaModule module ) {
114
- if (classLoader == null ) {
115
- classLoader = BOOTSTRAP_CLASSLOADER_PLACEHOLDER ;
116
- }
106
+ // transformer possible adding extra 3 methods to ServletInputStreamWrapper
107
+ static class CustomTransformer implements AdviceTransformer {
108
+ private static final String STREAM_WRAPPER_TYPE =
109
+ "datadog.trace.instrumentation.servlet.http.ServletInputStreamWrapper" ;
110
+ private final Map <ClassLoader , Boolean > injectedClassLoaders =
111
+ Collections .synchronizedMap (new WeakHashMap <ClassLoader , Boolean >());
117
112
118
- if (injectedClassLoaders .containsKey (classLoader )) {
119
- return builder ;
120
- }
121
- injectedClassLoaders .put (classLoader , Boolean .TRUE );
113
+ @ Override
114
+ public DynamicType .Builder <?> transform (
115
+ DynamicType .Builder <?> builder ,
116
+ TypeDescription typeDescription ,
117
+ ClassLoader classLoader ,
118
+ JavaModule module ) {
119
+ if (classLoader == null ) {
120
+ classLoader = BOOTSTRAP_CLASSLOADER_PLACEHOLDER ;
121
+ }
122
122
123
- TypePool .Resolution readListenerRes ;
124
- TypePool typePoolUserCl ;
125
- if (classLoader != BOOTSTRAP_CLASSLOADER_PLACEHOLDER ) {
126
- typePoolUserCl = TypePool .Default .of (classLoader );
127
- } else {
128
- typePoolUserCl = TypePool .Default .ofBootLoader ();
129
- }
130
- readListenerRes = typePoolUserCl .describe ("javax.servlet.ReadListener" );
131
- if (!readListenerRes .isResolved ()) {
132
- // likely servlet < 3.1
133
- // inject original
134
- return new HelperInjector (
135
- "servlet-request-body" , new String [] {packageName + ".ServletInputStreamWrapper" })
136
- .transform (builder , typeDescription , classLoader , module );
137
- }
123
+ if (injectedClassLoaders .containsKey (classLoader )) {
124
+ return builder ;
125
+ }
126
+ injectedClassLoaders .put (classLoader , Boolean .TRUE );
138
127
139
- // else at the very least servlet 3.1+ classes are available
140
- // modify ServletInputStreamWrapper before injecting it. This should be harmless even if
141
- // servlet 3.1 is on the
142
- // classpath without the implementation supporting it
143
- ClassFileLocator compoundLocator =
144
- new ClassFileLocator .Compound (
145
- ClassFileLocator .ForClassLoader .of (getClass ().getClassLoader ()),
146
- ClassFileLocator .ForClassLoader .of (classLoader ));
128
+ TypePool .Resolution readListenerRes ;
129
+ TypePool typePoolUserCl ;
130
+ if (classLoader != BOOTSTRAP_CLASSLOADER_PLACEHOLDER ) {
131
+ typePoolUserCl = TypePool .Default .of (classLoader );
132
+ } else {
133
+ typePoolUserCl = TypePool .Default .ofBootLoader ();
134
+ }
135
+ readListenerRes = typePoolUserCl .describe ("javax.servlet.ReadListener" );
136
+ if (!readListenerRes .isResolved ()) {
137
+ // likely servlet < 3.1
138
+ // inject original
139
+ return new HelperInjector ("servlet-request-body" , STREAM_WRAPPER_TYPE )
140
+ .transform (builder , typeDescription , classLoader , module );
141
+ }
147
142
148
- TypePool .Resolution origWrapperRes =
149
- TypePool .Default .of (compoundLocator )
150
- .describe (packageName + ".ServletInputStreamWrapper" );
151
- if (!origWrapperRes .isResolved ()) {
152
- throw new RuntimeException ("Could not load original ServletInputStreamWrapper" );
153
- }
154
- TypeDescription origWrapperType = origWrapperRes .resolve ();
143
+ // else at the very least servlet 3.1+ classes are available
144
+ // modify ServletInputStreamWrapper before injecting it. This should be harmless even if
145
+ // servlet 3.1 is on the
146
+ // classpath without the implementation supporting it
147
+ ClassFileLocator compoundLocator =
148
+ new ClassFileLocator .Compound (
149
+ ClassFileLocator .ForClassLoader .of (getClass ().getClassLoader ()),
150
+ ClassFileLocator .ForClassLoader .of (classLoader ));
155
151
156
- DynamicType .Unloaded <?> unloaded =
157
- new ByteBuddy ()
158
- .rebase (origWrapperType , compoundLocator )
159
- .method (named ("isFinished" ).and (takesNoArguments ()))
160
- .intercept (MethodDelegation .toField ("is" ))
161
- .method (named ("isReady" ).and (takesNoArguments ()))
162
- .intercept (MethodDelegation .toField ("is" ))
163
- .method (named ("setReadListener" ).and (takesArguments (readListenerRes .resolve ())))
164
- .intercept (MethodDelegation .toField ("is" ))
165
- .make ();
166
- return new HelperInjector (
167
- "servlet-request-body" ,
168
- Collections .singletonMap (origWrapperType .getName (), unloaded .getBytes ()))
169
- .transform (builder , typeDescription , classLoader , module );
152
+ TypePool .Resolution origWrapperRes =
153
+ TypePool .Default .of (compoundLocator ).describe (STREAM_WRAPPER_TYPE );
154
+ if (!origWrapperRes .isResolved ()) {
155
+ throw new RuntimeException ("Could not load original ServletInputStreamWrapper" );
170
156
}
171
- };
157
+ TypeDescription origWrapperType = origWrapperRes .resolve ();
158
+
159
+ DynamicType .Unloaded <?> unloaded =
160
+ new ByteBuddy ()
161
+ .rebase (origWrapperType , compoundLocator )
162
+ .method (named ("isFinished" ).and (takesNoArguments ()))
163
+ .intercept (MethodDelegation .toField ("is" ))
164
+ .method (named ("isReady" ).and (takesNoArguments ()))
165
+ .intercept (MethodDelegation .toField ("is" ))
166
+ .method (named ("setReadListener" ).and (takesArguments (readListenerRes .resolve ())))
167
+ .intercept (MethodDelegation .toField ("is" ))
168
+ .make ();
169
+ return new HelperInjector (
170
+ "servlet-request-body" ,
171
+ Collections .singletonMap (origWrapperType .getName (), unloaded .getBytes ()))
172
+ .transform (builder , typeDescription , classLoader , module );
173
+ }
172
174
}
173
175
174
176
@ SuppressWarnings ("Duplicates" )
0 commit comments