coleenp@4037: /* coleenp@4037: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. coleenp@4037: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. coleenp@4037: * coleenp@4037: * This code is free software; you can redistribute it and/or modify it coleenp@4037: * under the terms of the GNU General Public License version 2 only, as coleenp@4037: * published by the Free Software Foundation. coleenp@4037: * coleenp@4037: * This code is distributed in the hope that it will be useful, but WITHOUT coleenp@4037: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or coleenp@4037: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License coleenp@4037: * version 2 for more details (a copy is included in the LICENSE file that coleenp@4037: * accompanied this code). coleenp@4037: * coleenp@4037: * You should have received a copy of the GNU General Public License version coleenp@4037: * 2 along with this work; if not, write to the Free Software Foundation, coleenp@4037: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. coleenp@4037: * coleenp@4037: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA coleenp@4037: * or visit www.oracle.com if you need additional information or have any coleenp@4037: * questions. coleenp@4037: * coleenp@4037: */ coleenp@4037: coleenp@4037: #include "precompiled.hpp" coleenp@4037: #include "classfile/classLoaderData.hpp" coleenp@4037: #include "memory/metadataFactory.hpp" coleenp@4037: #include "memory/oopFactory.hpp" coleenp@4037: #include "oops/annotations.hpp" coleenp@4037: #include "oops/instanceKlass.hpp" coleenp@4037: #include "utilities/ostream.hpp" coleenp@4037: coleenp@4037: // Allocate annotations in metadata area coleenp@4037: Annotations* Annotations::allocate(ClassLoaderData* loader_data, TRAPS) { coleenp@4037: return new (loader_data, size(), true, THREAD) Annotations(); coleenp@4037: } coleenp@4037: coleenp@4037: Annotations* Annotations::allocate(ClassLoaderData* loader_data, coleenp@4037: Array* fa, coleenp@4037: Array* ma, coleenp@4037: Array* mpa, coleenp@4037: Array* mda, TRAPS) { coleenp@4037: return new (loader_data, size(), true, THREAD) Annotations(fa, ma, mpa, mda); coleenp@4037: } coleenp@4037: coleenp@4037: // helper coleenp@4037: static void free_contents(ClassLoaderData* loader_data, Array* p) { coleenp@4037: if (p != NULL) { coleenp@4037: for (int i = 0; i < p->length(); i++) { coleenp@4037: MetadataFactory::free_array(loader_data, p->at(i)); coleenp@4037: } coleenp@4037: MetadataFactory::free_array(loader_data, p); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void Annotations::deallocate_contents(ClassLoaderData* loader_data) { coleenp@4037: if (class_annotations() != NULL) { coleenp@4037: MetadataFactory::free_array(loader_data, class_annotations()); coleenp@4037: } coleenp@4037: free_contents(loader_data, fields_annotations()); coleenp@4037: free_contents(loader_data, methods_annotations()); coleenp@4037: free_contents(loader_data, methods_parameter_annotations()); coleenp@4037: free_contents(loader_data, methods_default_annotations()); stefank@4393: stefank@4393: // Recursively deallocate optional Annotations linked through this one stefank@4393: MetadataFactory::free_metadata(loader_data, type_annotations()); coleenp@4037: } coleenp@4037: coleenp@4037: // Set the annotation at 'idnum' to 'anno'. coleenp@4037: // We don't want to create or extend the array if 'anno' is NULL, since that is the coleenp@4037: // default value. However, if the array exists and is long enough, we must set NULL values. coleenp@4037: void Annotations::set_methods_annotations_of(instanceKlassHandle ik, coleenp@4037: int idnum, AnnotationArray* anno, coleenp@4037: Array** md_p, coleenp@4037: TRAPS) { coleenp@4037: Array* md = *md_p; coleenp@4037: if (md != NULL && md->length() > idnum) { coleenp@4037: md->at_put(idnum, anno); coleenp@4037: } else if (anno != NULL) { coleenp@4037: // create the array coleenp@4037: int length = MAX2(idnum+1, (int)ik->idnum_allocated_count()); coleenp@4037: md = MetadataFactory::new_array(ik->class_loader_data(), length, CHECK); coleenp@4037: if (*md_p != NULL) { coleenp@4037: // copy the existing entries coleenp@4037: for (int index = 0; index < (*md_p)->length(); index++) { coleenp@4037: md->at_put(index, (*md_p)->at(index)); coleenp@4037: } coleenp@4037: } coleenp@4037: set_annotations(md, md_p); coleenp@4037: md->at_put(idnum, anno); coleenp@4037: } // if no array and idnum isn't included there is nothing to do coleenp@4037: } coleenp@4037: coleenp@4037: // Keep created annotations in a global growable array (should be hashtable) coleenp@4037: // need to add, search, delete when class is unloaded. coleenp@4037: // Does it need a lock? yes. This sucks. coleenp@4037: coleenp@4037: // Copy annotations to JVM call or reflection to the java heap. coleenp@4037: typeArrayOop Annotations::make_java_array(AnnotationArray* annotations, TRAPS) { coleenp@4037: if (annotations != NULL) { coleenp@4037: int length = annotations->length(); coleenp@4037: typeArrayOop copy = oopFactory::new_byteArray(length, CHECK_NULL); coleenp@4037: for (int i = 0; i< length; i++) { coleenp@4037: copy->byte_at_put(i, annotations->at(i)); coleenp@4037: } coleenp@4037: return copy; coleenp@4037: } else { coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: void Annotations::print_value_on(outputStream* st) const { coleenp@4037: st->print("Anotations(" INTPTR_FORMAT ")", this); coleenp@4037: } coleenp@4037: coleenp@4037: #define BULLET " - " coleenp@4037: coleenp@4037: #ifndef PRODUCT coleenp@4037: void Annotations::print_on(outputStream* st) const { coleenp@4037: st->print(BULLET"class_annotations "); class_annotations()->print_value_on(st); coleenp@4037: st->print(BULLET"fields_annotations "); fields_annotations()->print_value_on(st); coleenp@4037: st->print(BULLET"methods_annotations "); methods_annotations()->print_value_on(st); coleenp@4037: st->print(BULLET"methods_parameter_annotations"); methods_parameter_annotations()->print_value_on(st); coleenp@4037: st->print(BULLET"methods_default_annotations "); methods_default_annotations()->print_value_on(st); coleenp@4037: } coleenp@4037: #endif // PRODUCT