< prev index next >

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

Print this page




  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.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


 567      * @since 1.5
 568      */
 569     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
 570         return super.getAnnotation(annotationClass);
 571     }
 572 
 573     /**
 574      * {@inheritDoc}
 575      * @since 1.5
 576      */
 577     public Annotation[] getDeclaredAnnotations()  {
 578         return super.getDeclaredAnnotations();
 579     }
 580 
 581     /**
 582      * {@inheritDoc}
 583      * @since 1.5
 584      */
 585     @Override
 586     public Annotation[][] getParameterAnnotations() {
 587         return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations);
 588     }
 589 
 590     @Override
 591     void handleParameterNumberMismatch(int resultLength, int numParameters) {
 592         Class<?> declaringClass = getDeclaringClass();
 593         if (declaringClass.isEnum() ||
 594             declaringClass.isAnonymousClass() ||
 595             declaringClass.isLocalClass() )
 596             return ; // Can't do reliable parameter counting
 597         else {
 598             if (!declaringClass.isMemberClass() || // top-level
 599                 // Check for the enclosing instance parameter for
 600                 // non-static member classes
 601                 (declaringClass.isMemberClass() &&
 602                  ((declaringClass.getModifiers() & Modifier.STATIC) == 0)  &&
 603                  resultLength + 1 != numParameters) ) {
 604                 throw new AnnotationFormatError(
 605                           "Parameter annotations don't match number of parameters");
 606             }

 607         }
 608     }
 609 
 610     /**
 611      * {@inheritDoc}
 612      * @since 1.8
 613      */
 614     @Override
 615     public AnnotatedType getAnnotatedReturnType() {
 616         return getAnnotatedReturnType0(getDeclaringClass());
 617     }
 618 
 619     /**
 620      * {@inheritDoc}
 621      * @since 1.8
 622      */
 623     @Override
 624     public AnnotatedType getAnnotatedReceiverType() {
 625         Class<?> thisDeclClass = getDeclaringClass();
 626         Class<?> enclosingClass = thisDeclClass.getEnclosingClass();




  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.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.AnnotationSupport;
  34 import sun.reflect.annotation.TypeAnnotation;
  35 import sun.reflect.annotation.TypeAnnotationParser;
  36 import sun.reflect.generics.repository.ConstructorRepository;
  37 import sun.reflect.generics.factory.CoreReflectionFactory;
  38 import sun.reflect.generics.factory.GenericsFactory;
  39 import sun.reflect.generics.scope.ConstructorScope;
  40 import java.lang.annotation.Annotation;
  41 import java.util.Arrays;
  42 import java.util.StringJoiner;
  43 
  44 /**
  45  * {@code Constructor} provides information about, and access to, a single
  46  * constructor for a class.
  47  *
  48  * <p>{@code Constructor} permits widening conversions to occur when matching the
  49  * actual parameters to newInstance() with the underlying
  50  * constructor's formal parameters, but throws an
  51  * {@code IllegalArgumentException} if a narrowing conversion would occur.
  52  *
  53  * @param <T> the class in which the constructor is declared
  54  *
  55  * @see Member
  56  * @see java.lang.Class
  57  * @see java.lang.Class#getConstructors()
  58  * @see java.lang.Class#getConstructor(Class[])
  59  * @see java.lang.Class#getDeclaredConstructors()
  60  *
  61  * @author      Kenneth Russell


 568      * @since 1.5
 569      */
 570     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
 571         return super.getAnnotation(annotationClass);
 572     }
 573 
 574     /**
 575      * {@inheritDoc}
 576      * @since 1.5
 577      */
 578     public Annotation[] getDeclaredAnnotations()  {
 579         return super.getDeclaredAnnotations();
 580     }
 581 
 582     /**
 583      * {@inheritDoc}
 584      * @since 1.5
 585      */
 586     @Override
 587     public Annotation[][] getParameterAnnotations() {
 588         if (parameterAnnotations == null) {
 589             return new Annotation[parameterTypes.length][0];
 590         } else {
 591             Annotation[][] anns = parseParameterAnnotations(parameterAnnotations);
 592             anns = AnnotationSupport.fixConstructorParameterAnnotations(
 593                 this, anns, new Annotation[0], Annotation[][]::new);
 594             if (anns.length != parameterTypes.length) {
 595                 throwParameterAnnotationsDontMatchNumberOfParameters();











 596             }
 597             return anns;
 598         }
 599     }
 600 
 601     /**
 602      * {@inheritDoc}
 603      * @since 1.8
 604      */
 605     @Override
 606     public AnnotatedType getAnnotatedReturnType() {
 607         return getAnnotatedReturnType0(getDeclaringClass());
 608     }
 609 
 610     /**
 611      * {@inheritDoc}
 612      * @since 1.8
 613      */
 614     @Override
 615     public AnnotatedType getAnnotatedReceiverType() {
 616         Class<?> thisDeclClass = getDeclaringClass();
 617         Class<?> enclosingClass = thisDeclClass.getEnclosingClass();


< prev index next >