diff options
| author | 2018-06-24 19:37:19 +0200 | |
|---|---|---|
| committer | 2018-06-24 19:37:19 +0200 | |
| commit | 43ce03c78a9c1307e0c6457dbbf9698ce59615af (patch) | |
| tree | 8c44a61df40b24c2b4d7546550944c8dc772d14e /src/main/java/cuchaz | |
| parent | Fix handling of null deobf mappings (diff) | |
| download | enigma-43ce03c78a9c1307e0c6457dbbf9698ce59615af.tar.gz enigma-43ce03c78a9c1307e0c6457dbbf9698ce59615af.tar.xz enigma-43ce03c78a9c1307e0c6457dbbf9698ce59615af.zip | |
Parameter translation in interfaces
Diffstat (limited to 'src/main/java/cuchaz')
| -rw-r--r-- | src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java | 67 |
1 files changed, 48 insertions, 19 deletions
diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java b/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java index 4f4a31e3..a2ebc203 100644 --- a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java +++ b/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java | |||
| @@ -7,12 +7,15 @@ import cuchaz.enigma.mapping.TypeDescriptor; | |||
| 7 | import cuchaz.enigma.mapping.entry.*; | 7 | import cuchaz.enigma.mapping.entry.*; |
| 8 | import org.objectweb.asm.*; | 8 | import org.objectweb.asm.*; |
| 9 | 9 | ||
| 10 | import java.util.List; | ||
| 10 | import java.util.Locale; | 11 | import java.util.Locale; |
| 11 | 12 | ||
| 12 | public class TranslationMethodVisitor extends MethodVisitor { | 13 | public class TranslationMethodVisitor extends MethodVisitor { |
| 13 | private final MethodDefEntry methodEntry; | 14 | private final MethodDefEntry methodEntry; |
| 14 | private final Translator translator; | 15 | private final Translator translator; |
| 15 | 16 | ||
| 17 | private boolean hasParameterMeta; | ||
| 18 | |||
| 16 | public TranslationMethodVisitor(Translator translator, MethodDefEntry methodEntry, int api, MethodVisitor mv) { | 19 | public TranslationMethodVisitor(Translator translator, MethodDefEntry methodEntry, int api, MethodVisitor mv) { |
| 17 | super(api, mv); | 20 | super(api, mv); |
| 18 | this.translator = translator; | 21 | this.translator = translator; |
| @@ -77,6 +80,8 @@ public class TranslationMethodVisitor extends MethodVisitor { | |||
| 77 | 80 | ||
| 78 | @Override | 81 | @Override |
| 79 | public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { | 82 | public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { |
| 83 | hasParameterMeta = true; | ||
| 84 | |||
| 80 | String translatedSignature = translator.getTranslatedSignature(Signature.createTypedSignature(signature)).toString(); | 85 | String translatedSignature = translator.getTranslatedSignature(Signature.createTypedSignature(signature)).toString(); |
| 81 | 86 | ||
| 82 | // If we're not static, "this" is bound to index 0 | 87 | // If we're not static, "this" is bound to index 0 |
| @@ -88,26 +93,8 @@ public class TranslationMethodVisitor extends MethodVisitor { | |||
| 88 | 93 | ||
| 89 | // TODO: Better name inference | 94 | // TODO: Better name inference |
| 90 | if (translatedName.equals(entry.getName())) { | 95 | if (translatedName.equals(entry.getName())) { |
| 91 | TypeDescriptor argDesc = translatedEntry.getDesc(); | ||
| 92 | int nameIndex = offsetIndex + 1; | ||
| 93 | String prefix = offsetIndex < methodEntry.getDesc().getArgumentDescs().size() ? "a" : "v"; | 96 | String prefix = offsetIndex < methodEntry.getDesc().getArgumentDescs().size() ? "a" : "v"; |
| 94 | StringBuilder nameBuilder = new StringBuilder(prefix); | 97 | translatedName = inferName(prefix, offsetIndex, translatedEntry.getDesc()); |
| 95 | // Unfortunately each of these have different name getters, so they have different code paths | ||
| 96 | if (argDesc.isPrimitive()) { | ||
| 97 | TypeDescriptor.Primitive argCls = argDesc.getPrimitive(); | ||
| 98 | nameBuilder.append(argCls.name()); | ||
| 99 | } else if (argDesc.isArray()) { | ||
| 100 | // List types would require this whole block again, so just go with aListx | ||
| 101 | nameBuilder.append(nameIndex); | ||
| 102 | } else if (argDesc.isType()) { | ||
| 103 | String typeName = argDesc.getTypeEntry().getSimpleName().replace("$", ""); | ||
| 104 | typeName = typeName.substring(0, 1).toUpperCase(Locale.ROOT) + typeName.substring(1); | ||
| 105 | nameBuilder.append(typeName); | ||
| 106 | } | ||
| 107 | if (methodEntry.getDesc().getArgumentDescs().size() > 1) { | ||
| 108 | nameBuilder.append(nameIndex); | ||
| 109 | } | ||
| 110 | translatedName = nameBuilder.toString(); | ||
| 111 | } | 98 | } |
| 112 | 99 | ||
| 113 | super.visitLocalVariable(translatedName, translatedEntry.getDesc().toString(), translatedSignature, start, end, index); | 100 | super.visitLocalVariable(translatedName, translatedEntry.getDesc().toString(), translatedSignature, start, end, index); |
| @@ -153,4 +140,46 @@ public class TranslationMethodVisitor extends MethodVisitor { | |||
| 153 | super.visitTryCatchBlock(start, end, handler, type); | 140 | super.visitTryCatchBlock(start, end, handler, type); |
| 154 | } | 141 | } |
| 155 | } | 142 | } |
| 143 | |||
| 144 | @Override | ||
| 145 | public void visitEnd() { | ||
| 146 | // If we didn't receive any parameter metadata, generate it | ||
| 147 | if (!hasParameterMeta) { | ||
| 148 | List<TypeDescriptor> arguments = methodEntry.getDesc().getArgumentDescs(); | ||
| 149 | for (int index = 0; index < arguments.size(); index++) { | ||
| 150 | LocalVariableEntry entry = new LocalVariableEntry(methodEntry, index, ""); | ||
| 151 | LocalVariableEntry translatedEntry = translator.getTranslatedVariable(entry); | ||
| 152 | String translatedName = translatedEntry.getName(); | ||
| 153 | if (translatedName.equals(entry.getName())) { | ||
| 154 | super.visitParameter(inferName("a", index, arguments.get(index)), 0); | ||
| 155 | } else { | ||
| 156 | super.visitParameter(translatedName, 0); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | super.visitEnd(); | ||
| 161 | } | ||
| 162 | |||
| 163 | private String inferName(String prefix, int argumentIndex, TypeDescriptor desc) { | ||
| 164 | String translatedName; | ||
| 165 | int nameIndex = argumentIndex + 1; | ||
| 166 | StringBuilder nameBuilder = new StringBuilder(prefix); | ||
| 167 | // Unfortunately each of these have different name getters, so they have different code paths | ||
| 168 | if (desc.isPrimitive()) { | ||
| 169 | TypeDescriptor.Primitive argCls = desc.getPrimitive(); | ||
| 170 | nameBuilder.append(argCls.name()); | ||
| 171 | } else if (desc.isArray()) { | ||
| 172 | // List types would require this whole block again, so just go with aListx | ||
| 173 | nameBuilder.append(nameIndex); | ||
| 174 | } else if (desc.isType()) { | ||
| 175 | String typeName = desc.getTypeEntry().getSimpleName().replace("$", ""); | ||
| 176 | typeName = typeName.substring(0, 1).toUpperCase(Locale.ROOT) + typeName.substring(1); | ||
| 177 | nameBuilder.append(typeName); | ||
| 178 | } | ||
| 179 | if (methodEntry.getDesc().getArgumentDescs().size() > 1) { | ||
| 180 | nameBuilder.append(nameIndex); | ||
| 181 | } | ||
| 182 | translatedName = nameBuilder.toString(); | ||
| 183 | return translatedName; | ||
| 184 | } | ||
| 156 | } | 185 | } |