diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/Signature.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/Signature.java | 117 |
1 files changed, 117 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..8f2b6b2 --- /dev/null +++ b/src/cuchaz/enigma/mapping/Signature.java | |||
| @@ -0,0 +1,117 @@ | |||
| 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 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.mapping; | ||
| 12 | |||
| 13 | import java.io.Serializable; | ||
| 14 | import java.util.List; | ||
| 15 | |||
| 16 | import com.google.common.collect.Lists; | ||
| 17 | |||
| 18 | import cuchaz.enigma.Util; | ||
| 19 | |||
| 20 | public class Signature implements Serializable { | ||
| 21 | |||
| 22 | private static final long serialVersionUID = -5843719505729497539L; | ||
| 23 | |||
| 24 | private List<Type> m_argumentTypes; | ||
| 25 | private Type m_returnType; | ||
| 26 | |||
| 27 | public Signature(String signature) { | ||
| 28 | try { | ||
| 29 | m_argumentTypes = Lists.newArrayList(); | ||
| 30 | int i=0; | ||
| 31 | while (i<signature.length()) { | ||
| 32 | char c = signature.charAt(i); | ||
| 33 | if (c == '(') { | ||
| 34 | assert(m_argumentTypes.isEmpty()); | ||
| 35 | assert(m_returnType == null); | ||
| 36 | i++; | ||
| 37 | } else if (c == ')') { | ||
| 38 | i++; | ||
| 39 | break; | ||
| 40 | } else { | ||
| 41 | String type = Type.parseFirst(signature.substring(i)); | ||
| 42 | m_argumentTypes.add(new Type(type)); | ||
| 43 | i += type.length(); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | m_returnType = new Type(Type.parseFirst(signature.substring(i))); | ||
| 47 | } catch (Exception ex) { | ||
| 48 | throw new IllegalArgumentException("Unable to parse signature: " + signature, ex); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | public Signature(Signature other) { | ||
| 53 | m_argumentTypes = Lists.newArrayList(other.m_argumentTypes); | ||
| 54 | m_returnType = new Type(other.m_returnType); | ||
| 55 | } | ||
| 56 | |||
| 57 | public Signature(Signature other, ClassNameReplacer replacer) { | ||
| 58 | m_argumentTypes = Lists.newArrayList(other.m_argumentTypes); | ||
| 59 | for (int i=0; i<m_argumentTypes.size(); i++) { | ||
| 60 | m_argumentTypes.set(i, new Type(m_argumentTypes.get(i), replacer)); | ||
| 61 | } | ||
| 62 | m_returnType = new Type(other.m_returnType, replacer); | ||
| 63 | } | ||
| 64 | |||
| 65 | public List<Type> getArgumentTypes() { | ||
| 66 | return m_argumentTypes; | ||
| 67 | } | ||
| 68 | |||
| 69 | public Type getReturnType() { | ||
| 70 | return m_returnType; | ||
| 71 | } | ||
| 72 | |||
| 73 | @Override | ||
| 74 | public String toString() { | ||
| 75 | StringBuilder buf = new StringBuilder(); | ||
| 76 | buf.append("("); | ||
| 77 | for (Type type : m_argumentTypes) { | ||
| 78 | buf.append(type.toString()); | ||
| 79 | } | ||
| 80 | buf.append(")"); | ||
| 81 | buf.append(m_returnType.toString()); | ||
| 82 | return buf.toString(); | ||
| 83 | } | ||
| 84 | |||
| 85 | public Iterable<Type> types() { | ||
| 86 | List<Type> types = Lists.newArrayList(); | ||
| 87 | types.addAll(m_argumentTypes); | ||
| 88 | types.add(m_returnType); | ||
| 89 | return types; | ||
| 90 | } | ||
| 91 | |||
| 92 | @Override | ||
| 93 | public boolean equals(Object other) { | ||
| 94 | if (other instanceof Signature) { | ||
| 95 | return equals((Signature)other); | ||
| 96 | } | ||
| 97 | return false; | ||
| 98 | } | ||
| 99 | |||
| 100 | public boolean equals(Signature other) { | ||
| 101 | return m_argumentTypes.equals(other.m_argumentTypes) && m_returnType.equals(other.m_returnType); | ||
| 102 | } | ||
| 103 | |||
| 104 | @Override | ||
| 105 | public int hashCode() { | ||
| 106 | return Util.combineHashesOrdered(m_argumentTypes.hashCode(), m_returnType.hashCode()); | ||
| 107 | } | ||
| 108 | |||
| 109 | public boolean hasClass(ClassEntry classEntry) { | ||
| 110 | for (Type type : types()) { | ||
| 111 | if (type.hasClass() && type.getClassEntry().equals(classEntry)) { | ||
| 112 | return true; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | return false; | ||
| 116 | } | ||
| 117 | } | ||