src/share/vm/oops/annotations.cpp

Thu, 07 Feb 2013 16:05:48 -0500

author
bpittore
date
Thu, 07 Feb 2013 16:05:48 -0500
changeset 4544
3c9bc17b9403
parent 4497
16fb9f942703
child 4572
927a311d00f9
permissions
-rw-r--r--

Merge

     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  */
    25 #include "precompiled.hpp"
    26 #include "classfile/classLoaderData.hpp"
    27 #include "memory/heapInspection.hpp"
    28 #include "memory/metadataFactory.hpp"
    29 #include "memory/oopFactory.hpp"
    30 #include "oops/annotations.hpp"
    31 #include "oops/instanceKlass.hpp"
    32 #include "utilities/ostream.hpp"
    34 // Allocate annotations in metadata area
    35 Annotations* Annotations::allocate(ClassLoaderData* loader_data, TRAPS) {
    36   return new (loader_data, size(), true, THREAD) Annotations();
    37 }
    39 Annotations* Annotations::allocate(ClassLoaderData* loader_data,
    40                                    Array<AnnotationArray*>* fa,
    41                                    Array<AnnotationArray*>* ma,
    42                                    Array<AnnotationArray*>* mpa,
    43                                    Array<AnnotationArray*>* mda, TRAPS) {
    44   return new (loader_data, size(), true, THREAD) Annotations(fa, ma, mpa, mda);
    45 }
    47 // helper
    48 static void free_contents(ClassLoaderData* loader_data, Array<AnnotationArray*>* p) {
    49   if (p != NULL) {
    50     for (int i = 0; i < p->length(); i++) {
    51       MetadataFactory::free_array<u1>(loader_data, p->at(i));
    52     }
    53     MetadataFactory::free_array<AnnotationArray*>(loader_data, p);
    54   }
    55 }
    57 void Annotations::deallocate_contents(ClassLoaderData* loader_data) {
    58   if (class_annotations() != NULL) {
    59     MetadataFactory::free_array<u1>(loader_data, class_annotations());
    60   }
    61   free_contents(loader_data, fields_annotations());
    62   free_contents(loader_data, methods_annotations());
    63   free_contents(loader_data, methods_parameter_annotations());
    64   free_contents(loader_data, methods_default_annotations());
    66   // Recursively deallocate optional Annotations linked through this one
    67   MetadataFactory::free_metadata(loader_data, type_annotations());
    68 }
    70 // Set the annotation at 'idnum' to 'anno'.
    71 // We don't want to create or extend the array if 'anno' is NULL, since that is the
    72 // default value.  However, if the array exists and is long enough, we must set NULL values.
    73 void Annotations::set_methods_annotations_of(instanceKlassHandle ik,
    74                                              int idnum, AnnotationArray* anno,
    75                                              Array<AnnotationArray*>** md_p,
    76                                              TRAPS) {
    77   Array<AnnotationArray*>* md = *md_p;
    78   if (md != NULL && md->length() > idnum) {
    79     md->at_put(idnum, anno);
    80   } else if (anno != NULL) {
    81     // create the array
    82     int length = MAX2(idnum+1, (int)ik->idnum_allocated_count());
    83     md = MetadataFactory::new_array<AnnotationArray*>(ik->class_loader_data(), length, CHECK);
    84     if (*md_p != NULL) {
    85       // copy the existing entries
    86       for (int index = 0; index < (*md_p)->length(); index++) {
    87         md->at_put(index, (*md_p)->at(index));
    88       }
    89     }
    90     set_annotations(md, md_p);
    91     md->at_put(idnum, anno);
    92   } // if no array and idnum isn't included there is nothing to do
    93 }
    95 // Keep created annotations in a global growable array (should be hashtable)
    96 // need to add, search, delete when class is unloaded.
    97 // Does it need a lock?  yes.  This sucks.
    99 // Copy annotations to JVM call or reflection to the java heap.
   100 typeArrayOop Annotations::make_java_array(AnnotationArray* annotations, TRAPS) {
   101   if (annotations != NULL) {
   102     int length = annotations->length();
   103     typeArrayOop copy = oopFactory::new_byteArray(length, CHECK_NULL);
   104     for (int i = 0; i< length; i++) {
   105       copy->byte_at_put(i, annotations->at(i));
   106     }
   107     return copy;
   108   } else {
   109     return NULL;
   110   }
   111 }
   114 void Annotations::print_value_on(outputStream* st) const {
   115   st->print("Anotations(" INTPTR_FORMAT ")", this);
   116 }
   118 #if INCLUDE_SERVICES
   119 // Size Statistics
   121 julong Annotations::count_bytes(Array<AnnotationArray*>* p) {
   122   julong bytes = 0;
   123   if (p != NULL) {
   124     for (int i = 0; i < p->length(); i++) {
   125       bytes += KlassSizeStats::count_array(p->at(i));
   126     }
   127     bytes += KlassSizeStats::count_array(p);
   128   }
   129   return bytes;
   130 }
   132 void Annotations::collect_statistics(KlassSizeStats *sz) const {
   133   sz->_annotations_bytes = sz->count(this);
   134   sz->_class_annotations_bytes = sz->count(class_annotations());
   135   sz->_fields_annotations_bytes = count_bytes(fields_annotations());
   136   sz->_methods_annotations_bytes = count_bytes(methods_annotations());
   137   sz->_methods_parameter_annotations_bytes =
   138                           count_bytes(methods_parameter_annotations());
   139   sz->_methods_default_annotations_bytes =
   140                           count_bytes(methods_default_annotations());
   142   const Annotations* type_anno = type_annotations();
   143   if (type_anno != NULL) {
   144     sz->_type_annotations_bytes = sz->count(type_anno);
   145     sz->_type_annotations_bytes += sz->count(type_anno->class_annotations());
   146     sz->_type_annotations_bytes += count_bytes(type_anno->fields_annotations());
   147     sz->_type_annotations_bytes += count_bytes(type_anno->methods_annotations());
   148   }
   150   sz->_annotations_bytes +=
   151       sz->_class_annotations_bytes +
   152       sz->_fields_annotations_bytes +
   153       sz->_methods_annotations_bytes +
   154       sz->_methods_parameter_annotations_bytes +
   155       sz->_methods_default_annotations_bytes +
   156       sz->_type_annotations_bytes;
   158   sz->_ro_bytes += sz->_annotations_bytes;
   159 }
   160 #endif // INCLUDE_SERVICES
   162 #define BULLET  " - "
   164 #ifndef PRODUCT
   165 void Annotations::print_on(outputStream* st) const {
   166   st->print(BULLET"class_annotations            "); class_annotations()->print_value_on(st);
   167   st->print(BULLET"fields_annotations           "); fields_annotations()->print_value_on(st);
   168   st->print(BULLET"methods_annotations          "); methods_annotations()->print_value_on(st);
   169   st->print(BULLET"methods_parameter_annotations"); methods_parameter_annotations()->print_value_on(st);
   170   st->print(BULLET"methods_default_annotations  "); methods_default_annotations()->print_value_on(st);
   171 }
   172 #endif // PRODUCT

mercurial