jdk/src/java.base/share/classes/java/lang/Class.java
changeset 28519 299787d4cf96
parent 28059 e576535359cc
child 29113 4c3e0acf325e
equal deleted inserted replaced
28518:9a6247bbed98 28519:299787d4cf96
   147      * string "class" or "interface", followed by a space, and then by the
   147      * string "class" or "interface", followed by a space, and then by the
   148      * fully qualified name of the class in the format returned by
   148      * fully qualified name of the class in the format returned by
   149      * {@code getName}.  If this {@code Class} object represents a
   149      * {@code getName}.  If this {@code Class} object represents a
   150      * primitive type, this method returns the name of the primitive type.  If
   150      * primitive type, this method returns the name of the primitive type.  If
   151      * this {@code Class} object represents void this method returns
   151      * this {@code Class} object represents void this method returns
   152      * "void".
   152      * "void". If this {@code Class} object represents an array type,
       
   153      * this method returns "class " followed by {@code getName}.
   153      *
   154      *
   154      * @return a string representation of this class object.
   155      * @return a string representation of this class object.
   155      */
   156      */
   156     public String toString() {
   157     public String toString() {
   157         return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
   158         return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
   172      * A space is used to separate modifiers from one another and to
   173      * A space is used to separate modifiers from one another and to
   173      * separate any modifiers from the kind of type. The modifiers
   174      * separate any modifiers from the kind of type. The modifiers
   174      * occur in canonical order. If there are no type parameters, the
   175      * occur in canonical order. If there are no type parameters, the
   175      * type parameter list is elided.
   176      * type parameter list is elided.
   176      *
   177      *
       
   178      * For an array type, the string starts with the type name,
       
   179      * followed by an angle-bracketed comma-separated list of the
       
   180      * type's type parameters, if any, followed by a sequence of
       
   181      * {@code []} characters, one set of brackets per dimension of
       
   182      * the array.
       
   183      *
   177      * <p>Note that since information about the runtime representation
   184      * <p>Note that since information about the runtime representation
   178      * of a type is being generated, modifiers not present on the
   185      * of a type is being generated, modifiers not present on the
   179      * originating source code or illegal on the originating source
   186      * originating source code or illegal on the originating source
   180      * code may be present.
   187      * code may be present.
   181      *
   188      *
   187     public String toGenericString() {
   194     public String toGenericString() {
   188         if (isPrimitive()) {
   195         if (isPrimitive()) {
   189             return toString();
   196             return toString();
   190         } else {
   197         } else {
   191             StringBuilder sb = new StringBuilder();
   198             StringBuilder sb = new StringBuilder();
   192 
   199             Class<?> component = this;
   193             // Class modifiers are a superset of interface modifiers
   200             int arrayDepth = 0;
   194             int modifiers = getModifiers() & Modifier.classModifiers();
   201 
   195             if (modifiers != 0) {
   202             if (isArray()) {
   196                 sb.append(Modifier.toString(modifiers));
   203                 do {
       
   204                     arrayDepth++;
       
   205                     component = component.getComponentType();
       
   206                 } while (component.isArray());
       
   207                 sb.append(component.getName());
       
   208             } else {
       
   209                 // Class modifiers are a superset of interface modifiers
       
   210                 int modifiers = getModifiers() & Modifier.classModifiers();
       
   211                 if (modifiers != 0) {
       
   212                     sb.append(Modifier.toString(modifiers));
       
   213                     sb.append(' ');
       
   214                 }
       
   215 
       
   216                 if (isAnnotation()) {
       
   217                     sb.append('@');
       
   218                 }
       
   219                 if (isInterface()) { // Note: all annotation types are interfaces
       
   220                     sb.append("interface");
       
   221                 } else {
       
   222                     if (isEnum())
       
   223                         sb.append("enum");
       
   224                     else
       
   225                         sb.append("class");
       
   226                 }
   197                 sb.append(' ');
   227                 sb.append(' ');
   198             }
   228                 sb.append(getName());
   199 
   229             }
   200             if (isAnnotation()) {
   230 
   201                 sb.append('@');
   231             TypeVariable<?>[] typeparms = component.getTypeParameters();
   202             }
       
   203             if (isInterface()) { // Note: all annotation types are interfaces
       
   204                 sb.append("interface");
       
   205             } else {
       
   206                 if (isEnum())
       
   207                     sb.append("enum");
       
   208                 else
       
   209                     sb.append("class");
       
   210             }
       
   211             sb.append(' ');
       
   212             sb.append(getName());
       
   213 
       
   214             TypeVariable<?>[] typeparms = getTypeParameters();
       
   215             if (typeparms.length > 0) {
   232             if (typeparms.length > 0) {
   216                 boolean first = true;
   233                 boolean first = true;
   217                 sb.append('<');
   234                 sb.append('<');
   218                 for(TypeVariable<?> typeparm: typeparms) {
   235                 for(TypeVariable<?> typeparm: typeparms) {
   219                     if (!first)
   236                     if (!first)
   221                     sb.append(typeparm.getTypeName());
   238                     sb.append(typeparm.getTypeName());
   222                     first = false;
   239                     first = false;
   223                 }
   240                 }
   224                 sb.append('>');
   241                 sb.append('>');
   225             }
   242             }
       
   243 
       
   244             for (int i = 0; i < arrayDepth; i++)
       
   245                 sb.append("[]");
   226 
   246 
   227             return sb.toString();
   247             return sb.toString();
   228         }
   248         }
   229     }
   249     }
   230 
   250