diff options
| author | 2018-05-19 17:02:46 +0200 | |
|---|---|---|
| committer | 2018-05-19 17:02:46 +0200 | |
| commit | 2b2249e873c4adfd2dd6e8f1f2489ccd9f6aa021 (patch) | |
| tree | 14c8b1e806449ace1641a1dbafae162855f79670 /src/main/java/cuchaz/enigma/mapping/MethodMapping.java | |
| parent | Fix build (diff) | |
| download | enigma-fork-2b2249e873c4adfd2dd6e8f1f2489ccd9f6aa021.tar.gz enigma-fork-2b2249e873c4adfd2dd6e8f1f2489ccd9f6aa021.tar.xz enigma-fork-2b2249e873c4adfd2dd6e8f1f2489ccd9f6aa021.zip | |
Initial port to ASM
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MethodMapping.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MethodMapping.java | 130 |
1 files changed, 59 insertions, 71 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MethodMapping.java b/src/main/java/cuchaz/enigma/mapping/MethodMapping.java index 1524ce6..2f7fe53 100644 --- a/src/main/java/cuchaz/enigma/mapping/MethodMapping.java +++ b/src/main/java/cuchaz/enigma/mapping/MethodMapping.java | |||
| @@ -11,50 +11,47 @@ | |||
| 11 | 11 | ||
| 12 | package cuchaz.enigma.mapping; | 12 | package cuchaz.enigma.mapping; |
| 13 | 13 | ||
| 14 | import com.google.common.base.Preconditions; | ||
| 14 | import com.google.common.collect.Maps; | 15 | import com.google.common.collect.Maps; |
| 15 | import cuchaz.enigma.throwables.IllegalNameException; | 16 | import cuchaz.enigma.throwables.IllegalNameException; |
| 16 | import cuchaz.enigma.throwables.MappingConflict; | 17 | import cuchaz.enigma.throwables.MappingConflict; |
| 17 | 18 | ||
| 18 | import java.util.Map; | 19 | import java.util.Map; |
| 19 | 20 | ||
| 20 | public class MethodMapping implements Comparable<MethodMapping>, MemberMapping<BehaviorEntry> { | 21 | public class MethodMapping implements Comparable<MethodMapping>, MemberMapping<MethodEntry> { |
| 21 | 22 | ||
| 22 | private String obfName; | 23 | private String obfName; |
| 23 | private String deobfName; | 24 | private String deobfName; |
| 24 | private Signature obfSignature; | 25 | private MethodDescriptor obfDescriptor; |
| 25 | private Map<Integer, ArgumentMapping> arguments; | 26 | private Map<Integer, LocalVariableMapping> localVariables; |
| 26 | private Mappings.EntryModifier modifier; | 27 | private Mappings.EntryModifier modifier; |
| 27 | 28 | ||
| 28 | public MethodMapping(String obfName, Signature obfSignature) { | 29 | public MethodMapping(String obfName, MethodDescriptor obfDescriptor) { |
| 29 | this(obfName, obfSignature, null, Mappings.EntryModifier.UNCHANGED); | 30 | this(obfName, obfDescriptor, null, Mappings.EntryModifier.UNCHANGED); |
| 30 | } | 31 | } |
| 31 | 32 | ||
| 32 | public MethodMapping(String obfName, Signature obfSignature, String deobfName) { | 33 | public MethodMapping(String obfName, MethodDescriptor obfDescriptor, String deobfName) { |
| 33 | this(obfName, obfSignature, deobfName, Mappings.EntryModifier.UNCHANGED); | 34 | this(obfName, obfDescriptor, deobfName, Mappings.EntryModifier.UNCHANGED); |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | public MethodMapping(String obfName, Signature obfSignature, String deobfName, Mappings.EntryModifier modifier) { | 37 | public MethodMapping(String obfName, MethodDescriptor obfDescriptor, String deobfName, Mappings.EntryModifier modifier) { |
| 37 | if (obfName == null) { | 38 | Preconditions.checkNotNull(obfName, "Method obf name cannot be null"); |
| 38 | throw new IllegalArgumentException("obf name cannot be null!"); | 39 | Preconditions.checkNotNull(obfDescriptor, "Method obf desc cannot be null"); |
| 39 | } | ||
| 40 | if (obfSignature == null) { | ||
| 41 | throw new IllegalArgumentException("obf signature cannot be null!"); | ||
| 42 | } | ||
| 43 | this.obfName = obfName; | 40 | this.obfName = obfName; |
| 44 | this.deobfName = NameValidator.validateMethodName(deobfName); | 41 | this.deobfName = NameValidator.validateMethodName(deobfName); |
| 45 | this.obfSignature = obfSignature; | 42 | this.obfDescriptor = obfDescriptor; |
| 46 | this.arguments = Maps.newTreeMap(); | 43 | this.localVariables = Maps.newTreeMap(); |
| 47 | this.modifier = modifier; | 44 | this.modifier = modifier; |
| 48 | } | 45 | } |
| 49 | 46 | ||
| 50 | public MethodMapping(MethodMapping other, ClassNameReplacer obfClassNameReplacer) { | 47 | public MethodMapping(MethodMapping other, Translator translator) { |
| 51 | this.obfName = other.obfName; | 48 | this.obfName = other.obfName; |
| 52 | this.deobfName = other.deobfName; | 49 | this.deobfName = other.deobfName; |
| 53 | this.modifier = other.modifier; | 50 | this.modifier = other.modifier; |
| 54 | this.obfSignature = new Signature(other.obfSignature, obfClassNameReplacer); | 51 | this.obfDescriptor = translator.getTranslatedMethodDesc(other.obfDescriptor); |
| 55 | this.arguments = Maps.newTreeMap(); | 52 | this.localVariables = Maps.newTreeMap(); |
| 56 | for (Map.Entry<Integer, ArgumentMapping> entry : other.arguments.entrySet()) { | 53 | for (Map.Entry<Integer, LocalVariableMapping> entry : other.localVariables.entrySet()) { |
| 57 | this.arguments.put(entry.getKey(), new ArgumentMapping(entry.getValue())); | 54 | this.localVariables.put(entry.getKey(), new LocalVariableMapping(entry.getValue())); |
| 58 | } | 55 | } |
| 59 | } | 56 | } |
| 60 | 57 | ||
| @@ -84,56 +81,56 @@ public class MethodMapping implements Comparable<MethodMapping>, MemberMapping<B | |||
| 84 | this.deobfName = NameValidator.validateMethodName(val); | 81 | this.deobfName = NameValidator.validateMethodName(val); |
| 85 | } | 82 | } |
| 86 | 83 | ||
| 87 | public Signature getObfSignature() { | 84 | public MethodDescriptor getObfDesc() { |
| 88 | return this.obfSignature; | 85 | return this.obfDescriptor; |
| 89 | } | 86 | } |
| 90 | 87 | ||
| 91 | public void setObfSignature(Signature val) { | 88 | public void setObfDescriptor(MethodDescriptor val) { |
| 92 | this.obfSignature = val; | 89 | this.obfDescriptor = val; |
| 93 | } | 90 | } |
| 94 | 91 | ||
| 95 | public Iterable<ArgumentMapping> arguments() { | 92 | public Iterable<LocalVariableMapping> arguments() { |
| 96 | return this.arguments.values(); | 93 | return this.localVariables.values(); |
| 97 | } | 94 | } |
| 98 | 95 | ||
| 99 | public void addArgumentMapping(ArgumentMapping argumentMapping) throws MappingConflict { | 96 | public void addArgumentMapping(LocalVariableMapping localVariableMapping) throws MappingConflict { |
| 100 | if (this.arguments.containsKey(argumentMapping.getIndex())) { | 97 | if (this.localVariables.containsKey(localVariableMapping.getIndex())) { |
| 101 | throw new MappingConflict("argument", argumentMapping.getName(), this.arguments.get(argumentMapping.getIndex()).getName()); | 98 | throw new MappingConflict("argument", localVariableMapping.getName(), this.localVariables.get(localVariableMapping.getIndex()).getName()); |
| 102 | } | 99 | } |
| 103 | this.arguments.put(argumentMapping.getIndex(), argumentMapping); | 100 | this.localVariables.put(localVariableMapping.getIndex(), localVariableMapping); |
| 104 | } | 101 | } |
| 105 | 102 | ||
| 106 | public String getObfArgumentName(int index) { | 103 | public String getObfLocalVariableName(int index) { |
| 107 | ArgumentMapping argumentMapping = this.arguments.get(index); | 104 | LocalVariableMapping localVariableMapping = this.localVariables.get(index); |
| 108 | if (argumentMapping != null) { | 105 | if (localVariableMapping != null) { |
| 109 | return argumentMapping.getName(); | 106 | return localVariableMapping.getName(); |
| 110 | } | 107 | } |
| 111 | 108 | ||
| 112 | return null; | 109 | return null; |
| 113 | } | 110 | } |
| 114 | 111 | ||
| 115 | public String getDeobfArgumentName(int index) { | 112 | public String getDeobfLocalVariableName(int index) { |
| 116 | ArgumentMapping argumentMapping = this.arguments.get(index); | 113 | LocalVariableMapping localVariableMapping = this.localVariables.get(index); |
| 117 | if (argumentMapping != null) { | 114 | if (localVariableMapping != null) { |
| 118 | return argumentMapping.getName(); | 115 | return localVariableMapping.getName(); |
| 119 | } | 116 | } |
| 120 | 117 | ||
| 121 | return null; | 118 | return null; |
| 122 | } | 119 | } |
| 123 | 120 | ||
| 124 | public void setArgumentName(int index, String name) { | 121 | public void setLocalVariableName(int index, String name) { |
| 125 | ArgumentMapping argumentMapping = this.arguments.get(index); | 122 | LocalVariableMapping localVariableMapping = this.localVariables.get(index); |
| 126 | if (argumentMapping == null) { | 123 | if (localVariableMapping == null) { |
| 127 | argumentMapping = new ArgumentMapping(index, name); | 124 | localVariableMapping = new LocalVariableMapping(index, name); |
| 128 | boolean wasAdded = this.arguments.put(index, argumentMapping) == null; | 125 | boolean wasAdded = this.localVariables.put(index, localVariableMapping) == null; |
| 129 | assert (wasAdded); | 126 | assert (wasAdded); |
| 130 | } else { | 127 | } else { |
| 131 | argumentMapping.setName(name); | 128 | localVariableMapping.setName(name); |
| 132 | } | 129 | } |
| 133 | } | 130 | } |
| 134 | 131 | ||
| 135 | public void removeArgumentName(int index) { | 132 | public void removeLocalVariableName(int index) { |
| 136 | boolean wasRemoved = this.arguments.remove(index) != null; | 133 | boolean wasRemoved = this.localVariables.remove(index) != null; |
| 137 | assert (wasRemoved); | 134 | assert (wasRemoved); |
| 138 | } | 135 | } |
| 139 | 136 | ||
| @@ -146,14 +143,14 @@ public class MethodMapping implements Comparable<MethodMapping>, MemberMapping<B | |||
| 146 | buf.append(this.deobfName); | 143 | buf.append(this.deobfName); |
| 147 | buf.append("\n"); | 144 | buf.append("\n"); |
| 148 | buf.append("\t"); | 145 | buf.append("\t"); |
| 149 | buf.append(this.obfSignature); | 146 | buf.append(this.obfDescriptor); |
| 150 | buf.append("\n"); | 147 | buf.append("\n"); |
| 151 | buf.append("\tArguments:\n"); | 148 | buf.append("\tLocal Variables:\n"); |
| 152 | for (ArgumentMapping argumentMapping : this.arguments.values()) { | 149 | for (LocalVariableMapping localVariableMapping : this.localVariables.values()) { |
| 153 | buf.append("\t\t"); | 150 | buf.append("\t\t"); |
| 154 | buf.append(argumentMapping.getIndex()); | 151 | buf.append(localVariableMapping.getIndex()); |
| 155 | buf.append(" -> "); | 152 | buf.append(" -> "); |
| 156 | buf.append(argumentMapping.getName()); | 153 | buf.append(localVariableMapping.getName()); |
| 157 | buf.append("\n"); | 154 | buf.append("\n"); |
| 158 | } | 155 | } |
| 159 | return buf.toString(); | 156 | return buf.toString(); |
| @@ -161,12 +158,12 @@ public class MethodMapping implements Comparable<MethodMapping>, MemberMapping<B | |||
| 161 | 158 | ||
| 162 | @Override | 159 | @Override |
| 163 | public int compareTo(MethodMapping other) { | 160 | public int compareTo(MethodMapping other) { |
| 164 | return (this.obfName + this.obfSignature).compareTo(other.obfName + other.obfSignature); | 161 | return (this.obfName + this.obfDescriptor).compareTo(other.obfName + other.obfDescriptor); |
| 165 | } | 162 | } |
| 166 | 163 | ||
| 167 | public boolean containsArgument(String name) { | 164 | public boolean containsLocalVariable(String name) { |
| 168 | for (ArgumentMapping argumentMapping : this.arguments.values()) { | 165 | for (LocalVariableMapping localVariableMapping : this.localVariables.values()) { |
| 169 | if (argumentMapping.getName().equals(name)) { | 166 | if (localVariableMapping.getName().equals(name)) { |
| 170 | return true; | 167 | return true; |
| 171 | } | 168 | } |
| 172 | } | 169 | } |
| @@ -175,32 +172,23 @@ public class MethodMapping implements Comparable<MethodMapping>, MemberMapping<B | |||
| 175 | 172 | ||
| 176 | public boolean renameObfClass(final String oldObfClassName, final String newObfClassName) { | 173 | public boolean renameObfClass(final String oldObfClassName, final String newObfClassName) { |
| 177 | // rename obf classes in the signature | 174 | // rename obf classes in the signature |
| 178 | Signature newSignature = new Signature(this.obfSignature, className -> | 175 | MethodDescriptor newDescriptor = obfDescriptor.remap(className -> { |
| 179 | { | ||
| 180 | if (className.equals(oldObfClassName)) { | 176 | if (className.equals(oldObfClassName)) { |
| 181 | return newObfClassName; | 177 | return newObfClassName; |
| 182 | } | 178 | } |
| 183 | return null; | 179 | return className; |
| 184 | }); | 180 | }); |
| 185 | 181 | ||
| 186 | if (!newSignature.equals(this.obfSignature)) { | 182 | if (!newDescriptor.equals(this.obfDescriptor)) { |
| 187 | this.obfSignature = newSignature; | 183 | this.obfDescriptor = newDescriptor; |
| 188 | return true; | 184 | return true; |
| 189 | } | 185 | } |
| 190 | return false; | 186 | return false; |
| 191 | } | 187 | } |
| 192 | 188 | ||
| 193 | public boolean isConstructor() { | ||
| 194 | return this.obfName.startsWith("<"); | ||
| 195 | } | ||
| 196 | |||
| 197 | @Override | 189 | @Override |
| 198 | public BehaviorEntry getObfEntry(ClassEntry classEntry) { | 190 | public MethodEntry getObfEntry(ClassEntry classEntry) { |
| 199 | if (isConstructor()) { | 191 | return new MethodEntry(classEntry, this.obfName, this.obfDescriptor); |
| 200 | return new ConstructorEntry(classEntry, this.obfSignature); | ||
| 201 | } else { | ||
| 202 | return new MethodEntry(classEntry, this.obfName, this.obfSignature); | ||
| 203 | } | ||
| 204 | } | 192 | } |
| 205 | 193 | ||
| 206 | public Mappings.EntryModifier getModifier() { | 194 | public Mappings.EntryModifier getModifier() { |