< prev index next >

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

Print this page




 255                     sb.append(' ');
 256                 }
 257 
 258                 if (isAnnotation()) {
 259                     sb.append('@');
 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(Stream.of(typeparms).map(Class::typeVarBounds).
 276                           collect(Collectors.joining(",", "<", ">")));

 277             }
 278 
 279             for (int i = 0; i < arrayDepth; i++)
 280                 sb.append("[]");
 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                 Stream.of(bounds).map(Type::getTypeName).
 293                 collect(Collectors.joining(" & "));

 294         }
 295     }
 296 
 297     /**
 298      * Returns the {@code Class} object associated with the class or
 299      * interface with the given string name.  Invoking this method is
 300      * equivalent to:
 301      *
 302      * <blockquote>
 303      *  {@code Class.forName(className, true, currentLoader)}
 304      * </blockquote>
 305      *
 306      * where {@code currentLoader} denotes the defining class loader of
 307      * the current class.
 308      *
 309      * <p> For example, the following code fragment returns the
 310      * runtime {@code Class} descriptor for the class named
 311      * {@code java.lang.Thread}:
 312      *
 313      * <blockquote>


3402         Constructor<U>[] out = arg.clone();
3403         ReflectionFactory fact = getReflectionFactory();
3404         for (int i = 0; i < out.length; i++) {
3405             out[i] = fact.copyConstructor(out[i]);
3406         }
3407         return out;
3408     }
3409 
3410     private native Field[]       getDeclaredFields0(boolean publicOnly);
3411     private native Method[]      getDeclaredMethods0(boolean publicOnly);
3412     private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
3413     private native Class<?>[]   getDeclaredClasses0();
3414 
3415     /**
3416      * Helper method to get the method name from arguments.
3417      */
3418     private String methodToString(String name, Class<?>[] argTypes) {
3419         StringBuilder sb = new StringBuilder();
3420         sb.append(getName() + "." + name + "(");
3421         if (argTypes != null) {
3422             sb.append(Stream.of(argTypes).map(c -> {return (c == null) ? "null" : c.getName();}).
3423                       collect(Collectors.joining(",")));

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




 255                     sb.append(' ');
 256                 }
 257 
 258                 if (isAnnotation()) {
 259                     sb.append('@');
 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
 302      * equivalent to:
 303      *
 304      * <blockquote>
 305      *  {@code Class.forName(className, true, currentLoader)}
 306      * </blockquote>
 307      *
 308      * where {@code currentLoader} denotes the defining class loader of
 309      * the current class.
 310      *
 311      * <p> For example, the following code fragment returns the
 312      * runtime {@code Class} descriptor for the class named
 313      * {@code java.lang.Thread}:
 314      *
 315      * <blockquote>


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


< prev index next >