hotspot/src/share/vm/runtime/reflectionUtils.hpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2005 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// A KlassStream is an abstract stream for streaming over self, superclasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
// and (super)interfaces. Streaming is done in reverse order (subclasses first,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
// interfaces last).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
//    for (KlassStream st(k, false, false); !st.eos(); st.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
//      klassOop k = st.klass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
//      ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
//    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
class KlassStream VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  instanceKlassHandle _klass;           // current klass/interface iterated over
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  objArrayHandle      _interfaces;      // transitive interfaces for initial class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  int                 _interface_index; // current interface being processed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  bool                _local_only;      // process initial class/interface only
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  bool                _classes_only;    // process classes only (no interfaces)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  int                 _index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  virtual int length() const = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // constructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  KlassStream(instanceKlassHandle klass, bool local_only, bool classes_only);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  // testing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  bool eos();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  // iterating
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  virtual void next() = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  // accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  instanceKlassHandle klass() const { return _klass; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  int index() const                 { return _index; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// A MethodStream streams over all methods in a class, superclasses and (super)interfaces.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
// Streaming is done in reverse order (subclasses first, methods in reverse order)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// Usage:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
//    for (MethodStream st(k, false, false); !st.eos(); st.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
//      methodOop m = st.method();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
//      ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
//    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
class MethodStream : public KlassStream {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  int length() const          { return methods()->length(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  objArrayOop methods() const { return _klass->methods(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  MethodStream(instanceKlassHandle klass, bool local_only, bool classes_only)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    : KlassStream(klass, local_only, classes_only) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
    _index = length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  void next() { _index--; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  methodOop method() const { return methodOop(methods()->obj_at(index())); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
// A FieldStream streams over all fields in a class, superclasses and (super)interfaces.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
// Streaming is done in reverse order (subclasses first, fields in reverse order)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
// Usage:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
//    for (FieldStream st(k, false, false); !st.eos(); st.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
//      symbolOop field_name = st.name();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
//      ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
//    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
class FieldStream : public KlassStream {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  int length() const                { return fields()->length(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  constantPoolOop constants() const { return _klass->constants(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  typeArrayOop fields() const       { return _klass->fields(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  FieldStream(instanceKlassHandle klass, bool local_only, bool classes_only)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    : KlassStream(klass, local_only, classes_only) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    _index = length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  void next() { _index -= instanceKlass::next_offset; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  // Accessors for current field
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  AccessFlags access_flags() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    AccessFlags flags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    flags.set_flags(fields()->ushort_at(index() + instanceKlass::access_flags_offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    return flags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  symbolOop name() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    int name_index = fields()->ushort_at(index() + instanceKlass::name_index_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    return constants()->symbol_at(name_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  symbolOop signature() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    int signature_index = fields()->ushort_at(index() +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
                                       instanceKlass::signature_index_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    return constants()->symbol_at(signature_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  // missing: initval()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  int offset() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    return _klass->offset_from_fields( index() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
class FilteredField {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  klassOop _klass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  int      _field_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  FilteredField(klassOop klass, int field_offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    _klass = klass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    _field_offset = field_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  klassOop klass() { return _klass; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  oop* klass_addr() { return (oop*) &_klass; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  int  field_offset() { return _field_offset; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
class FilteredFieldsMap : AllStatic {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  static GrowableArray<FilteredField *> *_filtered_fields;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  static void initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  static bool is_filtered_field(klassOop klass, int field_offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    for (int i=0; i < _filtered_fields->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
      if (klass == _filtered_fields->at(i)->klass() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
        field_offset == _filtered_fields->at(i)->field_offset()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
        return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  static int  filtered_fields_count(klassOop klass, bool local_only) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    int nflds = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    for (int i=0; i < _filtered_fields->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      if (local_only && klass == _filtered_fields->at(i)->klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
        nflds++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
      } else if (klass->klass_part()->is_subtype_of(_filtered_fields->at(i)->klass())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
        nflds++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    return nflds;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  // GC support.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  static void klasses_oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    for (int i = 0; i < _filtered_fields->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
      f->do_oop((oop*)_filtered_fields->at(i)->klass_addr());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
// A FilteredFieldStream streams over all fields in a class, superclasses and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
// (super)interfaces. Streaming is done in reverse order (subclasses first,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
// fields in reverse order)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
// Usage:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
//    for (FilteredFieldStream st(k, false, false); !st.eos(); st.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
//      symbolOop field_name = st.name();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
//      ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
//    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
class FilteredFieldStream : public FieldStream {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  int  _filtered_fields_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  bool has_filtered_field() { return (_filtered_fields_count > 0); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  FilteredFieldStream(instanceKlassHandle klass, bool local_only, bool classes_only)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    : FieldStream(klass, local_only, classes_only) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    _filtered_fields_count = FilteredFieldsMap::filtered_fields_count((klassOop)klass(), local_only);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  int field_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  void next() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    _index -= instanceKlass::next_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    if (has_filtered_field()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
      while (_index >=0 && FilteredFieldsMap::is_filtered_field((klassOop)_klass(), offset())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
        _index -= instanceKlass::next_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
};