|
10 | 10 | import net.bytebuddy.jar.asm.commons.Remapper; |
11 | 11 |
|
12 | 12 | /** Shades advice bytecode by applying relocations to all references. */ |
13 | | -public final class AdviceShader extends Remapper { |
14 | | - private final DDCache<String, String> cache = DDCaches.newFixedSizeCache(64); |
| 13 | +public final class AdviceShader { |
| 14 | + private final Map<String, String> relocations; |
15 | 15 |
|
16 | | - /** Flattened sequence of old-prefix, new-prefix relocations. */ |
17 | | - private final String[] prefixes; |
| 16 | + private volatile Remapper remapper; |
18 | 17 |
|
19 | 18 | public static AdviceShader with(Map<String, String> relocations) { |
20 | | - return relocations != null ? new AdviceShader(relocations) : null; |
| 19 | + if (relocations != null) { |
| 20 | + return new AdviceShader(relocations); |
| 21 | + } |
| 22 | + return null; |
21 | 23 | } |
22 | 24 |
|
23 | | - AdviceShader(Map<String, String> relocations) { |
24 | | - // convert relocations to a flattened sequence: old-prefix, new-prefix, etc. |
25 | | - this.prefixes = new String[relocations.size() * 2]; |
26 | | - int i = 0; |
27 | | - for (Map.Entry<String, String> e : relocations.entrySet()) { |
28 | | - String oldPrefix = e.getKey(); |
29 | | - String newPrefix = e.getValue(); |
30 | | - if (oldPrefix.indexOf('.') > 0) { |
31 | | - // accept dotted prefixes, but store them in their internal form |
32 | | - this.prefixes[i++] = oldPrefix.replace('.', '/'); |
33 | | - this.prefixes[i++] = newPrefix.replace('.', '/'); |
34 | | - } else { |
35 | | - this.prefixes[i++] = oldPrefix; |
36 | | - this.prefixes[i++] = newPrefix; |
37 | | - } |
38 | | - } |
| 25 | + private AdviceShader(Map<String, String> relocations) { |
| 26 | + this.relocations = relocations; |
39 | 27 | } |
40 | 28 |
|
41 | 29 | /** Applies shading before calling the given {@link ClassVisitor}. */ |
42 | | - public ClassVisitor shade(ClassVisitor cv) { |
43 | | - return new ClassRemapper(cv, this); |
| 30 | + public ClassVisitor shadeClass(ClassVisitor cv) { |
| 31 | + if (null == remapper) { |
| 32 | + remapper = new AdviceMapper(); |
| 33 | + } |
| 34 | + return new ClassRemapper(cv, remapper); |
44 | 35 | } |
45 | 36 |
|
46 | 37 | /** Returns the result of shading the given bytecode. */ |
47 | | - public byte[] shade(byte[] bytecode) { |
| 38 | + public byte[] shadeClass(byte[] bytecode) { |
48 | 39 | ClassReader cr = new ClassReader(bytecode); |
49 | 40 | ClassWriter cw = new ClassWriter(null, 0); |
50 | | - cr.accept(shade(cw), 0); |
| 41 | + cr.accept(shadeClass(cw), 0); |
51 | 42 | return cw.toByteArray(); |
52 | 43 | } |
53 | 44 |
|
54 | | - @Override |
55 | | - public String map(String internalName) { |
56 | | - if (internalName.startsWith("java/") |
57 | | - || internalName.startsWith("datadog/") |
58 | | - || internalName.startsWith("net/bytebuddy/")) { |
59 | | - return internalName; // never shade these references |
| 45 | + final class AdviceMapper extends Remapper { |
| 46 | + private final DDCache<String, String> mappingCache = DDCaches.newFixedSizeCache(64); |
| 47 | + |
| 48 | + /** Flattened sequence of old-prefix, new-prefix relocations. */ |
| 49 | + private final String[] prefixes; |
| 50 | + |
| 51 | + AdviceMapper() { |
| 52 | + // convert relocations to a flattened sequence: old-prefix, new-prefix, etc. |
| 53 | + this.prefixes = new String[relocations.size() * 2]; |
| 54 | + int i = 0; |
| 55 | + for (Map.Entry<String, String> e : relocations.entrySet()) { |
| 56 | + String oldPrefix = e.getKey(); |
| 57 | + String newPrefix = e.getValue(); |
| 58 | + if (oldPrefix.indexOf('.') > 0) { |
| 59 | + // accept dotted prefixes, but store them in their internal form |
| 60 | + this.prefixes[i++] = oldPrefix.replace('.', '/'); |
| 61 | + this.prefixes[i++] = newPrefix.replace('.', '/'); |
| 62 | + } else { |
| 63 | + this.prefixes[i++] = oldPrefix; |
| 64 | + this.prefixes[i++] = newPrefix; |
| 65 | + } |
| 66 | + } |
60 | 67 | } |
61 | | - return cache.computeIfAbsent(internalName, this::shade); |
62 | | - } |
63 | 68 |
|
64 | | - @Override |
65 | | - public Object mapValue(Object value) { |
66 | | - if (value instanceof String) { |
67 | | - String text = (String) value; |
68 | | - if (text.isEmpty()) { |
69 | | - return text; |
70 | | - } else if (text.indexOf('.') > 0) { |
71 | | - return shadeDottedName(text); |
| 69 | + @Override |
| 70 | + public String map(String internalName) { |
| 71 | + if (internalName.startsWith("java/") |
| 72 | + || internalName.startsWith("datadog/") |
| 73 | + || internalName.startsWith("net/bytebuddy/")) { |
| 74 | + return internalName; // never shade these references |
| 75 | + } |
| 76 | + return mappingCache.computeIfAbsent(internalName, this::shadeInternalName); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Object mapValue(Object value) { |
| 81 | + if (value instanceof String) { |
| 82 | + String text = (String) value; |
| 83 | + if (text.isEmpty()) { |
| 84 | + return text; |
| 85 | + } else if (text.indexOf('.') > 0) { |
| 86 | + return shadeDottedName(text); |
| 87 | + } else { |
| 88 | + return shadeInternalName(text); |
| 89 | + } |
72 | 90 | } else { |
73 | | - return shade(text); |
| 91 | + return super.mapValue(value); |
74 | 92 | } |
75 | | - } else { |
76 | | - return super.mapValue(value); |
77 | 93 | } |
78 | | - } |
79 | 94 |
|
80 | | - private String shade(String internalName) { |
81 | | - for (int i = 0; i < prefixes.length; i += 2) { |
82 | | - if (internalName.startsWith(prefixes[i])) { |
83 | | - return prefixes[i + 1] + internalName.substring(prefixes[i].length()); |
| 95 | + private String shadeInternalName(String internalName) { |
| 96 | + for (int i = 0; i < prefixes.length; i += 2) { |
| 97 | + if (internalName.startsWith(prefixes[i])) { |
| 98 | + return prefixes[i + 1] + internalName.substring(prefixes[i].length()); |
| 99 | + } |
84 | 100 | } |
| 101 | + return internalName; |
85 | 102 | } |
86 | | - return internalName; |
87 | | - } |
88 | 103 |
|
89 | | - private String shadeDottedName(String name) { |
90 | | - String internalName = name.replace('.', '/'); |
91 | | - for (int i = 0; i < prefixes.length; i += 2) { |
92 | | - if (internalName.startsWith(prefixes[i])) { |
93 | | - return prefixes[i + 1].replace('/', '.') + name.substring(prefixes[i].length()); |
| 104 | + private String shadeDottedName(String name) { |
| 105 | + String internalName = name.replace('.', '/'); |
| 106 | + for (int i = 0; i < prefixes.length; i += 2) { |
| 107 | + if (internalName.startsWith(prefixes[i])) { |
| 108 | + return prefixes[i + 1].replace('/', '.') + name.substring(prefixes[i].length()); |
| 109 | + } |
94 | 110 | } |
| 111 | + return name; |
95 | 112 | } |
96 | | - return name; |
97 | 113 | } |
98 | 114 | } |
0 commit comments