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