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.java166
1 files changed, 166 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..03d97ba
--- /dev/null
+++ b/src/cuchaz/enigma/mapping/EntryFactory.java
@@ -0,0 +1,166 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.mapping;
12
13import javassist.CtBehavior;
14import javassist.CtClass;
15import javassist.CtConstructor;
16import javassist.CtField;
17import javassist.CtMethod;
18import javassist.bytecode.Descriptor;
19import javassist.expr.ConstructorCall;
20import javassist.expr.FieldAccess;
21import javassist.expr.MethodCall;
22import javassist.expr.NewExpr;
23
24import cuchaz.enigma.analysis.JarIndex;
25
26public class EntryFactory {
27
28 public static ClassEntry getClassEntry(CtClass c) {
29 return new ClassEntry(Descriptor.toJvmName(c.getName()));
30 }
31
32 public static ClassEntry getObfClassEntry(JarIndex jarIndex, ClassMapping classMapping) {
33 ClassEntry obfClassEntry = new ClassEntry(classMapping.getObfFullName());
34 return obfClassEntry.buildClassEntry(jarIndex.getObfClassChain(obfClassEntry));
35 }
36
37 private static ClassEntry getObfClassEntry(ClassMapping classMapping) {
38 return new ClassEntry(classMapping.getObfFullName());
39 }
40
41 public static ClassEntry getDeobfClassEntry(ClassMapping classMapping) {
42 return new ClassEntry(classMapping.getDeobfName());
43 }
44
45 public static ClassEntry getSuperclassEntry(CtClass c) {
46 return new ClassEntry(Descriptor.toJvmName(c.getClassFile().getSuperclass()));
47 }
48
49 public static FieldEntry getFieldEntry(CtField field) {
50 return new FieldEntry(
51 getClassEntry(field.getDeclaringClass()),
52 field.getName(),
53 new Type(field.getFieldInfo().getDescriptor())
54 );
55 }
56
57 public static FieldEntry getFieldEntry(FieldAccess call) {
58 return new FieldEntry(
59 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
60 call.getFieldName(),
61 new Type(call.getSignature())
62 );
63 }
64
65 public static FieldEntry getFieldEntry(String className, String name, String type) {
66 return new FieldEntry(new ClassEntry(className), name, new Type(type));
67 }
68
69 public static FieldEntry getObfFieldEntry(ClassMapping classMapping, FieldMapping fieldMapping) {
70 return new FieldEntry(
71 getObfClassEntry(classMapping),
72 fieldMapping.getObfName(),
73 fieldMapping.getObfType()
74 );
75 }
76
77 public static MethodEntry getMethodEntry(CtMethod method) {
78 return new MethodEntry(
79 getClassEntry(method.getDeclaringClass()),
80 method.getName(),
81 new Signature(method.getMethodInfo().getDescriptor())
82 );
83 }
84
85 public static MethodEntry getMethodEntry(MethodCall call) {
86 return new MethodEntry(
87 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
88 call.getMethodName(),
89 new Signature(call.getSignature())
90 );
91 }
92
93 public static ConstructorEntry getConstructorEntry(CtConstructor constructor) {
94 if (constructor.isClassInitializer()) {
95 return new ConstructorEntry(
96 getClassEntry(constructor.getDeclaringClass())
97 );
98 } else {
99 return new ConstructorEntry(
100 getClassEntry(constructor.getDeclaringClass()),
101 new Signature(constructor.getMethodInfo().getDescriptor())
102 );
103 }
104 }
105
106 public static ConstructorEntry getConstructorEntry(ConstructorCall call) {
107 return new ConstructorEntry(
108 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
109 new Signature(call.getSignature())
110 );
111 }
112
113 public static ConstructorEntry getConstructorEntry(NewExpr call) {
114 return new ConstructorEntry(
115 new ClassEntry(Descriptor.toJvmName(call.getClassName())),
116 new Signature(call.getSignature())
117 );
118 }
119
120 public static BehaviorEntry getBehaviorEntry(CtBehavior behavior) {
121 if (behavior instanceof CtMethod) {
122 return getMethodEntry((CtMethod)behavior);
123 } else if (behavior instanceof CtConstructor) {
124 return getConstructorEntry((CtConstructor)behavior);
125 }
126 throw new Error("behavior is neither Method nor Constructor!");
127 }
128
129 public static BehaviorEntry getBehaviorEntry(String className, String behaviorName, String behaviorSignature) {
130 return getBehaviorEntry(new ClassEntry(className), behaviorName, new Signature(behaviorSignature));
131 }
132
133 public static BehaviorEntry getBehaviorEntry(String className, String behaviorName) {
134 return getBehaviorEntry(new ClassEntry(className), behaviorName);
135 }
136
137 public static BehaviorEntry getBehaviorEntry(String className) {
138 return new ConstructorEntry(new ClassEntry(className));
139 }
140
141 public static BehaviorEntry getBehaviorEntry(ClassEntry classEntry, String behaviorName, Signature behaviorSignature) {
142 if (behaviorName.equals("<init>")) {
143 return new ConstructorEntry(classEntry, behaviorSignature);
144 } else if(behaviorName.equals("<clinit>")) {
145 return new ConstructorEntry(classEntry);
146 } else {
147 return new MethodEntry(classEntry, behaviorName, behaviorSignature);
148 }
149 }
150
151 public static BehaviorEntry getBehaviorEntry(ClassEntry classEntry, String behaviorName) {
152 if(behaviorName.equals("<clinit>")) {
153 return new ConstructorEntry(classEntry);
154 } else {
155 throw new IllegalArgumentException("Only class initializers don't have signatures");
156 }
157 }
158
159 public static BehaviorEntry getObfBehaviorEntry(ClassEntry classEntry, MethodMapping methodMapping) {
160 return getBehaviorEntry(classEntry, methodMapping.getObfName(), methodMapping.getObfSignature());
161 }
162
163 public static BehaviorEntry getObfBehaviorEntry(ClassMapping classMapping, MethodMapping methodMapping) {
164 return getObfBehaviorEntry(getObfClassEntry(classMapping), methodMapping);
165 }
166}