diff options
| author | 2019-05-18 11:13:24 +0200 | |
|---|---|---|
| committer | 2019-05-18 11:13:24 +0200 | |
| commit | 463aee1dfc1222e8f6f0312aa46fb9b9dcfa13a1 (patch) | |
| tree | 2f2503cabe79145799db5f5e63ee272cf695b72f /src/main/java/cuchaz/enigma | |
| parent | Feature/weave (#138) (diff) | |
| download | enigma-fork-463aee1dfc1222e8f6f0312aa46fb9b9dcfa13a1.tar.gz enigma-fork-463aee1dfc1222e8f6f0312aa46fb9b9dcfa13a1.tar.xz enigma-fork-463aee1dfc1222e8f6f0312aa46fb9b9dcfa13a1.zip | |
Method type reference corrections (#142)
* Add more specific returns for translatables
* Only index method descriptors for implemented methods and methods in generated lambda classes
Diffstat (limited to 'src/main/java/cuchaz/enigma')
7 files changed, 167 insertions, 33 deletions
diff --git a/src/main/java/cuchaz/enigma/analysis/index/IndexReferenceVisitor.java b/src/main/java/cuchaz/enigma/analysis/index/IndexReferenceVisitor.java index ba5d3b6..b730a8a 100644 --- a/src/main/java/cuchaz/enigma/analysis/index/IndexReferenceVisitor.java +++ b/src/main/java/cuchaz/enigma/analysis/index/IndexReferenceVisitor.java | |||
| @@ -1,16 +1,11 @@ | |||
| 1 | package cuchaz.enigma.analysis.index; | 1 | package cuchaz.enigma.analysis.index; |
| 2 | 2 | ||
| 3 | import cuchaz.enigma.translation.representation.AccessFlags; | 3 | import cuchaz.enigma.translation.representation.AccessFlags; |
| 4 | import cuchaz.enigma.translation.representation.Lambda; | ||
| 4 | import cuchaz.enigma.translation.representation.MethodDescriptor; | 5 | import cuchaz.enigma.translation.representation.MethodDescriptor; |
| 5 | import cuchaz.enigma.translation.representation.Signature; | 6 | import cuchaz.enigma.translation.representation.Signature; |
| 6 | import cuchaz.enigma.translation.representation.entry.ClassEntry; | 7 | import cuchaz.enigma.translation.representation.entry.*; |
| 7 | import cuchaz.enigma.translation.representation.entry.FieldEntry; | 8 | import org.objectweb.asm.*; |
| 8 | import cuchaz.enigma.translation.representation.entry.MethodDefEntry; | ||
| 9 | import cuchaz.enigma.translation.representation.entry.MethodEntry; | ||
| 10 | import org.objectweb.asm.ClassVisitor; | ||
| 11 | import org.objectweb.asm.Handle; | ||
| 12 | import org.objectweb.asm.MethodVisitor; | ||
| 13 | import org.objectweb.asm.Opcodes; | ||
| 14 | 9 | ||
| 15 | public class IndexReferenceVisitor extends ClassVisitor { | 10 | public class IndexReferenceVisitor extends ClassVisitor { |
| 16 | private final JarIndexer indexer; | 11 | private final JarIndexer indexer; |
| @@ -54,29 +49,37 @@ public class IndexReferenceVisitor extends ClassVisitor { | |||
| 54 | this.indexer.indexMethodReference(callerEntry, methodEntry); | 49 | this.indexer.indexMethodReference(callerEntry, methodEntry); |
| 55 | } | 50 | } |
| 56 | 51 | ||
| 52 | private static ParentedEntry<?> getHandleEntry(Handle handle) { | ||
| 53 | switch (handle.getTag()) { | ||
| 54 | case Opcodes.H_GETFIELD: | ||
| 55 | case Opcodes.H_GETSTATIC: | ||
| 56 | case Opcodes.H_PUTFIELD: | ||
| 57 | case Opcodes.H_PUTSTATIC: | ||
| 58 | return FieldEntry.parse(handle.getOwner(), handle.getName(), handle.getDesc()); | ||
| 59 | case Opcodes.H_INVOKEINTERFACE: | ||
| 60 | case Opcodes.H_INVOKESPECIAL: | ||
| 61 | case Opcodes.H_INVOKESTATIC: | ||
| 62 | case Opcodes.H_INVOKEVIRTUAL: | ||
| 63 | case Opcodes.H_NEWINVOKESPECIAL: | ||
| 64 | return MethodEntry.parse(handle.getOwner(), handle.getName(), handle.getDesc()); | ||
| 65 | } | ||
| 66 | throw new RuntimeException("Invalid handle tag " + handle.getTag()); | ||
| 67 | } | ||
| 68 | |||
| 57 | @Override | 69 | @Override |
| 58 | public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) { | 70 | public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) { |
| 59 | for (Object bsmArg : bsmArgs) { | 71 | if ("java/lang/invoke/LambdaMetafactory".equals(bsm.getOwner()) && "metafactory".equals(bsm.getName())) { |
| 60 | if (bsmArg instanceof Handle) { | 72 | Type samMethodType = (Type) bsmArgs[0]; |
| 61 | Handle handle = (Handle) bsmArg; | 73 | Handle implMethod = (Handle) bsmArgs[1]; |
| 62 | switch (handle.getTag()) { | 74 | Type instantiatedMethodType = (Type) bsmArgs[2]; |
| 63 | case Opcodes.H_GETFIELD: | 75 | |
| 64 | case Opcodes.H_GETSTATIC: | 76 | this.indexer.indexLambda(callerEntry, new Lambda( |
| 65 | case Opcodes.H_PUTFIELD: | 77 | name, |
| 66 | case Opcodes.H_PUTSTATIC: | 78 | new MethodDescriptor(desc), |
| 67 | FieldEntry fieldEntry = FieldEntry.parse(handle.getOwner(), handle.getName(), handle.getDesc()); | 79 | new MethodDescriptor(samMethodType.getDescriptor()), |
| 68 | this.indexer.indexFieldReference(callerEntry, fieldEntry); | 80 | getHandleEntry(implMethod), |
| 69 | break; | 81 | new MethodDescriptor(instantiatedMethodType.getDescriptor()) |
| 70 | case Opcodes.H_INVOKEINTERFACE: | 82 | )); |
| 71 | case Opcodes.H_INVOKESPECIAL: | ||
| 72 | case Opcodes.H_INVOKESTATIC: | ||
| 73 | case Opcodes.H_INVOKEVIRTUAL: | ||
| 74 | case Opcodes.H_NEWINVOKESPECIAL: | ||
| 75 | MethodEntry methodEntry = MethodEntry.parse(handle.getOwner(), handle.getName(), handle.getDesc()); | ||
| 76 | this.indexer.indexMethodReference(callerEntry, methodEntry); | ||
| 77 | break; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | 83 | } |
| 81 | } | 84 | } |
| 82 | } | 85 | } |
diff --git a/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java b/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java index ac907af..fd4e618 100644 --- a/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java | |||
| @@ -16,6 +16,7 @@ import com.google.common.collect.Multimap; | |||
| 16 | import cuchaz.enigma.analysis.ParsedJar; | 16 | import cuchaz.enigma.analysis.ParsedJar; |
| 17 | import cuchaz.enigma.translation.mapping.EntryResolver; | 17 | import cuchaz.enigma.translation.mapping.EntryResolver; |
| 18 | import cuchaz.enigma.translation.mapping.IndexEntryResolver; | 18 | import cuchaz.enigma.translation.mapping.IndexEntryResolver; |
| 19 | import cuchaz.enigma.translation.representation.Lambda; | ||
| 19 | import cuchaz.enigma.translation.representation.entry.*; | 20 | import cuchaz.enigma.translation.representation.entry.*; |
| 20 | import org.objectweb.asm.ClassReader; | 21 | import org.objectweb.asm.ClassReader; |
| 21 | import org.objectweb.asm.Opcodes; | 22 | import org.objectweb.asm.Opcodes; |
| @@ -129,6 +130,15 @@ public class JarIndex implements JarIndexer { | |||
| 129 | indexers.forEach(indexer -> indexer.indexFieldReference(callerEntry, referencedEntry)); | 130 | indexers.forEach(indexer -> indexer.indexFieldReference(callerEntry, referencedEntry)); |
| 130 | } | 131 | } |
| 131 | 132 | ||
| 133 | @Override | ||
| 134 | public void indexLambda(MethodDefEntry callerEntry, Lambda lambda) { | ||
| 135 | if (callerEntry.getParent().isJre()) { | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | indexers.forEach(indexer -> indexer.indexLambda(callerEntry, lambda)); | ||
| 140 | } | ||
| 141 | |||
| 132 | public EntryIndex getEntryIndex() { | 142 | public EntryIndex getEntryIndex() { |
| 133 | return entryIndex; | 143 | return entryIndex; |
| 134 | } | 144 | } |
diff --git a/src/main/java/cuchaz/enigma/analysis/index/JarIndexer.java b/src/main/java/cuchaz/enigma/analysis/index/JarIndexer.java index 457c223..4f885b3 100644 --- a/src/main/java/cuchaz/enigma/analysis/index/JarIndexer.java +++ b/src/main/java/cuchaz/enigma/analysis/index/JarIndexer.java | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | package cuchaz.enigma.analysis.index; | 1 | package cuchaz.enigma.analysis.index; |
| 2 | 2 | ||
| 3 | import cuchaz.enigma.translation.representation.Lambda; | ||
| 3 | import cuchaz.enigma.translation.representation.entry.*; | 4 | import cuchaz.enigma.translation.representation.entry.*; |
| 4 | 5 | ||
| 5 | public interface JarIndexer { | 6 | public interface JarIndexer { |
| @@ -18,6 +19,9 @@ public interface JarIndexer { | |||
| 18 | default void indexFieldReference(MethodDefEntry callerEntry, FieldEntry referencedEntry) { | 19 | default void indexFieldReference(MethodDefEntry callerEntry, FieldEntry referencedEntry) { |
| 19 | } | 20 | } |
| 20 | 21 | ||
| 22 | default void indexLambda(MethodDefEntry callerEntry, Lambda lambda) { | ||
| 23 | } | ||
| 24 | |||
| 21 | default void processIndex(JarIndex index) { | 25 | default void processIndex(JarIndex index) { |
| 22 | } | 26 | } |
| 23 | } | 27 | } |
diff --git a/src/main/java/cuchaz/enigma/analysis/index/ReferenceIndex.java b/src/main/java/cuchaz/enigma/analysis/index/ReferenceIndex.java index 04306bd..f54c90d 100644 --- a/src/main/java/cuchaz/enigma/analysis/index/ReferenceIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/index/ReferenceIndex.java | |||
| @@ -4,6 +4,7 @@ import com.google.common.collect.HashMultimap; | |||
| 4 | import com.google.common.collect.Multimap; | 4 | import com.google.common.collect.Multimap; |
| 5 | import cuchaz.enigma.analysis.EntryReference; | 5 | import cuchaz.enigma.analysis.EntryReference; |
| 6 | import cuchaz.enigma.translation.mapping.ResolutionStrategy; | 6 | import cuchaz.enigma.translation.mapping.ResolutionStrategy; |
| 7 | import cuchaz.enigma.translation.representation.Lambda; | ||
| 7 | import cuchaz.enigma.translation.representation.MethodDescriptor; | 8 | import cuchaz.enigma.translation.representation.MethodDescriptor; |
| 8 | import cuchaz.enigma.translation.representation.TypeDescriptor; | 9 | import cuchaz.enigma.translation.representation.TypeDescriptor; |
| 9 | import cuchaz.enigma.translation.representation.entry.*; | 10 | import cuchaz.enigma.translation.representation.entry.*; |
| @@ -64,8 +65,6 @@ public class ReferenceIndex implements JarIndexer { | |||
| 64 | ClassEntry referencedClass = referencedEntry.getParent(); | 65 | ClassEntry referencedClass = referencedEntry.getParent(); |
| 65 | referencesToClasses.put(referencedClass, new EntryReference<>(referencedClass, referencedEntry.getName(), callerEntry)); | 66 | referencesToClasses.put(referencedClass, new EntryReference<>(referencedClass, referencedEntry.getName(), callerEntry)); |
| 66 | } | 67 | } |
| 67 | |||
| 68 | indexMethodDescriptor(callerEntry, referencedEntry.getDesc()); | ||
| 69 | } | 68 | } |
| 70 | 69 | ||
| 71 | @Override | 70 | @Override |
| @@ -74,6 +73,19 @@ public class ReferenceIndex implements JarIndexer { | |||
| 74 | } | 73 | } |
| 75 | 74 | ||
| 76 | @Override | 75 | @Override |
| 76 | public void indexLambda(MethodDefEntry callerEntry, Lambda lambda) { | ||
| 77 | if (lambda.getImplMethod() instanceof MethodEntry) { | ||
| 78 | indexMethodReference(callerEntry, (MethodEntry) lambda.getImplMethod()); | ||
| 79 | } else { | ||
| 80 | indexFieldReference(callerEntry, (FieldEntry) lambda.getImplMethod()); | ||
| 81 | } | ||
| 82 | |||
| 83 | indexMethodDescriptor(callerEntry, lambda.getInvokedType()); | ||
| 84 | indexMethodDescriptor(callerEntry, lambda.getSamMethodType()); | ||
| 85 | indexMethodDescriptor(callerEntry, lambda.getInstantiatedMethodType()); | ||
| 86 | } | ||
| 87 | |||
| 88 | @Override | ||
| 77 | public void processIndex(JarIndex index) { | 89 | public void processIndex(JarIndex index) { |
| 78 | methodReferences = remapReferences(index, methodReferences); | 90 | methodReferences = remapReferences(index, methodReferences); |
| 79 | referencesToMethods = remapReferencesTo(index, referencesToMethods); | 91 | referencesToMethods = remapReferencesTo(index, referencesToMethods); |
diff --git a/src/main/java/cuchaz/enigma/translation/representation/Lambda.java b/src/main/java/cuchaz/enigma/translation/representation/Lambda.java new file mode 100644 index 0000000..63eb563 --- /dev/null +++ b/src/main/java/cuchaz/enigma/translation/representation/Lambda.java | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | package cuchaz.enigma.translation.representation; | ||
| 2 | |||
| 3 | import cuchaz.enigma.translation.Translatable; | ||
| 4 | import cuchaz.enigma.translation.Translator; | ||
| 5 | import cuchaz.enigma.translation.mapping.EntryMap; | ||
| 6 | import cuchaz.enigma.translation.mapping.EntryMapping; | ||
| 7 | import cuchaz.enigma.translation.mapping.EntryResolver; | ||
| 8 | import cuchaz.enigma.translation.mapping.ResolutionStrategy; | ||
| 9 | import cuchaz.enigma.translation.representation.entry.ClassEntry; | ||
| 10 | import cuchaz.enigma.translation.representation.entry.MethodEntry; | ||
| 11 | import cuchaz.enigma.translation.representation.entry.ParentedEntry; | ||
| 12 | |||
| 13 | import java.util.Objects; | ||
| 14 | |||
| 15 | public class Lambda implements Translatable { | ||
| 16 | private final String invokedName; | ||
| 17 | private final MethodDescriptor invokedType; | ||
| 18 | private final MethodDescriptor samMethodType; | ||
| 19 | private final ParentedEntry<?> implMethod; | ||
| 20 | private final MethodDescriptor instantiatedMethodType; | ||
| 21 | |||
| 22 | public Lambda(String invokedName, MethodDescriptor invokedType, MethodDescriptor samMethodType, ParentedEntry<?> implMethod, MethodDescriptor instantiatedMethodType) { | ||
| 23 | this.invokedName = invokedName; | ||
| 24 | this.invokedType = invokedType; | ||
| 25 | this.samMethodType = samMethodType; | ||
| 26 | this.implMethod = implMethod; | ||
| 27 | this.instantiatedMethodType = instantiatedMethodType; | ||
| 28 | } | ||
| 29 | |||
| 30 | @Override | ||
| 31 | public Lambda translate(Translator translator, EntryResolver resolver, EntryMap<EntryMapping> mappings) { | ||
| 32 | MethodEntry samMethod = new MethodEntry(getInterface(), invokedName, samMethodType); | ||
| 33 | EntryMapping samMethodMapping = resolveMapping(resolver, mappings, samMethod); | ||
| 34 | |||
| 35 | return new Lambda( | ||
| 36 | samMethodMapping != null ? samMethodMapping.getTargetName() : invokedName, | ||
| 37 | invokedType.translate(translator, resolver, mappings), | ||
| 38 | samMethodType.translate(translator, resolver, mappings), | ||
| 39 | implMethod.translate(translator, resolver, mappings), | ||
| 40 | instantiatedMethodType.translate(translator, resolver, mappings) | ||
| 41 | ); | ||
| 42 | } | ||
| 43 | |||
| 44 | private EntryMapping resolveMapping(EntryResolver resolver, EntryMap<EntryMapping> mappings, MethodEntry methodEntry) { | ||
| 45 | for (MethodEntry entry : resolver.resolveEntry(methodEntry, ResolutionStrategy.RESOLVE_ROOT)) { | ||
| 46 | EntryMapping mapping = mappings.get(entry); | ||
| 47 | if (mapping != null) { | ||
| 48 | return mapping; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | return null; | ||
| 52 | } | ||
| 53 | |||
| 54 | public ClassEntry getInterface() { | ||
| 55 | return invokedType.getReturnDesc().getTypeEntry(); | ||
| 56 | } | ||
| 57 | |||
| 58 | public String getInvokedName() { | ||
| 59 | return invokedName; | ||
| 60 | } | ||
| 61 | |||
| 62 | public MethodDescriptor getInvokedType() { | ||
| 63 | return invokedType; | ||
| 64 | } | ||
| 65 | |||
| 66 | public MethodDescriptor getSamMethodType() { | ||
| 67 | return samMethodType; | ||
| 68 | } | ||
| 69 | |||
| 70 | public ParentedEntry<?> getImplMethod() { | ||
| 71 | return implMethod; | ||
| 72 | } | ||
| 73 | |||
| 74 | public MethodDescriptor getInstantiatedMethodType() { | ||
| 75 | return instantiatedMethodType; | ||
| 76 | } | ||
| 77 | |||
| 78 | @Override | ||
| 79 | public boolean equals(Object o) { | ||
| 80 | if (this == o) return true; | ||
| 81 | if (o == null || getClass() != o.getClass()) return false; | ||
| 82 | Lambda lambda = (Lambda) o; | ||
| 83 | return Objects.equals(invokedName, lambda.invokedName) && | ||
| 84 | Objects.equals(invokedType, lambda.invokedType) && | ||
| 85 | Objects.equals(samMethodType, lambda.samMethodType) && | ||
| 86 | Objects.equals(implMethod, lambda.implMethod) && | ||
| 87 | Objects.equals(instantiatedMethodType, lambda.instantiatedMethodType); | ||
| 88 | } | ||
| 89 | |||
| 90 | @Override | ||
| 91 | public int hashCode() { | ||
| 92 | return Objects.hash(invokedName, invokedType, samMethodType, implMethod, instantiatedMethodType); | ||
| 93 | } | ||
| 94 | |||
| 95 | @Override | ||
| 96 | public String toString() { | ||
| 97 | return "Lambda{" + | ||
| 98 | "invokedName='" + invokedName + '\'' + | ||
| 99 | ", invokedType=" + invokedType + | ||
| 100 | ", samMethodType=" + samMethodType + | ||
| 101 | ", implMethod=" + implMethod + | ||
| 102 | ", instantiatedMethodType=" + instantiatedMethodType + | ||
| 103 | '}'; | ||
| 104 | } | ||
| 105 | } | ||
diff --git a/src/main/java/cuchaz/enigma/translation/representation/MethodDescriptor.java b/src/main/java/cuchaz/enigma/translation/representation/MethodDescriptor.java index c59751f..37a7014 100644 --- a/src/main/java/cuchaz/enigma/translation/representation/MethodDescriptor.java +++ b/src/main/java/cuchaz/enigma/translation/representation/MethodDescriptor.java | |||
| @@ -118,7 +118,7 @@ public class MethodDescriptor implements Translatable { | |||
| 118 | } | 118 | } |
| 119 | 119 | ||
| 120 | @Override | 120 | @Override |
| 121 | public Translatable translate(Translator translator, EntryResolver resolver, EntryMap<EntryMapping> mappings) { | 121 | public MethodDescriptor translate(Translator translator, EntryResolver resolver, EntryMap<EntryMapping> mappings) { |
| 122 | List<TypeDescriptor> translatedArguments = new ArrayList<>(argumentDescs.size()); | 122 | List<TypeDescriptor> translatedArguments = new ArrayList<>(argumentDescs.size()); |
| 123 | for (TypeDescriptor argument : argumentDescs) { | 123 | for (TypeDescriptor argument : argumentDescs) { |
| 124 | translatedArguments.add(translator.translate(argument)); | 124 | translatedArguments.add(translator.translate(argument)); |
diff --git a/src/main/java/cuchaz/enigma/translation/representation/entry/ParentedEntry.java b/src/main/java/cuchaz/enigma/translation/representation/entry/ParentedEntry.java index 834da8d..b753d3a 100644 --- a/src/main/java/cuchaz/enigma/translation/representation/entry/ParentedEntry.java +++ b/src/main/java/cuchaz/enigma/translation/representation/entry/ParentedEntry.java | |||
| @@ -52,7 +52,7 @@ public abstract class ParentedEntry<P extends Entry<?>> implements Entry<P> { | |||
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | @Override | 54 | @Override |
| 55 | public Translatable translate(Translator translator, EntryResolver resolver, EntryMap<EntryMapping> mappings) { | 55 | public ParentedEntry<P> translate(Translator translator, EntryResolver resolver, EntryMap<EntryMapping> mappings) { |
| 56 | P parent = getParent(); | 56 | P parent = getParent(); |
| 57 | EntryMapping mapping = resolveMapping(resolver, mappings); | 57 | EntryMapping mapping = resolveMapping(resolver, mappings); |
| 58 | if (parent == null) { | 58 | if (parent == null) { |