From 18d02eb9c8165ec9aaabfe2d2b9d6e84cee91d2c Mon Sep 17 00:00:00 2001 From: eklaDFF Date: Thu, 30 May 2024 17:52:55 +0530 Subject: [PATCH] add descriptorString() method in Class.java --- .../modules/java.base/java/lang/Class.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/classes/modules/java.base/java/lang/Class.java b/src/classes/modules/java.base/java/lang/Class.java index e0baec71..d5f37a15 100644 --- a/src/classes/modules/java.base/java/lang/Class.java +++ b/src/classes/modules/java.base/java/lang/Class.java @@ -33,6 +33,7 @@ import java.util.Map; import jdk.internal.reflect.ConstantPool; +import sun.invoke.util.Wrapper; import sun.reflect.annotation.AnnotationType; /** @@ -57,14 +58,14 @@ public final class Class implements Serializable, GenericDeclaration, Type, A // we init this on demand (from MJIEnv) since it's not used too often private static Annotation[] emptyAnnotations; // = new Annotation[0]; - + private String name; // set by VM private transient Module module; private ClassLoader classLoader; - + /** * search global id of the corresponding ClassInfo, which factors in the classloader */ @@ -95,7 +96,7 @@ private Class() {} public native Class getComponentType (); public native Field[] getFields() throws SecurityException; - + public native Field getDeclaredField (String fieldName) throws NoSuchFieldException, SecurityException; @@ -110,7 +111,7 @@ public native Method getMethod (String mthName, Class... paramTypes) public native Method[] getDeclaredMethods () throws SecurityException; public native Method[] getMethods () throws SecurityException; - + public native Constructor[] getDeclaredConstructors() throws SecurityException; public native Constructor[] getConstructors() throws SecurityException; @@ -182,7 +183,7 @@ public String getPackageName() { T[] getEnumConstantsShared() { return getEnumConstants(); } - + // lazy initialized map for field name -> Enum constants // <2do> we should move this to the native side, since Enum constants don't change private transient Map enumConstantDirectory = null; @@ -229,16 +230,33 @@ public String getName () { public String getSimpleName () { int idx; // <2do> not really - inner classes? Class enclosingClass = getEnclosingClass(); - + if(enclosingClass!=null){ idx = enclosingClass.getName().length(); } else{ idx = name.lastIndexOf('.'); } - + return name.substring(idx+1); } + public String descriptorString(){ + if (isPrimitive()){ + return Wrapper.forPrimitiveType(this).basicTypeString(); + } + + if (isArray()){ + return "[" + getComponentType().descriptorString(); + }else { + String name = getName().replace('.', '/'); + + StringBuilder s = new StringBuilder(name.length()+2); + s.append('L').append(name).append(';'); + + return s.toString(); + } + } + static native Class getPrimitiveClass (String clsName); /**