src/share/vm/oops/annotations.cpp

changeset 4037
da91efe96a93
child 4393
35431a769282
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/oops/annotations.cpp	Sat Sep 01 13:25:18 2012 -0400
     1.3 @@ -0,0 +1,124 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/classLoaderData.hpp"
    1.30 +#include "memory/metadataFactory.hpp"
    1.31 +#include "memory/oopFactory.hpp"
    1.32 +#include "oops/annotations.hpp"
    1.33 +#include "oops/instanceKlass.hpp"
    1.34 +#include "utilities/ostream.hpp"
    1.35 +
    1.36 +// Allocate annotations in metadata area
    1.37 +Annotations* Annotations::allocate(ClassLoaderData* loader_data, TRAPS) {
    1.38 +  return new (loader_data, size(), true, THREAD) Annotations();
    1.39 +}
    1.40 +
    1.41 +Annotations* Annotations::allocate(ClassLoaderData* loader_data,
    1.42 +                                   Array<AnnotationArray*>* fa,
    1.43 +                                   Array<AnnotationArray*>* ma,
    1.44 +                                   Array<AnnotationArray*>* mpa,
    1.45 +                                   Array<AnnotationArray*>* mda, TRAPS) {
    1.46 +  return new (loader_data, size(), true, THREAD) Annotations(fa, ma, mpa, mda);
    1.47 +}
    1.48 +
    1.49 +// helper
    1.50 +static void free_contents(ClassLoaderData* loader_data, Array<AnnotationArray*>* p) {
    1.51 +  if (p != NULL) {
    1.52 +    for (int i = 0; i < p->length(); i++) {
    1.53 +      MetadataFactory::free_array<u1>(loader_data, p->at(i));
    1.54 +    }
    1.55 +    MetadataFactory::free_array<AnnotationArray*>(loader_data, p);
    1.56 +  }
    1.57 +}
    1.58 +
    1.59 +void Annotations::deallocate_contents(ClassLoaderData* loader_data) {
    1.60 +  if (class_annotations() != NULL) {
    1.61 +    MetadataFactory::free_array<u1>(loader_data, class_annotations());
    1.62 +  }
    1.63 +  free_contents(loader_data, fields_annotations());
    1.64 +  free_contents(loader_data, methods_annotations());
    1.65 +  free_contents(loader_data, methods_parameter_annotations());
    1.66 +  free_contents(loader_data, methods_default_annotations());
    1.67 +}
    1.68 +
    1.69 +// Set the annotation at 'idnum' to 'anno'.
    1.70 +// We don't want to create or extend the array if 'anno' is NULL, since that is the
    1.71 +// default value.  However, if the array exists and is long enough, we must set NULL values.
    1.72 +void Annotations::set_methods_annotations_of(instanceKlassHandle ik,
    1.73 +                                             int idnum, AnnotationArray* anno,
    1.74 +                                             Array<AnnotationArray*>** md_p,
    1.75 +                                             TRAPS) {
    1.76 +  Array<AnnotationArray*>* md = *md_p;
    1.77 +  if (md != NULL && md->length() > idnum) {
    1.78 +    md->at_put(idnum, anno);
    1.79 +  } else if (anno != NULL) {
    1.80 +    // create the array
    1.81 +    int length = MAX2(idnum+1, (int)ik->idnum_allocated_count());
    1.82 +    md = MetadataFactory::new_array<AnnotationArray*>(ik->class_loader_data(), length, CHECK);
    1.83 +    if (*md_p != NULL) {
    1.84 +      // copy the existing entries
    1.85 +      for (int index = 0; index < (*md_p)->length(); index++) {
    1.86 +        md->at_put(index, (*md_p)->at(index));
    1.87 +      }
    1.88 +    }
    1.89 +    set_annotations(md, md_p);
    1.90 +    md->at_put(idnum, anno);
    1.91 +  } // if no array and idnum isn't included there is nothing to do
    1.92 +}
    1.93 +
    1.94 +// Keep created annotations in a global growable array (should be hashtable)
    1.95 +// need to add, search, delete when class is unloaded.
    1.96 +// Does it need a lock?  yes.  This sucks.
    1.97 +
    1.98 +// Copy annotations to JVM call or reflection to the java heap.
    1.99 +typeArrayOop Annotations::make_java_array(AnnotationArray* annotations, TRAPS) {
   1.100 +  if (annotations != NULL) {
   1.101 +    int length = annotations->length();
   1.102 +    typeArrayOop copy = oopFactory::new_byteArray(length, CHECK_NULL);
   1.103 +    for (int i = 0; i< length; i++) {
   1.104 +      copy->byte_at_put(i, annotations->at(i));
   1.105 +    }
   1.106 +    return copy;
   1.107 +  } else {
   1.108 +    return NULL;
   1.109 +  }
   1.110 +}
   1.111 +
   1.112 +
   1.113 +void Annotations::print_value_on(outputStream* st) const {
   1.114 +  st->print("Anotations(" INTPTR_FORMAT ")", this);
   1.115 +}
   1.116 +
   1.117 +#define BULLET  " - "
   1.118 +
   1.119 +#ifndef PRODUCT
   1.120 +void Annotations::print_on(outputStream* st) const {
   1.121 +  st->print(BULLET"class_annotations            "); class_annotations()->print_value_on(st);
   1.122 +  st->print(BULLET"fields_annotations           "); fields_annotations()->print_value_on(st);
   1.123 +  st->print(BULLET"methods_annotations          "); methods_annotations()->print_value_on(st);
   1.124 +  st->print(BULLET"methods_parameter_annotations"); methods_parameter_annotations()->print_value_on(st);
   1.125 +  st->print(BULLET"methods_default_annotations  "); methods_default_annotations()->print_value_on(st);
   1.126 +}
   1.127 +#endif // PRODUCT

mercurial