src/share/classes/java/lang/reflect/Executable.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 java.lang.annotation.*;

  29 import java.util.Map;
  30 import sun.reflect.annotation.AnnotationParser;
  31 import sun.reflect.generics.repository.ConstructorRepository;
  32 
  33 /**
  34  * A shared superclass for the common functionality of {@link Method}
  35  * and {@link Constructor}.
  36  *
  37  * @since 1.8
  38  */
  39 public abstract class Executable extends AccessibleObject
  40     implements Member, GenericDeclaration {
  41     /*
  42      * Only grant package-visibility to the constructor.
  43      */
  44     Executable() {}
  45 
  46     /**
  47      * Accessor method to allow code sharing
  48      */
  49     abstract byte[] getAnnotationBytes();
  50 
  51     /**
  52      * Does the Executable have generic information.
  53      */
  54     abstract boolean hasGenericInformation();
  55 
  56     abstract ConstructorRepository getGenericInfo();
  57 


  58     boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) {
  59         /* Avoid unnecessary cloning */
  60         if (params1.length == params2.length) {
  61             for (int i = 0; i < params1.length; i++) {
  62                 if (params1[i] != params2[i])
  63                     return false;
  64             }
  65             return true;
  66         }
  67         return false;
  68     }
  69 
  70     Annotation[][] parseParameterAnnotations(byte[] parameterAnnotations) {
  71         return AnnotationParser.parseParameterAnnotations(
  72                parameterAnnotations,
  73                sun.misc.SharedSecrets.getJavaLangAccess().
  74                getConstantPool(getDeclaringClass()),
  75                getDeclaringClass());
  76     }
  77 


 206      *     the type variables declared by this generic declaration
 207      * @throws GenericSignatureFormatError if the generic
 208      *     signature of this generic declaration does not conform to
 209      *     the format specified in
 210      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 211      */
 212     public abstract TypeVariable<?>[] getTypeParameters();
 213 
 214     /**
 215      * Returns an array of {@code Class} objects that represent the formal
 216      * parameter types, in declaration order, of the executable
 217      * represented by this object.  Returns an array of length
 218      * 0 if the underlying executable takes no parameters.
 219      *
 220      * @return the parameter types for the executable this object
 221      * represents
 222      */
 223     public abstract Class<?>[] getParameterTypes();
 224 
 225     /**









 226      * Returns an array of {@code Type} objects that represent the formal
 227      * parameter types, in declaration order, of the executable represented by
 228      * this object. Returns an array of length 0 if the
 229      * underlying executable takes no parameters.
 230      *
 231      * <p>If a formal parameter type is a parameterized type,
 232      * the {@code Type} object returned for it must accurately reflect
 233      * the actual type parameters used in the source code.
 234      *
 235      * <p>If a formal parameter type is a type variable or a parameterized
 236      * type, it is created. Otherwise, it is resolved.
 237      *
 238      * @return an array of {@code Type}s that represent the formal
 239      *     parameter types of the underlying executable, in declaration order
 240      * @throws GenericSignatureFormatError
 241      *     if the generic method signature does not conform to the format
 242      *     specified in
 243      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 244      * @throws TypeNotPresentException if any of the parameter
 245      *     types of the underlying executable refers to a non-existent type
 246      *     declaration
 247      * @throws MalformedParameterizedTypeException if any of
 248      *     the underlying executable's parameter types refer to a parameterized
 249      *     type that cannot be instantiated for any reason
 250      */
 251     public Type[] getGenericParameterTypes() {
 252         if (hasGenericInformation())
 253             return getGenericInfo().getParameterTypes();
 254         else
 255             return getParameterTypes();
 256     }
 257 
 258     /**









































 259      * Returns an array of {@code Class} objects that represent the
 260      * types of exceptions declared to be thrown by the underlying
 261      * executable represented by this object.  Returns an array of
 262      * length 0 if the executable declares no exceptions in its {@code
 263      * throws} clause.
 264      *
 265      * @return the exception types declared as being thrown by the
 266      * executable this object represents
 267      */
 268     public abstract Class<?>[] getExceptionTypes();
 269 
 270     /**
 271      * Returns an array of {@code Type} objects that represent the
 272      * exceptions declared to be thrown by this executable object.
 273      * Returns an array of length 0 if the underlying executable declares
 274      * no exceptions in its {@code throws} clause.
 275      *
 276      * <p>If an exception type is a type variable or a parameterized
 277      * type, it is created. Otherwise, it is resolved.
 278      *


 373 
 374     /**
 375      * {@inheritDoc}
 376      */
 377     public Annotation[] getDeclaredAnnotations()  {
 378         return AnnotationParser.toArray(declaredAnnotations());
 379     }
 380 
 381     private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
 382 
 383     private synchronized  Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
 384         if (declaredAnnotations == null) {
 385             declaredAnnotations = AnnotationParser.parseAnnotations(
 386                 getAnnotationBytes(),
 387                 sun.misc.SharedSecrets.getJavaLangAccess().
 388                 getConstantPool(getDeclaringClass()),
 389                 getDeclaringClass());
 390         }
 391         return declaredAnnotations;
 392     }

 393 }


   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 java.lang.annotation.*;
  29 import java.lang.ref.SoftReference;
  30 import java.util.Map;
  31 import sun.reflect.annotation.AnnotationParser;
  32 import sun.reflect.generics.repository.ConstructorRepository;
  33 
  34 /**
  35  * A shared superclass for the common functionality of {@link Method}
  36  * and {@link Constructor}.
  37  *
  38  * @since 1.8
  39  */
  40 public abstract class Executable extends AccessibleObject
  41     implements Member, GenericDeclaration {
  42     /*
  43      * Only grant package-visibility to the constructor.
  44      */
  45     Executable() {}
  46 
  47     /**
  48      * Accessor method to allow code sharing
  49      */
  50     abstract byte[] getAnnotationBytes();
  51 
  52     /**
  53      * Does the Executable have generic information.
  54      */
  55     abstract boolean hasGenericInformation();
  56 
  57     abstract ConstructorRepository getGenericInfo();
  58 
  59     private volatile transient SoftReference<Parameter[]> parameters;
  60 
  61     boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) {
  62         /* Avoid unnecessary cloning */
  63         if (params1.length == params2.length) {
  64             for (int i = 0; i < params1.length; i++) {
  65                 if (params1[i] != params2[i])
  66                     return false;
  67             }
  68             return true;
  69         }
  70         return false;
  71     }
  72 
  73     Annotation[][] parseParameterAnnotations(byte[] parameterAnnotations) {
  74         return AnnotationParser.parseParameterAnnotations(
  75                parameterAnnotations,
  76                sun.misc.SharedSecrets.getJavaLangAccess().
  77                getConstantPool(getDeclaringClass()),
  78                getDeclaringClass());
  79     }
  80 


 209      *     the type variables declared by this generic declaration
 210      * @throws GenericSignatureFormatError if the generic
 211      *     signature of this generic declaration does not conform to
 212      *     the format specified in
 213      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 214      */
 215     public abstract TypeVariable<?>[] getTypeParameters();
 216 
 217     /**
 218      * Returns an array of {@code Class} objects that represent the formal
 219      * parameter types, in declaration order, of the executable
 220      * represented by this object.  Returns an array of length
 221      * 0 if the underlying executable takes no parameters.
 222      *
 223      * @return the parameter types for the executable this object
 224      * represents
 225      */
 226     public abstract Class<?>[] getParameterTypes();
 227 
 228     /**
 229      * Returns the number of formal parameters for the executable
 230      * represented by this object.
 231      *
 232      * @return The number of formal parameters for the executable this
 233      * object represents
 234      */
 235     public abstract int getNumParameters();
 236 
 237     /**
 238      * Returns an array of {@code Type} objects that represent the formal
 239      * parameter types, in declaration order, of the executable represented by
 240      * this object. Returns an array of length 0 if the
 241      * underlying executable takes no parameters.
 242      *
 243      * <p>If a formal parameter type is a parameterized type,
 244      * the {@code Type} object returned for it must accurately reflect
 245      * the actual type parameters used in the source code.
 246      *
 247      * <p>If a formal parameter type is a type variable or a parameterized
 248      * type, it is created. Otherwise, it is resolved.
 249      *
 250      * @return an array of {@code Type}s that represent the formal
 251      *     parameter types of the underlying executable, in declaration order
 252      * @throws GenericSignatureFormatError
 253      *     if the generic method signature does not conform to the format
 254      *     specified in
 255      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 256      * @throws TypeNotPresentException if any of the parameter
 257      *     types of the underlying executable refers to a non-existent type
 258      *     declaration
 259      * @throws MalformedParameterizedTypeException if any of
 260      *     the underlying executable's parameter types refer to a parameterized
 261      *     type that cannot be instantiated for any reason
 262      */
 263     public Type[] getGenericParameterTypes() {
 264         if (hasGenericInformation())
 265             return getGenericInfo().getParameterTypes();
 266         else
 267             return getParameterTypes();
 268     }
 269 
 270     /**
 271      * Returns an array of {@code Parameter} objects that represent
 272      * all the parameters to the underlying executable represented by
 273      * this object.  Returns an array of length 0 if the executable
 274      * has no parameters.
 275      *
 276      * @return an array of {@code Parameter} objects representing all
 277      * the parameters to the executable this object represents
 278      */
 279     public Parameter[] getParameters() {
 280         // TODO: This may eventually need to be guarded by security
 281         // mechanisms similar to those in Field, Method, etc.
 282         Parameter[] raw = privateGetParameters();
 283         Parameter[] out = new Parameter[raw.length];
 284         // Need to copy the cached array to prevent users from messing
 285         // with it
 286         for (int i = 0; i < raw.length; i++) {
 287             out[i] = new Parameter(raw[i]);
 288         }
 289         return out;
 290     }
 291 
 292     private Parameter[] privateGetParameters() {
 293         if (null != parameters)
 294             return parameters.get();
 295         Parameter[] res = getParameters0();
 296 
 297         if (null == res) {
 298             final int num = getNumParameters();
 299             res = new Parameter[num];
 300             for (int i = 0; i < num; i++)
 301                 // TODO: is there a way to synthetically derive the modifiers?
 302                 res[i] = new Parameter("arg" + i, 0, this, i);
 303         }
 304 
 305         parameters = new SoftReference<Parameter[]>(res);
 306         return res;
 307     }
 308 
 309     private native Parameter[] getParameters0();
 310 
 311     /**
 312      * Returns an array of {@code Class} objects that represent the
 313      * types of exceptions declared to be thrown by the underlying
 314      * executable represented by this object.  Returns an array of
 315      * length 0 if the executable declares no exceptions in its {@code
 316      * throws} clause.
 317      *
 318      * @return the exception types declared as being thrown by the
 319      * executable this object represents
 320      */
 321     public abstract Class<?>[] getExceptionTypes();
 322 
 323     /**
 324      * Returns an array of {@code Type} objects that represent the
 325      * exceptions declared to be thrown by this executable object.
 326      * Returns an array of length 0 if the underlying executable declares
 327      * no exceptions in its {@code throws} clause.
 328      *
 329      * <p>If an exception type is a type variable or a parameterized
 330      * type, it is created. Otherwise, it is resolved.
 331      *


 426 
 427     /**
 428      * {@inheritDoc}
 429      */
 430     public Annotation[] getDeclaredAnnotations()  {
 431         return AnnotationParser.toArray(declaredAnnotations());
 432     }
 433 
 434     private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
 435 
 436     private synchronized  Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
 437         if (declaredAnnotations == null) {
 438             declaredAnnotations = AnnotationParser.parseAnnotations(
 439                 getAnnotationBytes(),
 440                 sun.misc.SharedSecrets.getJavaLangAccess().
 441                 getConstantPool(getDeclaringClass()),
 442                 getDeclaringClass());
 443         }
 444         return declaredAnnotations;
 445     }
 446 
 447 }