Skip to content

Commit 18d02eb

Browse files
committed
add descriptorString() method in Class.java
1 parent 3650f5a commit 18d02eb

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/classes/modules/java.base/java/lang/Class.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Map;
3434

3535
import jdk.internal.reflect.ConstantPool;
36+
import sun.invoke.util.Wrapper;
3637
import sun.reflect.annotation.AnnotationType;
3738

3839
/**
@@ -57,14 +58,14 @@ public final class Class<T> implements Serializable, GenericDeclaration, Type, A
5758

5859
// we init this on demand (from MJIEnv) since it's not used too often
5960
private static Annotation[] emptyAnnotations; // = new Annotation[0];
60-
61+
6162
private String name;
6263

6364
// set by VM
6465
private transient Module module;
6566

6667
private ClassLoader classLoader;
67-
68+
6869
/**
6970
* search global id of the corresponding ClassInfo, which factors in the classloader
7071
*/
@@ -95,7 +96,7 @@ private Class() {}
9596
public native Class<?> getComponentType ();
9697

9798
public native Field[] getFields() throws SecurityException;
98-
99+
99100
public native Field getDeclaredField (String fieldName) throws NoSuchFieldException,
100101
SecurityException;
101102

@@ -110,7 +111,7 @@ public native Method getMethod (String mthName, Class<?>... paramTypes)
110111
public native Method[] getDeclaredMethods () throws SecurityException;
111112

112113
public native Method[] getMethods () throws SecurityException;
113-
114+
114115
public native Constructor<?>[] getDeclaredConstructors() throws SecurityException;
115116

116117
public native Constructor<?>[] getConstructors() throws SecurityException;
@@ -182,7 +183,7 @@ public String getPackageName() {
182183
T[] getEnumConstantsShared() {
183184
return getEnumConstants();
184185
}
185-
186+
186187
// lazy initialized map for field name -> Enum constants
187188
// <2do> we should move this to the native side, since Enum constants don't change
188189
private transient Map<String, T> enumConstantDirectory = null;
@@ -229,16 +230,33 @@ public String getName () {
229230
public String getSimpleName () {
230231
int idx; // <2do> not really - inner classes?
231232
Class<?> enclosingClass = getEnclosingClass();
232-
233+
233234
if(enclosingClass!=null){
234235
idx = enclosingClass.getName().length();
235236
} else{
236237
idx = name.lastIndexOf('.');
237238
}
238-
239+
239240
return name.substring(idx+1);
240241
}
241242

243+
public String descriptorString(){
244+
if (isPrimitive()){
245+
return Wrapper.forPrimitiveType(this).basicTypeString();
246+
}
247+
248+
if (isArray()){
249+
return "[" + getComponentType().descriptorString();
250+
}else {
251+
String name = getName().replace('.', '/');
252+
253+
StringBuilder s = new StringBuilder(name.length()+2);
254+
s.append('L').append(name).append(';');
255+
256+
return s.toString();
257+
}
258+
}
259+
242260
static native Class<?> getPrimitiveClass (String clsName);
243261

244262
/**

0 commit comments

Comments
 (0)