diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/MethodMapping.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/MethodMapping.java | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/MethodMapping.java b/src/cuchaz/enigma/mapping/MethodMapping.java new file mode 100644 index 0000000..055e1fe --- /dev/null +++ b/src/cuchaz/enigma/mapping/MethodMapping.java | |||
| @@ -0,0 +1,191 @@ | |||
| 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.Map; | ||
| 15 | import java.util.Map.Entry; | ||
| 16 | |||
| 17 | import com.google.common.collect.Maps; | ||
| 18 | |||
| 19 | public class MethodMapping implements Serializable, Comparable<MethodMapping>, MemberMapping<BehaviorEntry> { | ||
| 20 | |||
| 21 | private static final long serialVersionUID = -4409570216084263978L; | ||
| 22 | |||
| 23 | private String m_obfName; | ||
| 24 | private String m_deobfName; | ||
| 25 | private Signature m_obfSignature; | ||
| 26 | private Map<Integer,ArgumentMapping> m_arguments; | ||
| 27 | |||
| 28 | public MethodMapping(String obfName, Signature obfSignature) { | ||
| 29 | this(obfName, obfSignature, null); | ||
| 30 | } | ||
| 31 | |||
| 32 | public MethodMapping(String obfName, Signature obfSignature, String deobfName) { | ||
| 33 | if (obfName == null) { | ||
| 34 | throw new IllegalArgumentException("obf name cannot be null!"); | ||
| 35 | } | ||
| 36 | if (obfSignature == null) { | ||
| 37 | throw new IllegalArgumentException("obf signature cannot be null!"); | ||
| 38 | } | ||
| 39 | m_obfName = obfName; | ||
| 40 | m_deobfName = NameValidator.validateMethodName(deobfName); | ||
| 41 | m_obfSignature = obfSignature; | ||
| 42 | m_arguments = Maps.newTreeMap(); | ||
| 43 | } | ||
| 44 | |||
| 45 | public MethodMapping(MethodMapping other, ClassNameReplacer obfClassNameReplacer) { | ||
| 46 | m_obfName = other.m_obfName; | ||
| 47 | m_deobfName = other.m_deobfName; | ||
| 48 | m_obfSignature = new Signature(other.m_obfSignature, obfClassNameReplacer); | ||
| 49 | m_arguments = Maps.newTreeMap(); | ||
| 50 | for (Entry<Integer,ArgumentMapping> entry : other.m_arguments.entrySet()) { | ||
| 51 | m_arguments.put(entry.getKey(), new ArgumentMapping(entry.getValue())); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | @Override | ||
| 56 | public String getObfName() { | ||
| 57 | return m_obfName; | ||
| 58 | } | ||
| 59 | |||
| 60 | public void setObfName(String val) { | ||
| 61 | m_obfName = NameValidator.validateMethodName(val); | ||
| 62 | } | ||
| 63 | |||
| 64 | public String getDeobfName() { | ||
| 65 | return m_deobfName; | ||
| 66 | } | ||
| 67 | |||
| 68 | public void setDeobfName(String val) { | ||
| 69 | m_deobfName = NameValidator.validateMethodName(val); | ||
| 70 | } | ||
| 71 | |||
| 72 | public Signature getObfSignature() { | ||
| 73 | return m_obfSignature; | ||
| 74 | } | ||
| 75 | |||
| 76 | public void setObfSignature(Signature val) { | ||
| 77 | m_obfSignature = val; | ||
| 78 | } | ||
| 79 | |||
| 80 | public Iterable<ArgumentMapping> arguments() { | ||
| 81 | return m_arguments.values(); | ||
| 82 | } | ||
| 83 | |||
| 84 | public boolean isConstructor() { | ||
| 85 | return m_obfName.startsWith("<"); | ||
| 86 | } | ||
| 87 | |||
| 88 | public void addArgumentMapping(ArgumentMapping argumentMapping) { | ||
| 89 | boolean wasAdded = m_arguments.put(argumentMapping.getIndex(), argumentMapping) == null; | ||
| 90 | assert (wasAdded); | ||
| 91 | } | ||
| 92 | |||
| 93 | public String getObfArgumentName(int index) { | ||
| 94 | ArgumentMapping argumentMapping = m_arguments.get(index); | ||
| 95 | if (argumentMapping != null) { | ||
| 96 | return argumentMapping.getName(); | ||
| 97 | } | ||
| 98 | |||
| 99 | return null; | ||
| 100 | } | ||
| 101 | |||
| 102 | public String getDeobfArgumentName(int index) { | ||
| 103 | ArgumentMapping argumentMapping = m_arguments.get(index); | ||
| 104 | if (argumentMapping != null) { | ||
| 105 | return argumentMapping.getName(); | ||
| 106 | } | ||
| 107 | |||
| 108 | return null; | ||
| 109 | } | ||
| 110 | |||
| 111 | public void setArgumentName(int index, String name) { | ||
| 112 | ArgumentMapping argumentMapping = m_arguments.get(index); | ||
| 113 | if (argumentMapping == null) { | ||
| 114 | argumentMapping = new ArgumentMapping(index, name); | ||
| 115 | boolean wasAdded = m_arguments.put(index, argumentMapping) == null; | ||
| 116 | assert (wasAdded); | ||
| 117 | } else { | ||
| 118 | argumentMapping.setName(name); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | public void removeArgumentName(int index) { | ||
| 123 | boolean wasRemoved = m_arguments.remove(index) != null; | ||
| 124 | assert (wasRemoved); | ||
| 125 | } | ||
| 126 | |||
| 127 | @Override | ||
| 128 | public String toString() { | ||
| 129 | StringBuilder buf = new StringBuilder(); | ||
| 130 | buf.append("\t"); | ||
| 131 | buf.append(m_obfName); | ||
| 132 | buf.append(" <-> "); | ||
| 133 | buf.append(m_deobfName); | ||
| 134 | buf.append("\n"); | ||
| 135 | buf.append("\t"); | ||
| 136 | buf.append(m_obfSignature); | ||
| 137 | buf.append("\n"); | ||
| 138 | buf.append("\tArguments:\n"); | ||
| 139 | for (ArgumentMapping argumentMapping : m_arguments.values()) { | ||
| 140 | buf.append("\t\t"); | ||
| 141 | buf.append(argumentMapping.getIndex()); | ||
| 142 | buf.append(" -> "); | ||
| 143 | buf.append(argumentMapping.getName()); | ||
| 144 | buf.append("\n"); | ||
| 145 | } | ||
| 146 | return buf.toString(); | ||
| 147 | } | ||
| 148 | |||
| 149 | @Override | ||
| 150 | public int compareTo(MethodMapping other) { | ||
| 151 | return (m_obfName + m_obfSignature).compareTo(other.m_obfName + other.m_obfSignature); | ||
| 152 | } | ||
| 153 | |||
| 154 | public boolean renameObfClass(final String oldObfClassName, final String newObfClassName) { | ||
| 155 | |||
| 156 | // rename obf classes in the signature | ||
| 157 | Signature newSignature = new Signature(m_obfSignature, new ClassNameReplacer() { | ||
| 158 | @Override | ||
| 159 | public String replace(String className) { | ||
| 160 | if (className.equals(oldObfClassName)) { | ||
| 161 | return newObfClassName; | ||
| 162 | } | ||
| 163 | return null; | ||
| 164 | } | ||
| 165 | }); | ||
| 166 | |||
| 167 | if (!newSignature.equals(m_obfSignature)) { | ||
| 168 | m_obfSignature = newSignature; | ||
| 169 | return true; | ||
| 170 | } | ||
| 171 | return false; | ||
| 172 | } | ||
| 173 | |||
| 174 | public boolean containsArgument(String name) { | ||
| 175 | for (ArgumentMapping argumentMapping : m_arguments.values()) { | ||
| 176 | if (argumentMapping.getName().equals(name)) { | ||
| 177 | return true; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | return false; | ||
| 181 | } | ||
| 182 | |||
| 183 | @Override | ||
| 184 | public BehaviorEntry getObfEntry(ClassEntry classEntry) { | ||
| 185 | if (isConstructor()) { | ||
| 186 | return new ConstructorEntry(classEntry, m_obfSignature); | ||
| 187 | } else { | ||
| 188 | return new MethodEntry(classEntry, m_obfName, m_obfSignature); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||