summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/mapping/EntryFactory.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuchaz/enigma/mapping/EntryFactory.java')
-rw-r--r--src/cuchaz/enigma/mapping/EntryFactory.java184
1 files changed, 184 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/EntryFactory.java b/src/cuchaz/enigma/mapping/EntryFactory.java
new file mode 100644
index 0000000..dceea29
--- /dev/null
+++ b/src/cuchaz/enigma/mapping/EntryFactory.java
@@ -0,0 +1,184 @@
1package cuchaz.enigma.mapping;
2
3import java.util.List;
4
5import javassist.CtBehavior;
6import javassist.CtClass;
7import javassist.CtConstructor;
8import javassist.CtField;
9import javassist.CtMethod;
10import javassist.bytecode.Descriptor;
11import javassist.expr.ConstructorCall;
12import javassist.expr.FieldAccess;
13import javassist.expr.MethodCall;
14import javassist.expr.NewExpr;
15
16import com.beust.jcommander.internal.Lists;
17import com.strobel.assembler.metadata.MethodDefinition;
18
19import cuchaz.enigma.analysis.JarIndex;
20
21public class EntryFactory {
22
23 public static ClassEntry getClassEntry(CtClass c) {
24 return new ClassEntry(Descriptor.toJvmName(c.getName()));
25 }
26
27 public static ClassEntry getObfClassEntry(JarIndex jarIndex, ClassMapping classMapping) {
28 return new ClassEntry(getChainedOuterClassName(jarIndex, classMapping.getObfName()));
29 }
30
31 private static String getChainedOuterClassName(JarIndex jarIndex, String obfClassName) {
32
33 // lookup the chain of outer classes
34 List<String> obfOuterClassNames = Lists.newArrayList();
35 String checkName = obfClassName;
36 while (true) {
37
38 // if this class name has a package, then it can't be an inner class
39 if (!new ClassEntry(checkName).isInDefaultPackage()) {
40 break;
41 }
42
43 String obfOuterClassName = jarIndex.getOuterClass(checkName);
44 if (obfOuterClassName != null) {
45 obfOuterClassNames.add(obfOuterClassName);
46 checkName = obfOuterClassName;
47 } else {
48 break;
49 }
50 }
51
52 // build the chained class name
53 StringBuilder buf = new StringBuilder();
54 for (int i=obfOuterClassNames.size()-1; i>=0; i--) {
55 buf.append(obfOuterClassNames.get(i));
56 buf.append("$");
57 }
58 buf.append(obfClassName);
59 return buf.toString();
60 }
61
62 public static ClassEntry getDeobfClassEntry(ClassMapping classMapping) {
63 return new ClassEntry(classMapping.getDeobfName());
64 }
65
66 public static ClassEntry getSuperclassEntry(CtClass c) {
67 return new ClassEntry(Descriptor.toJvmName(c.getClassFile().getSuperclass()));
68 }
69
70 public static FieldEntry getFieldEntry(CtField field) {
71 return new FieldEntry(
72 getClassEntry(field.getDeclaringClass()),
73 field.getName(),
74 new Type(field.getFieldInfo().getDescriptor())
75 );
76 }
77
78 public static FieldEntry getFieldEntry(FieldAccess call) {
79 return new FieldEntry(
80 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
81 call.getFieldName(),
82 new Type(call.getSignature())
83 );
84 }
85
86 public static MethodEntry getMethodEntry(CtMethod method) {
87 return new MethodEntry(
88 getClassEntry(method.getDeclaringClass()),
89 method.getName(),
90 new Signature(method.getMethodInfo().getDescriptor())
91 );
92 }
93
94 public static MethodEntry getMethodEntry(MethodCall call) {
95 return new MethodEntry(
96 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
97 call.getMethodName(),
98 new Signature(call.getSignature())
99 );
100 }
101
102 public static MethodEntry getMethodEntry(MethodDefinition def) {
103 return new MethodEntry(
104 new ClassEntry(def.getDeclaringType().getInternalName()),
105 def.getName(),
106 new Signature(def.getSignature())
107 );
108 }
109
110 public static ConstructorEntry getConstructorEntry(CtConstructor constructor) {
111 if (constructor.isClassInitializer()) {
112 return new ConstructorEntry(
113 getClassEntry(constructor.getDeclaringClass())
114 );
115 } else {
116 return new ConstructorEntry(
117 getClassEntry(constructor.getDeclaringClass()),
118 new Signature(constructor.getMethodInfo().getDescriptor())
119 );
120 }
121 }
122
123 public static ConstructorEntry getConstructorEntry(ConstructorCall call) {
124 return new ConstructorEntry(
125 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
126 new Signature(call.getSignature())
127 );
128 }
129
130 public static ConstructorEntry getConstructorEntry(NewExpr call) {
131 return new ConstructorEntry(
132 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
133 new Signature(call.getSignature())
134 );
135 }
136
137 public static ConstructorEntry getConstructorEntry(MethodDefinition def) {
138 if (def.isTypeInitializer()) {
139 return new ConstructorEntry(
140 new ClassEntry(def.getDeclaringType().getInternalName())
141 );
142 } else {
143 return new ConstructorEntry(
144 new ClassEntry(def.getDeclaringType().getInternalName()),
145 new Signature(def.getSignature())
146 );
147 }
148 }
149
150 public static BehaviorEntry getBehaviorEntry(CtBehavior behavior) {
151 if (behavior instanceof CtMethod) {
152 return getMethodEntry((CtMethod)behavior);
153 } else if (behavior instanceof CtConstructor) {
154 return getConstructorEntry((CtConstructor)behavior);
155 }
156 throw new Error("behavior is neither Method nor Constructor!");
157 }
158
159 public static BehaviorEntry getBehaviorEntry(String className, String behaviorName, String behaviorSignature) {
160 return getBehaviorEntry(new ClassEntry(className), behaviorName, new Signature(behaviorSignature));
161 }
162
163 public static BehaviorEntry getBehaviorEntry(ClassEntry classEntry, String behaviorName, Signature behaviorSignature) {
164 if (behaviorName.equals("<init>")) {
165 return new ConstructorEntry(classEntry, behaviorSignature);
166 } else if(behaviorName.equals("<clinit>")) {
167 return new ConstructorEntry(classEntry);
168 } else {
169 return new MethodEntry(classEntry, behaviorName, behaviorSignature);
170 }
171 }
172
173 public static BehaviorEntry getBehaviorEntry(MethodDefinition def) {
174 if (def.isConstructor() || def.isTypeInitializer()) {
175 return getConstructorEntry(def);
176 } else {
177 return getMethodEntry(def);
178 }
179 }
180
181 public static BehaviorEntry getObfBehaviorEntry(ClassEntry classEntry, MethodMapping methodMapping) {
182 return getBehaviorEntry(classEntry, methodMapping.getObfName(), methodMapping.getObfSignature());
183 }
184}