diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/MethodMapping.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/MethodMapping.java | 161 |
1 files changed, 161 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..1704428 --- /dev/null +++ b/src/cuchaz/enigma/mapping/MethodMapping.java | |||
| @@ -0,0 +1,161 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2014 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Public License v3.0 | ||
| 5 | * which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/gpl.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.TreeMap; | ||
| 16 | |||
| 17 | public class MethodMapping implements Serializable, Comparable<MethodMapping> { | ||
| 18 | |||
| 19 | private static final long serialVersionUID = -4409570216084263978L; | ||
| 20 | |||
| 21 | private String m_obfName; | ||
| 22 | private String m_deobfName; | ||
| 23 | private Signature m_obfSignature; | ||
| 24 | private Map<Integer,ArgumentMapping> m_arguments; | ||
| 25 | |||
| 26 | public MethodMapping(String obfName, Signature obfSignature) { | ||
| 27 | this(obfName, obfSignature, null); | ||
| 28 | } | ||
| 29 | |||
| 30 | public MethodMapping(String obfName, Signature obfSignature, String deobfName) { | ||
| 31 | if (obfName == null) { | ||
| 32 | throw new IllegalArgumentException("obf name cannot be null!"); | ||
| 33 | } | ||
| 34 | if (obfSignature == null) { | ||
| 35 | throw new IllegalArgumentException("obf signature cannot be null!"); | ||
| 36 | } | ||
| 37 | m_obfName = obfName; | ||
| 38 | m_deobfName = NameValidator.validateMethodName(deobfName); | ||
| 39 | m_obfSignature = obfSignature; | ||
| 40 | m_arguments = new TreeMap<Integer,ArgumentMapping>(); | ||
| 41 | } | ||
| 42 | |||
| 43 | public String getObfName() { | ||
| 44 | return m_obfName; | ||
| 45 | } | ||
| 46 | |||
| 47 | public String getDeobfName() { | ||
| 48 | return m_deobfName; | ||
| 49 | } | ||
| 50 | |||
| 51 | public void setDeobfName(String val) { | ||
| 52 | m_deobfName = NameValidator.validateMethodName(val); | ||
| 53 | } | ||
| 54 | |||
| 55 | public Signature getObfSignature() { | ||
| 56 | return m_obfSignature; | ||
| 57 | } | ||
| 58 | |||
| 59 | public Iterable<ArgumentMapping> arguments() { | ||
| 60 | return m_arguments.values(); | ||
| 61 | } | ||
| 62 | |||
| 63 | public boolean isConstructor() { | ||
| 64 | return m_obfName.startsWith("<"); | ||
| 65 | } | ||
| 66 | |||
| 67 | public void addArgumentMapping(ArgumentMapping argumentMapping) { | ||
| 68 | boolean wasAdded = m_arguments.put(argumentMapping.getIndex(), argumentMapping) == null; | ||
| 69 | assert (wasAdded); | ||
| 70 | } | ||
| 71 | |||
| 72 | public String getObfArgumentName(int index) { | ||
| 73 | ArgumentMapping argumentMapping = m_arguments.get(index); | ||
| 74 | if (argumentMapping != null) { | ||
| 75 | return argumentMapping.getName(); | ||
| 76 | } | ||
| 77 | |||
| 78 | return null; | ||
| 79 | } | ||
| 80 | |||
| 81 | public String getDeobfArgumentName(int index) { | ||
| 82 | ArgumentMapping argumentMapping = m_arguments.get(index); | ||
| 83 | if (argumentMapping != null) { | ||
| 84 | return argumentMapping.getName(); | ||
| 85 | } | ||
| 86 | |||
| 87 | return null; | ||
| 88 | } | ||
| 89 | |||
| 90 | public void setArgumentName(int index, String name) { | ||
| 91 | ArgumentMapping argumentMapping = m_arguments.get(index); | ||
| 92 | if (argumentMapping == null) { | ||
| 93 | argumentMapping = new ArgumentMapping(index, name); | ||
| 94 | boolean wasAdded = m_arguments.put(index, argumentMapping) == null; | ||
| 95 | assert (wasAdded); | ||
| 96 | } else { | ||
| 97 | argumentMapping.setName(name); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | public void removeArgumentName(int index) { | ||
| 102 | boolean wasRemoved = m_arguments.remove(index) != null; | ||
| 103 | assert (wasRemoved); | ||
| 104 | } | ||
| 105 | |||
| 106 | @Override | ||
| 107 | public String toString() { | ||
| 108 | StringBuilder buf = new StringBuilder(); | ||
| 109 | buf.append("\t"); | ||
| 110 | buf.append(m_obfName); | ||
| 111 | buf.append(" <-> "); | ||
| 112 | buf.append(m_deobfName); | ||
| 113 | buf.append("\n"); | ||
| 114 | buf.append("\t"); | ||
| 115 | buf.append(m_obfSignature); | ||
| 116 | buf.append("\n"); | ||
| 117 | buf.append("\tArguments:\n"); | ||
| 118 | for (ArgumentMapping argumentMapping : m_arguments.values()) { | ||
| 119 | buf.append("\t\t"); | ||
| 120 | buf.append(argumentMapping.getIndex()); | ||
| 121 | buf.append(" -> "); | ||
| 122 | buf.append(argumentMapping.getName()); | ||
| 123 | buf.append("\n"); | ||
| 124 | } | ||
| 125 | return buf.toString(); | ||
| 126 | } | ||
| 127 | |||
| 128 | @Override | ||
| 129 | public int compareTo(MethodMapping other) { | ||
| 130 | return (m_obfName + m_obfSignature).compareTo(other.m_obfName + other.m_obfSignature); | ||
| 131 | } | ||
| 132 | |||
| 133 | public boolean renameObfClass(final String oldObfClassName, final String newObfClassName) { | ||
| 134 | |||
| 135 | // rename obf classes in the signature | ||
| 136 | Signature newSignature = new Signature(m_obfSignature, new ClassNameReplacer() { | ||
| 137 | @Override | ||
| 138 | public String replace(String className) { | ||
| 139 | if (className.equals(oldObfClassName)) { | ||
| 140 | return newObfClassName; | ||
| 141 | } | ||
| 142 | return null; | ||
| 143 | } | ||
| 144 | }); | ||
| 145 | |||
| 146 | if (!newSignature.equals(m_obfSignature)) { | ||
| 147 | m_obfSignature = newSignature; | ||
| 148 | return true; | ||
| 149 | } | ||
| 150 | return false; | ||
| 151 | } | ||
| 152 | |||
| 153 | public boolean containsArgument(String name) { | ||
| 154 | for (ArgumentMapping argumentMapping : m_arguments.values()) { | ||
| 155 | if (argumentMapping.getName().equals(name)) { | ||
| 156 | return true; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | return false; | ||
| 160 | } | ||
| 161 | } | ||