< prev index next >

src/java.base/share/classes/java/lang/reflect/Constructor.java

Print this page




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import jdk.internal.misc.SharedSecrets;
  29 import jdk.internal.reflect.CallerSensitive;
  30 import jdk.internal.reflect.ConstructorAccessor;
  31 import jdk.internal.reflect.Reflection;
  32 import jdk.internal.vm.annotation.ForceInline;
  33 import sun.reflect.annotation.TypeAnnotation;
  34 import sun.reflect.annotation.TypeAnnotationParser;
  35 import sun.reflect.generics.repository.ConstructorRepository;
  36 import sun.reflect.generics.factory.CoreReflectionFactory;
  37 import sun.reflect.generics.factory.GenericsFactory;
  38 import sun.reflect.generics.scope.ConstructorScope;
  39 import java.lang.annotation.Annotation;
  40 import java.lang.annotation.AnnotationFormatError;

  41 
  42 /**
  43  * {@code Constructor} provides information about, and access to, a single
  44  * constructor for a class.
  45  *
  46  * <p>{@code Constructor} permits widening conversions to occur when matching the
  47  * actual parameters to newInstance() with the underlying
  48  * constructor's formal parameters, but throws an
  49  * {@code IllegalArgumentException} if a narrowing conversion would occur.
  50  *
  51  * @param <T> the class in which the constructor is declared
  52  *
  53  * @see Member
  54  * @see java.lang.Class
  55  * @see java.lang.Class#getConstructors()
  56  * @see java.lang.Class#getConstructor(Class[])
  57  * @see java.lang.Class#getDeclaredConstructors()
  58  *
  59  * @author      Kenneth Russell
  60  * @author      Nakul Saraiya


 343      * modifiers {@code public}, {@code protected} or
 344      * {@code private}.  Only one of these may appear, or none if the
 345      * constructor has default (package) access.
 346      *
 347      * @return a string describing this {@code Constructor}
 348      * @jls 8.8.3 Constructor Modifiers
 349      * @jls 8.9.2 Enum Body Declarations
 350      */
 351     public String toString() {
 352         return sharedToString(Modifier.constructorModifiers(),
 353                               false,
 354                               parameterTypes,
 355                               exceptionTypes);
 356     }
 357 
 358     @Override
 359     void specificToStringHeader(StringBuilder sb) {
 360         sb.append(getDeclaringClass().getTypeName());
 361     }
 362 














 363     /**
 364      * Returns a string describing this {@code Constructor},
 365      * including type parameters.  The string is formatted as the
 366      * constructor access modifiers, if any, followed by an
 367      * angle-bracketed comma separated list of the constructor's type
 368      * parameters, if any, followed by the fully-qualified name of the
 369      * declaring class, followed by a parenthesized, comma-separated
 370      * list of the constructor's generic formal parameter types.
 371      *
 372      * If this constructor was declared to take a variable number of
 373      * arguments, instead of denoting the last parameter as
 374      * "<code><i>Type</i>[]</code>", it is denoted as
 375      * "<code><i>Type</i>...</code>".
 376      *
 377      * A space is used to separate access modifiers from one another
 378      * and from the type parameters or class name.  If there are no
 379      * type parameters, the type parameter list is elided; if the type
 380      * parameter list is present, a space separates the list from the
 381      * class name.  If the constructor is declared to throw
 382      * exceptions, the parameter list is followed by a space, followed




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import jdk.internal.misc.SharedSecrets;
  29 import jdk.internal.reflect.CallerSensitive;
  30 import jdk.internal.reflect.ConstructorAccessor;
  31 import jdk.internal.reflect.Reflection;
  32 import jdk.internal.vm.annotation.ForceInline;
  33 import sun.reflect.annotation.TypeAnnotation;
  34 import sun.reflect.annotation.TypeAnnotationParser;
  35 import sun.reflect.generics.repository.ConstructorRepository;
  36 import sun.reflect.generics.factory.CoreReflectionFactory;
  37 import sun.reflect.generics.factory.GenericsFactory;
  38 import sun.reflect.generics.scope.ConstructorScope;
  39 import java.lang.annotation.Annotation;
  40 import java.lang.annotation.AnnotationFormatError;
  41 import java.util.StringJoiner;
  42 
  43 /**
  44  * {@code Constructor} provides information about, and access to, a single
  45  * constructor for a class.
  46  *
  47  * <p>{@code Constructor} permits widening conversions to occur when matching the
  48  * actual parameters to newInstance() with the underlying
  49  * constructor's formal parameters, but throws an
  50  * {@code IllegalArgumentException} if a narrowing conversion would occur.
  51  *
  52  * @param <T> the class in which the constructor is declared
  53  *
  54  * @see Member
  55  * @see java.lang.Class
  56  * @see java.lang.Class#getConstructors()
  57  * @see java.lang.Class#getConstructor(Class[])
  58  * @see java.lang.Class#getDeclaredConstructors()
  59  *
  60  * @author      Kenneth Russell
  61  * @author      Nakul Saraiya


 344      * modifiers {@code public}, {@code protected} or
 345      * {@code private}.  Only one of these may appear, or none if the
 346      * constructor has default (package) access.
 347      *
 348      * @return a string describing this {@code Constructor}
 349      * @jls 8.8.3 Constructor Modifiers
 350      * @jls 8.9.2 Enum Body Declarations
 351      */
 352     public String toString() {
 353         return sharedToString(Modifier.constructorModifiers(),
 354                               false,
 355                               parameterTypes,
 356                               exceptionTypes);
 357     }
 358 
 359     @Override
 360     void specificToStringHeader(StringBuilder sb) {
 361         sb.append(getDeclaringClass().getTypeName());
 362     }
 363 
 364     @Override
 365     String toShortString() {
 366         StringBuilder sb = new StringBuilder("constructor ");
 367         sb.append(getDeclaringClass().getTypeName());
 368         sb.append('(');
 369         StringJoiner sj = new StringJoiner(",");
 370         for (Class<?> parameterType : getParameterTypes()) {
 371             sj.add(parameterType.getTypeName());
 372         }
 373         sb.append(sj);
 374         sb.append(')');
 375         return sb.toString();
 376     }
 377 
 378     /**
 379      * Returns a string describing this {@code Constructor},
 380      * including type parameters.  The string is formatted as the
 381      * constructor access modifiers, if any, followed by an
 382      * angle-bracketed comma separated list of the constructor's type
 383      * parameters, if any, followed by the fully-qualified name of the
 384      * declaring class, followed by a parenthesized, comma-separated
 385      * list of the constructor's generic formal parameter types.
 386      *
 387      * If this constructor was declared to take a variable number of
 388      * arguments, instead of denoting the last parameter as
 389      * "<code><i>Type</i>[]</code>", it is denoted as
 390      * "<code><i>Type</i>...</code>".
 391      *
 392      * A space is used to separate access modifiers from one another
 393      * and from the type parameters or class name.  If there are no
 394      * type parameters, the type parameter list is elided; if the type
 395      * parameter list is present, a space separates the list from the
 396      * class name.  If the constructor is declared to throw
 397      * exceptions, the parameter list is followed by a space, followed


< prev index next >