diff options
| author | 2017-03-08 08:17:04 +0100 | |
|---|---|---|
| committer | 2017-03-08 08:17:04 +0100 | |
| commit | 6e464ea251cab63c776ece0b2a356f1498ffa294 (patch) | |
| tree | 5ed30c03f5ac4cd2d6877874f5ede576049954f7 /src/main/java/cuchaz/enigma/mapping/ClassEntry.java | |
| parent | Drop unix case style and implement hashCode when equals is overrided (diff) | |
| download | enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.gz enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.xz enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.zip | |
Follow Fabric guidelines
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/ClassEntry.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/ClassEntry.java | 295 |
1 files changed, 148 insertions, 147 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/ClassEntry.java b/src/main/java/cuchaz/enigma/mapping/ClassEntry.java index 398b135..788811f 100644 --- a/src/main/java/cuchaz/enigma/mapping/ClassEntry.java +++ b/src/main/java/cuchaz/enigma/mapping/ClassEntry.java | |||
| @@ -8,6 +8,7 @@ | |||
| 8 | * Contributors: | 8 | * Contributors: |
| 9 | * Jeff Martin - initial API and implementation | 9 | * Jeff Martin - initial API and implementation |
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | |||
| 11 | package cuchaz.enigma.mapping; | 12 | package cuchaz.enigma.mapping; |
| 12 | 13 | ||
| 13 | import com.google.common.collect.Lists; | 14 | import com.google.common.collect.Lists; |
| @@ -16,151 +17,151 @@ import java.util.List; | |||
| 16 | 17 | ||
| 17 | public class ClassEntry implements Entry { | 18 | public class ClassEntry implements Entry { |
| 18 | 19 | ||
| 19 | private String name; | 20 | private String name; |
| 20 | 21 | ||
| 21 | public ClassEntry(String className) { | 22 | public ClassEntry(String className) { |
| 22 | if (className == null) { | 23 | if (className == null) { |
| 23 | throw new IllegalArgumentException("Class name cannot be null!"); | 24 | throw new IllegalArgumentException("Class name cannot be null!"); |
| 24 | } | 25 | } |
| 25 | if (className.indexOf('.') >= 0) { | 26 | if (className.indexOf('.') >= 0) { |
| 26 | throw new IllegalArgumentException("Class name must be in JVM format. ie, path/to/package/class$inner : " + className); | 27 | throw new IllegalArgumentException("Class name must be in JVM format. ie, path/to/package/class$inner : " + className); |
| 27 | } | 28 | } |
| 28 | 29 | ||
| 29 | this.name = className; | 30 | this.name = className; |
| 30 | 31 | ||
| 31 | if (isInnerClass() && getInnermostClassName().indexOf('/') >= 0) { | 32 | if (isInnerClass() && getInnermostClassName().indexOf('/') >= 0) { |
| 32 | throw new IllegalArgumentException("Inner class must not have a package: " + className); | 33 | throw new IllegalArgumentException("Inner class must not have a package: " + className); |
| 33 | } | 34 | } |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | public ClassEntry(ClassEntry other) { | 37 | public ClassEntry(ClassEntry other) { |
| 37 | this.name = other.name; | 38 | this.name = other.name; |
| 38 | } | 39 | } |
| 39 | 40 | ||
| 40 | @Override | 41 | @Override |
| 41 | public String getName() { | 42 | public String getName() { |
| 42 | return this.name; | 43 | return this.name; |
| 43 | } | 44 | } |
| 44 | 45 | ||
| 45 | @Override | 46 | @Override |
| 46 | public String getClassName() { | 47 | public String getClassName() { |
| 47 | return this.name; | 48 | return this.name; |
| 48 | } | 49 | } |
| 49 | 50 | ||
| 50 | @Override | 51 | @Override |
| 51 | public ClassEntry getClassEntry() { | 52 | public ClassEntry getClassEntry() { |
| 52 | return this; | 53 | return this; |
| 53 | } | 54 | } |
| 54 | 55 | ||
| 55 | @Override | 56 | @Override |
| 56 | public ClassEntry cloneToNewClass(ClassEntry classEntry) { | 57 | public ClassEntry cloneToNewClass(ClassEntry classEntry) { |
| 57 | return classEntry; | 58 | return classEntry; |
| 58 | } | 59 | } |
| 59 | 60 | ||
| 60 | @Override | 61 | @Override |
| 61 | public int hashCode() { | 62 | public int hashCode() { |
| 62 | return this.name.hashCode(); | 63 | return this.name.hashCode(); |
| 63 | } | 64 | } |
| 64 | 65 | ||
| 65 | @Override | 66 | @Override |
| 66 | public boolean equals(Object other) { | 67 | public boolean equals(Object other) { |
| 67 | return other instanceof ClassEntry && equals((ClassEntry) other); | 68 | return other instanceof ClassEntry && equals((ClassEntry) other); |
| 68 | } | 69 | } |
| 69 | 70 | ||
| 70 | public boolean equals(ClassEntry other) { | 71 | public boolean equals(ClassEntry other) { |
| 71 | return other != null && this.name.equals(other.name); | 72 | return other != null && this.name.equals(other.name); |
| 72 | } | 73 | } |
| 73 | 74 | ||
| 74 | @Override | 75 | @Override |
| 75 | public String toString() { | 76 | public String toString() { |
| 76 | return this.name; | 77 | return this.name; |
| 77 | } | 78 | } |
| 78 | 79 | ||
| 79 | public boolean isInnerClass() { | 80 | public boolean isInnerClass() { |
| 80 | return this.name.lastIndexOf('$') >= 0; | 81 | return this.name.lastIndexOf('$') >= 0; |
| 81 | } | 82 | } |
| 82 | 83 | ||
| 83 | public List<String> getClassChainNames() { | 84 | public List<String> getClassChainNames() { |
| 84 | return Lists.newArrayList(this.name.split("\\$")); | 85 | return Lists.newArrayList(this.name.split("\\$")); |
| 85 | } | 86 | } |
| 86 | 87 | ||
| 87 | public List<ClassEntry> getClassChain() { | 88 | public List<ClassEntry> getClassChain() { |
| 88 | List<ClassEntry> entries = Lists.newArrayList(); | 89 | List<ClassEntry> entries = Lists.newArrayList(); |
| 89 | StringBuilder buf = new StringBuilder(); | 90 | StringBuilder buf = new StringBuilder(); |
| 90 | for (String name : getClassChainNames()) { | 91 | for (String name : getClassChainNames()) { |
| 91 | if (buf.length() > 0) { | 92 | if (buf.length() > 0) { |
| 92 | buf.append("$"); | 93 | buf.append("$"); |
| 93 | } | 94 | } |
| 94 | buf.append(name); | 95 | buf.append(name); |
| 95 | entries.add(new ClassEntry(buf.toString())); | 96 | entries.add(new ClassEntry(buf.toString())); |
| 96 | } | 97 | } |
| 97 | return entries; | 98 | return entries; |
| 98 | } | 99 | } |
| 99 | 100 | ||
| 100 | public String getOutermostClassName() { | 101 | public String getOutermostClassName() { |
| 101 | if (isInnerClass()) { | 102 | if (isInnerClass()) { |
| 102 | return this.name.substring(0, this.name.indexOf('$')); | 103 | return this.name.substring(0, this.name.indexOf('$')); |
| 103 | } | 104 | } |
| 104 | return this.name; | 105 | return this.name; |
| 105 | } | 106 | } |
| 106 | 107 | ||
| 107 | public ClassEntry getOutermostClassEntry() { | 108 | public ClassEntry getOutermostClassEntry() { |
| 108 | return new ClassEntry(getOutermostClassName()); | 109 | return new ClassEntry(getOutermostClassName()); |
| 109 | } | 110 | } |
| 110 | 111 | ||
| 111 | public String getOuterClassName() { | 112 | public String getOuterClassName() { |
| 112 | if (!isInnerClass()) { | 113 | if (!isInnerClass()) { |
| 113 | throw new Error("This is not an inner class!"); | 114 | throw new Error("This is not an inner class!"); |
| 114 | } | 115 | } |
| 115 | return this.name.substring(0, this.name.lastIndexOf('$')); | 116 | return this.name.substring(0, this.name.lastIndexOf('$')); |
| 116 | } | 117 | } |
| 117 | 118 | ||
| 118 | public ClassEntry getOuterClassEntry() { | 119 | public ClassEntry getOuterClassEntry() { |
| 119 | return new ClassEntry(getOuterClassName()); | 120 | return new ClassEntry(getOuterClassName()); |
| 120 | } | 121 | } |
| 121 | 122 | ||
| 122 | public String getInnermostClassName() { | 123 | public String getInnermostClassName() { |
| 123 | if (!isInnerClass()) { | 124 | if (!isInnerClass()) { |
| 124 | throw new Error("This is not an inner class!"); | 125 | throw new Error("This is not an inner class!"); |
| 125 | } | 126 | } |
| 126 | return this.name.substring(this.name.lastIndexOf('$') + 1); | 127 | return this.name.substring(this.name.lastIndexOf('$') + 1); |
| 127 | } | 128 | } |
| 128 | 129 | ||
| 129 | public boolean isInDefaultPackage() { | 130 | public boolean isInDefaultPackage() { |
| 130 | return this.name.indexOf('/') < 0; | 131 | return this.name.indexOf('/') < 0; |
| 131 | } | 132 | } |
| 132 | 133 | ||
| 133 | public String getPackageName() { | 134 | public String getPackageName() { |
| 134 | int pos = this.name.lastIndexOf('/'); | 135 | int pos = this.name.lastIndexOf('/'); |
| 135 | if (pos > 0) { | 136 | if (pos > 0) { |
| 136 | return this.name.substring(0, pos); | 137 | return this.name.substring(0, pos); |
| 137 | } | 138 | } |
| 138 | return null; | 139 | return null; |
| 139 | } | 140 | } |
| 140 | 141 | ||
| 141 | public String getSimpleName() { | 142 | public String getSimpleName() { |
| 142 | int pos = this.name.lastIndexOf('/'); | 143 | int pos = this.name.lastIndexOf('/'); |
| 143 | if (pos > 0) { | 144 | if (pos > 0) { |
| 144 | return this.name.substring(pos + 1); | 145 | return this.name.substring(pos + 1); |
| 145 | } | 146 | } |
| 146 | return this.name; | 147 | return this.name; |
| 147 | } | 148 | } |
| 148 | 149 | ||
| 149 | public ClassEntry buildClassEntry(List<ClassEntry> classChain) { | 150 | public ClassEntry buildClassEntry(List<ClassEntry> classChain) { |
| 150 | assert (classChain.contains(this)); | 151 | assert (classChain.contains(this)); |
| 151 | StringBuilder buf = new StringBuilder(); | 152 | StringBuilder buf = new StringBuilder(); |
| 152 | for (ClassEntry chainEntry : classChain) { | 153 | for (ClassEntry chainEntry : classChain) { |
| 153 | if (buf.length() == 0) { | 154 | if (buf.length() == 0) { |
| 154 | buf.append(chainEntry.getName()); | 155 | buf.append(chainEntry.getName()); |
| 155 | } else { | 156 | } else { |
| 156 | buf.append("$"); | 157 | buf.append("$"); |
| 157 | buf.append(chainEntry.isInnerClass() ? chainEntry.getInnermostClassName() : chainEntry.getSimpleName()); | 158 | buf.append(chainEntry.isInnerClass() ? chainEntry.getInnermostClassName() : chainEntry.getSimpleName()); |
| 158 | } | 159 | } |
| 159 | 160 | ||
| 160 | if (chainEntry == this) { | 161 | if (chainEntry == this) { |
| 161 | break; | 162 | break; |
| 162 | } | 163 | } |
| 163 | } | 164 | } |
| 164 | return new ClassEntry(buf.toString()); | 165 | return new ClassEntry(buf.toString()); |
| 165 | } | 166 | } |
| 166 | } | 167 | } |