1
+ package net .cvs0 .core ;
2
+
3
+ import net .cvs0 .context .ObfuscationContext ;
4
+ import net .cvs0 .utils .BytecodeUtils ;
5
+ import org .objectweb .asm .ClassVisitor ;
6
+ import org .objectweb .asm .FieldVisitor ;
7
+ import org .objectweb .asm .MethodVisitor ;
8
+ import org .objectweb .asm .Opcodes ;
9
+
10
+ public abstract class BaseClassVisitor extends ClassVisitor implements ContextProvider
11
+ {
12
+ protected final ObfuscationContext context ;
13
+ protected String currentClassName ;
14
+
15
+ protected BaseClassVisitor (ClassVisitor classVisitor , ObfuscationContext context )
16
+ {
17
+ super (Opcodes .ASM9 , classVisitor );
18
+ this .context = context ;
19
+ }
20
+
21
+ @ Override
22
+ public final ObfuscationContext getContext ()
23
+ {
24
+ return context ;
25
+ }
26
+
27
+ @ Override
28
+ public final String getCurrentClassName ()
29
+ {
30
+ return currentClassName ;
31
+ }
32
+
33
+ @ Override
34
+ public void visit (int version , int access , String name , String signature , String superName , String [] interfaces )
35
+ {
36
+ this .currentClassName = name ;
37
+ super .visit (version , access , name , signature , superName , interfaces );
38
+ }
39
+
40
+ @ Override
41
+ public MethodVisitor visitMethod (int access , String name , String descriptor , String signature , String [] exceptions )
42
+ {
43
+ MethodVisitor mv = super .visitMethod (access , name , descriptor , signature , exceptions );
44
+
45
+ if (!shouldProcessMethod (name , access , descriptor )) {
46
+ return mv ;
47
+ }
48
+
49
+ return createMethodVisitor (mv , access , name , descriptor , signature , exceptions );
50
+ }
51
+
52
+ @ Override
53
+ public FieldVisitor visitField (int access , String name , String descriptor , String signature , Object value )
54
+ {
55
+ if (!shouldProcessField (name , access , descriptor )) {
56
+ return super .visitField (access , name , descriptor , signature , value );
57
+ }
58
+
59
+ return createFieldVisitor (super .visitField (access , name , descriptor , signature , value ),
60
+ access , name , descriptor , signature , value );
61
+ }
62
+
63
+ protected boolean shouldProcessMethod (String name , int access , String descriptor )
64
+ {
65
+ if (BytecodeUtils .isMethodSkippable (name , access )) {
66
+ return false ;
67
+ }
68
+
69
+ if (!context .getConfig ().isInPackageScope (currentClassName )) {
70
+ return false ;
71
+ }
72
+
73
+ return !context .getConfig ().shouldKeepMethod (currentClassName , name , descriptor );
74
+ }
75
+
76
+ protected boolean shouldProcessField (String name , int access , String descriptor )
77
+ {
78
+ if (BytecodeUtils .isFieldSkippable (name , access )) {
79
+ return false ;
80
+ }
81
+
82
+ if (!context .getConfig ().isInPackageScope (currentClassName )) {
83
+ return false ;
84
+ }
85
+
86
+ return !context .getConfig ().shouldKeepField (currentClassName , name );
87
+ }
88
+
89
+ protected boolean shouldProcessClass ()
90
+ {
91
+ return context .getConfig ().isInPackageScope (currentClassName ) &&
92
+ !context .getConfig ().shouldKeepClass (currentClassName );
93
+ }
94
+
95
+ protected void logTransformation (String message )
96
+ {
97
+ if (context .getConfig ().isVerbose ()) {
98
+ System .out .println ("[" + getTransformerName () + "] " + message + " in " + currentClassName );
99
+ }
100
+ }
101
+
102
+ protected abstract String getTransformerName ();
103
+
104
+ protected MethodVisitor createMethodVisitor (MethodVisitor mv , int access , String name ,
105
+ String descriptor , String signature , String [] exceptions )
106
+ {
107
+ return mv ;
108
+ }
109
+
110
+ protected FieldVisitor createFieldVisitor (FieldVisitor fv , int access , String name ,
111
+ String descriptor , String signature , Object value )
112
+ {
113
+ return fv ;
114
+ }
115
+ }
0 commit comments