diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/analysis/EntryReference.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/analysis/EntryReference.java | 207 |
1 files changed, 104 insertions, 103 deletions
diff --git a/src/main/java/cuchaz/enigma/analysis/EntryReference.java b/src/main/java/cuchaz/enigma/analysis/EntryReference.java index ad4baf8..3761fca 100644 --- a/src/main/java/cuchaz/enigma/analysis/EntryReference.java +++ b/src/main/java/cuchaz/enigma/analysis/EntryReference.java | |||
| @@ -8,116 +8,117 @@ | |||
| 8 | * Contributors: | 8 | * Contributors: |
| 9 | * Jeff Martin - initial API and implementation | 9 | * Jeff Martin - initial API and implementation |
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | package cuchaz.enigma.analysis; | ||
| 12 | 11 | ||
| 13 | import java.util.Arrays; | 12 | package cuchaz.enigma.analysis; |
| 14 | import java.util.List; | ||
| 15 | 13 | ||
| 16 | import cuchaz.enigma.mapping.ClassEntry; | 14 | import cuchaz.enigma.mapping.ClassEntry; |
| 17 | import cuchaz.enigma.mapping.ConstructorEntry; | 15 | import cuchaz.enigma.mapping.ConstructorEntry; |
| 18 | import cuchaz.enigma.mapping.Entry; | 16 | import cuchaz.enigma.mapping.Entry; |
| 19 | import cuchaz.enigma.utils.Utils; | 17 | import cuchaz.enigma.utils.Utils; |
| 20 | 18 | ||
| 19 | import java.util.Arrays; | ||
| 20 | import java.util.List; | ||
| 21 | |||
| 21 | public class EntryReference<E extends Entry, C extends Entry> { | 22 | public class EntryReference<E extends Entry, C extends Entry> { |
| 22 | 23 | ||
| 23 | private static final List<String> ConstructorNonNames = Arrays.asList("this", "super", "static"); | 24 | private static final List<String> ConstructorNonNames = Arrays.asList("this", "super", "static"); |
| 24 | public E entry; | 25 | public E entry; |
| 25 | public C context; | 26 | public C context; |
| 26 | 27 | ||
| 27 | private boolean sourceName; | 28 | private boolean sourceName; |
| 28 | 29 | ||
| 29 | public EntryReference(E entry, String sourceName) { | 30 | public EntryReference(E entry, String sourceName) { |
| 30 | this(entry, sourceName, null); | 31 | this(entry, sourceName, null); |
| 31 | } | 32 | } |
| 32 | 33 | ||
| 33 | public EntryReference(E entry, String sourceName, C context) { | 34 | public EntryReference(E entry, String sourceName, C context) { |
| 34 | if (entry == null) { | 35 | if (entry == null) { |
| 35 | throw new IllegalArgumentException("Entry cannot be null!"); | 36 | throw new IllegalArgumentException("Entry cannot be null!"); |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | this.entry = entry; | 39 | this.entry = entry; |
| 39 | this.context = context; | 40 | this.context = context; |
| 40 | 41 | ||
| 41 | this.sourceName = sourceName != null && sourceName.length() > 0; | 42 | this.sourceName = sourceName != null && !sourceName.isEmpty(); |
| 42 | if (entry instanceof ConstructorEntry && ConstructorNonNames.contains(sourceName)) { | 43 | if (entry instanceof ConstructorEntry && ConstructorNonNames.contains(sourceName)) { |
| 43 | this.sourceName = false; | 44 | this.sourceName = false; |
| 44 | } | 45 | } |
| 45 | } | 46 | } |
| 46 | 47 | ||
| 47 | public EntryReference(E entry, C context, EntryReference<E, C> other) { | 48 | public EntryReference(E entry, C context, EntryReference<E, C> other) { |
| 48 | this.entry = entry; | 49 | this.entry = entry; |
| 49 | this.context = context; | 50 | this.context = context; |
| 50 | this.sourceName = other.sourceName; | 51 | this.sourceName = other.sourceName; |
| 51 | } | 52 | } |
| 52 | 53 | ||
| 53 | public ClassEntry getLocationClassEntry() { | 54 | public ClassEntry getLocationClassEntry() { |
| 54 | if (context != null) { | 55 | if (context != null) { |
| 55 | return context.getClassEntry(); | 56 | return context.getClassEntry(); |
| 56 | } | 57 | } |
| 57 | return entry.getClassEntry(); | 58 | return entry.getClassEntry(); |
| 58 | } | 59 | } |
| 59 | 60 | ||
| 60 | public boolean isNamed() { | 61 | public boolean isNamed() { |
| 61 | return this.sourceName; | 62 | return this.sourceName; |
| 62 | } | 63 | } |
| 63 | 64 | ||
| 64 | public Entry getNameableEntry() { | 65 | public Entry getNameableEntry() { |
| 65 | if (entry instanceof ConstructorEntry) { | 66 | if (entry instanceof ConstructorEntry) { |
| 66 | // renaming a constructor really means renaming the class | 67 | // renaming a constructor really means renaming the class |
| 67 | return entry.getClassEntry(); | 68 | return entry.getClassEntry(); |
| 68 | } | 69 | } |
| 69 | return entry; | 70 | return entry; |
| 70 | } | 71 | } |
| 71 | 72 | ||
| 72 | public String getNamableName() { | 73 | public String getNamableName() { |
| 73 | if (getNameableEntry() instanceof ClassEntry) { | 74 | if (getNameableEntry() instanceof ClassEntry) { |
| 74 | ClassEntry classEntry = (ClassEntry) getNameableEntry(); | 75 | ClassEntry classEntry = (ClassEntry) getNameableEntry(); |
| 75 | if (classEntry.isInnerClass()) { | 76 | if (classEntry.isInnerClass()) { |
| 76 | // make sure we only rename the inner class name | 77 | // make sure we only rename the inner class name |
| 77 | return classEntry.getInnermostClassName(); | 78 | return classEntry.getInnermostClassName(); |
| 78 | } | 79 | } |
| 79 | } | 80 | } |
| 80 | 81 | ||
| 81 | return getNameableEntry().getName(); | 82 | return getNameableEntry().getName(); |
| 82 | } | 83 | } |
| 83 | 84 | ||
| 84 | @Override | 85 | @Override |
| 85 | public int hashCode() { | 86 | public int hashCode() { |
| 86 | if (context != null) { | 87 | if (context != null) { |
| 87 | return Utils.combineHashesOrdered(entry.hashCode(), context.hashCode()); | 88 | return Utils.combineHashesOrdered(entry.hashCode(), context.hashCode()); |
| 88 | } | 89 | } |
| 89 | return entry.hashCode(); | 90 | return entry.hashCode(); |
| 90 | } | 91 | } |
| 91 | 92 | ||
| 92 | @Override | 93 | @Override |
| 93 | public boolean equals(Object other) { | 94 | public boolean equals(Object other) { |
| 94 | return other instanceof EntryReference && equals((EntryReference<?, ?>) other); | 95 | return other instanceof EntryReference && equals((EntryReference<?, ?>) other); |
| 95 | } | 96 | } |
| 96 | 97 | ||
| 97 | public boolean equals(EntryReference<?, ?> other) { | 98 | public boolean equals(EntryReference<?, ?> other) { |
| 98 | // check entry first | 99 | // check entry first |
| 99 | boolean isEntrySame = entry.equals(other.entry); | 100 | boolean isEntrySame = entry.equals(other.entry); |
| 100 | if (!isEntrySame) { | 101 | if (!isEntrySame) { |
| 101 | return false; | 102 | return false; |
| 102 | } | 103 | } |
| 103 | 104 | ||
| 104 | // check caller | 105 | // check caller |
| 105 | if (context == null && other.context == null) { | 106 | if (context == null && other.context == null) { |
| 106 | return true; | 107 | return true; |
| 107 | } else if (context != null && other.context != null) { | 108 | } else if (context != null && other.context != null) { |
| 108 | return context.equals(other.context); | 109 | return context.equals(other.context); |
| 109 | } | 110 | } |
| 110 | return false; | 111 | return false; |
| 111 | } | 112 | } |
| 112 | 113 | ||
| 113 | @Override | 114 | @Override |
| 114 | public String toString() { | 115 | public String toString() { |
| 115 | StringBuilder buf = new StringBuilder(); | 116 | StringBuilder buf = new StringBuilder(); |
| 116 | buf.append(entry); | 117 | buf.append(entry); |
| 117 | if (context != null) { | 118 | if (context != null) { |
| 118 | buf.append(" called from "); | 119 | buf.append(" called from "); |
| 119 | buf.append(context); | 120 | buf.append(context); |
| 120 | } | 121 | } |
| 121 | return buf.toString(); | 122 | return buf.toString(); |
| 122 | } | 123 | } |
| 123 | } | 124 | } |