Skip to content

Commit 1a9edb8

Browse files
liachasotona
authored andcommitted
8309838: Classfile API Util.toBinaryName and other cleanup
Reviewed-by: asotona
1 parent f7de726 commit 1a9edb8

File tree

4 files changed

+15
-76
lines changed

4 files changed

+15
-76
lines changed

src/java.base/share/classes/jdk/internal/classfile/ClassHierarchyResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static ClassHierarchyResolver ofClassLoading(ClassLoader loader) {
196196
@Override
197197
public Class<?> apply(ClassDesc cd) {
198198
try {
199-
return Class.forName(Util.toBinaryName(cd.descriptorString()), false, loader);
199+
return Class.forName(Util.toBinaryName(cd), false, loader);
200200
} catch (ClassNotFoundException ex) {
201201
return null;
202202
}

src/java.base/share/classes/jdk/internal/classfile/impl/ClassHierarchyImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ public boolean isAssignableFrom(ClassDesc thisClass, ClassDesc fromClass) {
120120
}
121121

122122
public static final class CachedClassHierarchyResolver implements ClassHierarchyResolver {
123-
//this instance should leak out, appears only in cache in order to utilize Map.computeIfAbsent
123+
// this instance should not leak out, appears only in cache in order to utilize Map.computeIfAbsent
124+
// is already an invalid combination, so it can be compared with equals or as value class safely
124125
private static final ClassHierarchyResolver.ClassHierarchyInfo NOPE =
125126
new ClassHierarchyInfoImpl(null, true);
126127

@@ -221,7 +222,7 @@ public static final class ClassLoadingClassHierarchyResolver implements ClassHie
221222
@Override
222223
public Class<?> apply(ClassDesc cd) {
223224
try {
224-
return Class.forName(Util.toBinaryName(cd.descriptorString()), false, ClassLoader.getSystemClassLoader());
225+
return Class.forName(Util.toBinaryName(cd), false, ClassLoader.getSystemClassLoader());
225226
} catch (ClassNotFoundException ex) {
226227
return null;
227228
}

src/java.base/share/classes/jdk/internal/classfile/impl/Util.java

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ public class Util {
5454
private Util() {
5555
}
5656

57-
public static String arrayOf(CharSequence s) {
58-
return "[" + s;
59-
}
60-
6157
public static int parameterSlots(MethodTypeDesc mDesc) {
6258
int count = 0;
6359
for (int i = 0; i < mDesc.parameterCount(); i++) {
@@ -85,70 +81,19 @@ public static int maxLocals(int flags, MethodTypeDesc mDesc) {
8581
}
8682

8783
/**
88-
* Convert a descriptor of classes or interfaces or arrays, or an internal
89-
* name of a class or interface, into a fully qualified binary name, that can
90-
* be resolved by {@link Class#forName(String) Class::forName}. Primitive type
91-
* descriptors should never be passed into this method.
92-
*
93-
* @param descOrInternalName a descriptor or internal name
94-
* @return the fully qualified binary name
84+
* Converts a descriptor of classes or interfaces into
85+
* a binary name. Rejects primitive types or arrays.
86+
* This is an inverse of {@link ClassDesc#of(String)}.
9587
*/
96-
public static String toBinaryName(String descOrInternalName) {
97-
if (descOrInternalName.startsWith("L")) {
98-
// descriptors of classes or interfaces
99-
if (descOrInternalName.length() <= 2 || !descOrInternalName.endsWith(";")) {
100-
throw new IllegalArgumentException(descOrInternalName);
101-
}
102-
return descOrInternalName.substring(1, descOrInternalName.length() - 1).replace('/', '.');
103-
} else {
104-
// arrays, classes or interfaces' internal names
105-
return descOrInternalName.replace('/', '.');
106-
}
107-
}
108-
109-
public static Iterator<String> parameterTypes(String s) {
110-
//TODO: gracefully non-method types
111-
return new Iterator<>() {
112-
int ch = 1;
113-
114-
@Override
115-
public boolean hasNext() {
116-
return s.charAt(ch) != ')';
117-
}
118-
119-
@Override
120-
public String next() {
121-
char curr = s.charAt(ch);
122-
switch (curr) {
123-
case 'C', 'B', 'S', 'I', 'J', 'F', 'D', 'Z':
124-
ch++;
125-
return String.valueOf(curr);
126-
case '[':
127-
ch++;
128-
return "[" + next();
129-
case 'L': {
130-
int start = ch;
131-
while (s.charAt(++ch) != ';') { }
132-
++ch;
133-
return s.substring(start, ch);
134-
}
135-
default:
136-
throw new AssertionError("cannot parse string: " + s);
137-
}
138-
}
139-
};
140-
}
141-
142-
public static String returnDescriptor(String s) {
143-
return s.substring(s.indexOf(')') + 1);
88+
public static String toBinaryName(ClassDesc cd) {
89+
return toInternalName(cd).replace('/', '.');
14490
}
14591

14692
public static String toInternalName(ClassDesc cd) {
14793
var desc = cd.descriptorString();
148-
return switch (desc.charAt(0)) {
149-
case 'L' -> desc.substring(1, desc.length() - 1);
150-
default -> throw new IllegalArgumentException(desc);
151-
};
94+
if (desc.charAt(0) == 'L')
95+
return desc.substring(1, desc.length() - 1);
96+
throw new IllegalArgumentException(desc);
15297
}
15398

15499
public static ClassDesc toClassDesc(String classInternalNameOrArrayDesc) {

test/jdk/jdk/classfile/UtilTest.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,13 @@ class UtilTest {
4646
Object.class,
4747
Util.class,
4848
Test.class,
49-
int[][].class,
50-
Object[].class,
5149
})
5250
void testDescToBinaryName(Class<?> type) throws ReflectiveOperationException {
53-
if (!type.isArray()) {
54-
// Test internal name
55-
var internal = type.getName().replace('.', '/');
56-
assertEquals(type, Class.forName(Util.toBinaryName(internal)));
57-
}
58-
// Test descriptor
59-
assertEquals(type, Class.forName(Util.toBinaryName(type.descriptorString())));
51+
var cd = type.describeConstable().orElseThrow();
52+
assertEquals(type, Class.forName(Util.toBinaryName(cd)));
53+
assertEquals(type.getName(), Util.toBinaryName(cd));
6054
}
6155

62-
6356
@Test
6457
void testParameterSlots() {
6558
assertSlots("(IIII)V", 4);

0 commit comments

Comments
 (0)