src/hotspot/share/classfile/defaultMethods.cpp
author lfoltan
Mon, 21 Oct 2019 13:13:16 -0400
changeset 58722 cba8afa5cfed
parent 58689 72e605a88500
child 58760 1f7f707c1aa9
permissions -rw-r--r--
8231844: Enhance type signature characters in classfile_constants.h and improve the JVM to use type signature characters more consistently Summary: Increase the use of type signature constants instead of hard coded characters within the JVM. Reviewed-by: coleenp, dholmes, fparain Contributed-by: lois.foltan@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
     1
/*
53432
1ec56532ae0c 8217424: Remove the idempotent parameter to Method::sort_methods
iklam
parents: 51444
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
     4
 *
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
     8
 *
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    13
 * accompanied this code).
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    14
 *
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    18
 *
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    21
 * questions.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    22
 *
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    23
 */
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    24
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    25
#include "precompiled.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    26
#include "classfile/bytecodeAssembler.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    27
#include "classfile/defaultMethods.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    28
#include "classfile/symbolTable.hpp"
48463
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
    29
#include "classfile/systemDictionary.hpp"
33736
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
    30
#include "logging/log.hpp"
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
    31
#include "logging/logStream.hpp"
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    32
#include "memory/allocation.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    33
#include "memory/metadataFactory.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    34
#include "memory/resourceArea.hpp"
54786
ebf733a324d4 8223624: Cleanup includes of universe.hpp
stefank
parents: 54013
diff changeset
    35
#include "memory/universe.hpp"
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 33736
diff changeset
    36
#include "runtime/handles.inline.hpp"
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    37
#include "runtime/signature.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    38
#include "runtime/thread.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    39
#include "oops/instanceKlass.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    40
#include "oops/klass.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    41
#include "oops/method.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    42
#include "utilities/accessFlags.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    43
#include "utilities/exceptions.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    44
#include "utilities/ostream.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    45
#include "utilities/pair.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    46
#include "utilities/resourceHash.hpp"
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    47
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    48
typedef enum { QUALIFIED, DISQUALIFIED } QualifiedState;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    49
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    50
// Because we use an iterative algorithm when iterating over the type
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    51
// hierarchy, we can't use traditional scoped objects which automatically do
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    52
// cleanup in the destructor when the scope is exited.  PseudoScope (and
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    53
// PseudoScopeMark) provides a similar functionality, but for when you want a
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    54
// scoped object in non-stack memory (such as in resource memory, as we do
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    55
// here).  You've just got to remember to call 'destroy()' on the scope when
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    56
// leaving it (and marks have to be explicitly added).
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    57
class PseudoScopeMark : public ResourceObj {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    58
 public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    59
  virtual void destroy() = 0;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    60
};
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    61
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    62
class PseudoScope : public ResourceObj {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    63
 private:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    64
  GrowableArray<PseudoScopeMark*> _marks;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    65
 public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    66
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    67
  static PseudoScope* cast(void* data) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    68
    return static_cast<PseudoScope*>(data);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    69
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    70
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    71
  void add_mark(PseudoScopeMark* psm) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    72
   _marks.append(psm);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    73
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    74
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    75
  void destroy() {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    76
    for (int i = 0; i < _marks.length(); ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    77
      _marks.at(i)->destroy();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    78
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    79
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    80
};
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    81
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    82
static void print_slot(outputStream* str, Symbol* name, Symbol* signature) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    83
  str->print("%s%s", name->as_C_string(), signature->as_C_string());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    84
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    85
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    86
static void print_method(outputStream* str, Method* mo, bool with_class=true) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    87
  if (with_class) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    88
    str->print("%s.", mo->klass_name()->as_C_string());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    89
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    90
  print_slot(str, mo->name(), mo->signature());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    91
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    92
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    93
/**
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    94
 * Perform a depth-first iteration over the class hierarchy, applying
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    95
 * algorithmic logic as it goes.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    96
 *
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    97
 * This class is one half of the inheritance hierarchy analysis mechanism.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    98
 * It is meant to be used in conjunction with another class, the algorithm,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
    99
 * which is indicated by the ALGO template parameter.  This class can be
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   100
 * paired with any algorithm class that provides the required methods.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   101
 *
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   102
 * This class contains all the mechanics for iterating over the class hierarchy
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   103
 * starting at a particular root, without recursing (thus limiting stack growth
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   104
 * from this point).  It visits each superclass (if present) and superinterface
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   105
 * in a depth-first manner, with callbacks to the ALGO class as each class is
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   106
 * encountered (visit()), The algorithm can cut-off further exploration of a
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   107
 * particular branch by returning 'false' from a visit() call.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   108
 *
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   109
 * The ALGO class, must provide a visit() method, which each of which will be
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   110
 * called once for each node in the inheritance tree during the iteration.  In
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   111
 * addition, it can provide a memory block via new_node_data(InstanceKlass*),
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   112
 * which it can use for node-specific storage (and access via the
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   113
 * current_data() and data_at_depth(int) methods).
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   114
 *
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   115
 * Bare minimum needed to be an ALGO class:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   116
 * class Algo : public HierarchyVisitor<Algo> {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   117
 *   void* new_node_data(InstanceKlass* cls) { return NULL; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   118
 *   void free_node_data(void* data) { return; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   119
 *   bool visit() { return true; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   120
 * };
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   121
 */
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   122
template <class ALGO>
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   123
class HierarchyVisitor : StackObj {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   124
 private:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   125
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   126
  class Node : public ResourceObj {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   127
   public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   128
    InstanceKlass* _class;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   129
    bool _super_was_visited;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   130
    int _interface_index;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   131
    void* _algorithm_data;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   132
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   133
    Node(InstanceKlass* cls, void* data, bool visit_super)
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   134
        : _class(cls), _super_was_visited(!visit_super),
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   135
          _interface_index(0), _algorithm_data(data) {}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   136
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   137
    int number_of_interfaces() { return _class->local_interfaces()->length(); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   138
    int interface_index() { return _interface_index; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   139
    void set_super_visited() { _super_was_visited = true; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   140
    void increment_visited_interface() { ++_interface_index; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   141
    void set_all_interfaces_visited() {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   142
      _interface_index = number_of_interfaces();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   143
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   144
    bool has_visited_super() { return _super_was_visited; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   145
    bool has_visited_all_interfaces() {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   146
      return interface_index() >= number_of_interfaces();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   147
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   148
    InstanceKlass* interface_at(int index) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   149
      return InstanceKlass::cast(_class->local_interfaces()->at(index));
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   150
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   151
    InstanceKlass* next_super() { return _class->java_super(); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   152
    InstanceKlass* next_interface() {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   153
      return interface_at(interface_index());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   154
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   155
  };
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   156
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   157
  bool _visited_Object;
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   158
  GrowableArray<Node*> _path;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   159
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   160
  Node* current_top() const { return _path.top(); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   161
  bool has_more_nodes() const { return !_path.is_empty(); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   162
  void push(InstanceKlass* cls, void* data) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   163
    assert(cls != NULL, "Requires a valid instance class");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   164
    Node* node = new Node(cls, data, has_super(cls));
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   165
    if (cls == SystemDictionary::Object_klass()) {
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   166
      _visited_Object = true;
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   167
    }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   168
    _path.push(node);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   169
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   170
  void pop() { _path.pop(); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   171
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   172
  // Since the starting point can be an interface, we must ensure we catch
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   173
  // j.l.Object as the super once in those cases. The _visited_Object flag
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   174
  // only ensures we don't then repeatedly enqueue Object for each interface
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   175
  // in the class hierarchy.
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   176
  bool has_super(InstanceKlass* cls) {
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   177
    return cls->super() != NULL && (!_visited_Object || !cls->is_interface());
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   178
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   179
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   180
  Node* node_at_depth(int i) const {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   181
    return (i >= _path.length()) ? NULL : _path.at(_path.length() - i - 1);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   182
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   183
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   184
 protected:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   185
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   186
  // Accessors available to the algorithm
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   187
  int current_depth() const { return _path.length() - 1; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   188
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   189
  InstanceKlass* class_at_depth(int i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   190
    Node* n = node_at_depth(i);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   191
    return n == NULL ? NULL : n->_class;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   192
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   193
  InstanceKlass* current_class() { return class_at_depth(0); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   194
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   195
  void* data_at_depth(int i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   196
    Node* n = node_at_depth(i);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   197
    return n == NULL ? NULL : n->_algorithm_data;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   198
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   199
  void* current_data() { return data_at_depth(0); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   200
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   201
 public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   202
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   203
  void run(InstanceKlass* root) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   204
    ALGO* algo = static_cast<ALGO*>(this);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   205
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   206
    void* algo_data = algo->new_node_data(root);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   207
    push(root, algo_data);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   208
    bool top_needs_visit = true;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   209
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   210
    do {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   211
      Node* top = current_top();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   212
      if (top_needs_visit) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   213
        if (algo->visit() == false) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   214
          // algorithm does not want to continue along this path.  Arrange
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   215
          // it so that this state is immediately popped off the stack
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   216
          top->set_super_visited();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   217
          top->set_all_interfaces_visited();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   218
        }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   219
        top_needs_visit = false;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   220
      }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   221
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   222
      if (top->has_visited_super() && top->has_visited_all_interfaces()) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   223
        algo->free_node_data(top->_algorithm_data);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   224
        pop();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   225
      } else {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   226
        InstanceKlass* next = NULL;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   227
        if (top->has_visited_super() == false) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   228
          next = top->next_super();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   229
          top->set_super_visited();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   230
        } else {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   231
          next = top->next_interface();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   232
          top->increment_visited_interface();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   233
        }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   234
        assert(next != NULL, "Otherwise we shouldn't be here");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   235
        algo_data = algo->new_node_data(next);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   236
        push(next, algo_data);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   237
        top_needs_visit = true;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   238
      }
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   239
    } while (has_more_nodes());
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   240
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   241
};
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   242
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   243
class PrintHierarchy : public HierarchyVisitor<PrintHierarchy> {
33736
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   244
 private:
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   245
   outputStream* _st;
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   246
 public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   247
  bool visit() {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   248
    InstanceKlass* cls = current_class();
33736
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   249
    streamIndentor si(_st, current_depth() * 2);
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   250
    _st->indent().print_cr("%s", cls->name()->as_C_string());
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   251
    return true;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   252
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   253
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   254
  void* new_node_data(InstanceKlass* cls) { return NULL; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   255
  void free_node_data(void* data) { return; }
33736
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   256
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   257
  PrintHierarchy(outputStream* st = tty) : _st(st) {}
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   258
};
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   259
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   260
// Used to register InstanceKlass objects and all related metadata structures
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   261
// (Methods, ConstantPools) as "in-use" by the current thread so that they can't
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   262
// be deallocated by class redefinition while we're using them.  The classes are
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   263
// de-registered when this goes out of scope.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   264
//
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   265
// Once a class is registered, we need not bother with methodHandles or
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   266
// constantPoolHandles for it's associated metadata.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   267
class KeepAliveRegistrar : public StackObj {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   268
 private:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   269
  Thread* _thread;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   270
  GrowableArray<ConstantPool*> _keep_alive;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   271
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   272
 public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   273
  KeepAliveRegistrar(Thread* thread) : _thread(thread), _keep_alive(20) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   274
    assert(thread == Thread::current(), "Must be current thread");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   275
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   276
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   277
  ~KeepAliveRegistrar() {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   278
    for (int i = _keep_alive.length() - 1; i >= 0; --i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   279
      ConstantPool* cp = _keep_alive.at(i);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   280
      int idx = _thread->metadata_handles()->find_from_end(cp);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   281
      assert(idx > 0, "Must be in the list");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   282
      _thread->metadata_handles()->remove_at(idx);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   283
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   284
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   285
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   286
  // Register a class as 'in-use' by the thread.  It's fine to register a class
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   287
  // multiple times (though perhaps inefficient)
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   288
  void register_class(InstanceKlass* ik) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   289
    ConstantPool* cp = ik->constants();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   290
    _keep_alive.push(cp);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   291
    _thread->metadata_handles()->push(cp);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   292
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   293
};
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   294
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   295
class KeepAliveVisitor : public HierarchyVisitor<KeepAliveVisitor> {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   296
 private:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   297
  KeepAliveRegistrar* _registrar;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   298
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   299
 public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   300
  KeepAliveVisitor(KeepAliveRegistrar* registrar) : _registrar(registrar) {}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   301
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   302
  void* new_node_data(InstanceKlass* cls) { return NULL; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   303
  void free_node_data(void* data) { return; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   304
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   305
  bool visit() {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   306
    _registrar->register_class(current_class());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   307
    return true;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   308
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   309
};
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   310
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   311
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   312
// A method family contains a set of all methods that implement a single
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   313
// erased method. As members of the set are collected while walking over the
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   314
// hierarchy, they are tagged with a qualification state.  The qualification
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   315
// state for an erased method is set to disqualified if there exists a path
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   316
// from the root of hierarchy to the method that contains an interleaving
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   317
// erased method defined in an interface.
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   318
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   319
class MethodFamily : public ResourceObj {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   320
 private:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   321
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   322
  GrowableArray<Pair<Method*,QualifiedState> > _members;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   323
  ResourceHashtable<Method*, int> _member_index;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   324
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   325
  Method* _selected_target;  // Filled in later, if a unique target exists
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   326
  Symbol* _exception_message; // If no unique target is found
20284
595a25ab9474 8011311: Private interface methods. Default conflicts:ICCE. no erased_super_default.
acorn
parents: 19966
diff changeset
   327
  Symbol* _exception_name;    // If no unique target is found
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   328
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   329
  bool contains_method(Method* method) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   330
    int* lookup = _member_index.get(method);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   331
    return lookup != NULL;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   332
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   333
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   334
  void add_method(Method* method, QualifiedState state) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   335
    Pair<Method*,QualifiedState> entry(method, state);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   336
    _member_index.put(method, _members.length());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   337
    _members.append(entry);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   338
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   339
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   340
  void disqualify_method(Method* method) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   341
    int* index = _member_index.get(method);
16379
02ed75a9a421 8009578: [parfait] Null pointer deference in hotspot/src/share/vm/classfile/defaultMethods.cpp
morris
parents: 15601
diff changeset
   342
    guarantee(index != NULL && *index >= 0 && *index < _members.length(), "bad index");
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   343
    _members.at(*index).second = DISQUALIFIED;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   344
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   345
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   346
  Symbol* generate_no_defaults_message(TRAPS) const;
22232
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   347
  Symbol* generate_method_message(Symbol *klass_name, Method* method, TRAPS) const;
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   348
  Symbol* generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   349
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   350
 public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   351
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   352
  MethodFamily()
20284
595a25ab9474 8011311: Private interface methods. Default conflicts:ICCE. no erased_super_default.
acorn
parents: 19966
diff changeset
   353
      : _selected_target(NULL), _exception_message(NULL), _exception_name(NULL) {}
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   354
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   355
  void set_target_if_empty(Method* m) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   356
    if (_selected_target == NULL && !m->is_overpass()) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   357
      _selected_target = m;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   358
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   359
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   360
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   361
  void record_qualified_method(Method* m) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   362
    // If the method already exists in the set as qualified, this operation is
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   363
    // redundant.  If it already exists as disqualified, then we leave it as
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   364
    // disqualfied.  Thus we only add to the set if it's not already in the
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   365
    // set.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   366
    if (!contains_method(m)) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   367
      add_method(m, QUALIFIED);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   368
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   369
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   370
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   371
  void record_disqualified_method(Method* m) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   372
    // If not in the set, add it as disqualified.  If it's already in the set,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   373
    // then set the state to disqualified no matter what the previous state was.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   374
    if (!contains_method(m)) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   375
      add_method(m, DISQUALIFIED);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   376
    } else {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   377
      disqualify_method(m);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   378
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   379
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   380
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   381
  bool has_target() const { return _selected_target != NULL; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   382
  bool throws_exception() { return _exception_message != NULL; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   383
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   384
  Method* get_selected_target() { return _selected_target; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   385
  Symbol* get_exception_message() { return _exception_message; }
20284
595a25ab9474 8011311: Private interface methods. Default conflicts:ICCE. no erased_super_default.
acorn
parents: 19966
diff changeset
   386
  Symbol* get_exception_name() { return _exception_name; }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   387
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   388
  // Either sets the target or the exception error message
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   389
  void determine_target(InstanceKlass* root, TRAPS) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   390
    if (has_target() || throws_exception()) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   391
      return;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   392
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   393
21556
e75cd34a59e0 8027229: ICCE expected for >=2 maximally specific default methods.
acorn
parents: 21516
diff changeset
   394
    // Qualified methods are maximally-specific methods
e75cd34a59e0 8027229: ICCE expected for >=2 maximally specific default methods.
acorn
parents: 21516
diff changeset
   395
    // These include public, instance concrete (=default) and abstract methods
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   396
    GrowableArray<Method*> qualified_methods;
21516
8fa5308ab970 8027304: Lambda: inheriting abstract + 1 default -> default, not ICCE
acorn
parents: 20710
diff changeset
   397
    int num_defaults = 0;
8fa5308ab970 8027304: Lambda: inheriting abstract + 1 default -> default, not ICCE
acorn
parents: 20710
diff changeset
   398
    int default_index = -1;
21556
e75cd34a59e0 8027229: ICCE expected for >=2 maximally specific default methods.
acorn
parents: 21516
diff changeset
   399
    int qualified_index = -1;
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   400
    for (int i = 0; i < _members.length(); ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   401
      Pair<Method*,QualifiedState> entry = _members.at(i);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   402
      if (entry.second == QUALIFIED) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   403
        qualified_methods.append(entry.first);
21556
e75cd34a59e0 8027229: ICCE expected for >=2 maximally specific default methods.
acorn
parents: 21516
diff changeset
   404
        qualified_index++;
21516
8fa5308ab970 8027304: Lambda: inheriting abstract + 1 default -> default, not ICCE
acorn
parents: 20710
diff changeset
   405
        if (entry.first->is_default_method()) {
8fa5308ab970 8027304: Lambda: inheriting abstract + 1 default -> default, not ICCE
acorn
parents: 20710
diff changeset
   406
          num_defaults++;
21556
e75cd34a59e0 8027229: ICCE expected for >=2 maximally specific default methods.
acorn
parents: 21516
diff changeset
   407
          default_index = qualified_index;
e75cd34a59e0 8027229: ICCE expected for >=2 maximally specific default methods.
acorn
parents: 21516
diff changeset
   408
21516
8fa5308ab970 8027304: Lambda: inheriting abstract + 1 default -> default, not ICCE
acorn
parents: 20710
diff changeset
   409
        }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   410
      }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   411
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   412
22232
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   413
    if (num_defaults == 0) {
22493
af3de4cee5e5 8031059: invokestatic: ICCE trying to invoke static method when it clashes with an abstract method inherited from an interface
hseigel
parents: 22233
diff changeset
   414
      // If the root klass has a static method with matching name and signature
af3de4cee5e5 8031059: invokestatic: ICCE trying to invoke static method when it clashes with an abstract method inherited from an interface
hseigel
parents: 22233
diff changeset
   415
      // then do not generate an overpass method because it will hide the
af3de4cee5e5 8031059: invokestatic: ICCE trying to invoke static method when it clashes with an abstract method inherited from an interface
hseigel
parents: 22233
diff changeset
   416
      // static method during resolution.
23999
22eb7be3d99d 8033150: invokestatic: IncompatibleClassChangeError trying to invoke static method from a parent in presence of conflicting defaults.
lfoltan
parents: 22493
diff changeset
   417
      if (qualified_methods.length() == 0) {
22eb7be3d99d 8033150: invokestatic: IncompatibleClassChangeError trying to invoke static method from a parent in presence of conflicting defaults.
lfoltan
parents: 22493
diff changeset
   418
        _exception_message = generate_no_defaults_message(CHECK);
22eb7be3d99d 8033150: invokestatic: IncompatibleClassChangeError trying to invoke static method from a parent in presence of conflicting defaults.
lfoltan
parents: 22493
diff changeset
   419
      } else {
22eb7be3d99d 8033150: invokestatic: IncompatibleClassChangeError trying to invoke static method from a parent in presence of conflicting defaults.
lfoltan
parents: 22493
diff changeset
   420
        assert(root != NULL, "Null root class");
22eb7be3d99d 8033150: invokestatic: IncompatibleClassChangeError trying to invoke static method from a parent in presence of conflicting defaults.
lfoltan
parents: 22493
diff changeset
   421
        _exception_message = generate_method_message(root->name(), qualified_methods.at(0), CHECK);
22232
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   422
      }
23999
22eb7be3d99d 8033150: invokestatic: IncompatibleClassChangeError trying to invoke static method from a parent in presence of conflicting defaults.
lfoltan
parents: 22493
diff changeset
   423
      _exception_name = vmSymbols::java_lang_AbstractMethodError();
22493
af3de4cee5e5 8031059: invokestatic: ICCE trying to invoke static method when it clashes with an abstract method inherited from an interface
hseigel
parents: 22233
diff changeset
   424
21556
e75cd34a59e0 8027229: ICCE expected for >=2 maximally specific default methods.
acorn
parents: 21516
diff changeset
   425
    // If only one qualified method is default, select that
21516
8fa5308ab970 8027304: Lambda: inheriting abstract + 1 default -> default, not ICCE
acorn
parents: 20710
diff changeset
   426
    } else if (num_defaults == 1) {
8fa5308ab970 8027304: Lambda: inheriting abstract + 1 default -> default, not ICCE
acorn
parents: 20710
diff changeset
   427
        _selected_target = qualified_methods.at(default_index);
22493
af3de4cee5e5 8031059: invokestatic: ICCE trying to invoke static method when it clashes with an abstract method inherited from an interface
hseigel
parents: 22233
diff changeset
   428
23999
22eb7be3d99d 8033150: invokestatic: IncompatibleClassChangeError trying to invoke static method from a parent in presence of conflicting defaults.
lfoltan
parents: 22493
diff changeset
   429
    } else if (num_defaults > 1) {
22493
af3de4cee5e5 8031059: invokestatic: ICCE trying to invoke static method when it clashes with an abstract method inherited from an interface
hseigel
parents: 22233
diff changeset
   430
      _exception_message = generate_conflicts_message(&qualified_methods,CHECK);
af3de4cee5e5 8031059: invokestatic: ICCE trying to invoke static method when it clashes with an abstract method inherited from an interface
hseigel
parents: 22233
diff changeset
   431
      _exception_name = vmSymbols::java_lang_IncompatibleClassChangeError();
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   432
      LogTarget(Debug, defaultmethods) lt;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   433
      if (lt.is_enabled()) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   434
        LogStream ls(lt);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   435
        _exception_message->print_value_on(&ls);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   436
        ls.cr();
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   437
      }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   438
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   439
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   440
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   441
  void print_selected(outputStream* str, int indent) const {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   442
    assert(has_target(), "Should be called otherwise");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   443
    streamIndentor si(str, indent * 2);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   444
    str->indent().print("Selected method: ");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   445
    print_method(str, _selected_target);
19966
64732b96b5f5 8024647: Default method resolution with private superclass method
acorn
parents: 19690
diff changeset
   446
    Klass* method_holder = _selected_target->method_holder();
64732b96b5f5 8024647: Default method resolution with private superclass method
acorn
parents: 19690
diff changeset
   447
    if (!method_holder->is_interface()) {
33736
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   448
      str->print(" : in superclass");
19966
64732b96b5f5 8024647: Default method resolution with private superclass method
acorn
parents: 19690
diff changeset
   449
    }
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 23999
diff changeset
   450
    str->cr();
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   451
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   452
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   453
  void print_exception(outputStream* str, int indent) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   454
    assert(throws_exception(), "Should be called otherwise");
20284
595a25ab9474 8011311: Private interface methods. Default conflicts:ICCE. no erased_super_default.
acorn
parents: 19966
diff changeset
   455
    assert(_exception_name != NULL, "exception_name should be set");
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   456
    streamIndentor si(str, indent * 2);
20284
595a25ab9474 8011311: Private interface methods. Default conflicts:ICCE. no erased_super_default.
acorn
parents: 19966
diff changeset
   457
    str->indent().print_cr("%s: %s", _exception_name->as_C_string(), _exception_message->as_C_string());
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   458
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   459
};
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   460
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   461
Symbol* MethodFamily::generate_no_defaults_message(TRAPS) const {
54847
59ea39bb2809 8223657: Remove unused THREAD argument from SymbolTable functions
coleenp
parents: 54786
diff changeset
   462
  return SymbolTable::new_symbol("No qualifying defaults found");
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   463
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   464
22232
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   465
Symbol* MethodFamily::generate_method_message(Symbol *klass_name, Method* method, TRAPS) const {
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   466
  stringStream ss;
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   467
  ss.print("Method ");
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   468
  Symbol* name = method->name();
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   469
  Symbol* signature = method->signature();
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   470
  ss.write((const char*)klass_name->bytes(), klass_name->utf8_length());
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   471
  ss.print(".");
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   472
  ss.write((const char*)name->bytes(), name->utf8_length());
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   473
  ss.write((const char*)signature->bytes(), signature->utf8_length());
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   474
  ss.print(" is abstract");
54847
59ea39bb2809 8223657: Remove unused THREAD argument from SymbolTable functions
coleenp
parents: 54786
diff changeset
   475
  return SymbolTable::new_symbol(ss.base(), (int)ss.size());
22232
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   476
}
26acfad336c0 8027804: JCK resolveMethod test fails expecting AbstractMethodError
hseigel
parents: 21913
diff changeset
   477
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   478
Symbol* MethodFamily::generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   479
  stringStream ss;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   480
  ss.print("Conflicting default methods:");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   481
  for (int i = 0; i < methods->length(); ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   482
    Method* method = methods->at(i);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   483
    Symbol* klass = method->klass_name();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   484
    Symbol* name = method->name();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   485
    ss.print(" ");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   486
    ss.write((const char*)klass->bytes(), klass->utf8_length());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   487
    ss.print(".");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   488
    ss.write((const char*)name->bytes(), name->utf8_length());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   489
  }
54847
59ea39bb2809 8223657: Remove unused THREAD argument from SymbolTable functions
coleenp
parents: 54786
diff changeset
   490
  return SymbolTable::new_symbol(ss.base(), (int)ss.size());
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   491
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   492
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   493
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   494
class StateRestorer;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   495
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   496
// StatefulMethodFamily is a wrapper around a MethodFamily that maintains the
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   497
// qualification state during hierarchy visitation, and applies that state
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   498
// when adding members to the MethodFamily
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   499
class StatefulMethodFamily : public ResourceObj {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   500
  friend class StateRestorer;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   501
 private:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   502
  QualifiedState _qualification_state;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   503
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   504
  void set_qualification_state(QualifiedState state) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   505
    _qualification_state = state;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   506
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   507
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   508
 protected:
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   509
  MethodFamily _method_family;
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   510
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   511
 public:
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   512
  StatefulMethodFamily() {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   513
   _qualification_state = QUALIFIED;
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   514
  }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   515
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   516
  void set_target_if_empty(Method* m) { _method_family.set_target_if_empty(m); }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   517
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   518
  MethodFamily* get_method_family() { return &_method_family; }
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   519
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   520
  StateRestorer* record_method_and_dq_further(Method* mo);
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   521
};
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   522
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   523
class StateRestorer : public PseudoScopeMark {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   524
 private:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   525
  StatefulMethodFamily* _method;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   526
  QualifiedState _state_to_restore;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   527
 public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   528
  StateRestorer(StatefulMethodFamily* dm, QualifiedState state)
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   529
      : _method(dm), _state_to_restore(state) {}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   530
  ~StateRestorer() { destroy(); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   531
  void restore_state() { _method->set_qualification_state(_state_to_restore); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   532
  virtual void destroy() { restore_state(); }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   533
};
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   534
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   535
StateRestorer* StatefulMethodFamily::record_method_and_dq_further(Method* mo) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   536
  StateRestorer* mark = new StateRestorer(this, _qualification_state);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   537
  if (_qualification_state == QUALIFIED) {
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   538
    _method_family.record_qualified_method(mo);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   539
  } else {
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   540
    _method_family.record_disqualified_method(mo);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   541
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   542
  // Everything found "above"??? this method in the hierarchy walk is set to
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   543
  // disqualified
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   544
  set_qualification_state(DISQUALIFIED);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   545
  return mark;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   546
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   547
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   548
// Represents a location corresponding to a vtable slot for methods that
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   549
// neither the class nor any of it's ancestors provide an implementaion.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   550
// Default methods may be present to fill this slot.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   551
class EmptyVtableSlot : public ResourceObj {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   552
 private:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   553
  Symbol* _name;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   554
  Symbol* _signature;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   555
  int _size_of_parameters;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   556
  MethodFamily* _binding;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   557
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   558
 public:
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   559
  EmptyVtableSlot(Method* method)
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   560
      : _name(method->name()), _signature(method->signature()),
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   561
        _size_of_parameters(method->size_of_parameters()), _binding(NULL) {}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   562
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   563
  Symbol* name() const { return _name; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   564
  Symbol* signature() const { return _signature; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   565
  int size_of_parameters() const { return _size_of_parameters; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   566
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   567
  void bind_family(MethodFamily* lm) { _binding = lm; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   568
  bool is_bound() { return _binding != NULL; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   569
  MethodFamily* get_binding() { return _binding; }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   570
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   571
  void print_on(outputStream* str) const {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   572
    print_slot(str, name(), signature());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   573
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   574
};
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   575
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   576
static bool already_in_vtable_slots(GrowableArray<EmptyVtableSlot*>* slots, Method* m) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   577
  bool found = false;
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   578
  for (int j = 0; j < slots->length(); ++j) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   579
    if (slots->at(j)->name() == m->name() &&
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   580
        slots->at(j)->signature() == m->signature() ) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   581
      found = true;
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   582
      break;
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   583
    }
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   584
  }
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   585
  return found;
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   586
}
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   587
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   588
static void find_empty_vtable_slots(GrowableArray<EmptyVtableSlot*>* slots,
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 33736
diff changeset
   589
    InstanceKlass* klass, const GrowableArray<Method*>* mirandas, TRAPS) {
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   590
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   591
  assert(klass != NULL, "Must be valid class");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   592
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   593
  // All miranda methods are obvious candidates
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   594
  for (int i = 0; i < mirandas->length(); ++i) {
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   595
    Method* m = mirandas->at(i);
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   596
    if (!already_in_vtable_slots(slots, m)) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   597
      slots->append(new EmptyVtableSlot(m));
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   598
    }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   599
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   600
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   601
  // Also any overpasses in our superclasses, that we haven't implemented.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   602
  // (can't use the vtable because it is not guaranteed to be initialized yet)
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   603
  InstanceKlass* super = klass->java_super();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   604
  while (super != NULL) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   605
    for (int i = 0; i < super->methods()->length(); ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   606
      Method* m = super->methods()->at(i);
21913
0e2fd7282ac6 8028438: static superclass method masks default methods
acorn
parents: 21556
diff changeset
   607
      if (m->is_overpass() || m->is_static()) {
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   608
        // m is a method that would have been a miranda if not for the
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   609
        // default method processing that occurred on behalf of our superclass,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   610
        // so it's a method we want to re-examine in this new context.  That is,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   611
        // unless we have a real implementation of it in the current class.
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   612
        if (!already_in_vtable_slots(slots, m)) {
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   613
          Method *impl = klass->lookup_method(m->name(), m->signature());
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   614
          if (impl == NULL || impl->is_overpass() || impl->is_static()) {
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   615
            slots->append(new EmptyVtableSlot(m));
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   616
          }
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   617
        }
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   618
      }
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   619
    }
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   620
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   621
    // also any default methods in our superclasses
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   622
    if (super->default_methods() != NULL) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   623
      for (int i = 0; i < super->default_methods()->length(); ++i) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   624
        Method* m = super->default_methods()->at(i);
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   625
        // m is a method that would have been a miranda if not for the
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   626
        // default method processing that occurred on behalf of our superclass,
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   627
        // so it's a method we want to re-examine in this new context.  That is,
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   628
        // unless we have a real implementation of it in the current class.
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   629
        if (!already_in_vtable_slots(slots, m)) {
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   630
          Method* impl = klass->lookup_method(m->name(), m->signature());
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   631
          if (impl == NULL || impl->is_overpass() || impl->is_static()) {
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   632
            slots->append(new EmptyVtableSlot(m));
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   633
          }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   634
        }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   635
      }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   636
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   637
    super = super->java_super();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   638
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   639
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   640
  LogTarget(Debug, defaultmethods) lt;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   641
  if (lt.is_enabled()) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   642
    lt.print("Slots that need filling:");
33736
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   643
    ResourceMark rm;
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   644
    LogStream ls(lt);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   645
    streamIndentor si(&ls);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   646
    for (int i = 0; i < slots->length(); ++i) {
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   647
      ls.indent();
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   648
      slots->at(i)->print_on(&ls);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   649
      ls.cr();
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   650
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   651
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   652
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   653
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   654
// Iterates over the superinterface type hierarchy looking for all methods
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   655
// with a specific erased signature.
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   656
class FindMethodsByErasedSig : public HierarchyVisitor<FindMethodsByErasedSig> {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   657
 private:
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   658
  // Context data
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   659
  Symbol* _method_name;
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   660
  Symbol* _method_signature;
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   661
  StatefulMethodFamily*  _family;
48463
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   662
  bool _cur_class_is_interface;
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   663
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   664
 public:
48463
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   665
  FindMethodsByErasedSig(Symbol* name, Symbol* signature, bool is_interf) :
51334
cc2c79d22508 8208671: Runtime, JFR, Serviceability changes to allow enabling -Wreorder
tschatzl
parents: 49677
diff changeset
   666
      _method_name(name), _method_signature(signature), _family(NULL),
cc2c79d22508 8208671: Runtime, JFR, Serviceability changes to allow enabling -Wreorder
tschatzl
parents: 49677
diff changeset
   667
      _cur_class_is_interface(is_interf) {}
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   668
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   669
  void get_discovered_family(MethodFamily** family) {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   670
      if (_family != NULL) {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   671
        *family = _family->get_method_family();
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   672
      } else {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   673
        *family = NULL;
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   674
      }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   675
  }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   676
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   677
  void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   678
  void free_node_data(void* node_data) {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   679
    PseudoScope::cast(node_data)->destroy();
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   680
  }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   681
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   682
  // Find all methods on this hierarchy that match this
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   683
  // method's erased (name, signature)
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   684
  bool visit() {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   685
    PseudoScope* scope = PseudoScope::cast(current_data());
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   686
    InstanceKlass* iklass = current_class();
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   687
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   688
    Method* m = iklass->find_method(_method_name, _method_signature);
48463
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   689
    // Private interface methods are not candidates for default methods.
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   690
    // invokespecial to private interface methods doesn't use default method logic.
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   691
    // Private class methods are not candidates for default methods.
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   692
    // Private methods do not override default methods, so need to perform
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   693
    // default method inheritance without including private methods.
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   694
    // The overpasses are your supertypes' errors, we do not include them.
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   695
    // Non-public methods in java.lang.Object are not candidates for default
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   696
    // methods.
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   697
    // Future: take access controls into account for superclass methods
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   698
    if (m != NULL && !m->is_static() && !m->is_overpass() && !m->is_private() &&
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   699
     (!_cur_class_is_interface || !SystemDictionary::is_nonpublic_Object_method(m))) {
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   700
      if (_family == NULL) {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   701
        _family = new StatefulMethodFamily();
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   702
      }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   703
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   704
      if (iklass->is_interface()) {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   705
        StateRestorer* restorer = _family->record_method_and_dq_further(m);
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   706
        scope->add_mark(restorer);
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   707
      } else {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   708
        // This is the rule that methods in classes "win" (bad word) over
48463
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   709
        // methods in interfaces. This works because of single inheritance.
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   710
        // Private methods in classes do not "win", they will be found
29871
77e7fe063568 8065366: Better private method resolution
acorn
parents: 29316
diff changeset
   711
        // first on searching, but overriding for invokevirtual needs
77e7fe063568 8065366: Better private method resolution
acorn
parents: 29316
diff changeset
   712
        // to find default method candidates for the same signature
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   713
        _family->set_target_if_empty(m);
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   714
      }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   715
    }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   716
    return true;
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   717
  }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   718
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   719
};
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   720
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   721
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   722
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   723
static void create_defaults_and_exceptions(
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   724
    GrowableArray<EmptyVtableSlot*>* slots, InstanceKlass* klass, TRAPS);
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   725
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   726
static void generate_erased_defaults(
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   727
     InstanceKlass* klass, EmptyVtableSlot* slot, bool is_intf, TRAPS) {
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   728
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   729
  // sets up a set of methods with the same exact erased signature
48463
474cec233fb2 8154587: Resolution fails for default method named 'clone'
hseigel
parents: 47554
diff changeset
   730
  FindMethodsByErasedSig visitor(slot->name(), slot->signature(), is_intf);
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   731
  visitor.run(klass);
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   732
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   733
  MethodFamily* family;
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   734
  visitor.get_discovered_family(&family);
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   735
  if (family != NULL) {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   736
    family->determine_target(klass, CHECK);
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   737
    slot->bind_family(family);
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   738
  }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   739
}
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   740
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   741
static void merge_in_new_methods(InstanceKlass* klass,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   742
    GrowableArray<Method*>* new_methods, TRAPS);
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   743
static void create_default_methods( InstanceKlass* klass,
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   744
    GrowableArray<Method*>* new_methods, TRAPS);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   745
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   746
// This is the guts of the default methods implementation.  This is called just
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   747
// after the classfile has been parsed if some ancestor has default methods.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   748
//
47554
bc112140e089 8186092: Unnecessary loader constraints produced when there are multiple defaults
hseigel
parents: 47216
diff changeset
   749
// First it finds any name/signature slots that need any implementation (either
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   750
// because they are miranda or a superclass's implementation is an overpass
19681
1b35da7b1d85 8012294: remove generic handling for default methods
acorn
parents: 18695
diff changeset
   751
// itself).  For each slot, iterate over the hierarchy, to see if they contain a
1b35da7b1d85 8012294: remove generic handling for default methods
acorn
parents: 18695
diff changeset
   752
// signature that matches the slot we are looking at.
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   753
//
47554
bc112140e089 8186092: Unnecessary loader constraints produced when there are multiple defaults
hseigel
parents: 47216
diff changeset
   754
// For each slot filled, we either record the default method candidate in the
bc112140e089 8186092: Unnecessary loader constraints produced when there are multiple defaults
hseigel
parents: 47216
diff changeset
   755
// klass default_methods list or, only to handle exception cases, we create an
bc112140e089 8186092: Unnecessary loader constraints produced when there are multiple defaults
hseigel
parents: 47216
diff changeset
   756
// overpass method that throws an exception and add it to the klass methods list.
19681
1b35da7b1d85 8012294: remove generic handling for default methods
acorn
parents: 18695
diff changeset
   757
// The JVM does not create bridges nor handle generic signatures here.
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   758
void DefaultMethods::generate_default_methods(
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 33736
diff changeset
   759
    InstanceKlass* klass, const GrowableArray<Method*>* mirandas, TRAPS) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 33736
diff changeset
   760
  assert(klass != NULL, "invariant");
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   761
  assert(klass != SystemDictionary::Object_klass(), "Shouldn't be called for Object");
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   762
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   763
  // This resource mark is the bound for all memory allocation that takes
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   764
  // place during default method processing.  After this goes out of scope,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   765
  // all (Resource) objects' memory will be reclaimed.  Be careful if adding an
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   766
  // embedded resource mark under here as that memory can't be used outside
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   767
  // whatever scope it's in.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   768
  ResourceMark rm(THREAD);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   769
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   770
  // Keep entire hierarchy alive for the duration of the computation
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 33736
diff changeset
   771
  constantPoolHandle cp(THREAD, klass->constants());
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   772
  KeepAliveRegistrar keepAlive(THREAD);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   773
  KeepAliveVisitor loadKeepAlive(&keepAlive);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   774
  loadKeepAlive.run(klass);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   775
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   776
  LogTarget(Debug, defaultmethods) lt;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   777
  if (lt.is_enabled()) {
33736
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   778
    ResourceMark rm;
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   779
    lt.print("%s %s requires default method processing",
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   780
             klass->is_interface() ? "Interface" : "Class",
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   781
             klass->name()->as_klass_external_name());
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   782
    LogStream ls(lt);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   783
    PrintHierarchy printer(&ls);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   784
    printer.run(klass);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   785
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   786
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   787
  GrowableArray<EmptyVtableSlot*> empty_slots;
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   788
  find_empty_vtable_slots(&empty_slots, klass, mirandas, CHECK);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   789
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   790
  if (empty_slots.length() > 0) {
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   791
    for (int i = 0; i < empty_slots.length(); ++i) {
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   792
      EmptyVtableSlot* slot = empty_slots.at(i);
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   793
      LogTarget(Debug, defaultmethods) lt;
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   794
      if (lt.is_enabled()) {
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   795
        LogStream ls(lt);
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   796
        streamIndentor si(&ls, 2);
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   797
        ls.indent().print("Looking for default methods for slot ");
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   798
        slot->print_on(&ls);
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   799
        ls.cr();
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   800
      }
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   801
      generate_erased_defaults(klass, slot, klass->is_interface(), CHECK);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   802
    }
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   803
    log_debug(defaultmethods)("Creating defaults and overpasses...");
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   804
    create_defaults_and_exceptions(&empty_slots, klass, CHECK);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   805
  }
33736
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   806
  log_debug(defaultmethods)("Default method processing complete");
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   807
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   808
20284
595a25ab9474 8011311: Private interface methods. Default conflicts:ICCE. no erased_super_default.
acorn
parents: 19966
diff changeset
   809
static int assemble_method_error(
595a25ab9474 8011311: Private interface methods. Default conflicts:ICCE. no erased_super_default.
acorn
parents: 19966
diff changeset
   810
    BytecodeConstantPool* cp, BytecodeBuffer* buffer, Symbol* errorName, Symbol* message, TRAPS) {
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   811
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   812
  Symbol* init = vmSymbols::object_initializer_name();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   813
  Symbol* sig = vmSymbols::string_void_signature();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   814
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   815
  BytecodeAssembler assem(buffer, cp);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   816
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   817
  assem._new(errorName);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   818
  assem.dup();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   819
  assem.load_string(message);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   820
  assem.invokespecial(errorName, init, sig);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   821
  assem.athrow();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   822
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   823
  return 3; // max stack size: [ exception, exception, string ]
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   824
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   825
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   826
static Method* new_method(
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   827
    BytecodeConstantPool* cp, BytecodeBuffer* bytecodes, Symbol* name,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   828
    Symbol* sig, AccessFlags flags, int max_stack, int params,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   829
    ConstMethod::MethodType mt, TRAPS) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   830
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   831
  address code_start = 0;
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   832
  int code_length = 0;
15601
df8faef6efaf 8007320: NPG: move method annotations
coleenp
parents: 15219
diff changeset
   833
  InlineTableSizes sizes;
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   834
18695
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   835
  if (bytecodes != NULL && bytecodes->length() > 0) {
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   836
    code_start = static_cast<address>(bytecodes->adr_at(0));
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   837
    code_length = bytecodes->length();
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   838
  }
be902722fe0a 8013635: VM should no longer create bridges for generic signatures.
acorn
parents: 17859
diff changeset
   839
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   840
  Method* m = Method::allocate(cp->pool_holder()->class_loader_data(),
15601
df8faef6efaf 8007320: NPG: move method annotations
coleenp
parents: 15219
diff changeset
   841
                               code_length, flags, &sizes,
15102
0a86564e5f61 8004728: Add hotspot support for parameter reflection
coleenp
parents: 14586
diff changeset
   842
                               mt, CHECK_NULL);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   843
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   844
  m->set_constants(NULL); // This will get filled in later
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   845
  m->set_name_index(cp->utf8(name));
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   846
  m->set_signature_index(cp->utf8(sig));
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   847
  ResultTypeFinder rtf(sig);
37480
291ee208fb72 8132051: Better byte behavior
coleenp
parents: 37242
diff changeset
   848
  m->constMethod()->set_result_type(rtf.type());
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   849
  m->set_size_of_parameters(params);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   850
  m->set_max_stack(max_stack);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   851
  m->set_max_locals(params);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   852
  m->constMethod()->set_stackmap_data(NULL);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   853
  m->set_code(code_start);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   854
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   855
  return m;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   856
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   857
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   858
static void switchover_constant_pool(BytecodeConstantPool* bpool,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   859
    InstanceKlass* klass, GrowableArray<Method*>* new_methods, TRAPS) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   860
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   861
  if (new_methods->length() > 0) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   862
    ConstantPool* cp = bpool->create_constant_pool(CHECK);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   863
    if (cp != klass->constants()) {
49677
a1a7456dd8b9 8200261: Regression with JVM anonymous class
hseigel
parents: 48463
diff changeset
   864
      // Copy resolved anonymous class into new constant pool.
51444
3e5d28e6de32 8209301: JVM rename is_anonymous, host_klass to unsafe specific terminology ahead of Unsafe.defineAnonymousClass deprecation
lfoltan
parents: 51334
diff changeset
   865
      if (klass->is_unsafe_anonymous()) {
49677
a1a7456dd8b9 8200261: Regression with JVM anonymous class
hseigel
parents: 48463
diff changeset
   866
        cp->klass_at_put(klass->this_class_index(), klass);
a1a7456dd8b9 8200261: Regression with JVM anonymous class
hseigel
parents: 48463
diff changeset
   867
      }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   868
      klass->class_loader_data()->add_to_deallocate_list(klass->constants());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   869
      klass->set_constants(cp);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   870
      cp->set_pool_holder(klass);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   871
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   872
      for (int i = 0; i < new_methods->length(); ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   873
        new_methods->at(i)->set_constants(cp);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   874
      }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   875
      for (int i = 0; i < klass->methods()->length(); ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   876
        Method* mo = klass->methods()->at(i);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   877
        mo->set_constants(cp);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   878
      }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   879
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   880
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   881
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   882
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   883
// Create default_methods list for the current class.
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   884
// With the VM only processing erased signatures, the VM only
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   885
// creates an overpass in a conflict case or a case with no candidates.
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   886
// This allows virtual methods to override the overpass, but ensures
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   887
// that a local method search will find the exception rather than an abstract
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   888
// or default method that is not a valid candidate.
47554
bc112140e089 8186092: Unnecessary loader constraints produced when there are multiple defaults
hseigel
parents: 47216
diff changeset
   889
//
bc112140e089 8186092: Unnecessary loader constraints produced when there are multiple defaults
hseigel
parents: 47216
diff changeset
   890
// Note that if overpass method are ever created that are not exception
bc112140e089 8186092: Unnecessary loader constraints produced when there are multiple defaults
hseigel
parents: 47216
diff changeset
   891
// throwing methods then the loader constraint checking logic for vtable and
bc112140e089 8186092: Unnecessary loader constraints produced when there are multiple defaults
hseigel
parents: 47216
diff changeset
   892
// itable creation needs to be changed to check loader constraints for the
bc112140e089 8186092: Unnecessary loader constraints produced when there are multiple defaults
hseigel
parents: 47216
diff changeset
   893
// overpass methods that do not throw exceptions.
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   894
static void create_defaults_and_exceptions(GrowableArray<EmptyVtableSlot*>* slots,
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   895
    InstanceKlass* klass, TRAPS) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   896
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   897
  GrowableArray<Method*> overpasses;
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   898
  GrowableArray<Method*> defaults;
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   899
  BytecodeConstantPool bpool(klass->constants());
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   900
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   901
  for (int i = 0; i < slots->length(); ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   902
    EmptyVtableSlot* slot = slots->at(i);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   903
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   904
    if (slot->is_bound()) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   905
      MethodFamily* method = slot->get_binding();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   906
      BytecodeBuffer buffer;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   907
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   908
      LogTarget(Debug, defaultmethods) lt;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   909
      if (lt.is_enabled()) {
41669
2091069b6851 8081800: AbstractMethodError when evaluating a private method in an interface via debugger
dholmes
parents: 37480
diff changeset
   910
        ResourceMark rm(THREAD);
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   911
        LogStream ls(lt);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   912
        ls.print("for slot: ");
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   913
        slot->print_on(&ls);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   914
        ls.cr();
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   915
        if (method->has_target()) {
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   916
          method->print_selected(&ls, 1);
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   917
        } else if (method->throws_exception()) {
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 42059
diff changeset
   918
          method->print_exception(&ls, 1);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   919
        }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   920
      }
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   921
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   922
      if (method->has_target()) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   923
        Method* selected = method->get_selected_target();
19966
64732b96b5f5 8024647: Default method resolution with private superclass method
acorn
parents: 19690
diff changeset
   924
        if (selected->method_holder()->is_interface()) {
41669
2091069b6851 8081800: AbstractMethodError when evaluating a private method in an interface via debugger
dholmes
parents: 37480
diff changeset
   925
          assert(!selected->is_private(), "pushing private interface method as default");
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   926
          defaults.push(selected);
19966
64732b96b5f5 8024647: Default method resolution with private superclass method
acorn
parents: 19690
diff changeset
   927
        }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   928
      } else if (method->throws_exception()) {
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   929
        int max_stack = assemble_method_error(&bpool, &buffer,
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   930
           method->get_exception_name(), method->get_exception_message(), CHECK);
19966
64732b96b5f5 8024647: Default method resolution with private superclass method
acorn
parents: 19690
diff changeset
   931
        AccessFlags flags = accessFlags_from(
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   932
          JVM_ACC_PUBLIC | JVM_ACC_SYNTHETIC | JVM_ACC_BRIDGE);
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   933
         Method* m = new_method(&bpool, &buffer, slot->name(), slot->signature(),
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   934
          flags, max_stack, slot->size_of_parameters(),
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   935
          ConstMethod::OVERPASS, CHECK);
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   936
        // We push to the methods list:
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   937
        // overpass methods which are exception throwing methods
19966
64732b96b5f5 8024647: Default method resolution with private superclass method
acorn
parents: 19690
diff changeset
   938
        if (m != NULL) {
64732b96b5f5 8024647: Default method resolution with private superclass method
acorn
parents: 19690
diff changeset
   939
          overpasses.push(m);
64732b96b5f5 8024647: Default method resolution with private superclass method
acorn
parents: 19690
diff changeset
   940
        }
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   941
      }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   942
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   943
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   944
33736
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   945
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   946
  log_debug(defaultmethods)("Created %d overpass methods", overpasses.length());
1b3950243443 8139564: Convert TraceDefaultMethods to Unified Logging
rprotacio
parents: 29871
diff changeset
   947
  log_debug(defaultmethods)("Created %d default  methods", defaults.length());
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   948
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   949
  if (overpasses.length() > 0) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   950
    switchover_constant_pool(&bpool, klass, &overpasses, CHECK);
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   951
    merge_in_new_methods(klass, &overpasses, CHECK);
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   952
  }
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   953
  if (defaults.length() > 0) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   954
    create_default_methods(klass, &defaults, CHECK);
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   955
  }
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   956
}
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   957
54013
c5cf23055fcb 8219713: Reduce work in DefaultMethods::generate_default_methods
redestad
parents: 53432
diff changeset
   958
static void create_default_methods(InstanceKlass* klass,
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   959
    GrowableArray<Method*>* new_methods, TRAPS) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   960
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   961
  int new_size = new_methods->length();
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   962
  Array<Method*>* total_default_methods = MetadataFactory::new_array<Method*>(
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   963
      klass->class_loader_data(), new_size, NULL, CHECK);
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   964
  for (int index = 0; index < new_size; index++ ) {
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   965
    total_default_methods->at_put(index, new_methods->at(index));
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   966
  }
53432
1ec56532ae0c 8217424: Remove the idempotent parameter to Method::sort_methods
iklam
parents: 51444
diff changeset
   967
  Method::sort_methods(total_default_methods, /*set_idnums=*/false);
20391
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   968
7b146c5ebb18 8009130: Lambda: Fix access controls, loader constraints.
acorn
parents: 20300
diff changeset
   969
  klass->set_default_methods(total_default_methods);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   970
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   971
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   972
static void sort_methods(GrowableArray<Method*>* methods) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   973
  // Note that this must sort using the same key as is used for sorting
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   974
  // methods in InstanceKlass.
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   975
  bool sorted = true;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   976
  for (int i = methods->length() - 1; i > 0; --i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   977
    for (int j = 0; j < i; ++j) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   978
      Method* m1 = methods->at(j);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   979
      Method* m2 = methods->at(j + 1);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   980
      if ((uintptr_t)m1->name() > (uintptr_t)m2->name()) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   981
        methods->at_put(j, m2);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   982
        methods->at_put(j + 1, m1);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   983
        sorted = false;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   984
      }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   985
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   986
    if (sorted) break;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   987
    sorted = true;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   988
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   989
#ifdef ASSERT
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   990
  uintptr_t prev = 0;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   991
  for (int i = 0; i < methods->length(); ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   992
    Method* mh = methods->at(i);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   993
    uintptr_t nv = (uintptr_t)mh->name();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   994
    assert(nv >= prev, "Incorrect overpass method ordering");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   995
    prev = nv;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   996
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   997
#endif
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   998
}
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
   999
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1000
static void merge_in_new_methods(InstanceKlass* klass,
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1001
    GrowableArray<Method*>* new_methods, TRAPS) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1002
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1003
  enum { ANNOTATIONS, PARAMETERS, DEFAULTS, NUM_ARRAYS };
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1004
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1005
  Array<Method*>* original_methods = klass->methods();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1006
  Array<int>* original_ordering = klass->method_ordering();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1007
  Array<int>* merged_ordering = Universe::the_empty_int_array();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1008
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1009
  int new_size = klass->methods()->length() + new_methods->length();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1010
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1011
  Array<Method*>* merged_methods = MetadataFactory::new_array<Method*>(
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1012
      klass->class_loader_data(), new_size, NULL, CHECK);
15601
df8faef6efaf 8007320: NPG: move method annotations
coleenp
parents: 15219
diff changeset
  1013
22233
f0028de67b30 8030633: nsk/jvmti/RedefineClasses/StressRedefine failed invalid method ordering length on Solaris
coleenp
parents: 22232
diff changeset
  1014
  // original_ordering might be empty if this class has no methods of its own
58689
72e605a88500 8232030: HelloDynamic.java fails with latest Graal
ccheung
parents: 54847
diff changeset
  1015
  if (JvmtiExport::can_maintain_original_method_order() || Arguments::is_dumping_archive()) {
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1016
    merged_ordering = MetadataFactory::new_array<int>(
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1017
        klass->class_loader_data(), new_size, CHECK);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1018
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1019
  int method_order_index = klass->methods()->length();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1020
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1021
  sort_methods(new_methods);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1022
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1023
  // Perform grand merge of existing methods and new methods
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1024
  int orig_idx = 0;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1025
  int new_idx = 0;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1026
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1027
  for (int i = 0; i < new_size; ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1028
    Method* orig_method = NULL;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1029
    Method* new_method = NULL;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1030
    if (orig_idx < original_methods->length()) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1031
      orig_method = original_methods->at(orig_idx);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1032
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1033
    if (new_idx < new_methods->length()) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1034
      new_method = new_methods->at(new_idx);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1035
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1036
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1037
    if (orig_method != NULL &&
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1038
        (new_method == NULL || orig_method->name() < new_method->name())) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1039
      merged_methods->at_put(i, orig_method);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1040
      original_methods->at_put(orig_idx, NULL);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1041
      if (merged_ordering->length() > 0) {
22233
f0028de67b30 8030633: nsk/jvmti/RedefineClasses/StressRedefine failed invalid method ordering length on Solaris
coleenp
parents: 22232
diff changeset
  1042
        assert(original_ordering != NULL && original_ordering->length() > 0,
f0028de67b30 8030633: nsk/jvmti/RedefineClasses/StressRedefine failed invalid method ordering length on Solaris
coleenp
parents: 22232
diff changeset
  1043
               "should have original order information for this method");
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1044
        merged_ordering->at_put(i, original_ordering->at(orig_idx));
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1045
      }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1046
      ++orig_idx;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1047
    } else {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1048
      merged_methods->at_put(i, new_method);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1049
      if (merged_ordering->length() > 0) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1050
        merged_ordering->at_put(i, method_order_index++);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1051
      }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1052
      ++new_idx;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1053
    }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1054
    // update idnum for new location
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1055
    merged_methods->at(i)->set_method_idnum(i);
29316
5287df8a8972 8046246: the constantPoolCacheOopDesc::adjust_method_entries() used in RedefineClasses does not scale
sspitsyn
parents: 27680
diff changeset
  1056
    merged_methods->at(i)->set_orig_method_idnum(i);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1057
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1058
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1059
  // Verify correct order
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1060
#ifdef ASSERT
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1061
  uintptr_t prev = 0;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1062
  for (int i = 0; i < merged_methods->length(); ++i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1063
    Method* mo = merged_methods->at(i);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1064
    uintptr_t nv = (uintptr_t)mo->name();
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1065
    assert(nv >= prev, "Incorrect method ordering");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1066
    prev = nv;
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1067
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1068
#endif
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1069
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1070
  // Replace klass methods with new merged lists
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1071
  klass->set_methods(merged_methods);
17859
cda7f55ca4dc 8015436: compiler/ciReplay/TestSA.sh fails with assert() index is out of bounds
sspitsyn
parents: 16379
diff changeset
  1072
  klass->set_initial_method_idnum(new_size);
22233
f0028de67b30 8030633: nsk/jvmti/RedefineClasses/StressRedefine failed invalid method ordering length on Solaris
coleenp
parents: 22232
diff changeset
  1073
  klass->set_method_ordering(merged_ordering);
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1074
22233
f0028de67b30 8030633: nsk/jvmti/RedefineClasses/StressRedefine failed invalid method ordering length on Solaris
coleenp
parents: 22232
diff changeset
  1075
  // Free metadata
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1076
  ClassLoaderData* cld = klass->class_loader_data();
22233
f0028de67b30 8030633: nsk/jvmti/RedefineClasses/StressRedefine failed invalid method ordering length on Solaris
coleenp
parents: 22232
diff changeset
  1077
  if (original_methods->length() > 0) {
21556
e75cd34a59e0 8027229: ICCE expected for >=2 maximally specific default methods.
acorn
parents: 21516
diff changeset
  1078
    MetadataFactory::free_array(cld, original_methods);
e75cd34a59e0 8027229: ICCE expected for >=2 maximally specific default methods.
acorn
parents: 21516
diff changeset
  1079
  }
22233
f0028de67b30 8030633: nsk/jvmti/RedefineClasses/StressRedefine failed invalid method ordering length on Solaris
coleenp
parents: 22232
diff changeset
  1080
  if (original_ordering != NULL && original_ordering->length() > 0) {
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1081
    MetadataFactory::free_array(cld, original_ordering);
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1082
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents:
diff changeset
  1083
}