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

Print this page


   1 /*
   2  * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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 sun.reflect.ConstructorAccessor;
  29 import sun.reflect.Reflection;

  30 import sun.reflect.generics.repository.ConstructorRepository;
  31 import sun.reflect.generics.factory.CoreReflectionFactory;
  32 import sun.reflect.generics.factory.GenericsFactory;
  33 import sun.reflect.generics.scope.ConstructorScope;
  34 import java.lang.annotation.Annotation;
  35 import java.lang.annotation.AnnotationFormatError;
  36 
  37 /**
  38  * {@code Constructor} provides information about, and access to, a single
  39  * constructor for a class.
  40  *
  41  * <p>{@code Constructor} permits widening conversions to occur when matching the
  42  * actual parameters to newInstance() with the underlying
  43  * constructor's formal parameters, but throws an
  44  * {@code IllegalArgumentException} if a narrowing conversion would occur.
  45  *
  46  * @param <T> the class in which the constructor is declared
  47  *
  48  * @see Member
  49  * @see java.lang.Class


 137                                                signature,
 138                                                annotations,
 139                                                parameterAnnotations);
 140         res.root = this;
 141         // Might as well eagerly propagate this if already present
 142         res.constructorAccessor = constructorAccessor;
 143 
 144         res.typeAnnotations = typeAnnotations;
 145         return res;
 146     }
 147 
 148     @Override
 149     boolean hasGenericInformation() {
 150         return (getSignature() != null);
 151     }
 152 
 153     @Override
 154     byte[] getAnnotationBytes() {
 155         return annotations;
 156     }




 157 
 158     /**
 159      * {@inheritDoc}
 160      */
 161     @Override
 162     public Class<T> getDeclaringClass() {
 163         return clazz;
 164     }
 165 
 166     /**
 167      * Returns the name of this constructor, as a string.  This is
 168      * the binary name of the constructor's declaring class.
 169      */
 170     @Override
 171     public String getName() {
 172         return getDeclaringClass().getName();
 173     }
 174 
 175     /**
 176      * {@inheritDoc}


 501 
 502     @Override
 503     void handleParameterNumberMismatch(int resultLength, int numParameters) {
 504         Class<?> declaringClass = getDeclaringClass();
 505         if (declaringClass.isEnum() ||
 506             declaringClass.isAnonymousClass() ||
 507             declaringClass.isLocalClass() )
 508             return ; // Can't do reliable parameter counting
 509         else {
 510             if (!declaringClass.isMemberClass() || // top-level
 511                 // Check for the enclosing instance parameter for
 512                 // non-static member classes
 513                 (declaringClass.isMemberClass() &&
 514                  ((declaringClass.getModifiers() & Modifier.STATIC) == 0)  &&
 515                  resultLength + 1 != numParameters) ) {
 516                 throw new AnnotationFormatError(
 517                           "Parameter annotations don't match number of parameters");
 518             }
 519         }
 520     }




 521 }
   1 /*
   2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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 sun.reflect.ConstructorAccessor;
  29 import sun.reflect.Reflection;
  30 import sun.reflect.annotation.TypeAnnotationParser;
  31 import sun.reflect.generics.repository.ConstructorRepository;
  32 import sun.reflect.generics.factory.CoreReflectionFactory;
  33 import sun.reflect.generics.factory.GenericsFactory;
  34 import sun.reflect.generics.scope.ConstructorScope;
  35 import java.lang.annotation.Annotation;
  36 import java.lang.annotation.AnnotationFormatError;
  37 
  38 /**
  39  * {@code Constructor} provides information about, and access to, a single
  40  * constructor for a class.
  41  *
  42  * <p>{@code Constructor} permits widening conversions to occur when matching the
  43  * actual parameters to newInstance() with the underlying
  44  * constructor's formal parameters, but throws an
  45  * {@code IllegalArgumentException} if a narrowing conversion would occur.
  46  *
  47  * @param <T> the class in which the constructor is declared
  48  *
  49  * @see Member
  50  * @see java.lang.Class


 138                                                signature,
 139                                                annotations,
 140                                                parameterAnnotations);
 141         res.root = this;
 142         // Might as well eagerly propagate this if already present
 143         res.constructorAccessor = constructorAccessor;
 144 
 145         res.typeAnnotations = typeAnnotations;
 146         return res;
 147     }
 148 
 149     @Override
 150     boolean hasGenericInformation() {
 151         return (getSignature() != null);
 152     }
 153 
 154     @Override
 155     byte[] getAnnotationBytes() {
 156         return annotations;
 157     }
 158     @Override
 159     byte[] getTypeAnnotationBytes() {
 160         return typeAnnotations;
 161     }
 162 
 163     /**
 164      * {@inheritDoc}
 165      */
 166     @Override
 167     public Class<T> getDeclaringClass() {
 168         return clazz;
 169     }
 170 
 171     /**
 172      * Returns the name of this constructor, as a string.  This is
 173      * the binary name of the constructor's declaring class.
 174      */
 175     @Override
 176     public String getName() {
 177         return getDeclaringClass().getName();
 178     }
 179 
 180     /**
 181      * {@inheritDoc}


 506 
 507     @Override
 508     void handleParameterNumberMismatch(int resultLength, int numParameters) {
 509         Class<?> declaringClass = getDeclaringClass();
 510         if (declaringClass.isEnum() ||
 511             declaringClass.isAnonymousClass() ||
 512             declaringClass.isLocalClass() )
 513             return ; // Can't do reliable parameter counting
 514         else {
 515             if (!declaringClass.isMemberClass() || // top-level
 516                 // Check for the enclosing instance parameter for
 517                 // non-static member classes
 518                 (declaringClass.isMemberClass() &&
 519                  ((declaringClass.getModifiers() & Modifier.STATIC) == 0)  &&
 520                  resultLength + 1 != numParameters) ) {
 521                 throw new AnnotationFormatError(
 522                           "Parameter annotations don't match number of parameters");
 523             }
 524         }
 525     }
 526 
 527     public AnnotatedType getAnnotatedReturnType() {
 528         return getAnnotatedReturnType0(getDeclaringClass());
 529 }
 530 }