< prev index next >

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

Print this page




 260                 }
 261                 if (isInterface()) { // Note: all annotation types are interfaces
 262                     sb.append("interface");
 263                 } else {
 264                     if (isEnum())
 265                         sb.append("enum");
 266                     else
 267                         sb.append("class");
 268                 }
 269                 sb.append(' ');
 270                 sb.append(getName());
 271             }
 272 
 273             TypeVariable<?>[] typeparms = component.getTypeParameters();
 274             if (typeparms.length > 0) {
 275                 sb.append(Arrays.stream(typeparms)
 276                           .map(Class::typeVarBounds)
 277                           .collect(Collectors.joining(",", "<", ">")));
 278             }
 279 
 280             if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));

 281 
 282             return sb.toString();
 283         }
 284     }
 285 
 286     static String typeVarBounds(TypeVariable<?> typeVar) {
 287         Type[] bounds = typeVar.getBounds();
 288         if (bounds.length == 1 && bounds[0].equals(Object.class)) {
 289             return typeVar.getName();
 290         } else {
 291             return typeVar.getName() + " extends " +
 292                 Arrays.stream(bounds)
 293                 .map(Type::getTypeName)
 294                 .collect(Collectors.joining(" & "));
 295         }
 296     }
 297 
 298     /**
 299      * Returns the {@code Class} object associated with the class or
 300      * interface with the given string name.  Invoking this method is


1571             simpleName = simpleName.substring(simpleName.lastIndexOf('.') + 1); // strip the package name
1572         }
1573         return simpleName;
1574     }
1575 
1576     /**
1577      * Return an informative string for the name of this type.
1578      *
1579      * @return an informative string for the name of this type
1580      * @since 1.8
1581      */
1582     public String getTypeName() {
1583         if (isArray()) {
1584             try {
1585                 Class<?> cl = this;
1586                 int dimensions = 0;
1587                 do {
1588                     dimensions++;
1589                     cl = cl.getComponentType();
1590                 } while (cl.isArray());
1591                 return cl.getName() + "[]".repeat(dimensions);





1592             } catch (Throwable e) { /*FALLTHRU*/ }
1593         }
1594         return getName();
1595     }
1596 
1597     /**
1598      * Returns the canonical name of the underlying class as
1599      * defined by the Java Language Specification.  Returns null if
1600      * the underlying class does not have a canonical name (i.e., if
1601      * it is a local or anonymous class or an array whose component
1602      * type does not have a canonical name).
1603      * @return the canonical name of the underlying class if it exists, and
1604      * {@code null} otherwise.
1605      * @since 1.5
1606      */
1607     public String getCanonicalName() {
1608         ReflectionData<T> rd = reflectionData();
1609         String canonicalName = rd.canonicalName;
1610         if (canonicalName == null) {
1611             rd.canonicalName = canonicalName = getCanonicalName0();


3395     }
3396 
3397     private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
3398         Constructor<U>[] out = arg.clone();
3399         ReflectionFactory fact = getReflectionFactory();
3400         for (int i = 0; i < out.length; i++) {
3401             out[i] = fact.copyConstructor(out[i]);
3402         }
3403         return out;
3404     }
3405 
3406     private native Field[]       getDeclaredFields0(boolean publicOnly);
3407     private native Method[]      getDeclaredMethods0(boolean publicOnly);
3408     private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
3409     private native Class<?>[]   getDeclaredClasses0();
3410 
3411     /**
3412      * Helper method to get the method name from arguments.
3413      */
3414     private String methodToString(String name, Class<?>[] argTypes) {
3415         return getName() + '.' + name +
3416                 ((argTypes == null || argTypes.length == 0) ?
3417                 "()" :
3418                 Arrays.stream(argTypes)
3419                         .map(c -> c == null ? "null" : c.getName())
3420                         .collect(Collectors.joining(",", "(", ")")));



3421     }
3422 
3423     /** use serialVersionUID from JDK 1.1 for interoperability */
3424     private static final long serialVersionUID = 3206093459760846163L;
3425 
3426 
3427     /**
3428      * Class Class is special cased within the Serialization Stream Protocol.
3429      *
3430      * A Class instance is written initially into an ObjectOutputStream in the
3431      * following format:
3432      * <pre>
3433      *      {@code TC_CLASS} ClassDescriptor
3434      *      A ClassDescriptor is a special cased serialization of
3435      *      a {@code java.io.ObjectStreamClass} instance.
3436      * </pre>
3437      * A new handle is generated for the initial time the class descriptor
3438      * is written into the stream. Future references to the class descriptor
3439      * are written as references to the initial class descriptor instance.
3440      *




 260                 }
 261                 if (isInterface()) { // Note: all annotation types are interfaces
 262                     sb.append("interface");
 263                 } else {
 264                     if (isEnum())
 265                         sb.append("enum");
 266                     else
 267                         sb.append("class");
 268                 }
 269                 sb.append(' ');
 270                 sb.append(getName());
 271             }
 272 
 273             TypeVariable<?>[] typeparms = component.getTypeParameters();
 274             if (typeparms.length > 0) {
 275                 sb.append(Arrays.stream(typeparms)
 276                           .map(Class::typeVarBounds)
 277                           .collect(Collectors.joining(",", "<", ">")));
 278             }
 279 
 280             for (int i = 0; i < arrayDepth; i++)
 281                 sb.append("[]");
 282 
 283             return sb.toString();
 284         }
 285     }
 286 
 287     static String typeVarBounds(TypeVariable<?> typeVar) {
 288         Type[] bounds = typeVar.getBounds();
 289         if (bounds.length == 1 && bounds[0].equals(Object.class)) {
 290             return typeVar.getName();
 291         } else {
 292             return typeVar.getName() + " extends " +
 293                 Arrays.stream(bounds)
 294                 .map(Type::getTypeName)
 295                 .collect(Collectors.joining(" & "));
 296         }
 297     }
 298 
 299     /**
 300      * Returns the {@code Class} object associated with the class or
 301      * interface with the given string name.  Invoking this method is


1572             simpleName = simpleName.substring(simpleName.lastIndexOf('.') + 1); // strip the package name
1573         }
1574         return simpleName;
1575     }
1576 
1577     /**
1578      * Return an informative string for the name of this type.
1579      *
1580      * @return an informative string for the name of this type
1581      * @since 1.8
1582      */
1583     public String getTypeName() {
1584         if (isArray()) {
1585             try {
1586                 Class<?> cl = this;
1587                 int dimensions = 0;
1588                 do {
1589                     dimensions++;
1590                     cl = cl.getComponentType();
1591                 } while (cl.isArray());
1592                 StringBuilder sb = new StringBuilder();
1593                 sb.append(cl.getName());
1594                 for (int i = 0; i < dimensions; i++) {
1595                     sb.append("[]");
1596                 }
1597                 return sb.toString();
1598             } catch (Throwable e) { /*FALLTHRU*/ }
1599         }
1600         return getName();
1601     }
1602 
1603     /**
1604      * Returns the canonical name of the underlying class as
1605      * defined by the Java Language Specification.  Returns null if
1606      * the underlying class does not have a canonical name (i.e., if
1607      * it is a local or anonymous class or an array whose component
1608      * type does not have a canonical name).
1609      * @return the canonical name of the underlying class if it exists, and
1610      * {@code null} otherwise.
1611      * @since 1.5
1612      */
1613     public String getCanonicalName() {
1614         ReflectionData<T> rd = reflectionData();
1615         String canonicalName = rd.canonicalName;
1616         if (canonicalName == null) {
1617             rd.canonicalName = canonicalName = getCanonicalName0();


3401     }
3402 
3403     private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
3404         Constructor<U>[] out = arg.clone();
3405         ReflectionFactory fact = getReflectionFactory();
3406         for (int i = 0; i < out.length; i++) {
3407             out[i] = fact.copyConstructor(out[i]);
3408         }
3409         return out;
3410     }
3411 
3412     private native Field[]       getDeclaredFields0(boolean publicOnly);
3413     private native Method[]      getDeclaredMethods0(boolean publicOnly);
3414     private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
3415     private native Class<?>[]   getDeclaredClasses0();
3416 
3417     /**
3418      * Helper method to get the method name from arguments.
3419      */
3420     private String methodToString(String name, Class<?>[] argTypes) {
3421         StringBuilder sb = new StringBuilder();
3422         sb.append(getName() + "." + name + "(");
3423         if (argTypes != null) {
3424             sb.append(Arrays.stream(argTypes)
3425                       .map(c -> (c == null) ? "null" : c.getName())
3426                       .collect(Collectors.joining(",")));
3427         }
3428         sb.append(")");
3429         return sb.toString();
3430     }
3431 
3432     /** use serialVersionUID from JDK 1.1 for interoperability */
3433     private static final long serialVersionUID = 3206093459760846163L;
3434 
3435 
3436     /**
3437      * Class Class is special cased within the Serialization Stream Protocol.
3438      *
3439      * A Class instance is written initially into an ObjectOutputStream in the
3440      * following format:
3441      * <pre>
3442      *      {@code TC_CLASS} ClassDescriptor
3443      *      A ClassDescriptor is a special cased serialization of
3444      *      a {@code java.io.ObjectStreamClass} instance.
3445      * </pre>
3446      * A new handle is generated for the initial time the class descriptor
3447      * is written into the stream. Future references to the class descriptor
3448      * are written as references to the initial class descriptor instance.
3449      *


< prev index next >