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