1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package io.earcam.instrumental.agent.defy;
20
21 import static org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;
22
23 import java.lang.instrument.ClassFileTransformer;
24 import java.lang.instrument.IllegalClassFormatException;
25 import java.security.ProtectionDomain;
26
27 import org.objectweb.asm.ClassReader;
28 import org.objectweb.asm.ClassVisitor;
29 import org.objectweb.asm.ClassWriter;
30
31 class RemoveFinalTransformer implements ClassFileTransformer {
32
33 private final String[] prefixes;
34
35
36
37
38
39
40
41
42
43
44 RemoveFinalTransformer(String[] prefixes)
45 {
46 this.prefixes = prefixes;
47 }
48
49
50 @Override
51 public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)
52 throws IllegalClassFormatException
53 {
54 return prefixMatched(className) ? transform(classfileBuffer) : classfileBuffer;
55 }
56
57
58 private boolean prefixMatched(String className)
59 {
60 for(String prefix : prefixes) {
61 if(className.startsWith(prefix)) {
62 return true;
63 }
64 }
65 return false;
66 }
67
68
69 private byte[] transform(byte[] classfileBuffer)
70 {
71 ClassReader cr = new ClassReader(classfileBuffer);
72 ClassWriter cw = new ClassWriter(cr, COMPUTE_FRAMES);
73 ClassVisitor cv = new RemoveFinalVisitor(cw);
74 cr.accept(cv, 0);
75 return cw.toByteArray();
76 }
77 }