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

Print this page


   1 /*
   2  * Copyright (c) 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 java.lang.annotation.*;
  29 import java.util.Collections;
  30 import java.util.Map;
  31 import java.util.Objects;
  32 import sun.reflect.annotation.AnnotationParser;
  33 import sun.reflect.annotation.AnnotationSupport;


  34 import sun.reflect.generics.repository.ConstructorRepository;
  35 
  36 /**
  37  * A shared superclass for the common functionality of {@link Method}
  38  * and {@link Constructor}.
  39  *
  40  * @since 1.8
  41  */
  42 public abstract class Executable extends AccessibleObject
  43     implements Member, GenericDeclaration {
  44     /*
  45      * Only grant package-visibility to the constructor.
  46      */
  47     Executable() {}
  48 
  49     /**
  50      * Accessor method to allow code sharing
  51      */
  52     abstract byte[] getAnnotationBytes();

  53 
  54     /**
  55      * Does the Executable have generic information.
  56      */
  57     abstract boolean hasGenericInformation();
  58 
  59     abstract ConstructorRepository getGenericInfo();
  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 


 385     }
 386 
 387     /**
 388      * {@inheritDoc}
 389      */
 390     public Annotation[] getDeclaredAnnotations()  {
 391         return AnnotationSupport.unpackToArray(declaredAnnotations());
 392     }
 393 
 394     private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
 395 
 396     private synchronized  Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
 397         if (declaredAnnotations == null) {
 398             declaredAnnotations = AnnotationParser.parseAnnotations(
 399                 getAnnotationBytes(),
 400                 sun.misc.SharedSecrets.getJavaLangAccess().
 401                 getConstantPool(getDeclaringClass()),
 402                 getDeclaringClass());
 403         }
 404         return declaredAnnotations;








































































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


 388     }
 389 
 390     /**
 391      * {@inheritDoc}
 392      */
 393     public Annotation[] getDeclaredAnnotations()  {
 394         return AnnotationSupport.unpackToArray(declaredAnnotations());
 395     }
 396 
 397     private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
 398 
 399     private synchronized  Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
 400         if (declaredAnnotations == null) {
 401             declaredAnnotations = AnnotationParser.parseAnnotations(
 402                 getAnnotationBytes(),
 403                 sun.misc.SharedSecrets.getJavaLangAccess().
 404                 getConstantPool(getDeclaringClass()),
 405                 getDeclaringClass());
 406         }
 407         return declaredAnnotations;
 408     }
 409 
 410     // AnnotatedType since 1.8
 411     /* Helper for subclasses of Executable.
 412      *
 413      * Returns an AnnotatedType object that represents the use of a type to denote
 414      * the return type of the method/constructor represented by this
 415      * Executable.
 416      */
 417     AnnotatedType getAnnotatedReturnType0(Type returnType) {
 418         return TypeAnnotationParser.buildAnnotatedType0(getTypeAnnotationBytes(),
 419                                                         sun.misc.SharedSecrets.getJavaLangAccess().
 420                                                             getConstantPool(getDeclaringClass()),
 421                                                         this,
 422                                                         getDeclaringClass(),
 423                                                         returnType,
 424                                                         TypeAnnotation.TypeAnnotationTarget.METHOD_RETURN_TYPE);
 425 }
 426 
 427     /**
 428      * Returns an AnnotatedType object that represents the use of a type to denote
 429      * (via a 'this' parameter) the implicit receiver type of the
 430      * method/constructor represented by this Executable.
 431      *
 432      * @since 1.8
 433      */
 434     public AnnotatedType getAnnotatedReceiverType() {
 435         return TypeAnnotationParser.buildAnnotatedType0(getTypeAnnotationBytes(),
 436                                                         sun.misc.SharedSecrets.getJavaLangAccess().
 437                                                             getConstantPool(getDeclaringClass()),
 438                                                         this,
 439                                                         getDeclaringClass(),
 440                                                         getDeclaringClass(),
 441                                                         TypeAnnotation.TypeAnnotationTarget.METHOD_RECEIVER_TYPE);
 442     }
 443 
 444     /**
 445      * Returns an array of AnnotatedType objects that represent the use of
 446      * types to denote formal parameter types of the method/constructor
 447      * represented by this Executable. The order of the objects in the array
 448      * corresponds to the order of the formal parameter types in the
 449      * declaration of the method/constructor.
 450      *
 451      * Returns an array of length 0 if the method/constructor declares no
 452      * parameters.
 453      *
 454      * @since 1.8
 455      */
 456     public AnnotatedType[] getAnnotatedParameterTypes() {
 457         return null;
 458     }
 459 
 460     /**
 461      * Returns an array of AnnotatedType objects that represent the use of
 462      * types to denote the declared exceptions of the method/constructor
 463      * represented by this Executable. The order of the objects in the array
 464      * corresponds to the order of the exception types in the declaration of
 465      * the method/constructor.
 466      *
 467      * Returns an array of length 0 if the method/constructor declares no
 468      * exceptions.
 469      *
 470      * @since 1.8
 471      */
 472     public AnnotatedType[] getAnnotatedExceptionTypes() {
 473         return TypeAnnotationParser.buildAnnotatedTypes0(getTypeAnnotationBytes(),
 474                                                          sun.misc.SharedSecrets.getJavaLangAccess().
 475                                                              getConstantPool(getDeclaringClass()),
 476                                                          this,
 477                                                          getDeclaringClass(),
 478                                                          getGenericExceptionTypes(),
 479                                                          TypeAnnotation.TypeAnnotationTarget.THROWS);
 480     }
 481 }