diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/ClassEntry.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/ClassEntry.java | 171 |
1 files changed, 0 insertions, 171 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/ClassEntry.java b/src/main/java/cuchaz/enigma/mapping/ClassEntry.java deleted file mode 100644 index a49f8dd..0000000 --- a/src/main/java/cuchaz/enigma/mapping/ClassEntry.java +++ /dev/null | |||
| @@ -1,171 +0,0 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * <p> | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | |||
| 12 | package cuchaz.enigma.mapping; | ||
| 13 | |||
| 14 | import com.google.common.base.Preconditions; | ||
| 15 | import com.google.common.collect.Lists; | ||
| 16 | |||
| 17 | import java.util.List; | ||
| 18 | |||
| 19 | public class ClassEntry implements Entry { | ||
| 20 | |||
| 21 | private final String name; | ||
| 22 | |||
| 23 | public ClassEntry(String className) { | ||
| 24 | Preconditions.checkNotNull(className, "Class name cannot be null"); | ||
| 25 | |||
| 26 | if (className.indexOf('.') >= 0) { | ||
| 27 | throw new IllegalArgumentException("Class name must be in JVM format. ie, path/to/package/class$inner : " + className); | ||
| 28 | } | ||
| 29 | |||
| 30 | this.name = className; | ||
| 31 | |||
| 32 | if (isInnerClass() && getInnermostClassName().indexOf('/') >= 0) { | ||
| 33 | throw new IllegalArgumentException("Inner class must not have a package: " + className); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | public ClassEntry(ClassEntry other) { | ||
| 38 | this.name = other.name; | ||
| 39 | } | ||
| 40 | |||
| 41 | @Override | ||
| 42 | public String getName() { | ||
| 43 | return this.name; | ||
| 44 | } | ||
| 45 | |||
| 46 | @Override | ||
| 47 | public String getClassName() { | ||
| 48 | return this.name; | ||
| 49 | } | ||
| 50 | |||
| 51 | @Override | ||
| 52 | public ClassEntry getOwnerClassEntry() { | ||
| 53 | return this; | ||
| 54 | } | ||
| 55 | |||
| 56 | @Override | ||
| 57 | public ClassEntry updateOwnership(ClassEntry classEntry) { | ||
| 58 | return classEntry; | ||
| 59 | } | ||
| 60 | |||
| 61 | @Override | ||
| 62 | public int hashCode() { | ||
| 63 | return this.name.hashCode(); | ||
| 64 | } | ||
| 65 | |||
| 66 | @Override | ||
| 67 | public boolean equals(Object other) { | ||
| 68 | return other instanceof ClassEntry && equals((ClassEntry) other); | ||
| 69 | } | ||
| 70 | |||
| 71 | public boolean equals(ClassEntry other) { | ||
| 72 | return other != null && this.name.equals(other.name); | ||
| 73 | } | ||
| 74 | |||
| 75 | @Override | ||
| 76 | public String toString() { | ||
| 77 | return this.name; | ||
| 78 | } | ||
| 79 | |||
| 80 | public boolean isInnerClass() { | ||
| 81 | return this.name.lastIndexOf('$') >= 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | public List<String> getClassChainNames() { | ||
| 85 | return Lists.newArrayList(this.name.split("\\$")); | ||
| 86 | } | ||
| 87 | |||
| 88 | public List<ClassEntry> getClassChain() { | ||
| 89 | List<ClassEntry> entries = Lists.newArrayList(); | ||
| 90 | StringBuilder buf = new StringBuilder(); | ||
| 91 | for (String name : getClassChainNames()) { | ||
| 92 | if (buf.length() > 0) { | ||
| 93 | buf.append("$"); | ||
| 94 | } | ||
| 95 | buf.append(name); | ||
| 96 | entries.add(new ClassEntry(buf.toString())); | ||
| 97 | } | ||
| 98 | return entries; | ||
| 99 | } | ||
| 100 | |||
| 101 | public String getOutermostClassName() { | ||
| 102 | if (isInnerClass()) { | ||
| 103 | return this.name.substring(0, this.name.indexOf('$')); | ||
| 104 | } | ||
| 105 | return this.name; | ||
| 106 | } | ||
| 107 | |||
| 108 | public ClassEntry getOutermostClassEntry() { | ||
| 109 | return new ClassEntry(getOutermostClassName()); | ||
| 110 | } | ||
| 111 | |||
| 112 | public String getOuterClassName() { | ||
| 113 | if (!isInnerClass()) { | ||
| 114 | throw new Error("This is not an inner class!"); | ||
| 115 | } | ||
| 116 | return this.name.substring(0, this.name.lastIndexOf('$')); | ||
| 117 | } | ||
| 118 | |||
| 119 | public ClassEntry getOuterClassEntry() { | ||
| 120 | return new ClassEntry(getOuterClassName()); | ||
| 121 | } | ||
| 122 | |||
| 123 | public String getInnermostClassName() { | ||
| 124 | if (!isInnerClass()) { | ||
| 125 | throw new Error("This is not an inner class!"); | ||
| 126 | } | ||
| 127 | return this.name.substring(this.name.lastIndexOf('$') + 1); | ||
| 128 | } | ||
| 129 | |||
| 130 | public boolean isInDefaultPackage() { | ||
| 131 | return this.name.indexOf('/') < 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | public String getPackageName() { | ||
| 135 | return getPackageName(this.name); | ||
| 136 | } | ||
| 137 | |||
| 138 | public String getSimpleName() { | ||
| 139 | int pos = this.name.lastIndexOf('/'); | ||
| 140 | if (pos > 0) { | ||
| 141 | return this.name.substring(pos + 1); | ||
| 142 | } | ||
| 143 | return this.name; | ||
| 144 | } | ||
| 145 | |||
| 146 | public static String getPackageName(String name) { | ||
| 147 | int pos = name.lastIndexOf('/'); | ||
| 148 | if (pos > 0) { | ||
| 149 | return name.substring(0, pos); | ||
| 150 | } | ||
| 151 | return null; | ||
| 152 | } | ||
| 153 | |||
| 154 | public ClassEntry buildClassEntry(List<ClassEntry> classChain) { | ||
| 155 | assert (classChain.contains(this)); | ||
| 156 | StringBuilder buf = new StringBuilder(); | ||
| 157 | for (ClassEntry chainEntry : classChain) { | ||
| 158 | if (buf.length() == 0) { | ||
| 159 | buf.append(chainEntry.getName()); | ||
| 160 | } else { | ||
| 161 | buf.append("$"); | ||
| 162 | buf.append(chainEntry.isInnerClass() ? chainEntry.getInnermostClassName() : chainEntry.getSimpleName()); | ||
| 163 | } | ||
| 164 | |||
| 165 | if (chainEntry == this) { | ||
| 166 | break; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | return new ClassEntry(buf.toString()); | ||
| 170 | } | ||
| 171 | } | ||