@@ -102,13 +102,13 @@ public List<Reference.Mismatch> getMismatchedReferenceSources(ClassLoader loader
102
102
loader = Utils .getBootstrapProxy ();
103
103
}
104
104
105
- final List <Mismatch > mismatches = new ArrayList <> ();
105
+ List <Mismatch > mismatches = Collections . emptyList ();
106
106
107
107
for (final Reference reference : references ) {
108
108
// Don't reference-check helper classes.
109
109
// They will be injected by the instrumentation's HelperInjector.
110
110
if (!helperClassNames .contains (reference .getClassName ())) {
111
- mismatches . addAll ( checkMatch (reference , loader ));
111
+ mismatches = lazyAddAll ( mismatches , checkMatch (reference , loader ));
112
112
}
113
113
}
114
114
@@ -126,10 +126,8 @@ private static List<Reference.Mismatch> checkMatch(
126
126
final TypePool typePool =
127
127
AgentTooling .poolStrategy ()
128
128
.typePool (AgentTooling .locationStrategy ().classFileLocator (loader ), loader );
129
- final List <Mismatch > mismatches = new ArrayList <>();
130
129
try {
131
- final TypePool .Resolution resolution =
132
- typePool .describe (Utils .getClassName (reference .getClassName ()));
130
+ final TypePool .Resolution resolution = typePool .describe (reference .getClassName ());
133
131
if (!resolution .isResolved ()) {
134
132
return Collections .<Mismatch >singletonList (
135
133
new Mismatch .MissingClass (
@@ -141,41 +139,45 @@ private static List<Reference.Mismatch> checkMatch(
141
139
// bytebuddy throws an illegal state exception with this message if it cannot resolve types
142
140
// TODO: handle missing type resolutions without catching bytebuddy's exceptions
143
141
final String className = e .getMessage ().replace ("Cannot resolve type description for " , "" );
144
- mismatches . add (
142
+ return Collections .< Mismatch > singletonList (
145
143
new Mismatch .MissingClass (reference .getSources ().toArray (new Source [0 ]), className ));
146
144
} else {
147
145
// Shouldn't happen. Fail the reference check and add a mismatch for debug logging.
148
- mismatches .add (new Mismatch .ReferenceCheckError (e , reference , loader ));
146
+ return Collections .<Mismatch >singletonList (
147
+ new Mismatch .ReferenceCheckError (e , reference , loader ));
149
148
}
150
149
}
151
- return mismatches ;
152
150
}
153
151
154
152
public static List <Reference .Mismatch > checkMatch (
155
153
final Reference reference , final TypeDescription typeOnClasspath ) {
156
- final List <Mismatch > mismatches = new ArrayList <> ();
154
+ List <Mismatch > mismatches = Collections . emptyList ();
157
155
158
156
for (final Reference .Flag flag : reference .getFlags ()) {
159
157
if (!flag .matches (typeOnClasspath .getModifiers ())) {
160
158
final String desc = reference .getClassName ();
161
- mismatches .add (
162
- new Mismatch .MissingFlag (
163
- reference .getSources ().toArray (new Source [0 ]),
164
- desc ,
165
- flag ,
166
- typeOnClasspath .getModifiers ()));
159
+ mismatches =
160
+ lazyAdd (
161
+ mismatches ,
162
+ new Mismatch .MissingFlag (
163
+ reference .getSources ().toArray (new Source [0 ]),
164
+ desc ,
165
+ flag ,
166
+ typeOnClasspath .getModifiers ()));
167
167
}
168
168
}
169
169
170
170
for (final Reference .Field fieldRef : reference .getFields ()) {
171
171
final FieldDescription .InDefinedShape fieldDescription = findField (fieldRef , typeOnClasspath );
172
172
if (fieldDescription == null ) {
173
- mismatches .add (
174
- new Reference .Mismatch .MissingField (
175
- fieldRef .getSources ().toArray (new Reference .Source [0 ]),
176
- reference .getClassName (),
177
- fieldRef .getName (),
178
- fieldRef .getType ().getInternalName ()));
173
+ mismatches =
174
+ lazyAdd (
175
+ mismatches ,
176
+ new Reference .Mismatch .MissingField (
177
+ fieldRef .getSources ().toArray (new Reference .Source [0 ]),
178
+ reference .getClassName (),
179
+ fieldRef .getName (),
180
+ fieldRef .getType ().getInternalName ()));
179
181
} else {
180
182
for (final Reference .Flag flag : fieldRef .getFlags ()) {
181
183
if (!flag .matches (fieldDescription .getModifiers ())) {
@@ -184,12 +186,14 @@ public static List<Reference.Mismatch> checkMatch(
184
186
+ "#"
185
187
+ fieldRef .getName ()
186
188
+ fieldRef .getType ().getInternalName ();
187
- mismatches .add (
188
- new Mismatch .MissingFlag (
189
- fieldRef .getSources ().toArray (new Source [0 ]),
190
- desc ,
191
- flag ,
192
- fieldDescription .getModifiers ()));
189
+ mismatches =
190
+ lazyAdd (
191
+ mismatches ,
192
+ new Mismatch .MissingFlag (
193
+ fieldRef .getSources ().toArray (new Source [0 ]),
194
+ desc ,
195
+ flag ,
196
+ fieldDescription .getModifiers ()));
193
197
}
194
198
}
195
199
}
@@ -199,27 +203,30 @@ public static List<Reference.Mismatch> checkMatch(
199
203
final MethodDescription .InDefinedShape methodDescription =
200
204
findMethod (methodRef , typeOnClasspath );
201
205
if (methodDescription == null ) {
202
- mismatches .add (
203
- new Reference .Mismatch .MissingMethod (
204
- methodRef .getSources ().toArray (new Reference .Source [0 ]),
205
- methodRef .getName (),
206
- methodRef .getDescriptor ()));
206
+ mismatches =
207
+ lazyAdd (
208
+ mismatches ,
209
+ new Reference .Mismatch .MissingMethod (
210
+ methodRef .getSources ().toArray (new Reference .Source [0 ]),
211
+ methodRef .getName (),
212
+ methodRef .getDescriptor ()));
207
213
} else {
208
214
for (final Reference .Flag flag : methodRef .getFlags ()) {
209
215
if (!flag .matches (methodDescription .getModifiers ())) {
210
216
final String desc =
211
217
reference .getClassName () + "#" + methodRef .getName () + methodRef .getDescriptor ();
212
- mismatches .add (
213
- new Mismatch .MissingFlag (
214
- methodRef .getSources ().toArray (new Source [0 ]),
215
- desc ,
216
- flag ,
217
- methodDescription .getModifiers ()));
218
+ mismatches =
219
+ lazyAdd (
220
+ mismatches ,
221
+ new Mismatch .MissingFlag (
222
+ methodRef .getSources ().toArray (new Source [0 ]),
223
+ desc ,
224
+ flag ,
225
+ methodDescription .getModifiers ()));
218
226
}
219
227
}
220
228
}
221
229
}
222
-
223
230
return mismatches ;
224
231
}
225
232
@@ -293,4 +300,21 @@ private static MethodDescription.InDefinedShape findMethod(
293
300
}
294
301
return null ;
295
302
}
303
+
304
+ // optimization to avoid ArrayList allocation in the common case when there are no mismatches
305
+ private static List <Mismatch > lazyAdd (List <Mismatch > mismatches , Mismatch mismatch ) {
306
+ List <Mismatch > result = mismatches .isEmpty () ? new ArrayList <Mismatch >() : mismatches ;
307
+ result .add (mismatch );
308
+ return result ;
309
+ }
310
+
311
+ // optimization to avoid ArrayList allocation in the common case when there are no mismatches
312
+ private static List <Mismatch > lazyAddAll (List <Mismatch > mismatches , List <Mismatch > toAdd ) {
313
+ if (!toAdd .isEmpty ()) {
314
+ List <Mismatch > result = mismatches .isEmpty () ? new ArrayList <Mismatch >() : mismatches ;
315
+ result .addAll (toAdd );
316
+ return result ;
317
+ }
318
+ return mismatches ;
319
+ }
296
320
}
0 commit comments