< prev index next >

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

Print this page




 756      * (new Object[3]).getClass().getName()
 757      *     returns "[Ljava.lang.Object;"
 758      * (new int[3][4][5][6][7][8][9]).getClass().getName()
 759      *     returns "[[[[[[[I"
 760      * </pre></blockquote>
 761      *
 762      * @return  the name of the class or interface
 763      *          represented by this object.
 764      */
 765     public String getName() {
 766         String name = this.name;
 767         if (name == null)
 768             this.name = name = getName0();
 769         return name;
 770     }
 771 
 772     // cache the name to reduce the number of calls into the VM
 773     private transient String name;
 774     private native String getName0();
 775 












































































 776     /**
 777      * Returns the class loader for the class.  Some implementations may use
 778      * null to represent the bootstrap class loader. This method will return
 779      * null in such implementations if this class was loaded by the bootstrap
 780      * class loader.
 781      *
 782      * <p>If this object
 783      * represents a primitive type or void, null is returned.
 784      *
 785      * @return  the class loader that loaded the class or interface
 786      *          represented by this object.
 787      * @throws  SecurityException
 788      *          if a security manager is present, and the caller's class loader
 789      *          is not {@code null} and is not the same as or an ancestor of the
 790      *          class loader for the class whose class loader is requested,
 791      *          and the caller does not have the
 792      *          {@link RuntimePermission}{@code ("getClassLoader")}
 793      * @see java.lang.ClassLoader
 794      * @see SecurityManager#checkPermission
 795      * @see java.lang.RuntimePermission




 756      * (new Object[3]).getClass().getName()
 757      *     returns "[Ljava.lang.Object;"
 758      * (new int[3][4][5][6][7][8][9]).getClass().getName()
 759      *     returns "[[[[[[[I"
 760      * </pre></blockquote>
 761      *
 762      * @return  the name of the class or interface
 763      *          represented by this object.
 764      */
 765     public String getName() {
 766         String name = this.name;
 767         if (name == null)
 768             this.name = name = getName0();
 769         return name;
 770     }
 771 
 772     // cache the name to reduce the number of calls into the VM
 773     private transient String name;
 774     private native String getName0();
 775 
 776     // JVM type signature, calculated lazily, interned and cached
 777     private transient String signature;
 778 
 779     /**
 780      * Returns a type signature for a Java Virtual Machine type represented by this
 781      * Class object.
 782      * <p>
 783      * <table>
 784      * <tr><th style="text-align:left">JVM Type Signature</th><th>&nbsp;</th><th style="text-align:left">Java Type</th></tr>
 785      * <tr><td>V</td><td/><td>void</td></tr>
 786      * <tr><td>Z</td><td/><td>boolean</td></tr>
 787      * <tr><td>B</td><td/><td>byte</td></tr>
 788      * <tr><td>C</td><td/><td>char</td></tr>
 789      * <tr><td>S</td><td/><td>short</td></tr>
 790      * <tr><td>I</td><td/><td>int</td></tr>
 791      * <tr><td>J</td><td/><td>long</td></tr>
 792      * <tr><td>F</td><td/><td>float</td></tr>
 793      * <tr><td>D</td><td/><td>double</td></tr>
 794      * <tr><td>Lfully-qualified-class ;</td><td/><td>fully-qualified-class</td></tr>
 795      * <tr><td>[ type</td><td/><td>type[]</td></tr>
 796      * </table>
 797      * 
 798      * @return Java Virtual Machine type signature represented by this Class, interned.
 799      * @since 10
 800      */
 801     public String getJvmTypeSignature() {
 802         String sig = signature;
 803         if (sig == null) {
 804             signature = sig = isPrimitive()
 805                               ? getPrimitiveSignature(this) // already interned
 806                               : appendClassSignature(new StringBuilder(), this)
 807                                   .toString().intern();
 808         }
 809         return sig;
 810     }
 811 
 812     // Returns JVM type signature for given primitive.
 813     private static String getPrimitiveSignature(Class<?> cl) {
 814         if (cl == Integer.TYPE)
 815             return "I";
 816         else if (cl == Byte.TYPE)
 817             return "B";
 818         else if (cl == Long.TYPE)
 819             return "J";
 820         else if (cl == Float.TYPE)
 821             return "F";
 822         else if (cl == Double.TYPE)
 823             return "D";
 824         else if (cl == Short.TYPE)
 825             return "S";
 826         else if (cl == Character.TYPE)
 827             return "C";
 828         else if (cl == Boolean.TYPE)
 829             return "Z";
 830         else if (cl == Void.TYPE)
 831             return "V";
 832         else
 833             throw new InternalError();
 834     }
 835 
 836     // appends JVM type signature for given non-primitive
 837     private static StringBuilder appendClassSignature(StringBuilder sbuf, Class<?> cl) {
 838         while (cl.isArray()) {
 839             sbuf.append('[');
 840             cl = cl.getComponentType();
 841         }
 842 
 843         if (cl.isPrimitive()) {
 844             sbuf.append(getPrimitiveSignature(cl));
 845         } else {
 846             sbuf.append('L').append(cl.getName().replace('.', '/')).append(';');
 847         }
 848 
 849         return sbuf;
 850     }
 851 
 852     /**
 853      * Returns the class loader for the class.  Some implementations may use
 854      * null to represent the bootstrap class loader. This method will return
 855      * null in such implementations if this class was loaded by the bootstrap
 856      * class loader.
 857      *
 858      * <p>If this object
 859      * represents a primitive type or void, null is returned.
 860      *
 861      * @return  the class loader that loaded the class or interface
 862      *          represented by this object.
 863      * @throws  SecurityException
 864      *          if a security manager is present, and the caller's class loader
 865      *          is not {@code null} and is not the same as or an ancestor of the
 866      *          class loader for the class whose class loader is requested,
 867      *          and the caller does not have the
 868      *          {@link RuntimePermission}{@code ("getClassLoader")}
 869      * @see java.lang.ClassLoader
 870      * @see SecurityManager#checkPermission
 871      * @see java.lang.RuntimePermission


< prev index next >