diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/analysis')
6 files changed, 254 insertions, 34 deletions
diff --git a/src/main/java/cuchaz/enigma/analysis/index/EntryIndex.java b/src/main/java/cuchaz/enigma/analysis/index/EntryIndex.java index 773eaf1..31c6f54 100644 --- a/src/main/java/cuchaz/enigma/analysis/index/EntryIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/index/EntryIndex.java | |||
| @@ -65,6 +65,11 @@ public class EntryIndex implements JarIndexer { | |||
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | @Nullable | 67 | @Nullable |
| 68 | public AccessFlags getClassAccess(ClassEntry entry) { | ||
| 69 | return classes.get(entry); | ||
| 70 | } | ||
| 71 | |||
| 72 | @Nullable | ||
| 68 | public AccessFlags getEntryAccess(Entry<?> entry) { | 73 | public AccessFlags getEntryAccess(Entry<?> entry) { |
| 69 | if (entry instanceof MethodEntry) { | 74 | if (entry instanceof MethodEntry) { |
| 70 | return getMethodAccess((MethodEntry) entry); | 75 | return getMethodAccess((MethodEntry) entry); |
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 a429ff6..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; |
| @@ -29,18 +30,20 @@ public class JarIndex implements JarIndexer { | |||
| 29 | private final InheritanceIndex inheritanceIndex; | 30 | private final InheritanceIndex inheritanceIndex; |
| 30 | private final ReferenceIndex referenceIndex; | 31 | private final ReferenceIndex referenceIndex; |
| 31 | private final BridgeMethodIndex bridgeMethodIndex; | 32 | private final BridgeMethodIndex bridgeMethodIndex; |
| 33 | private final PackageVisibilityIndex packageVisibilityIndex; | ||
| 32 | private final EntryResolver entryResolver; | 34 | private final EntryResolver entryResolver; |
| 33 | 35 | ||
| 34 | private final Collection<JarIndexer> indexers; | 36 | private final Collection<JarIndexer> indexers; |
| 35 | 37 | ||
| 36 | private final Multimap<String, MethodDefEntry> methodImplementations = HashMultimap.create(); | 38 | private final Multimap<String, MethodDefEntry> methodImplementations = HashMultimap.create(); |
| 37 | 39 | ||
| 38 | public JarIndex(EntryIndex entryIndex, InheritanceIndex inheritanceIndex, ReferenceIndex referenceIndex, BridgeMethodIndex bridgeMethodIndex) { | 40 | public JarIndex(EntryIndex entryIndex, InheritanceIndex inheritanceIndex, ReferenceIndex referenceIndex, BridgeMethodIndex bridgeMethodIndex, PackageVisibilityIndex packageVisibilityIndex) { |
| 39 | this.entryIndex = entryIndex; | 41 | this.entryIndex = entryIndex; |
| 40 | this.inheritanceIndex = inheritanceIndex; | 42 | this.inheritanceIndex = inheritanceIndex; |
| 41 | this.referenceIndex = referenceIndex; | 43 | this.referenceIndex = referenceIndex; |
| 42 | this.bridgeMethodIndex = bridgeMethodIndex; | 44 | this.bridgeMethodIndex = bridgeMethodIndex; |
| 43 | this.indexers = Arrays.asList(entryIndex, inheritanceIndex, referenceIndex, bridgeMethodIndex); | 45 | this.packageVisibilityIndex = packageVisibilityIndex; |
| 46 | this.indexers = Arrays.asList(entryIndex, inheritanceIndex, referenceIndex, bridgeMethodIndex, packageVisibilityIndex); | ||
| 44 | this.entryResolver = new IndexEntryResolver(this); | 47 | this.entryResolver = new IndexEntryResolver(this); |
| 45 | } | 48 | } |
| 46 | 49 | ||
| @@ -49,7 +52,8 @@ public class JarIndex implements JarIndexer { | |||
| 49 | InheritanceIndex inheritanceIndex = new InheritanceIndex(entryIndex); | 52 | InheritanceIndex inheritanceIndex = new InheritanceIndex(entryIndex); |
| 50 | ReferenceIndex referenceIndex = new ReferenceIndex(); | 53 | ReferenceIndex referenceIndex = new ReferenceIndex(); |
| 51 | BridgeMethodIndex bridgeMethodIndex = new BridgeMethodIndex(entryIndex, inheritanceIndex, referenceIndex); | 54 | BridgeMethodIndex bridgeMethodIndex = new BridgeMethodIndex(entryIndex, inheritanceIndex, referenceIndex); |
| 52 | return new JarIndex(entryIndex, inheritanceIndex, referenceIndex, bridgeMethodIndex); | 55 | PackageVisibilityIndex packageVisibilityIndex = new PackageVisibilityIndex(); |
| 56 | return new JarIndex(entryIndex, inheritanceIndex, referenceIndex, bridgeMethodIndex, packageVisibilityIndex); | ||
| 53 | } | 57 | } |
| 54 | 58 | ||
| 55 | public void indexJar(ParsedJar jar, Consumer<String> progress) { | 59 | public void indexJar(ParsedJar jar, Consumer<String> progress) { |
| @@ -126,6 +130,15 @@ public class JarIndex implements JarIndexer { | |||
| 126 | indexers.forEach(indexer -> indexer.indexFieldReference(callerEntry, referencedEntry)); | 130 | indexers.forEach(indexer -> indexer.indexFieldReference(callerEntry, referencedEntry)); |
| 127 | } | 131 | } |
| 128 | 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 | |||
| 129 | public EntryIndex getEntryIndex() { | 142 | public EntryIndex getEntryIndex() { |
| 130 | return entryIndex; | 143 | return entryIndex; |
| 131 | } | 144 | } |
| @@ -142,6 +155,10 @@ public class JarIndex implements JarIndexer { | |||
| 142 | return bridgeMethodIndex; | 155 | return bridgeMethodIndex; |
| 143 | } | 156 | } |
| 144 | 157 | ||
| 158 | public PackageVisibilityIndex getPackageVisibilityIndex() { | ||
| 159 | return packageVisibilityIndex; | ||
| 160 | } | ||
| 161 | |||
| 145 | public EntryResolver getEntryResolver() { | 162 | public EntryResolver getEntryResolver() { |
| 146 | return entryResolver; | 163 | return entryResolver; |
| 147 | } | 164 | } |
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/PackageVisibilityIndex.java b/src/main/java/cuchaz/enigma/analysis/index/PackageVisibilityIndex.java new file mode 100644 index 0000000..da28ac4 --- /dev/null +++ b/src/main/java/cuchaz/enigma/analysis/index/PackageVisibilityIndex.java | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | package cuchaz.enigma.analysis.index; | ||
| 2 | |||
| 3 | import com.google.common.collect.HashMultimap; | ||
| 4 | import com.google.common.collect.Lists; | ||
| 5 | import com.google.common.collect.Maps; | ||
| 6 | import com.google.common.collect.Sets; | ||
| 7 | import cuchaz.enigma.analysis.EntryReference; | ||
| 8 | import cuchaz.enigma.translation.representation.AccessFlags; | ||
| 9 | import cuchaz.enigma.translation.representation.entry.*; | ||
| 10 | |||
| 11 | import java.util.*; | ||
| 12 | |||
| 13 | public class PackageVisibilityIndex implements JarIndexer { | ||
| 14 | private static boolean requiresSamePackage(AccessFlags entryAcc, EntryReference ref, InheritanceIndex inheritanceIndex) { | ||
| 15 | if (entryAcc.isPublic()) return false; | ||
| 16 | if (entryAcc.isProtected()) { | ||
| 17 | Set<ClassEntry> callerAncestors = inheritanceIndex.getAncestors(ref.context.getContainingClass()); | ||
| 18 | return !callerAncestors.contains(ref.entry.getContainingClass()); | ||
| 19 | } | ||
| 20 | return !entryAcc.isPrivate(); // if isPrivate is false, it must be package-private | ||
| 21 | } | ||
| 22 | |||
| 23 | private final HashMultimap<ClassEntry, ClassEntry> connections = HashMultimap.create(); | ||
| 24 | private final List<Set<ClassEntry>> partitions = Lists.newArrayList(); | ||
| 25 | private final Map<ClassEntry, Set<ClassEntry>> classPartitions = Maps.newHashMap(); | ||
| 26 | |||
| 27 | private void addConnection(ClassEntry classA, ClassEntry classB) { | ||
| 28 | connections.put(classA, classB); | ||
| 29 | connections.put(classB, classA); | ||
| 30 | } | ||
| 31 | |||
| 32 | private void buildPartition(Set<ClassEntry> unassignedClasses, Set<ClassEntry> partition, ClassEntry member) { | ||
| 33 | for (ClassEntry connected : connections.get(member)) { | ||
| 34 | if (unassignedClasses.remove(connected)) { | ||
| 35 | partition.add(connected); | ||
| 36 | buildPartition(unassignedClasses, partition, connected); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | private void addConnections(EntryIndex entryIndex, ReferenceIndex referenceIndex, InheritanceIndex inheritanceIndex) { | ||
| 42 | for (FieldEntry entry : entryIndex.getFields()) { | ||
| 43 | AccessFlags entryAcc = entryIndex.getFieldAccess(entry); | ||
| 44 | if (!entryAcc.isPublic() && !entryAcc.isPrivate()) { | ||
| 45 | for (EntryReference<FieldEntry, MethodDefEntry> ref : referenceIndex.getReferencesToField(entry)) { | ||
| 46 | if (requiresSamePackage(entryAcc, ref, inheritanceIndex)) { | ||
| 47 | addConnection(ref.entry.getContainingClass(), ref.context.getContainingClass()); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | for (MethodEntry entry : entryIndex.getMethods()) { | ||
| 54 | AccessFlags entryAcc = entryIndex.getMethodAccess(entry); | ||
| 55 | if (!entryAcc.isPublic() && !entryAcc.isPrivate()) { | ||
| 56 | for (EntryReference<MethodEntry, MethodDefEntry> ref : referenceIndex.getReferencesToMethod(entry)) { | ||
| 57 | if (requiresSamePackage(entryAcc, ref, inheritanceIndex)) { | ||
| 58 | addConnection(ref.entry.getContainingClass(), ref.context.getContainingClass()); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | for (ClassEntry entry : entryIndex.getClasses()) { | ||
| 65 | AccessFlags entryAcc = entryIndex.getClassAccess(entry); | ||
| 66 | if (!entryAcc.isPublic() && !entryAcc.isPrivate()) { | ||
| 67 | for (EntryReference<ClassEntry, FieldDefEntry> ref : referenceIndex.getFieldTypeReferencesToClass(entry)) { | ||
| 68 | if (requiresSamePackage(entryAcc, ref, inheritanceIndex)) { | ||
| 69 | addConnection(ref.entry.getContainingClass(), ref.context.getContainingClass()); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | for (EntryReference<ClassEntry, MethodDefEntry> ref : referenceIndex.getMethodTypeReferencesToClass(entry)) { | ||
| 74 | if (requiresSamePackage(entryAcc, ref, inheritanceIndex)) { | ||
| 75 | addConnection(ref.entry.getContainingClass(), ref.context.getContainingClass()); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | for (ClassEntry parent : inheritanceIndex.getParents(entry)) { | ||
| 81 | AccessFlags parentAcc = entryIndex.getClassAccess(parent); | ||
| 82 | if (parentAcc != null && !parentAcc.isPublic() && !parentAcc.isPrivate()) { | ||
| 83 | addConnection(entry, parent); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | ClassEntry outerClass = entry.getOuterClass(); | ||
| 88 | if (outerClass != null) { | ||
| 89 | addConnection(entry, outerClass); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | private void addPartitions(EntryIndex entryIndex) { | ||
| 95 | Set<ClassEntry> unassignedClasses = Sets.newHashSet(entryIndex.getClasses()); | ||
| 96 | while (!unassignedClasses.isEmpty()) { | ||
| 97 | Iterator<ClassEntry> iterator = unassignedClasses.iterator(); | ||
| 98 | ClassEntry initialEntry = iterator.next(); | ||
| 99 | iterator.remove(); | ||
| 100 | |||
| 101 | HashSet<ClassEntry> partition = Sets.newHashSet(); | ||
| 102 | partition.add(initialEntry); | ||
| 103 | buildPartition(unassignedClasses, partition, initialEntry); | ||
| 104 | partitions.add(partition); | ||
| 105 | for (ClassEntry entry : partition) { | ||
| 106 | classPartitions.put(entry, partition); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | public Collection<Set<ClassEntry>> getPartitions() { | ||
| 112 | return partitions; | ||
| 113 | } | ||
| 114 | |||
| 115 | public Set<ClassEntry> getPartition(ClassEntry classEntry) { | ||
| 116 | return classPartitions.get(classEntry); | ||
| 117 | } | ||
| 118 | |||
| 119 | @Override | ||
| 120 | public void processIndex(JarIndex index) { | ||
| 121 | EntryIndex entryIndex = index.getEntryIndex(); | ||
| 122 | ReferenceIndex referenceIndex = index.getReferenceIndex(); | ||
| 123 | InheritanceIndex inheritanceIndex = index.getInheritanceIndex(); | ||
| 124 | addConnections(entryIndex, referenceIndex, inheritanceIndex); | ||
| 125 | addPartitions(entryIndex); | ||
| 126 | } | ||
| 127 | } | ||
diff --git a/src/main/java/cuchaz/enigma/analysis/index/ReferenceIndex.java b/src/main/java/cuchaz/enigma/analysis/index/ReferenceIndex.java index 2b63c5d..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,9 @@ 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; | ||
| 8 | import cuchaz.enigma.translation.representation.MethodDescriptor; | ||
| 9 | import cuchaz.enigma.translation.representation.TypeDescriptor; | ||
| 7 | import cuchaz.enigma.translation.representation.entry.*; | 10 | import cuchaz.enigma.translation.representation.entry.*; |
| 8 | 11 | ||
| 9 | import java.util.Collection; | 12 | import java.util.Collection; |
| @@ -15,6 +18,43 @@ public class ReferenceIndex implements JarIndexer { | |||
| 15 | private Multimap<MethodEntry, EntryReference<MethodEntry, MethodDefEntry>> referencesToMethods = HashMultimap.create(); | 18 | private Multimap<MethodEntry, EntryReference<MethodEntry, MethodDefEntry>> referencesToMethods = HashMultimap.create(); |
| 16 | private Multimap<ClassEntry, EntryReference<ClassEntry, MethodDefEntry>> referencesToClasses = HashMultimap.create(); | 19 | private Multimap<ClassEntry, EntryReference<ClassEntry, MethodDefEntry>> referencesToClasses = HashMultimap.create(); |
| 17 | private Multimap<FieldEntry, EntryReference<FieldEntry, MethodDefEntry>> referencesToFields = HashMultimap.create(); | 20 | private Multimap<FieldEntry, EntryReference<FieldEntry, MethodDefEntry>> referencesToFields = HashMultimap.create(); |
| 21 | private Multimap<ClassEntry, EntryReference<ClassEntry, FieldDefEntry>> fieldTypeReferences = HashMultimap.create(); | ||
| 22 | private Multimap<ClassEntry, EntryReference<ClassEntry, MethodDefEntry>> methodTypeReferences = HashMultimap.create(); | ||
| 23 | |||
| 24 | @Override | ||
| 25 | public void indexMethod(MethodDefEntry methodEntry) { | ||
| 26 | indexMethodDescriptor(methodEntry, methodEntry.getDesc()); | ||
| 27 | } | ||
| 28 | |||
| 29 | private void indexMethodDescriptor(MethodDefEntry entry, MethodDescriptor descriptor) { | ||
| 30 | for (TypeDescriptor typeDescriptor : descriptor.getArgumentDescs()) { | ||
| 31 | indexMethodTypeDescriptor(entry, typeDescriptor); | ||
| 32 | } | ||
| 33 | indexMethodTypeDescriptor(entry, descriptor.getReturnDesc()); | ||
| 34 | } | ||
| 35 | |||
| 36 | private void indexMethodTypeDescriptor(MethodDefEntry method, TypeDescriptor typeDescriptor) { | ||
| 37 | if (typeDescriptor.isType()) { | ||
| 38 | ClassEntry referencedClass = typeDescriptor.getTypeEntry(); | ||
| 39 | methodTypeReferences.put(referencedClass, new EntryReference<>(referencedClass, referencedClass.getName(), method)); | ||
| 40 | } else if (typeDescriptor.isArray()) { | ||
| 41 | indexMethodTypeDescriptor(method, typeDescriptor.getArrayType()); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | @Override | ||
| 46 | public void indexField(FieldDefEntry fieldEntry) { | ||
| 47 | indexFieldTypeDescriptor(fieldEntry, fieldEntry.getDesc()); | ||
| 48 | } | ||
| 49 | |||
| 50 | private void indexFieldTypeDescriptor(FieldDefEntry field, TypeDescriptor typeDescriptor) { | ||
| 51 | if (typeDescriptor.isType()) { | ||
| 52 | ClassEntry referencedClass = typeDescriptor.getTypeEntry(); | ||
| 53 | fieldTypeReferences.put(referencedClass, new EntryReference<>(referencedClass, referencedClass.getName(), field)); | ||
| 54 | } else if (typeDescriptor.isArray()) { | ||
| 55 | indexFieldTypeDescriptor(field, typeDescriptor.getArrayType()); | ||
| 56 | } | ||
| 57 | } | ||
| 18 | 58 | ||
| 19 | @Override | 59 | @Override |
| 20 | public void indexMethodReference(MethodDefEntry callerEntry, MethodEntry referencedEntry) { | 60 | public void indexMethodReference(MethodDefEntry callerEntry, MethodEntry referencedEntry) { |
| @@ -33,15 +73,30 @@ public class ReferenceIndex implements JarIndexer { | |||
| 33 | } | 73 | } |
| 34 | 74 | ||
| 35 | @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 | ||
| 36 | public void processIndex(JarIndex index) { | 89 | public void processIndex(JarIndex index) { |
| 37 | methodReferences = remapReferences(index, methodReferences); | 90 | methodReferences = remapReferences(index, methodReferences); |
| 38 | referencesToMethods = remapReferencesTo(index, referencesToMethods); | 91 | referencesToMethods = remapReferencesTo(index, referencesToMethods); |
| 39 | referencesToClasses = remapReferencesTo(index, referencesToClasses); | 92 | referencesToClasses = remapReferencesTo(index, referencesToClasses); |
| 40 | referencesToFields = remapReferencesTo(index, referencesToFields); | 93 | referencesToFields = remapReferencesTo(index, referencesToFields); |
| 94 | fieldTypeReferences = remapReferencesTo(index, fieldTypeReferences); | ||
| 95 | methodTypeReferences = remapReferencesTo(index, methodTypeReferences); | ||
| 41 | } | 96 | } |
| 42 | 97 | ||
| 43 | private <K extends Entry<?>, V extends Entry<?>> Multimap<K, V> remapReferences(JarIndex index, Multimap<K, V> multimap) { | 98 | private <K extends Entry<?>, V extends Entry<?>> Multimap<K, V> remapReferences(JarIndex index, Multimap<K, V> multimap) { |
| 44 | Multimap<K, V> resolved = HashMultimap.create(); | 99 | Multimap<K, V> resolved = HashMultimap.create(multimap.keySet().size(), multimap.size() / multimap.keySet().size()); |
| 45 | for (Map.Entry<K, V> entry : multimap.entries()) { | 100 | for (Map.Entry<K, V> entry : multimap.entries()) { |
| 46 | resolved.put(remap(index, entry.getKey()), remap(index, entry.getValue())); | 101 | resolved.put(remap(index, entry.getKey()), remap(index, entry.getValue())); |
| 47 | } | 102 | } |
| @@ -49,7 +104,8 @@ public class ReferenceIndex implements JarIndexer { | |||
| 49 | } | 104 | } |
| 50 | 105 | ||
| 51 | private <E extends Entry<?>, C extends Entry<?>> Multimap<E, EntryReference<E, C>> remapReferencesTo(JarIndex index, Multimap<E, EntryReference<E, C>> multimap) { | 106 | private <E extends Entry<?>, C extends Entry<?>> Multimap<E, EntryReference<E, C>> remapReferencesTo(JarIndex index, Multimap<E, EntryReference<E, C>> multimap) { |
| 52 | Multimap<E, EntryReference<E, C>> resolved = HashMultimap.create(); | 107 | final int keySetSize = multimap.keySet().size(); |
| 108 | Multimap<E, EntryReference<E, C>> resolved = HashMultimap.create(keySetSize, keySetSize == 0 ? 0 : multimap.size() / keySetSize); | ||
| 53 | for (Map.Entry<E, EntryReference<E, C>> entry : multimap.entries()) { | 109 | for (Map.Entry<E, EntryReference<E, C>> entry : multimap.entries()) { |
| 54 | resolved.put(remap(index, entry.getKey()), remap(index, entry.getValue())); | 110 | resolved.put(remap(index, entry.getKey()), remap(index, entry.getValue())); |
| 55 | } | 111 | } |
| @@ -79,4 +135,12 @@ public class ReferenceIndex implements JarIndexer { | |||
| 79 | public Collection<EntryReference<MethodEntry, MethodDefEntry>> getReferencesToMethod(MethodEntry entry) { | 135 | public Collection<EntryReference<MethodEntry, MethodDefEntry>> getReferencesToMethod(MethodEntry entry) { |
| 80 | return referencesToMethods.get(entry); | 136 | return referencesToMethods.get(entry); |
| 81 | } | 137 | } |
| 138 | |||
| 139 | public Collection<EntryReference<ClassEntry, FieldDefEntry>> getFieldTypeReferencesToClass(ClassEntry entry) { | ||
| 140 | return fieldTypeReferences.get(entry); | ||
| 141 | } | ||
| 142 | |||
| 143 | public Collection<EntryReference<ClassEntry, MethodDefEntry>> getMethodTypeReferencesToClass(ClassEntry entry) { | ||
| 144 | return methodTypeReferences.get(entry); | ||
| 145 | } | ||
| 82 | } | 146 | } |