Thu, 11 Oct 2012 12:25:42 -0400
7200776: Implement default methods in interfaces
Summary: Add generic type analysis and default method selection algorithms
Reviewed-by: coleenp, acorn
coleenp@4037 | 1 | /* |
coleenp@4037 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
coleenp@4037 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
coleenp@4037 | 4 | * |
coleenp@4037 | 5 | * This code is free software; you can redistribute it and/or modify it |
coleenp@4037 | 6 | * under the terms of the GNU General Public License version 2 only, as |
coleenp@4037 | 7 | * published by the Free Software Foundation. |
coleenp@4037 | 8 | * |
coleenp@4037 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
coleenp@4037 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
coleenp@4037 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
coleenp@4037 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
coleenp@4037 | 13 | * accompanied this code). |
coleenp@4037 | 14 | * |
coleenp@4037 | 15 | * You should have received a copy of the GNU General Public License version |
coleenp@4037 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
coleenp@4037 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
coleenp@4037 | 18 | * |
coleenp@4037 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
coleenp@4037 | 20 | * or visit www.oracle.com if you need additional information or have any |
coleenp@4037 | 21 | * questions. |
coleenp@4037 | 22 | * |
coleenp@4037 | 23 | */ |
coleenp@4037 | 24 | |
coleenp@4037 | 25 | #ifndef SHARE_VM_OOPS_ANNOTATIONS_HPP |
coleenp@4037 | 26 | #define SHARE_VM_OOPS_ANNOTATIONS_HPP |
coleenp@4037 | 27 | |
coleenp@4037 | 28 | #include "oops/metadata.hpp" |
coleenp@4037 | 29 | #include "runtime/handles.hpp" |
coleenp@4037 | 30 | #include "utilities/array.hpp" |
coleenp@4037 | 31 | #include "utilities/exceptions.hpp" |
coleenp@4037 | 32 | #include "utilities/globalDefinitions.hpp" |
coleenp@4037 | 33 | |
coleenp@4037 | 34 | |
coleenp@4037 | 35 | class ClassLoaderData; |
coleenp@4037 | 36 | class outputStream; |
coleenp@4037 | 37 | |
coleenp@4037 | 38 | typedef Array<u1> AnnotationArray; |
coleenp@4037 | 39 | |
coleenp@4037 | 40 | // Class to hold the various types of annotations. The only metadata that points |
coleenp@4037 | 41 | // to this is InstanceKlass. |
coleenp@4037 | 42 | |
coleenp@4037 | 43 | class Annotations: public MetaspaceObj { |
coleenp@4037 | 44 | |
coleenp@4037 | 45 | // Annotations for this class, or null if none. |
coleenp@4037 | 46 | AnnotationArray* _class_annotations; |
coleenp@4037 | 47 | // Annotation objects (byte arrays) for fields, or null if no annotations. |
coleenp@4037 | 48 | // Indices correspond to entries (not indices) in fields array. |
coleenp@4037 | 49 | Array<AnnotationArray*>* _fields_annotations; |
coleenp@4037 | 50 | // Annotation objects (byte arrays) for methods, or null if no annotations. |
coleenp@4037 | 51 | // Index is the idnum, which is initially the same as the methods array index. |
coleenp@4037 | 52 | Array<AnnotationArray*>* _methods_annotations; |
coleenp@4037 | 53 | // Annotation objects (byte arrays) for methods' parameters, or null if no |
coleenp@4037 | 54 | // such annotations. |
coleenp@4037 | 55 | // Index is the idnum, which is initially the same as the methods array index. |
coleenp@4037 | 56 | Array<AnnotationArray*>* _methods_parameter_annotations; |
coleenp@4037 | 57 | // Annotation objects (byte arrays) for methods' default values, or null if no |
coleenp@4037 | 58 | // such annotations. |
coleenp@4037 | 59 | // Index is the idnum, which is initially the same as the methods array index. |
coleenp@4037 | 60 | Array<AnnotationArray*>* _methods_default_annotations; |
coleenp@4037 | 61 | |
coleenp@4037 | 62 | // Constructor where some some values are known to not be null |
coleenp@4037 | 63 | Annotations(Array<AnnotationArray*>* fa, Array<AnnotationArray*>* ma, |
coleenp@4037 | 64 | Array<AnnotationArray*>* mpa, Array<AnnotationArray*>* mda) : |
coleenp@4037 | 65 | _class_annotations(NULL), |
coleenp@4037 | 66 | _fields_annotations(fa), |
coleenp@4037 | 67 | _methods_annotations(ma), |
coleenp@4037 | 68 | _methods_parameter_annotations(mpa), |
coleenp@4037 | 69 | _methods_default_annotations(mda) {} |
coleenp@4037 | 70 | |
coleenp@4037 | 71 | public: |
coleenp@4037 | 72 | // Allocate instance of this class |
coleenp@4037 | 73 | static Annotations* allocate(ClassLoaderData* loader_data, TRAPS); |
coleenp@4037 | 74 | static Annotations* allocate(ClassLoaderData* loader_data, |
coleenp@4037 | 75 | Array<AnnotationArray*>* fa, |
coleenp@4037 | 76 | Array<AnnotationArray*>* ma, |
coleenp@4037 | 77 | Array<AnnotationArray*>* mpa, |
coleenp@4037 | 78 | Array<AnnotationArray*>* mda, TRAPS); |
coleenp@4037 | 79 | void deallocate_contents(ClassLoaderData* loader_data); |
coleenp@4037 | 80 | DEBUG_ONLY(bool on_stack() { return false; }) // for template |
coleenp@4037 | 81 | static int size() { return sizeof(Annotations) / wordSize; } |
coleenp@4037 | 82 | |
coleenp@4037 | 83 | // Constructor to initialize to null |
coleenp@4037 | 84 | Annotations() : _class_annotations(NULL), _fields_annotations(NULL), |
coleenp@4037 | 85 | _methods_annotations(NULL), |
coleenp@4037 | 86 | _methods_parameter_annotations(NULL), |
coleenp@4037 | 87 | _methods_default_annotations(NULL) {} |
coleenp@4037 | 88 | |
coleenp@4037 | 89 | AnnotationArray* class_annotations() const { return _class_annotations; } |
coleenp@4037 | 90 | Array<AnnotationArray*>* fields_annotations() const { return _fields_annotations; } |
coleenp@4037 | 91 | Array<AnnotationArray*>* methods_annotations() const { return _methods_annotations; } |
coleenp@4037 | 92 | Array<AnnotationArray*>* methods_parameter_annotations() const { return _methods_parameter_annotations; } |
coleenp@4037 | 93 | Array<AnnotationArray*>* methods_default_annotations() const { return _methods_default_annotations; } |
coleenp@4037 | 94 | |
coleenp@4037 | 95 | void set_class_annotations(AnnotationArray* md) { _class_annotations = md; } |
coleenp@4037 | 96 | void set_fields_annotations(Array<AnnotationArray*>* md) { _fields_annotations = md; } |
coleenp@4037 | 97 | void set_methods_annotations(Array<AnnotationArray*>* md) { _methods_annotations = md; } |
coleenp@4037 | 98 | void set_methods_parameter_annotations(Array<AnnotationArray*>* md) { _methods_parameter_annotations = md; } |
coleenp@4037 | 99 | void set_methods_default_annotations(Array<AnnotationArray*>* md) { _methods_default_annotations = md; } |
coleenp@4037 | 100 | |
coleenp@4037 | 101 | // Redefine classes support |
coleenp@4037 | 102 | AnnotationArray* get_method_annotations_of(int idnum) |
coleenp@4037 | 103 | { return get_method_annotations_from(idnum, _methods_annotations); } |
coleenp@4037 | 104 | |
coleenp@4037 | 105 | AnnotationArray* get_method_parameter_annotations_of(int idnum) |
coleenp@4037 | 106 | { return get_method_annotations_from(idnum, _methods_parameter_annotations); } |
coleenp@4037 | 107 | AnnotationArray* get_method_default_annotations_of(int idnum) |
coleenp@4037 | 108 | { return get_method_annotations_from(idnum, _methods_default_annotations); } |
coleenp@4037 | 109 | |
coleenp@4037 | 110 | |
coleenp@4037 | 111 | void set_method_annotations_of(instanceKlassHandle ik, |
coleenp@4037 | 112 | int idnum, AnnotationArray* anno, TRAPS) { |
coleenp@4037 | 113 | set_methods_annotations_of(ik, idnum, anno, &_methods_annotations, THREAD); |
coleenp@4037 | 114 | } |
coleenp@4037 | 115 | |
coleenp@4037 | 116 | void set_method_parameter_annotations_of(instanceKlassHandle ik, |
coleenp@4037 | 117 | int idnum, AnnotationArray* anno, TRAPS) { |
coleenp@4037 | 118 | set_methods_annotations_of(ik, idnum, anno, &_methods_parameter_annotations, THREAD); |
coleenp@4037 | 119 | } |
coleenp@4037 | 120 | |
coleenp@4037 | 121 | void set_method_default_annotations_of(instanceKlassHandle ik, |
coleenp@4037 | 122 | int idnum, AnnotationArray* anno, TRAPS) { |
coleenp@4037 | 123 | set_methods_annotations_of(ik, idnum, anno, &_methods_default_annotations, THREAD); |
coleenp@4037 | 124 | } |
coleenp@4037 | 125 | |
coleenp@4037 | 126 | // Turn metadata annotations into a Java heap object (oop) |
coleenp@4037 | 127 | static typeArrayOop make_java_array(AnnotationArray* annotations, TRAPS); |
coleenp@4037 | 128 | |
coleenp@4037 | 129 | inline AnnotationArray* get_method_annotations_from(int idnum, Array<AnnotationArray*>* annos); |
coleenp@4037 | 130 | void set_annotations(Array<AnnotationArray*>* md, Array<AnnotationArray*>** md_p) { *md_p = md; } |
coleenp@4037 | 131 | |
coleenp@4037 | 132 | private: |
coleenp@4037 | 133 | void set_methods_annotations_of(instanceKlassHandle ik, |
coleenp@4037 | 134 | int idnum, AnnotationArray* anno, |
coleenp@4037 | 135 | Array<AnnotationArray*>** md_p, TRAPS); |
coleenp@4037 | 136 | |
coleenp@4037 | 137 | public: |
coleenp@4037 | 138 | const char* internal_name() const { return "{constant pool}"; } |
coleenp@4037 | 139 | #ifndef PRODUCT |
coleenp@4037 | 140 | void print_on(outputStream* st) const; |
coleenp@4037 | 141 | #endif |
coleenp@4037 | 142 | void print_value_on(outputStream* st) const; |
coleenp@4037 | 143 | }; |
coleenp@4037 | 144 | |
coleenp@4037 | 145 | |
coleenp@4037 | 146 | // For method with idnum get the method's Annotations |
coleenp@4037 | 147 | inline AnnotationArray* Annotations::get_method_annotations_from(int idnum, Array<AnnotationArray*>* annos) { |
coleenp@4037 | 148 | if (annos == NULL || annos->length() <= idnum) { |
coleenp@4037 | 149 | return NULL; |
coleenp@4037 | 150 | } |
coleenp@4037 | 151 | return annos->at(idnum); |
coleenp@4037 | 152 | } |
coleenp@4037 | 153 | #endif // SHARE_VM_OOPS_ANNOTATIONS_HPP |