diff options
| author | 2015-02-08 21:29:25 -0500 | |
|---|---|---|
| committer | 2015-02-08 21:29:25 -0500 | |
| commit | ed9b5cdfc648e86fd463bfa8d86b94c41671e14c (patch) | |
| tree | 2619bbc7e04dfa3b82f8dfd3b1d31f529766cd4b /src/cuchaz/enigma/mapping/Signature.java | |
| download | enigma-fork-ed9b5cdfc648e86fd463bfa8d86b94c41671e14c.tar.gz enigma-fork-ed9b5cdfc648e86fd463bfa8d86b94c41671e14c.tar.xz enigma-fork-ed9b5cdfc648e86fd463bfa8d86b94c41671e14c.zip | |
switch all classes to new signature/type system
Diffstat (limited to 'src/cuchaz/enigma/mapping/Signature.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/Signature.java | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/Signature.java b/src/cuchaz/enigma/mapping/Signature.java new file mode 100644 index 0000000..ff7f807 --- /dev/null +++ b/src/cuchaz/enigma/mapping/Signature.java | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | package cuchaz.enigma.mapping; | ||
| 2 | |||
| 3 | import java.util.List; | ||
| 4 | |||
| 5 | import com.beust.jcommander.internal.Lists; | ||
| 6 | |||
| 7 | import cuchaz.enigma.Util; | ||
| 8 | |||
| 9 | public class Signature { | ||
| 10 | |||
| 11 | private List<Type> m_argumentTypes; | ||
| 12 | private Type m_returnType; | ||
| 13 | |||
| 14 | public Signature(String signature) { | ||
| 15 | try { | ||
| 16 | m_argumentTypes = Lists.newArrayList(); | ||
| 17 | int i=0; | ||
| 18 | while (i<signature.length()) { | ||
| 19 | char c = signature.charAt(i); | ||
| 20 | if (c == '(') { | ||
| 21 | assert(m_argumentTypes.isEmpty()); | ||
| 22 | assert(m_returnType == null); | ||
| 23 | i++; | ||
| 24 | } else if (c == ')') { | ||
| 25 | i++; | ||
| 26 | break; | ||
| 27 | } else { | ||
| 28 | String type = Type.parseFirst(signature.substring(i)); | ||
| 29 | m_argumentTypes.add(new Type(type)); | ||
| 30 | i += type.length(); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | m_returnType = new Type(Type.parseFirst(signature.substring(i))); | ||
| 34 | } catch (Exception ex) { | ||
| 35 | throw new IllegalArgumentException("Unable to parse signature: " + signature, ex); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | public Signature(Signature other, ClassNameReplacer replacer) { | ||
| 40 | m_argumentTypes = Lists.newArrayList(other.m_argumentTypes); | ||
| 41 | for (int i=0; i<m_argumentTypes.size(); i++) { | ||
| 42 | m_argumentTypes.set(i, new Type(m_argumentTypes.get(i), replacer)); | ||
| 43 | } | ||
| 44 | m_returnType = new Type(other.m_returnType, replacer); | ||
| 45 | } | ||
| 46 | |||
| 47 | public List<Type> getArgumentTypes() { | ||
| 48 | return m_argumentTypes; | ||
| 49 | } | ||
| 50 | |||
| 51 | public Type getReturnType() { | ||
| 52 | return m_returnType; | ||
| 53 | } | ||
| 54 | |||
| 55 | @Override | ||
| 56 | public String toString() { | ||
| 57 | StringBuilder buf = new StringBuilder(); | ||
| 58 | buf.append("("); | ||
| 59 | for (Type type : m_argumentTypes) { | ||
| 60 | buf.append(type.toString()); | ||
| 61 | } | ||
| 62 | buf.append(")"); | ||
| 63 | buf.append(m_returnType.toString()); | ||
| 64 | return buf.toString(); | ||
| 65 | } | ||
| 66 | |||
| 67 | public Iterable<Type> types() { | ||
| 68 | List<Type> types = Lists.newArrayList(); | ||
| 69 | types.addAll(m_argumentTypes); | ||
| 70 | types.add(m_returnType); | ||
| 71 | return types; | ||
| 72 | } | ||
| 73 | |||
| 74 | public Iterable<ClassEntry> classes() { | ||
| 75 | List<ClassEntry> out = Lists.newArrayList(); | ||
| 76 | for (Type type : types()) { | ||
| 77 | if (type.isClass()) { | ||
| 78 | out.add(type.getClassEntry()); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | return out; | ||
| 82 | } | ||
| 83 | |||
| 84 | @Override | ||
| 85 | public boolean equals(Object other) { | ||
| 86 | if (other instanceof Signature) { | ||
| 87 | return equals((Signature)other); | ||
| 88 | } | ||
| 89 | return false; | ||
| 90 | } | ||
| 91 | |||
| 92 | public boolean equals(Signature other) { | ||
| 93 | return m_argumentTypes.equals(other.m_argumentTypes) && m_returnType.equals(other.m_returnType); | ||
| 94 | } | ||
| 95 | |||
| 96 | @Override | ||
| 97 | public int hashCode() { | ||
| 98 | return Util.combineHashesOrdered(m_argumentTypes.hashCode(), m_returnType.hashCode()); | ||
| 99 | } | ||
| 100 | |||
| 101 | public boolean hasClass(ClassEntry classEntry) { | ||
| 102 | for (Type type : types()) { | ||
| 103 | if (type.hasClass() && type.getClassEntry().equals(classEntry)) { | ||
| 104 | return true; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | return false; | ||
| 108 | } | ||
| 109 | } | ||