reflectionUtils.hpp 6.6 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36
#ifndef SHARE_VM_RUNTIME_REFLECTIONUTILS_HPP
#define SHARE_VM_RUNTIME_REFLECTIONUTILS_HPP

#include "memory/allocation.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/objArrayOop.hpp"
#include "oops/oopsHierarchy.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/reflection.hpp"
#include "utilities/accessFlags.hpp"
#include "utilities/globalDefinitions.hpp"

D
duke 已提交
37 38 39 40 41
// A KlassStream is an abstract stream for streaming over self, superclasses
// and (super)interfaces. Streaming is done in reverse order (subclasses first,
// interfaces last).
//
//    for (KlassStream st(k, false, false); !st.eos(); st.next()) {
42
//      Klass* k = st.klass();
D
duke 已提交
43 44 45 46 47 48
//      ...
//    }

class KlassStream VALUE_OBJ_CLASS_SPEC {
 protected:
  instanceKlassHandle _klass;           // current klass/interface iterated over
49
  Array<Klass*>*    _interfaces;      // transitive interfaces for initial class
D
duke 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  int                 _interface_index; // current interface being processed
  bool                _local_only;      // process initial class/interface only
  bool                _classes_only;    // process classes only (no interfaces)
  int                 _index;

  virtual int length() const = 0;

 public:
  // constructor
  KlassStream(instanceKlassHandle klass, bool local_only, bool classes_only);

  // testing
  bool eos();

  // iterating
  virtual void next() = 0;

  // accessors
  instanceKlassHandle klass() const { return _klass; }
  int index() const                 { return _index; }
};


// A MethodStream streams over all methods in a class, superclasses and (super)interfaces.
// Streaming is done in reverse order (subclasses first, methods in reverse order)
// Usage:
//
//    for (MethodStream st(k, false, false); !st.eos(); st.next()) {
78
//      Method* m = st.method();
D
duke 已提交
79 80 81 82 83 84
//      ...
//    }

class MethodStream : public KlassStream {
 private:
  int length() const          { return methods()->length(); }
85
  Array<Method*>* methods() const { return _klass->methods(); }
D
duke 已提交
86 87 88 89 90 91 92 93
 public:
  MethodStream(instanceKlassHandle klass, bool local_only, bool classes_only)
    : KlassStream(klass, local_only, classes_only) {
    _index = length();
    next();
  }

  void next() { _index--; }
94
  Method* method() const { return methods()->at(index()); }
D
duke 已提交
95 96 97 98 99 100 101 102
};


// A FieldStream streams over all fields in a class, superclasses and (super)interfaces.
// Streaming is done in reverse order (subclasses first, fields in reverse order)
// Usage:
//
//    for (FieldStream st(k, false, false); !st.eos(); st.next()) {
103
//      Symbol* field_name = st.name();
D
duke 已提交
104 105 106 107 108 109
//      ...
//    }


class FieldStream : public KlassStream {
 private:
110 111
  int length() const                { return _klass->java_fields_count(); }

D
duke 已提交
112 113 114 115 116 117 118
 public:
  FieldStream(instanceKlassHandle klass, bool local_only, bool classes_only)
    : KlassStream(klass, local_only, classes_only) {
    _index = length();
    next();
  }

119
  void next() { _index -= 1; }
D
duke 已提交
120 121 122 123

  // Accessors for current field
  AccessFlags access_flags() const {
    AccessFlags flags;
124
    flags.set_flags(_klass->field_access_flags(_index));
D
duke 已提交
125 126
    return flags;
  }
127
  Symbol* name() const {
128
    return _klass->field_name(_index);
D
duke 已提交
129
  }
130
  Symbol* signature() const {
131
    return _klass->field_signature(_index);
D
duke 已提交
132 133 134
  }
  // missing: initval()
  int offset() const {
135
    return _klass->field_offset( index() );
D
duke 已提交
136 137 138 139 140
  }
};

class FilteredField {
 private:
141
  Klass* _klass;
D
duke 已提交
142 143 144
  int      _field_offset;

 public:
145
  FilteredField(Klass* klass, int field_offset) {
D
duke 已提交
146 147 148
    _klass = klass;
    _field_offset = field_offset;
  }
149
  Klass* klass() { return _klass; }
D
duke 已提交
150 151 152 153 154 155 156 157
  int  field_offset() { return _field_offset; }
};

class FilteredFieldsMap : AllStatic {
 private:
  static GrowableArray<FilteredField *> *_filtered_fields;
 public:
  static void initialize();
158
  static bool is_filtered_field(Klass* klass, int field_offset) {
D
duke 已提交
159 160 161 162 163 164 165 166
    for (int i=0; i < _filtered_fields->length(); i++) {
      if (klass == _filtered_fields->at(i)->klass() &&
        field_offset == _filtered_fields->at(i)->field_offset()) {
        return true;
      }
    }
    return false;
  }
167
  static int  filtered_fields_count(Klass* klass, bool local_only) {
D
duke 已提交
168 169 170 171
    int nflds = 0;
    for (int i=0; i < _filtered_fields->length(); i++) {
      if (local_only && klass == _filtered_fields->at(i)->klass()) {
        nflds++;
172
      } else if (klass->is_subtype_of(_filtered_fields->at(i)->klass())) {
D
duke 已提交
173 174 175 176 177
        nflds++;
      }
    }
    return nflds;
  }
178 179
  // Enhance Class Redefinition Support
  static void classes_do(KlassClosure* f) {
D
duke 已提交
180
    for (int i = 0; i < _filtered_fields->length(); i++) {
181
      f->do_klass(_filtered_fields->at(i)->klass());
D
duke 已提交
182 183 184 185 186 187 188 189 190 191 192 193
    }
  }
};


// A FilteredFieldStream streams over all fields in a class, superclasses and
// (super)interfaces. Streaming is done in reverse order (subclasses first,
// fields in reverse order)
//
// Usage:
//
//    for (FilteredFieldStream st(k, false, false); !st.eos(); st.next()) {
194
//      Symbol* field_name = st.name();
D
duke 已提交
195 196 197 198 199 200 201 202 203 204 205
//      ...
//    }

class FilteredFieldStream : public FieldStream {
 private:
  int  _filtered_fields_count;
  bool has_filtered_field() { return (_filtered_fields_count > 0); }

 public:
  FilteredFieldStream(instanceKlassHandle klass, bool local_only, bool classes_only)
    : FieldStream(klass, local_only, classes_only) {
206
    _filtered_fields_count = FilteredFieldsMap::filtered_fields_count((Klass*)klass(), local_only);
D
duke 已提交
207 208 209
  }
  int field_count();
  void next() {
210
    _index -= 1;
D
duke 已提交
211
    if (has_filtered_field()) {
212
      while (_index >=0 && FilteredFieldsMap::is_filtered_field((Klass*)_klass(), offset())) {
213
        _index -= 1;
D
duke 已提交
214 215 216 217
      }
    }
  }
};
218 219

#endif // SHARE_VM_RUNTIME_REFLECTIONUTILS_HPP