< prev index next >

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

Print this page

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 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.access.SharedSecrets;

 29 import jdk.internal.misc.VM;
 30 import jdk.internal.reflect.CallerSensitive;
 31 import jdk.internal.reflect.ConstructorAccessor;
 32 import jdk.internal.reflect.Reflection;
 33 import jdk.internal.vm.annotation.ForceInline;
 34 import jdk.internal.vm.annotation.Stable;
 35 import sun.reflect.annotation.TypeAnnotation;
 36 import sun.reflect.annotation.TypeAnnotationParser;
 37 import sun.reflect.generics.repository.ConstructorRepository;
 38 import sun.reflect.generics.repository.GenericDeclRepository;
 39 import sun.reflect.generics.factory.CoreReflectionFactory;
 40 import sun.reflect.generics.factory.GenericsFactory;
 41 import sun.reflect.generics.scope.ConstructorScope;
 42 import java.lang.annotation.Annotation;
 43 import java.lang.annotation.AnnotationFormatError;
 44 import java.util.StringJoiner;
 45 
 46 /**
 47  * {@code Constructor} provides information about, and access to, a single
 48  * constructor for a class.

108     private Constructor<T>      root;
109 
110     @Override
111     Constructor<T> getRoot() {
112         return root;
113     }
114 
115     /**
116      * Package-private constructor used by ReflectAccess to enable
117      * instantiation of these objects in Java code from the java.lang
118      * package via jdk.internal.access.JavaLangReflectAccess.
119      */
120     Constructor(Class<T> declaringClass,
121                 Class<?>[] parameterTypes,
122                 Class<?>[] checkedExceptions,
123                 int modifiers,
124                 int slot,
125                 String signature,
126                 byte[] annotations,
127                 byte[] parameterAnnotations) {

128         this.clazz = declaringClass;
129         this.parameterTypes = parameterTypes;
130         this.exceptionTypes = checkedExceptions;
131         this.modifiers = modifiers;
132         this.slot = slot;
133         this.signature = signature;
134         this.annotations = annotations;
135         this.parameterAnnotations = parameterAnnotations;
136     }
137 
138     /**
139      * Package-private routine (exposed to java.lang.Class via
140      * ReflectAccess) which returns a copy of this Constructor. The copy's
141      * "root" field points to this Constructor.
142      */
143     Constructor<T> copy() {
144         // This routine enables sharing of ConstructorAccessor objects
145         // among Constructor objects which refer to the same underlying
146         // method in the VM. (All of this contortion is only necessary
147         // because of the "accessibility" bit in AccessibleObject,

346      * thrown exception types.
347      *
348      * <p>The only possible modifiers for constructors are the access
349      * modifiers {@code public}, {@code protected} or
350      * {@code private}.  Only one of these may appear, or none if the
351      * constructor has default (package) access.
352      *
353      * @return a string describing this {@code Constructor}
354      * @jls 8.8.3 Constructor Modifiers
355      * @jls 8.9.2 Enum Body Declarations
356      */
357     public String toString() {
358         return sharedToString(Modifier.constructorModifiers(),
359                               false,
360                               parameterTypes,
361                               exceptionTypes);
362     }
363 
364     @Override
365     void specificToStringHeader(StringBuilder sb) {
366         sb.append(getDeclaringClass().getTypeName());
367     }
368 
369     @Override
370     String toShortString() {
371         StringBuilder sb = new StringBuilder("constructor ");
372         sb.append(getDeclaringClass().getTypeName());
373         sb.append('(');
374         StringJoiner sj = new StringJoiner(",");
375         for (Class<?> parameterType : getSharedParameterTypes()) {
376             sj.add(parameterType.getTypeName());
377         }
378         sb.append(sj);
379         sb.append(')');
380         return sb.toString();
381     }
382 
383     /**
384      * Returns a string describing this {@code Constructor},
385      * including type parameters.  The string is formatted as the
386      * constructor access modifiers, if any, followed by an
387      * angle-bracketed comma separated list of the constructor's type
388      * parameters, if any, including  informative bounds of the
389      * type parameters, if any, followed by the fully-qualified name of the
390      * declaring class, followed by a parenthesized, comma-separated
391      * list of the constructor's generic formal parameter types.
392      *

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 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.access.SharedSecrets;
 29 import jdk.internal.value.PrimitiveClass;
 30 import jdk.internal.misc.VM;
 31 import jdk.internal.reflect.CallerSensitive;
 32 import jdk.internal.reflect.ConstructorAccessor;
 33 import jdk.internal.reflect.Reflection;
 34 import jdk.internal.vm.annotation.ForceInline;
 35 import jdk.internal.vm.annotation.Stable;
 36 import sun.reflect.annotation.TypeAnnotation;
 37 import sun.reflect.annotation.TypeAnnotationParser;
 38 import sun.reflect.generics.repository.ConstructorRepository;
 39 import sun.reflect.generics.repository.GenericDeclRepository;
 40 import sun.reflect.generics.factory.CoreReflectionFactory;
 41 import sun.reflect.generics.factory.GenericsFactory;
 42 import sun.reflect.generics.scope.ConstructorScope;
 43 import java.lang.annotation.Annotation;
 44 import java.lang.annotation.AnnotationFormatError;
 45 import java.util.StringJoiner;
 46 
 47 /**
 48  * {@code Constructor} provides information about, and access to, a single
 49  * constructor for a class.

109     private Constructor<T>      root;
110 
111     @Override
112     Constructor<T> getRoot() {
113         return root;
114     }
115 
116     /**
117      * Package-private constructor used by ReflectAccess to enable
118      * instantiation of these objects in Java code from the java.lang
119      * package via jdk.internal.access.JavaLangReflectAccess.
120      */
121     Constructor(Class<T> declaringClass,
122                 Class<?>[] parameterTypes,
123                 Class<?>[] checkedExceptions,
124                 int modifiers,
125                 int slot,
126                 String signature,
127                 byte[] annotations,
128                 byte[] parameterAnnotations) {
129         assert PrimitiveClass.isPrimaryType(declaringClass);
130         this.clazz = declaringClass;
131         this.parameterTypes = parameterTypes;
132         this.exceptionTypes = checkedExceptions;
133         this.modifiers = modifiers;
134         this.slot = slot;
135         this.signature = signature;
136         this.annotations = annotations;
137         this.parameterAnnotations = parameterAnnotations;
138     }
139 
140     /**
141      * Package-private routine (exposed to java.lang.Class via
142      * ReflectAccess) which returns a copy of this Constructor. The copy's
143      * "root" field points to this Constructor.
144      */
145     Constructor<T> copy() {
146         // This routine enables sharing of ConstructorAccessor objects
147         // among Constructor objects which refer to the same underlying
148         // method in the VM. (All of this contortion is only necessary
149         // because of the "accessibility" bit in AccessibleObject,

348      * thrown exception types.
349      *
350      * <p>The only possible modifiers for constructors are the access
351      * modifiers {@code public}, {@code protected} or
352      * {@code private}.  Only one of these may appear, or none if the
353      * constructor has default (package) access.
354      *
355      * @return a string describing this {@code Constructor}
356      * @jls 8.8.3 Constructor Modifiers
357      * @jls 8.9.2 Enum Body Declarations
358      */
359     public String toString() {
360         return sharedToString(Modifier.constructorModifiers(),
361                               false,
362                               parameterTypes,
363                               exceptionTypes);
364     }
365 
366     @Override
367     void specificToStringHeader(StringBuilder sb) {
368         sb.append(getDeclaringClassTypeName());
369     }
370 
371     @Override
372     String toShortString() {
373         StringBuilder sb = new StringBuilder("constructor ");
374         sb.append(getDeclaringClassTypeName());
375         sb.append('(');
376         StringJoiner sj = new StringJoiner(",");
377         for (Class<?> parameterType : getSharedParameterTypes()) {
378             sj.add(parameterType.getTypeName());
379         }
380         sb.append(sj);
381         sb.append(')');
382         return sb.toString();
383     }
384 
385     /**
386      * Returns a string describing this {@code Constructor},
387      * including type parameters.  The string is formatted as the
388      * constructor access modifiers, if any, followed by an
389      * angle-bracketed comma separated list of the constructor's type
390      * parameters, if any, including  informative bounds of the
391      * type parameters, if any, followed by the fully-qualified name of the
392      * declaring class, followed by a parenthesized, comma-separated
393      * list of the constructor's generic formal parameter types.
394      *
< prev index next >