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

Print this page
rev 10466 : 8054987: (reflect) Add sharing of annotations between instances of Executable
Reviewed-by: duke


  62     private Class<?>            clazz;
  63     private int                 slot;
  64     // This is guaranteed to be interned by the VM in the 1.4
  65     // reflection implementation
  66     private String              name;
  67     private Class<?>            returnType;
  68     private Class<?>[]          parameterTypes;
  69     private Class<?>[]          exceptionTypes;
  70     private int                 modifiers;
  71     // Generics and annotations support
  72     private transient String              signature;
  73     // generic info repository; lazily initialized
  74     private transient MethodRepository genericInfo;
  75     private byte[]              annotations;
  76     private byte[]              parameterAnnotations;
  77     private byte[]              annotationDefault;
  78     private volatile MethodAccessor methodAccessor;
  79     // For sharing of MethodAccessors. This branching structure is
  80     // currently only two levels deep (i.e., one root Method and
  81     // potentially many Method objects pointing to it.)



  82     private Method              root;
  83 
  84     // Generics infrastructure
  85     private String getGenericSignature() {return signature;}
  86 
  87     // Accessor for factory
  88     private GenericsFactory getFactory() {
  89         // create scope and factory
  90         return CoreReflectionFactory.make(this, MethodScope.make(this));
  91     }
  92 
  93     // Accessor for generic info repository
  94     @Override
  95     MethodRepository getGenericInfo() {
  96         // lazily initialize repository if necessary
  97         if (genericInfo == null) {
  98             // create and cache generic info repository
  99             genericInfo = MethodRepository.make(getGenericSignature(),
 100                                                 getFactory());
 101         }


 127         this.slot = slot;
 128         this.signature = signature;
 129         this.annotations = annotations;
 130         this.parameterAnnotations = parameterAnnotations;
 131         this.annotationDefault = annotationDefault;
 132     }
 133 
 134     /**
 135      * Package-private routine (exposed to java.lang.Class via
 136      * ReflectAccess) which returns a copy of this Method. The copy's
 137      * "root" field points to this Method.
 138      */
 139     Method copy() {
 140         // This routine enables sharing of MethodAccessor objects
 141         // among Method objects which refer to the same underlying
 142         // method in the VM. (All of this contortion is only necessary
 143         // because of the "accessibility" bit in AccessibleObject,
 144         // which implicitly requires that new java.lang.reflect
 145         // objects be fabricated for each reflective call on Class
 146         // objects.)



 147         Method res = new Method(clazz, name, parameterTypes, returnType,
 148                                 exceptionTypes, modifiers, slot, signature,
 149                                 annotations, parameterAnnotations, annotationDefault);
 150         res.root = this;
 151         // Might as well eagerly propagate this if already present
 152         res.methodAccessor = methodAccessor;
 153         return res;








 154     }
 155 
 156     @Override
 157     boolean hasGenericInformation() {
 158         return (getGenericSignature() != null);
 159     }
 160 
 161     @Override
 162     byte[] getAnnotationBytes() {
 163         return annotations;
 164     }
 165 
 166     /**
 167      * {@inheritDoc}
 168      */
 169     @Override
 170     public Class<?> getDeclaringClass() {
 171         return clazz;
 172     }
 173 




  62     private Class<?>            clazz;
  63     private int                 slot;
  64     // This is guaranteed to be interned by the VM in the 1.4
  65     // reflection implementation
  66     private String              name;
  67     private Class<?>            returnType;
  68     private Class<?>[]          parameterTypes;
  69     private Class<?>[]          exceptionTypes;
  70     private int                 modifiers;
  71     // Generics and annotations support
  72     private transient String              signature;
  73     // generic info repository; lazily initialized
  74     private transient MethodRepository genericInfo;
  75     private byte[]              annotations;
  76     private byte[]              parameterAnnotations;
  77     private byte[]              annotationDefault;
  78     private volatile MethodAccessor methodAccessor;
  79     // For sharing of MethodAccessors. This branching structure is
  80     // currently only two levels deep (i.e., one root Method and
  81     // potentially many Method objects pointing to it.)
  82     //
  83     // If this branching structure would ever contain cycles, deadlocks can
  84     // occur in annotation code.
  85     private Method              root;
  86 
  87     // Generics infrastructure
  88     private String getGenericSignature() {return signature;}
  89 
  90     // Accessor for factory
  91     private GenericsFactory getFactory() {
  92         // create scope and factory
  93         return CoreReflectionFactory.make(this, MethodScope.make(this));
  94     }
  95 
  96     // Accessor for generic info repository
  97     @Override
  98     MethodRepository getGenericInfo() {
  99         // lazily initialize repository if necessary
 100         if (genericInfo == null) {
 101             // create and cache generic info repository
 102             genericInfo = MethodRepository.make(getGenericSignature(),
 103                                                 getFactory());
 104         }


 130         this.slot = slot;
 131         this.signature = signature;
 132         this.annotations = annotations;
 133         this.parameterAnnotations = parameterAnnotations;
 134         this.annotationDefault = annotationDefault;
 135     }
 136 
 137     /**
 138      * Package-private routine (exposed to java.lang.Class via
 139      * ReflectAccess) which returns a copy of this Method. The copy's
 140      * "root" field points to this Method.
 141      */
 142     Method copy() {
 143         // This routine enables sharing of MethodAccessor objects
 144         // among Method objects which refer to the same underlying
 145         // method in the VM. (All of this contortion is only necessary
 146         // because of the "accessibility" bit in AccessibleObject,
 147         // which implicitly requires that new java.lang.reflect
 148         // objects be fabricated for each reflective call on Class
 149         // objects.)
 150         if (this.root != null)
 151             throw new IllegalArgumentException("Can not copy a non-root Method");
 152 
 153         Method res = new Method(clazz, name, parameterTypes, returnType,
 154                                 exceptionTypes, modifiers, slot, signature,
 155                                 annotations, parameterAnnotations, annotationDefault);
 156         res.root = this;
 157         // Might as well eagerly propagate this if already present
 158         res.methodAccessor = methodAccessor;
 159         return res;
 160     }
 161 
 162     /**
 163      * Used by Excecutable for annotation sharing.
 164      */
 165     @Override
 166     Executable getRoot() {
 167         return root;
 168     }
 169 
 170     @Override
 171     boolean hasGenericInformation() {
 172         return (getGenericSignature() != null);
 173     }
 174 
 175     @Override
 176     byte[] getAnnotationBytes() {
 177         return annotations;
 178     }
 179 
 180     /**
 181      * {@inheritDoc}
 182      */
 183     @Override
 184     public Class<?> getDeclaringClass() {
 185         return clazz;
 186     }
 187