src/share/vm/oops/annotations.hpp

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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_OOPS_ANNOTATIONS_HPP
  26 #define SHARE_VM_OOPS_ANNOTATIONS_HPP
  27 
  28 #include "oops/metadata.hpp"
  29 #include "runtime/handles.hpp"
  30 #include "utilities/array.hpp"
  31 #include "utilities/exceptions.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 
  35 class ClassLoaderData;
  36 class outputStream;

  37 
  38 typedef Array<u1> AnnotationArray;
  39 
  40 // Class to hold the various types of annotations. The only metadata that points
  41 // to this is InstanceKlass, or another Annotations instance if this is a
  42 // a type_annotation instance.
  43 
  44 class Annotations: public MetaspaceObj {
  45 
  46   // Annotations for this class, or null if none.
  47   AnnotationArray*             _class_annotations;
  48   // Annotation objects (byte arrays) for fields, or null if no annotations.
  49   // Indices correspond to entries (not indices) in fields array.
  50   Array<AnnotationArray*>*     _fields_annotations;
  51   // Annotation objects (byte arrays) for methods, or null if no annotations.
  52   // Index is the idnum, which is initially the same as the methods array index.
  53   Array<AnnotationArray*>*     _methods_annotations;
  54   // Annotation objects (byte arrays) for methods' parameters, or null if no
  55   // such annotations.
  56   // Index is the idnum, which is initially the same as the methods array index.


  65   // Constructor where some some values are known to not be null
  66   Annotations(Array<AnnotationArray*>* fa, Array<AnnotationArray*>* ma,
  67               Array<AnnotationArray*>* mpa, Array<AnnotationArray*>* mda) :
  68                  _class_annotations(NULL),
  69                  _fields_annotations(fa),
  70                  _methods_annotations(ma),
  71                  _methods_parameter_annotations(mpa),
  72                  _methods_default_annotations(mda),
  73                  _type_annotations(NULL) {}
  74 
  75  public:
  76   // Allocate instance of this class
  77   static Annotations* allocate(ClassLoaderData* loader_data, TRAPS);
  78   static Annotations* allocate(ClassLoaderData* loader_data,
  79                                Array<AnnotationArray*>* fa,
  80                                Array<AnnotationArray*>* ma,
  81                                Array<AnnotationArray*>* mpa,
  82                                Array<AnnotationArray*>* mda, TRAPS);
  83   void deallocate_contents(ClassLoaderData* loader_data);
  84   DEBUG_ONLY(bool on_stack() { return false; })  // for template


  85   static int size()    { return sizeof(Annotations) / wordSize; }



  86 
  87   // Constructor to initialize to null
  88   Annotations() : _class_annotations(NULL),
  89                   _fields_annotations(NULL),
  90                   _methods_annotations(NULL),
  91                   _methods_parameter_annotations(NULL),
  92                   _methods_default_annotations(NULL),
  93                   _type_annotations(NULL) {}
  94 
  95   AnnotationArray* class_annotations() const                       { return _class_annotations; }
  96   Array<AnnotationArray*>* fields_annotations() const              { return _fields_annotations; }
  97   Array<AnnotationArray*>* methods_annotations() const             { return _methods_annotations; }
  98   Array<AnnotationArray*>* methods_parameter_annotations() const   { return _methods_parameter_annotations; }
  99   Array<AnnotationArray*>* methods_default_annotations() const     { return _methods_default_annotations; }
 100   Annotations* type_annotations() const                            { return _type_annotations; }
 101 
 102   void set_class_annotations(AnnotationArray* md)                     { _class_annotations = md; }
 103   void set_fields_annotations(Array<AnnotationArray*>* md)            { _fields_annotations = md; }
 104   void set_methods_annotations(Array<AnnotationArray*>* md)           { _methods_annotations = md; }
 105   void set_methods_parameter_annotations(Array<AnnotationArray*>* md) { _methods_parameter_annotations = md; }


 125                                  int idnum, AnnotationArray* anno, TRAPS) {
 126     set_methods_annotations_of(ik, idnum, anno, &_methods_parameter_annotations, THREAD);
 127   }
 128 
 129   void set_method_default_annotations_of(instanceKlassHandle ik,
 130                                  int idnum, AnnotationArray* anno, TRAPS) {
 131     set_methods_annotations_of(ik, idnum, anno, &_methods_default_annotations, THREAD);
 132   }
 133 
 134   // Turn metadata annotations into a Java heap object (oop)
 135   static typeArrayOop make_java_array(AnnotationArray* annotations, TRAPS);
 136 
 137   inline AnnotationArray* get_method_annotations_from(int idnum, Array<AnnotationArray*>* annos);
 138   void set_annotations(Array<AnnotationArray*>* md, Array<AnnotationArray*>** md_p)  { *md_p = md; }
 139 
 140   bool is_klass() const { return false; }
 141  private:
 142   void set_methods_annotations_of(instanceKlassHandle ik,
 143                                   int idnum, AnnotationArray* anno,
 144                                   Array<AnnotationArray*>** md_p, TRAPS);
 145 
 146  public:
 147   const char* internal_name() const { return "{constant pool}"; }
 148 #ifndef PRODUCT
 149   void print_on(outputStream* st) const;
 150 #endif
 151   void print_value_on(outputStream* st) const;
 152 };
 153 
 154 
 155 // For method with idnum get the method's Annotations
 156 inline AnnotationArray* Annotations::get_method_annotations_from(int idnum, Array<AnnotationArray*>* annos) {
 157   if (annos == NULL || annos->length() <= idnum) {
 158     return NULL;
 159   }
 160   return annos->at(idnum);
 161 }
 162 #endif // SHARE_VM_OOPS_ANNOTATIONS_HPP
   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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_OOPS_ANNOTATIONS_HPP
  26 #define SHARE_VM_OOPS_ANNOTATIONS_HPP
  27 
  28 #include "oops/metadata.hpp"
  29 #include "runtime/handles.hpp"
  30 #include "utilities/array.hpp"
  31 #include "utilities/exceptions.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 
  35 class ClassLoaderData;
  36 class outputStream;
  37 class KlassSizeStats;
  38 
  39 typedef Array<u1> AnnotationArray;
  40 
  41 // Class to hold the various types of annotations. The only metadata that points
  42 // to this is InstanceKlass, or another Annotations instance if this is a
  43 // a type_annotation instance.
  44 
  45 class Annotations: public MetaspaceObj {
  46 
  47   // Annotations for this class, or null if none.
  48   AnnotationArray*             _class_annotations;
  49   // Annotation objects (byte arrays) for fields, or null if no annotations.
  50   // Indices correspond to entries (not indices) in fields array.
  51   Array<AnnotationArray*>*     _fields_annotations;
  52   // Annotation objects (byte arrays) for methods, or null if no annotations.
  53   // Index is the idnum, which is initially the same as the methods array index.
  54   Array<AnnotationArray*>*     _methods_annotations;
  55   // Annotation objects (byte arrays) for methods' parameters, or null if no
  56   // such annotations.
  57   // Index is the idnum, which is initially the same as the methods array index.


  66   // Constructor where some some values are known to not be null
  67   Annotations(Array<AnnotationArray*>* fa, Array<AnnotationArray*>* ma,
  68               Array<AnnotationArray*>* mpa, Array<AnnotationArray*>* mda) :
  69                  _class_annotations(NULL),
  70                  _fields_annotations(fa),
  71                  _methods_annotations(ma),
  72                  _methods_parameter_annotations(mpa),
  73                  _methods_default_annotations(mda),
  74                  _type_annotations(NULL) {}
  75 
  76  public:
  77   // Allocate instance of this class
  78   static Annotations* allocate(ClassLoaderData* loader_data, TRAPS);
  79   static Annotations* allocate(ClassLoaderData* loader_data,
  80                                Array<AnnotationArray*>* fa,
  81                                Array<AnnotationArray*>* ma,
  82                                Array<AnnotationArray*>* mpa,
  83                                Array<AnnotationArray*>* mda, TRAPS);
  84   void deallocate_contents(ClassLoaderData* loader_data);
  85   DEBUG_ONLY(bool on_stack() { return false; })  // for template
  86 
  87   // Sizing (in words)
  88   static int size()    { return sizeof(Annotations) / wordSize; }
  89 #if INCLUDE_SERVICES
  90   void collect_statistics(KlassSizeStats *sz) const;
  91 #endif
  92 
  93   // Constructor to initialize to null
  94   Annotations() : _class_annotations(NULL),
  95                   _fields_annotations(NULL),
  96                   _methods_annotations(NULL),
  97                   _methods_parameter_annotations(NULL),
  98                   _methods_default_annotations(NULL),
  99                   _type_annotations(NULL) {}
 100 
 101   AnnotationArray* class_annotations() const                       { return _class_annotations; }
 102   Array<AnnotationArray*>* fields_annotations() const              { return _fields_annotations; }
 103   Array<AnnotationArray*>* methods_annotations() const             { return _methods_annotations; }
 104   Array<AnnotationArray*>* methods_parameter_annotations() const   { return _methods_parameter_annotations; }
 105   Array<AnnotationArray*>* methods_default_annotations() const     { return _methods_default_annotations; }
 106   Annotations* type_annotations() const                            { return _type_annotations; }
 107 
 108   void set_class_annotations(AnnotationArray* md)                     { _class_annotations = md; }
 109   void set_fields_annotations(Array<AnnotationArray*>* md)            { _fields_annotations = md; }
 110   void set_methods_annotations(Array<AnnotationArray*>* md)           { _methods_annotations = md; }
 111   void set_methods_parameter_annotations(Array<AnnotationArray*>* md) { _methods_parameter_annotations = md; }


 131                                  int idnum, AnnotationArray* anno, TRAPS) {
 132     set_methods_annotations_of(ik, idnum, anno, &_methods_parameter_annotations, THREAD);
 133   }
 134 
 135   void set_method_default_annotations_of(instanceKlassHandle ik,
 136                                  int idnum, AnnotationArray* anno, TRAPS) {
 137     set_methods_annotations_of(ik, idnum, anno, &_methods_default_annotations, THREAD);
 138   }
 139 
 140   // Turn metadata annotations into a Java heap object (oop)
 141   static typeArrayOop make_java_array(AnnotationArray* annotations, TRAPS);
 142 
 143   inline AnnotationArray* get_method_annotations_from(int idnum, Array<AnnotationArray*>* annos);
 144   void set_annotations(Array<AnnotationArray*>* md, Array<AnnotationArray*>** md_p)  { *md_p = md; }
 145 
 146   bool is_klass() const { return false; }
 147  private:
 148   void set_methods_annotations_of(instanceKlassHandle ik,
 149                                   int idnum, AnnotationArray* anno,
 150                                   Array<AnnotationArray*>** md_p, TRAPS);
 151   static julong count_bytes(Array<AnnotationArray*>* p);
 152  public:
 153   const char* internal_name() const { return "{constant pool}"; }
 154 #ifndef PRODUCT
 155   void print_on(outputStream* st) const;
 156 #endif
 157   void print_value_on(outputStream* st) const;
 158 };
 159 
 160 
 161 // For method with idnum get the method's Annotations
 162 inline AnnotationArray* Annotations::get_method_annotations_from(int idnum, Array<AnnotationArray*>* annos) {
 163   if (annos == NULL || annos->length() <= idnum) {
 164     return NULL;
 165   }
 166   return annos->at(idnum);
 167 }
 168 #endif // SHARE_VM_OOPS_ANNOTATIONS_HPP