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.
  57   Array<AnnotationArray*>*     _methods_parameter_annotations;
  58   // Annotation objects (byte arrays) for methods' default values, or null if no
  59   // such annotations.
  60   // Index is the idnum, which is initially the same as the methods array index.
  61   Array<AnnotationArray*>*     _methods_default_annotations;
  62   // Type annotations for this class, or null if none.
  63   Annotations*                 _type_annotations;
  64 
  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; }
 106   void set_methods_default_annotations(Array<AnnotationArray*>* md)   { _methods_default_annotations = md; }
 107   void set_type_annotations(Annotations* annos)                       { _type_annotations = annos; }
 108 
 109   // Redefine classes support
 110   AnnotationArray* get_method_annotations_of(int idnum)
 111                                                 { return get_method_annotations_from(idnum, _methods_annotations); }
 112 
 113   AnnotationArray* get_method_parameter_annotations_of(int idnum)
 114                                                 { return get_method_annotations_from(idnum, _methods_parameter_annotations); }
 115   AnnotationArray* get_method_default_annotations_of(int idnum)
 116                                                 { return get_method_annotations_from(idnum, _methods_default_annotations); }
 117 
 118 
 119   void set_method_annotations_of(instanceKlassHandle ik,
 120                                  int idnum, AnnotationArray* anno, TRAPS) {
 121     set_methods_annotations_of(ik, idnum, anno, &_methods_annotations, THREAD);
 122   }
 123 
 124   void set_method_parameter_annotations_of(instanceKlassHandle ik,
 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