< prev index next >

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

Print this page




  42 import java.lang.reflect.InvocationTargetException;
  43 import java.lang.reflect.Member;
  44 import java.lang.reflect.Method;
  45 import java.lang.reflect.Modifier;
  46 import java.lang.reflect.Proxy;
  47 import java.lang.reflect.Type;
  48 import java.lang.reflect.TypeVariable;
  49 import java.net.URL;
  50 import java.security.AccessController;
  51 import java.security.PrivilegedAction;
  52 import java.util.ArrayList;
  53 import java.util.Arrays;
  54 import java.util.Collection;
  55 import java.util.HashMap;
  56 import java.util.LinkedHashMap;
  57 import java.util.LinkedHashSet;
  58 import java.util.List;
  59 import java.util.Map;
  60 import java.util.Objects;
  61 import java.util.StringJoiner;


  62 
  63 import jdk.internal.HotSpotIntrinsicCandidate;
  64 import jdk.internal.loader.BootLoader;
  65 import jdk.internal.loader.BuiltinClassLoader;
  66 import jdk.internal.misc.Unsafe;
  67 import jdk.internal.module.Resources;
  68 import jdk.internal.reflect.CallerSensitive;
  69 import jdk.internal.reflect.ConstantPool;
  70 import jdk.internal.reflect.Reflection;
  71 import jdk.internal.reflect.ReflectionFactory;
  72 import jdk.internal.vm.annotation.ForceInline;
  73 import sun.reflect.generics.factory.CoreReflectionFactory;
  74 import sun.reflect.generics.factory.GenericsFactory;
  75 import sun.reflect.generics.repository.ClassRepository;
  76 import sun.reflect.generics.repository.MethodRepository;
  77 import sun.reflect.generics.repository.ConstructorRepository;
  78 import sun.reflect.generics.scope.ClassScope;
  79 import sun.security.util.SecurityConstants;
  80 import sun.reflect.annotation.*;
  81 import sun.reflect.misc.ReflectUtil;


 183      * this {@code Class} object represents void this method returns
 184      * "void". If this {@code Class} object represents an array type,
 185      * this method returns "class " followed by {@code getName}.
 186      *
 187      * @return a string representation of this class object.
 188      */
 189     public String toString() {
 190         return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
 191             + getName();
 192     }
 193 
 194     /**
 195      * Returns a string describing this {@code Class}, including
 196      * information about modifiers and type parameters.
 197      *
 198      * The string is formatted as a list of type modifiers, if any,
 199      * followed by the kind of type (empty string for primitive types
 200      * and {@code class}, {@code enum}, {@code interface}, or
 201      * <code>@</code>{@code interface}, as appropriate), followed
 202      * by the type's name, followed by an angle-bracketed
 203      * comma-separated list of the type's type parameters, if any.

 204      *
 205      * A space is used to separate modifiers from one another and to
 206      * separate any modifiers from the kind of type. The modifiers
 207      * occur in canonical order. If there are no type parameters, the
 208      * type parameter list is elided.
 209      *
 210      * For an array type, the string starts with the type name,
 211      * followed by an angle-bracketed comma-separated list of the
 212      * type's type parameters, if any, followed by a sequence of
 213      * {@code []} characters, one set of brackets per dimension of
 214      * the array.
 215      *
 216      * <p>Note that since information about the runtime representation
 217      * of a type is being generated, modifiers not present on the
 218      * originating source code or illegal on the originating source
 219      * code may be present.
 220      *
 221      * @return a string describing this {@code Class}, including
 222      * information about modifiers and type parameters
 223      *


 245                     sb.append(' ');
 246                 }
 247 
 248                 if (isAnnotation()) {
 249                     sb.append('@');
 250                 }
 251                 if (isInterface()) { // Note: all annotation types are interfaces
 252                     sb.append("interface");
 253                 } else {
 254                     if (isEnum())
 255                         sb.append("enum");
 256                     else
 257                         sb.append("class");
 258                 }
 259                 sb.append(' ');
 260                 sb.append(getName());
 261             }
 262 
 263             TypeVariable<?>[] typeparms = component.getTypeParameters();
 264             if (typeparms.length > 0) {
 265                 StringJoiner sj = new StringJoiner(",", "<", ">");
 266                 for(TypeVariable<?> typeparm: typeparms) {
 267                     sj.add(typeparm.getTypeName());
 268                 }
 269                 sb.append(sj.toString());
 270             }
 271 
 272             for (int i = 0; i < arrayDepth; i++)
 273                 sb.append("[]");
 274 
 275             return sb.toString();
 276         }











 277     }
 278 
 279     /**
 280      * Returns the {@code Class} object associated with the class or
 281      * interface with the given string name.  Invoking this method is
 282      * equivalent to:
 283      *
 284      * <blockquote>
 285      *  {@code Class.forName(className, true, currentLoader)}
 286      * </blockquote>
 287      *
 288      * where {@code currentLoader} denotes the defining class loader of
 289      * the current class.
 290      *
 291      * <p> For example, the following code fragment returns the
 292      * runtime {@code Class} descriptor for the class named
 293      * {@code java.lang.Thread}:
 294      *
 295      * <blockquote>
 296      *   {@code Class t = Class.forName("java.lang.Thread")}




  42 import java.lang.reflect.InvocationTargetException;
  43 import java.lang.reflect.Member;
  44 import java.lang.reflect.Method;
  45 import java.lang.reflect.Modifier;
  46 import java.lang.reflect.Proxy;
  47 import java.lang.reflect.Type;
  48 import java.lang.reflect.TypeVariable;
  49 import java.net.URL;
  50 import java.security.AccessController;
  51 import java.security.PrivilegedAction;
  52 import java.util.ArrayList;
  53 import java.util.Arrays;
  54 import java.util.Collection;
  55 import java.util.HashMap;
  56 import java.util.LinkedHashMap;
  57 import java.util.LinkedHashSet;
  58 import java.util.List;
  59 import java.util.Map;
  60 import java.util.Objects;
  61 import java.util.StringJoiner;
  62 import java.util.stream.Stream;
  63 import java.util.stream.Collectors;
  64 
  65 import jdk.internal.HotSpotIntrinsicCandidate;
  66 import jdk.internal.loader.BootLoader;
  67 import jdk.internal.loader.BuiltinClassLoader;
  68 import jdk.internal.misc.Unsafe;
  69 import jdk.internal.module.Resources;
  70 import jdk.internal.reflect.CallerSensitive;
  71 import jdk.internal.reflect.ConstantPool;
  72 import jdk.internal.reflect.Reflection;
  73 import jdk.internal.reflect.ReflectionFactory;
  74 import jdk.internal.vm.annotation.ForceInline;
  75 import sun.reflect.generics.factory.CoreReflectionFactory;
  76 import sun.reflect.generics.factory.GenericsFactory;
  77 import sun.reflect.generics.repository.ClassRepository;
  78 import sun.reflect.generics.repository.MethodRepository;
  79 import sun.reflect.generics.repository.ConstructorRepository;
  80 import sun.reflect.generics.scope.ClassScope;
  81 import sun.security.util.SecurityConstants;
  82 import sun.reflect.annotation.*;
  83 import sun.reflect.misc.ReflectUtil;


 185      * this {@code Class} object represents void this method returns
 186      * "void". If this {@code Class} object represents an array type,
 187      * this method returns "class " followed by {@code getName}.
 188      *
 189      * @return a string representation of this class object.
 190      */
 191     public String toString() {
 192         return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
 193             + getName();
 194     }
 195 
 196     /**
 197      * Returns a string describing this {@code Class}, including
 198      * information about modifiers and type parameters.
 199      *
 200      * The string is formatted as a list of type modifiers, if any,
 201      * followed by the kind of type (empty string for primitive types
 202      * and {@code class}, {@code enum}, {@code interface}, or
 203      * <code>@</code>{@code interface}, as appropriate), followed
 204      * by the type's name, followed by an angle-bracketed
 205      * comma-separated list of the type's type parameters, if any,
 206      * including informative bounds on the type parameters, if any.
 207      *
 208      * A space is used to separate modifiers from one another and to
 209      * separate any modifiers from the kind of type. The modifiers
 210      * occur in canonical order. If there are no type parameters, the
 211      * type parameter list is elided.
 212      *
 213      * For an array type, the string starts with the type name,
 214      * followed by an angle-bracketed comma-separated list of the
 215      * type's type parameters, if any, followed by a sequence of
 216      * {@code []} characters, one set of brackets per dimension of
 217      * the array.
 218      *
 219      * <p>Note that since information about the runtime representation
 220      * of a type is being generated, modifiers not present on the
 221      * originating source code or illegal on the originating source
 222      * code may be present.
 223      *
 224      * @return a string describing this {@code Class}, including
 225      * information about modifiers and type parameters
 226      *


 248                     sb.append(' ');
 249                 }
 250 
 251                 if (isAnnotation()) {
 252                     sb.append('@');
 253                 }
 254                 if (isInterface()) { // Note: all annotation types are interfaces
 255                     sb.append("interface");
 256                 } else {
 257                     if (isEnum())
 258                         sb.append("enum");
 259                     else
 260                         sb.append("class");
 261                 }
 262                 sb.append(' ');
 263                 sb.append(getName());
 264             }
 265 
 266             TypeVariable<?>[] typeparms = component.getTypeParameters();
 267             if (typeparms.length > 0) {
 268                 sb.append(Stream.of(typeparms).map(t -> typeVarBounds(t)).
 269                           collect(Collectors.joining(",", "<", ">")));



 270             }
 271 
 272             for (int i = 0; i < arrayDepth; i++)
 273                 sb.append("[]");
 274 
 275             return sb.toString();
 276         }
 277     }
 278 
 279     String typeVarBounds(TypeVariable<?> typeVar) {
 280         Type[] bounds = typeVar.getBounds();
 281         if (bounds.length == 1 && bounds[0].equals(Object.class)) {
 282             return typeVar.getName();
 283         } else {
 284             return typeVar.getName() + " extends " +
 285                 Stream.of(bounds).map(e -> e.getTypeName()).
 286                 collect(Collectors.joining(" & "));
 287         }
 288     }
 289 
 290     /**
 291      * Returns the {@code Class} object associated with the class or
 292      * interface with the given string name.  Invoking this method is
 293      * equivalent to:
 294      *
 295      * <blockquote>
 296      *  {@code Class.forName(className, true, currentLoader)}
 297      * </blockquote>
 298      *
 299      * where {@code currentLoader} denotes the defining class loader of
 300      * the current class.
 301      *
 302      * <p> For example, the following code fragment returns the
 303      * runtime {@code Class} descriptor for the class named
 304      * {@code java.lang.Thread}:
 305      *
 306      * <blockquote>
 307      *   {@code Class t = Class.forName("java.lang.Thread")}


< prev index next >