src/share/vm/runtime/reflectionUtils.hpp

Wed, 02 Feb 2011 11:35:26 -0500

author
bobv
date
Wed, 02 Feb 2011 11:35:26 -0500
changeset 2508
b92c45f2bc75
parent 2497
3582bf76420e
child 2708
1d1603768966
permissions
-rw-r--r--

7016023: Enable building ARM and PPC from src/closed repository
Reviewed-by: dholmes, bdelsart

     1 /*
     2  * Copyright (c) 1999, 2010, 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 #ifndef SHARE_VM_RUNTIME_REFLECTIONUTILS_HPP
    26 #define SHARE_VM_RUNTIME_REFLECTIONUTILS_HPP
    28 #include "memory/allocation.hpp"
    29 #include "oops/instanceKlass.hpp"
    30 #include "oops/objArrayOop.hpp"
    31 #include "oops/oopsHierarchy.hpp"
    32 #include "runtime/handles.inline.hpp"
    33 #include "runtime/reflection.hpp"
    34 #include "utilities/accessFlags.hpp"
    35 #include "utilities/globalDefinitions.hpp"
    37 // A KlassStream is an abstract stream for streaming over self, superclasses
    38 // and (super)interfaces. Streaming is done in reverse order (subclasses first,
    39 // interfaces last).
    40 //
    41 //    for (KlassStream st(k, false, false); !st.eos(); st.next()) {
    42 //      klassOop k = st.klass();
    43 //      ...
    44 //    }
    46 class KlassStream VALUE_OBJ_CLASS_SPEC {
    47  protected:
    48   instanceKlassHandle _klass;           // current klass/interface iterated over
    49   objArrayHandle      _interfaces;      // transitive interfaces for initial class
    50   int                 _interface_index; // current interface being processed
    51   bool                _local_only;      // process initial class/interface only
    52   bool                _classes_only;    // process classes only (no interfaces)
    53   int                 _index;
    55   virtual int length() const = 0;
    57  public:
    58   // constructor
    59   KlassStream(instanceKlassHandle klass, bool local_only, bool classes_only);
    61   // testing
    62   bool eos();
    64   // iterating
    65   virtual void next() = 0;
    67   // accessors
    68   instanceKlassHandle klass() const { return _klass; }
    69   int index() const                 { return _index; }
    70 };
    73 // A MethodStream streams over all methods in a class, superclasses and (super)interfaces.
    74 // Streaming is done in reverse order (subclasses first, methods in reverse order)
    75 // Usage:
    76 //
    77 //    for (MethodStream st(k, false, false); !st.eos(); st.next()) {
    78 //      methodOop m = st.method();
    79 //      ...
    80 //    }
    82 class MethodStream : public KlassStream {
    83  private:
    84   int length() const          { return methods()->length(); }
    85   objArrayOop methods() const { return _klass->methods(); }
    86  public:
    87   MethodStream(instanceKlassHandle klass, bool local_only, bool classes_only)
    88     : KlassStream(klass, local_only, classes_only) {
    89     _index = length();
    90     next();
    91   }
    93   void next() { _index--; }
    94   methodOop method() const { return methodOop(methods()->obj_at(index())); }
    95 };
    98 // A FieldStream streams over all fields in a class, superclasses and (super)interfaces.
    99 // Streaming is done in reverse order (subclasses first, fields in reverse order)
   100 // Usage:
   101 //
   102 //    for (FieldStream st(k, false, false); !st.eos(); st.next()) {
   103 //      Symbol* field_name = st.name();
   104 //      ...
   105 //    }
   108 class FieldStream : public KlassStream {
   109  private:
   110   int length() const                { return fields()->length(); }
   111   constantPoolOop constants() const { return _klass->constants(); }
   112  protected:
   113   typeArrayOop fields() const       { return _klass->fields(); }
   114  public:
   115   FieldStream(instanceKlassHandle klass, bool local_only, bool classes_only)
   116     : KlassStream(klass, local_only, classes_only) {
   117     _index = length();
   118     next();
   119   }
   121   void next() { _index -= instanceKlass::next_offset; }
   123   // Accessors for current field
   124   AccessFlags access_flags() const {
   125     AccessFlags flags;
   126     flags.set_flags(fields()->ushort_at(index() + instanceKlass::access_flags_offset));
   127     return flags;
   128   }
   129   Symbol* name() const {
   130     int name_index = fields()->ushort_at(index() + instanceKlass::name_index_offset);
   131     return constants()->symbol_at(name_index);
   132   }
   133   Symbol* signature() const {
   134     int signature_index = fields()->ushort_at(index() +
   135                                        instanceKlass::signature_index_offset);
   136     return constants()->symbol_at(signature_index);
   137   }
   138   // missing: initval()
   139   int offset() const {
   140     return _klass->offset_from_fields( index() );
   141   }
   142 };
   144 class FilteredField {
   145  private:
   146   klassOop _klass;
   147   int      _field_offset;
   149  public:
   150   FilteredField(klassOop klass, int field_offset) {
   151     _klass = klass;
   152     _field_offset = field_offset;
   153   }
   154   klassOop klass() { return _klass; }
   155   oop* klass_addr() { return (oop*) &_klass; }
   156   int  field_offset() { return _field_offset; }
   157 };
   159 class FilteredFieldsMap : AllStatic {
   160  private:
   161   static GrowableArray<FilteredField *> *_filtered_fields;
   162  public:
   163   static void initialize();
   164   static bool is_filtered_field(klassOop klass, int field_offset) {
   165     for (int i=0; i < _filtered_fields->length(); i++) {
   166       if (klass == _filtered_fields->at(i)->klass() &&
   167         field_offset == _filtered_fields->at(i)->field_offset()) {
   168         return true;
   169       }
   170     }
   171     return false;
   172   }
   173   static int  filtered_fields_count(klassOop klass, bool local_only) {
   174     int nflds = 0;
   175     for (int i=0; i < _filtered_fields->length(); i++) {
   176       if (local_only && klass == _filtered_fields->at(i)->klass()) {
   177         nflds++;
   178       } else if (klass->klass_part()->is_subtype_of(_filtered_fields->at(i)->klass())) {
   179         nflds++;
   180       }
   181     }
   182     return nflds;
   183   }
   184   // GC support.
   185   static void klasses_oops_do(OopClosure* f) {
   186     for (int i = 0; i < _filtered_fields->length(); i++) {
   187       f->do_oop((oop*)_filtered_fields->at(i)->klass_addr());
   188     }
   189   }
   190 };
   193 // A FilteredFieldStream streams over all fields in a class, superclasses and
   194 // (super)interfaces. Streaming is done in reverse order (subclasses first,
   195 // fields in reverse order)
   196 //
   197 // Usage:
   198 //
   199 //    for (FilteredFieldStream st(k, false, false); !st.eos(); st.next()) {
   200 //      Symbol* field_name = st.name();
   201 //      ...
   202 //    }
   204 class FilteredFieldStream : public FieldStream {
   205  private:
   206   int  _filtered_fields_count;
   207   bool has_filtered_field() { return (_filtered_fields_count > 0); }
   209  public:
   210   FilteredFieldStream(instanceKlassHandle klass, bool local_only, bool classes_only)
   211     : FieldStream(klass, local_only, classes_only) {
   212     _filtered_fields_count = FilteredFieldsMap::filtered_fields_count((klassOop)klass(), local_only);
   213   }
   214   int field_count();
   215   void next() {
   216     _index -= instanceKlass::next_offset;
   217     if (has_filtered_field()) {
   218       while (_index >=0 && FilteredFieldsMap::is_filtered_field((klassOop)_klass(), offset())) {
   219         _index -= instanceKlass::next_offset;
   220       }
   221     }
   222   }
   223 };
   225 #endif // SHARE_VM_RUNTIME_REFLECTIONUTILS_HPP

mercurial