summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/mapping
diff options
context:
space:
mode:
authorGravatar jeff2015-02-09 22:23:45 -0500
committerGravatar jeff2015-02-09 22:23:45 -0500
commitaf1041731c8c0ce1846ff7e7b6052ed7327a5dbc (patch)
treee781b93f526a6c1ba7b6f5e14c319450199aa1df /src/cuchaz/enigma/mapping
parentDon't automatically move mappings around. We're more interested in stability ... (diff)
downloadenigma-fork-af1041731c8c0ce1846ff7e7b6052ed7327a5dbc.tar.gz
enigma-fork-af1041731c8c0ce1846ff7e7b6052ed7327a5dbc.tar.xz
enigma-fork-af1041731c8c0ce1846ff7e7b6052ed7327a5dbc.zip
fix translation issues, particularly with fields
Diffstat (limited to 'src/cuchaz/enigma/mapping')
-rw-r--r--src/cuchaz/enigma/mapping/BehaviorEntryFactory.java57
-rw-r--r--src/cuchaz/enigma/mapping/EntryFactory.java184
-rw-r--r--src/cuchaz/enigma/mapping/JavassistUtil.java85
3 files changed, 184 insertions, 142 deletions
diff --git a/src/cuchaz/enigma/mapping/BehaviorEntryFactory.java b/src/cuchaz/enigma/mapping/BehaviorEntryFactory.java
deleted file mode 100644
index 61e501b..0000000
--- a/src/cuchaz/enigma/mapping/BehaviorEntryFactory.java
+++ /dev/null
@@ -1,57 +0,0 @@
1/*******************************************************************************
2 * Copyright (c) 2014 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Public License v3.0
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.mapping;
12
13import javassist.CtBehavior;
14import javassist.CtConstructor;
15import javassist.CtMethod;
16import javassist.bytecode.Descriptor;
17
18public class BehaviorEntryFactory {
19
20 public static BehaviorEntry create(String className, String name, String signature) {
21 return create(new ClassEntry(className), name, signature);
22 }
23
24 public static BehaviorEntry create(ClassEntry classEntry, String name, String signature) {
25 if (name.equals("<init>")) {
26 return new ConstructorEntry(classEntry, new Signature(signature));
27 } else if (name.equals("<clinit>")) {
28 return new ConstructorEntry(classEntry);
29 } else {
30 return new MethodEntry(classEntry, name, new Signature(signature));
31 }
32 }
33
34 public static BehaviorEntry create(CtBehavior behavior) {
35 String className = Descriptor.toJvmName(behavior.getDeclaringClass().getName());
36 if (behavior instanceof CtMethod) {
37 return create(className, behavior.getName(), behavior.getSignature());
38 } else if (behavior instanceof CtConstructor) {
39 CtConstructor constructor = (CtConstructor)behavior;
40 if (constructor.isClassInitializer()) {
41 return create(className, "<clinit>", null);
42 } else {
43 return create(className, "<init>", constructor.getSignature());
44 }
45 } else {
46 throw new IllegalArgumentException("Unable to create BehaviorEntry from " + behavior);
47 }
48 }
49
50 public static BehaviorEntry createObf(ClassEntry classEntry, MethodMapping methodMapping) {
51 return create(classEntry, methodMapping.getObfName(), methodMapping.getObfSignature().toString());
52 }
53
54 public static BehaviorEntry createDeobf(ClassEntry classEntry, MethodMapping methodMapping) {
55 return create(classEntry, methodMapping.getDeobfName(), methodMapping.getObfSignature().toString());
56 }
57}
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}
diff --git a/src/cuchaz/enigma/mapping/JavassistUtil.java b/src/cuchaz/enigma/mapping/JavassistUtil.java
deleted file mode 100644
index 0d6ce6a..0000000
--- a/src/cuchaz/enigma/mapping/JavassistUtil.java
+++ /dev/null
@@ -1,85 +0,0 @@
1package cuchaz.enigma.mapping;
2
3import javassist.CtBehavior;
4import javassist.CtClass;
5import javassist.CtConstructor;
6import javassist.CtField;
7import javassist.CtMethod;
8import javassist.bytecode.Descriptor;
9import javassist.expr.ConstructorCall;
10import javassist.expr.FieldAccess;
11import javassist.expr.MethodCall;
12import javassist.expr.NewExpr;
13
14public class JavassistUtil {
15
16 public static ClassEntry getClassEntry(CtClass c) {
17 return new ClassEntry(Descriptor.toJvmName(c.getName()));
18 }
19
20 public static ClassEntry getSuperclassEntry(CtClass c) {
21 return new ClassEntry(Descriptor.toJvmName(c.getClassFile().getSuperclass()));
22 }
23
24 public static MethodEntry getMethodEntry(CtMethod method) {
25 return new MethodEntry(
26 getClassEntry(method.getDeclaringClass()),
27 method.getName(),
28 new Signature(method.getMethodInfo().getDescriptor())
29 );
30 }
31
32 public static MethodEntry getMethodEntry(MethodCall call) {
33 return new MethodEntry(
34 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
35 call.getMethodName(),
36 new Signature(call.getSignature())
37 );
38 }
39
40 public static ConstructorEntry getConstructorEntry(CtConstructor constructor) {
41 return new ConstructorEntry(
42 getClassEntry(constructor.getDeclaringClass()),
43 new Signature(constructor.getMethodInfo().getDescriptor())
44 );
45 }
46
47 public static ConstructorEntry getConstructorEntry(ConstructorCall call) {
48 return new ConstructorEntry(
49 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
50 new Signature(call.getSignature())
51 );
52 }
53
54 public static ConstructorEntry getConstructorEntry(NewExpr call) {
55 return new ConstructorEntry(
56 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
57 new Signature(call.getSignature())
58 );
59 }
60
61 public static BehaviorEntry getBehaviorEntry(CtBehavior behavior) {
62 if (behavior instanceof CtMethod) {
63 return getMethodEntry((CtMethod)behavior);
64 } else if (behavior instanceof CtConstructor) {
65 return getConstructorEntry((CtConstructor)behavior);
66 }
67 throw new Error("behavior is neither Method nor Constructor!");
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}