diff options
| author | 2015-02-05 23:51:40 -0500 | |
|---|---|---|
| committer | 2015-02-05 23:51:40 -0500 | |
| commit | 73313b0f1c1660986afc1449960889cac242eee0 (patch) | |
| tree | 90799533267205999d983821f70e81edc1593a83 /src/cuchaz/enigma/mapping/BehaviorSignature.java | |
| parent | turn off debug stuff (diff) | |
| download | enigma-fork-73313b0f1c1660986afc1449960889cac242eee0.tar.gz enigma-fork-73313b0f1c1660986afc1449960889cac242eee0.tar.xz enigma-fork-73313b0f1c1660986afc1449960889cac242eee0.zip | |
add new type/signature system
Diffstat (limited to 'src/cuchaz/enigma/mapping/BehaviorSignature.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/BehaviorSignature.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/BehaviorSignature.java b/src/cuchaz/enigma/mapping/BehaviorSignature.java new file mode 100644 index 0000000..a6371f8 --- /dev/null +++ b/src/cuchaz/enigma/mapping/BehaviorSignature.java | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | package cuchaz.enigma.mapping; | ||
| 2 | |||
| 3 | import java.util.List; | ||
| 4 | |||
| 5 | import com.beust.jcommander.internal.Lists; | ||
| 6 | |||
| 7 | public class BehaviorSignature { | ||
| 8 | |||
| 9 | public static interface ClassReplacer { | ||
| 10 | ClassEntry replace(ClassEntry entry); | ||
| 11 | } | ||
| 12 | |||
| 13 | private List<Type> m_argumentTypes; | ||
| 14 | private Type m_returnType; | ||
| 15 | |||
| 16 | public BehaviorSignature(String signature) { | ||
| 17 | m_argumentTypes = Lists.newArrayList(); | ||
| 18 | int i=0; | ||
| 19 | while (i<signature.length()) { | ||
| 20 | char c = signature.charAt(i); | ||
| 21 | if (c == '(') { | ||
| 22 | assert(m_argumentTypes.isEmpty()); | ||
| 23 | assert(m_returnType == null); | ||
| 24 | i++; | ||
| 25 | } else if (c == ')') { | ||
| 26 | i++; | ||
| 27 | break; | ||
| 28 | } else { | ||
| 29 | String type = Type.parseFirst(signature.substring(i)); | ||
| 30 | m_argumentTypes.add(new Type(type)); | ||
| 31 | i += type.length(); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | m_returnType = new Type(Type.parseFirst(signature.substring(i))); | ||
| 35 | } | ||
| 36 | |||
| 37 | public BehaviorSignature(BehaviorSignature other, ClassReplacer replacer) { | ||
| 38 | m_argumentTypes = Lists.newArrayList(other.m_argumentTypes); | ||
| 39 | for (int i=0; i<m_argumentTypes.size(); i++) { | ||
| 40 | Type type = m_argumentTypes.get(i); | ||
| 41 | if (type.isClass()) { | ||
| 42 | ClassEntry newClassEntry = replacer.replace(type.getClassEntry()); | ||
| 43 | if (newClassEntry != null) { | ||
| 44 | m_argumentTypes.set(i, new Type(newClassEntry)); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | m_returnType = other.m_returnType; | ||
| 49 | if (other.m_returnType.isClass()) { | ||
| 50 | ClassEntry newClassEntry = replacer.replace(m_returnType.getClassEntry()); | ||
| 51 | if (newClassEntry != null) { | ||
| 52 | m_returnType = new Type(newClassEntry); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | public List<Type> getArgumentTypes() { | ||
| 58 | return m_argumentTypes; | ||
| 59 | } | ||
| 60 | |||
| 61 | public Type getReturnType() { | ||
| 62 | return m_returnType; | ||
| 63 | } | ||
| 64 | |||
| 65 | @Override | ||
| 66 | public String toString() { | ||
| 67 | StringBuilder buf = new StringBuilder(); | ||
| 68 | buf.append("("); | ||
| 69 | for (int i=0; i<m_argumentTypes.size(); i++) { | ||
| 70 | if (i > 0) { | ||
| 71 | buf.append(","); | ||
| 72 | } | ||
| 73 | buf.append(m_argumentTypes.get(i).toString()); | ||
| 74 | } | ||
| 75 | buf.append(")"); | ||
| 76 | buf.append(m_returnType.toString()); | ||
| 77 | return buf.toString(); | ||
| 78 | } | ||
| 79 | |||
| 80 | public Iterable<Type> types() { | ||
| 81 | List<Type> types = Lists.newArrayList(); | ||
| 82 | types.addAll(m_argumentTypes); | ||
| 83 | types.add(m_returnType); | ||
| 84 | return types; | ||
| 85 | } | ||
| 86 | |||
| 87 | public Iterable<ClassEntry> classes() { | ||
| 88 | List<ClassEntry> out = Lists.newArrayList(); | ||
| 89 | for (Type type : types()) { | ||
| 90 | if (type.isClass()) { | ||
| 91 | out.add(type.getClassEntry()); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | return out; | ||
| 95 | } | ||
| 96 | } | ||