< prev index next >

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

Print this page

        

@@ -771,10 +771,86 @@
 
     // cache the name to reduce the number of calls into the VM
     private transient String name;
     private native String getName0();
 
+    // JVM type signature, calculated lazily, interned and cached
+    private transient String signature;
+
+    /**
+     * Returns a type signature for a Java Virtual Machine type represented by this
+     * Class object.
+     * <p>
+     * <table>
+     * <tr><th style="text-align:left">JVM Type Signature</th><th>&nbsp;</th><th style="text-align:left">Java Type</th></tr>
+     * <tr><td>V</td><td/><td>void</td></tr>
+     * <tr><td>Z</td><td/><td>boolean</td></tr>
+     * <tr><td>B</td><td/><td>byte</td></tr>
+     * <tr><td>C</td><td/><td>char</td></tr>
+     * <tr><td>S</td><td/><td>short</td></tr>
+     * <tr><td>I</td><td/><td>int</td></tr>
+     * <tr><td>J</td><td/><td>long</td></tr>
+     * <tr><td>F</td><td/><td>float</td></tr>
+     * <tr><td>D</td><td/><td>double</td></tr>
+     * <tr><td>Lfully-qualified-class ;</td><td/><td>fully-qualified-class</td></tr>
+     * <tr><td>[ type</td><td/><td>type[]</td></tr>
+     * </table>
+     * 
+     * @return Java Virtual Machine type signature represented by this Class, interned.
+     * @since 10
+     */
+    public String getJvmTypeSignature() {
+        String sig = signature;
+        if (sig == null) {
+            signature = sig = isPrimitive()
+                              ? getPrimitiveSignature(this) // already interned
+                              : appendClassSignature(new StringBuilder(), this)
+                                  .toString().intern();
+        }
+        return sig;
+    }
+
+    // Returns JVM type signature for given primitive.
+    private static String getPrimitiveSignature(Class<?> cl) {
+        if (cl == Integer.TYPE)
+            return "I";
+        else if (cl == Byte.TYPE)
+            return "B";
+        else if (cl == Long.TYPE)
+            return "J";
+        else if (cl == Float.TYPE)
+            return "F";
+        else if (cl == Double.TYPE)
+            return "D";
+        else if (cl == Short.TYPE)
+            return "S";
+        else if (cl == Character.TYPE)
+            return "C";
+        else if (cl == Boolean.TYPE)
+            return "Z";
+        else if (cl == Void.TYPE)
+            return "V";
+        else
+            throw new InternalError();
+    }
+
+    // appends JVM type signature for given non-primitive
+    private static StringBuilder appendClassSignature(StringBuilder sbuf, Class<?> cl) {
+        while (cl.isArray()) {
+            sbuf.append('[');
+            cl = cl.getComponentType();
+        }
+
+        if (cl.isPrimitive()) {
+            sbuf.append(getPrimitiveSignature(cl));
+        } else {
+            sbuf.append('L').append(cl.getName().replace('.', '/')).append(';');
+        }
+
+        return sbuf;
+    }
+
     /**
      * Returns the class loader for the class.  Some implementations may use
      * null to represent the bootstrap class loader. This method will return
      * null in such implementations if this class was loaded by the bootstrap
      * class loader.
< prev index next >