-
Notifications
You must be signed in to change notification settings - Fork 677
Expand file tree
/
Copy pathSWDescriptionStrategy.java
More file actions
404 lines (343 loc) · 14.9 KB
/
SWDescriptionStrategy.java
File metadata and controls
404 lines (343 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.bytebuddy.agent.builder;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.MethodList;
import net.bytebuddy.description.type.*;
import net.bytebuddy.dynamic.TargetType;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.utility.JavaModule;
import net.bytebuddy.utility.nullability.MaybeNull;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* A DescriptionStrategy to get the original class description by removing dynamic field and method tokens
* generated by SkyWalking.
*/
public class SWDescriptionStrategy implements AgentBuilder.DescriptionStrategy {
/**
* A cache of type descriptions for commonly used types to avoid unnecessary allocations.
*/
private static final Map<Class<?>, TypeDescription> TYPE_CACHE;
/*
* Initializes the type cache.
*/
static {
TYPE_CACHE = new HashMap<Class<?>, TypeDescription>();
TYPE_CACHE.put(TargetType.class, new TypeDescription.ForLoadedType(TargetType.class));
TYPE_CACHE.put(Class.class, new TypeDescription.ForLoadedType(Class.class));
TYPE_CACHE.put(Throwable.class, new TypeDescription.ForLoadedType(Throwable.class));
TYPE_CACHE.put(Annotation.class, new TypeDescription.ForLoadedType(Annotation.class));
TYPE_CACHE.put(Object.class, new TypeDescription.ForLoadedType(Object.class));
TYPE_CACHE.put(String.class, new TypeDescription.ForLoadedType(String.class));
TYPE_CACHE.put(Boolean.class, new TypeDescription.ForLoadedType(Boolean.class));
TYPE_CACHE.put(Byte.class, new TypeDescription.ForLoadedType(Byte.class));
TYPE_CACHE.put(Short.class, new TypeDescription.ForLoadedType(Short.class));
TYPE_CACHE.put(Character.class, new TypeDescription.ForLoadedType(Character.class));
TYPE_CACHE.put(Integer.class, new TypeDescription.ForLoadedType(Integer.class));
TYPE_CACHE.put(Long.class, new TypeDescription.ForLoadedType(Long.class));
TYPE_CACHE.put(Float.class, new TypeDescription.ForLoadedType(Float.class));
TYPE_CACHE.put(Double.class, new TypeDescription.ForLoadedType(Double.class));
TYPE_CACHE.put(void.class, new TypeDescription.ForLoadedType(void.class));
TYPE_CACHE.put(boolean.class, new TypeDescription.ForLoadedType(boolean.class));
TYPE_CACHE.put(byte.class, new TypeDescription.ForLoadedType(byte.class));
TYPE_CACHE.put(short.class, new TypeDescription.ForLoadedType(short.class));
TYPE_CACHE.put(char.class, new TypeDescription.ForLoadedType(char.class));
TYPE_CACHE.put(int.class, new TypeDescription.ForLoadedType(int.class));
TYPE_CACHE.put(long.class, new TypeDescription.ForLoadedType(long.class));
TYPE_CACHE.put(float.class, new TypeDescription.ForLoadedType(float.class));
TYPE_CACHE.put(double.class, new TypeDescription.ForLoadedType(double.class));
}
private AgentBuilder.DescriptionStrategy delegate = Default.HYBRID;
private String nameTrait;
public SWDescriptionStrategy(String nameTrait) {
this.nameTrait = nameTrait;
}
@Override
public boolean isLoadedFirst() {
return true;
}
@Override
public TypeDescription apply(String name,
@MaybeNull Class<?> type,
TypePool typePool,
AgentBuilder.CircularityLock circularityLock,
@MaybeNull ClassLoader classLoader,
@MaybeNull JavaModule module) {
// find from type cache
if (type != null) {
TypeDescription typeDescription = TYPE_CACHE.get(type);
if (typeDescription != null) {
return typeDescription;
}
}
// wrap result
return new SWTypeDescriptionWrapper(delegate.apply(name, type, typePool, circularityLock, classLoader, module), nameTrait, classLoader, name);
}
/**
* A TypeDescription wrapper to remove fields, methods, interface generated by SkyWalking.
*/
static class SWTypeDescriptionWrapper extends TypeDescription.AbstractBase implements Serializable {
/**
* The class's serial version UID.
*/
private static final long serialVersionUID = 1L;
/**
* Original type cache.
* classloader hashcode -> ( typeName -> type cache )
*/
private static final Map<Integer, Map<String, TypeCache>> CLASS_LOADER_TYPE_CACHE = new ConcurrentHashMap<>();
private static final List<String> IGNORED_INTERFACES = Arrays.asList(EnhancedInstance.class.getName());
private MethodList<MethodDescription.InDefinedShape> methods;
private FieldList<FieldDescription.InDefinedShape> fields;
private final String nameTrait;
private ClassLoader classLoader;
private String typeName;
private TypeList.Generic interfaces;
private TypeDescription delegate;
private Generic superClass;
public SWTypeDescriptionWrapper(TypeDescription delegate, String nameTrait, ClassLoader classLoader, String typeName) {
this.delegate = delegate;
this.nameTrait = nameTrait;
this.classLoader = classLoader;
this.typeName = typeName;
}
private TypeCache getTypeCache() {
int classLoaderHashCode = classLoader != null ? classLoader.hashCode() : 0;
Map<String, TypeCache> typeCacheMap = CLASS_LOADER_TYPE_CACHE.computeIfAbsent(classLoaderHashCode, k -> new ConcurrentHashMap<>());
TypeCache typeCache = typeCacheMap.computeIfAbsent(typeName, k -> new TypeCache(typeName));
return typeCache;
}
@Override
public TypeList.Generic getInterfaces() {
if (this.interfaces == null) {
TypeList.Generic allInterfaces = delegate.getInterfaces();
if (allInterfaces.stream().anyMatch(s -> IGNORED_INTERFACES.contains(s.getTypeName()))) {
// remove interfaces added by SkyWalking
List<Generic> list = allInterfaces.stream()
.filter(s -> !IGNORED_INTERFACES.contains(s.getTypeName()))
.collect(Collectors.toList());
this.interfaces = new TypeList.Generic.Explicit(list);
} else {
this.interfaces = allInterfaces;
}
}
return this.interfaces;
}
@Override
public FieldList<FieldDescription.InDefinedShape> getDeclaredFields() {
if (this.fields == null) {
FieldList<FieldDescription.InDefinedShape> declaredFields = delegate.getDeclaredFields();
TypeCache typeCache = getTypeCache();
if (typeCache.fieldNames == null) {
// save origin fields
typeCache.fieldNames = declaredFields.stream().map(WithRuntimeName::getName).collect(Collectors.toSet());
fields = declaredFields;
} else {
// return origin fields
fields = new FieldList.Explicit<>(declaredFields.stream()
.filter(f -> typeCache.fieldNames.contains(f.getName()))
.collect(Collectors.toList()));
}
}
return fields;
}
@Override
public MethodList<MethodDescription.InDefinedShape> getDeclaredMethods() {
if (this.methods == null) {
MethodList<MethodDescription.InDefinedShape> declaredMethods = delegate.getDeclaredMethods();
TypeCache typeCache = getTypeCache();
if (typeCache.methodCodes == null) {
// save original methods
typeCache.methodCodes = declaredMethods.stream().map(m -> m.toString().hashCode()).collect(Collectors.toSet());
methods = declaredMethods;
} else {
// return original methods in the same order, remove dynamic method tokens generated by SkyWalking and ByteBuddy
// remove generated methods for delegating superclass methods, such as Jedis.
methods = new MethodList.Explicit<>(declaredMethods.stream()
.filter(m -> typeCache.methodCodes.contains(m.toString().hashCode()))
.collect(Collectors.toList()));
}
}
return methods;
}
@Override
public boolean isAssignableTo(Class<?> type) {
// ignore interface added by SkyWalking
if (IGNORED_INTERFACES.contains(type.getName())) {
return false;
}
return delegate.isAssignableTo(type);
}
@Override
public boolean isAccessibleTo(TypeDescription typeDescription) {
// ignore interface added by SkyWalking
if (IGNORED_INTERFACES.contains(typeDescription.getName())) {
return false;
}
return delegate.isAccessibleTo(typeDescription);
}
@Override
public RecordComponentList<RecordComponentDescription.InDefinedShape> getRecordComponents() {
return delegate.getRecordComponents();
}
@Override
public TypeDescription getComponentType() {
return delegate.getComponentType();
}
@Override
public TypeDescription getDeclaringType() {
return delegate.getDeclaringType();
}
@Override
public TypeList getDeclaredTypes() {
return delegate.getDeclaredTypes();
}
@Override
public MethodDescription.InDefinedShape getEnclosingMethod() {
return delegate.getEnclosingMethod();
}
@Override
public TypeDescription getEnclosingType() {
return delegate.getEnclosingType();
}
@Override
public String getSimpleName() {
return delegate.getSimpleName();
}
@Override
public String getCanonicalName() {
return delegate.getCanonicalName();
}
@Override
public boolean isAnonymousType() {
return delegate.isAnonymousType();
}
@Override
public boolean isLocalType() {
return delegate.isLocalType();
}
@Override
public PackageDescription getPackage() {
return delegate.getPackage();
}
@Override
public TypeDescription getNestHost() {
return delegate.getNestHost();
}
@Override
public TypeList getNestMembers() {
return delegate.getNestMembers();
}
@Override
public TypeList getPermittedSubtypes() {
return delegate.getPermittedSubtypes();
}
@Override
public String getDescriptor() {
return delegate.getDescriptor();
}
@Override
public String getName() {
return delegate.getName();
}
@Override
public TypeList.Generic getTypeVariables() {
return delegate.getTypeVariables();
}
@Override
public AnnotationList getDeclaredAnnotations() {
return delegate.getDeclaredAnnotations();
}
@Override
public Generic getSuperClass() {
if (this.superClass == null) {
Generic delegateSuperClass = delegate.getSuperClass();
if (delegateSuperClass == null) {
return null;
}
this.superClass = new ForLoadedSuperClassWrapper(
new SWTypeDescriptionWrapper(delegateSuperClass.asErasure(), this.nameTrait, null, delegateSuperClass.asErasure().getName()),
delegateSuperClass
);
}
return this.superClass;
}
@Override
public StackSize getStackSize() {
return delegate.getStackSize();
}
@Override
public boolean isArray() {
return delegate.isArray();
}
@Override
public boolean isRecord() {
return delegate.isRecord();
}
@Override
public boolean isPrimitive() {
return delegate.isPrimitive();
}
@Override
public int getModifiers() {
return delegate.getModifiers();
}
}
static class ForLoadedSuperClassWrapper extends TypeDescription.Generic.LazyProjection.ForLoadedSuperClass {
private final TypeDescription delegation;
private final TypeDescription.Generic superGeneric;
protected ForLoadedSuperClassWrapper(TypeDescription delegation, TypeDescription.Generic superGeneric) {
super(null); // HACK: a trick here since type is not used anywhere in the wrapper
this.delegation = delegation;
this.superGeneric = superGeneric;
}
@Override
protected TypeDescription.Generic resolve() {
return this.delegation.asGenericType();
}
@Override
public TypeDescription asErasure() {
return this.delegation;
}
@Override
public AnnotationList getDeclaredAnnotations() {
return superGeneric.getDeclaredAnnotations();
}
}
static class TypeCache {
private String typeName;
private Set<Integer> methodCodes;
private Set<String> fieldNames;
public TypeCache(String typeName) {
this.typeName = typeName;
}
}
}