src/share/vm/oops/instanceKlass.cpp

changeset 8982
8f1acbb637e3
parent 8951
0612a789929b
parent 8721
575f637864df
child 9041
95a08233f46c
child 9183
f95c67788f18
child 9291
a2c8195708cc
     1.1 --- a/src/share/vm/oops/instanceKlass.cpp	Mon Jun 12 13:58:09 2017 -0400
     1.2 +++ b/src/share/vm/oops/instanceKlass.cpp	Fri Jun 30 23:45:31 2017 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -616,7 +616,11 @@
    1.11  
    1.12  bool InstanceKlass::link_class_impl(
    1.13      instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
    1.14 -  // check for error state
    1.15 +  // check for error state.
    1.16 +  // This is checking for the wrong state.  If the state is initialization_error,
    1.17 +  // then this class *was* linked.  The CDS code does a try_link_class and uses
    1.18 +  // initialization_error to mark classes to not include in the archive during
    1.19 +  // DumpSharedSpaces.  This should be removed when the CDS bug is fixed.
    1.20    if (this_oop->is_in_error_state()) {
    1.21      ResourceMark rm(THREAD);
    1.22      THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(),
    1.23 @@ -801,37 +805,22 @@
    1.24  }
    1.25  
    1.26  // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
    1.27 -void InstanceKlass::initialize_super_interfaces(instanceKlassHandle this_oop, TRAPS) {
    1.28 -  if (this_oop->has_default_methods()) {
    1.29 -    for (int i = 0; i < this_oop->local_interfaces()->length(); ++i) {
    1.30 -      Klass* iface = this_oop->local_interfaces()->at(i);
    1.31 -      InstanceKlass* ik = InstanceKlass::cast(iface);
    1.32 -      if (ik->should_be_initialized()) {
    1.33 -        if (ik->has_default_methods()) {
    1.34 -          ik->initialize_super_interfaces(ik, THREAD);
    1.35 -        }
    1.36 -        // Only initialize() interfaces that "declare" concrete methods.
    1.37 -        // has_default_methods drives searching superinterfaces since it
    1.38 -        // means has_default_methods in its superinterface hierarchy
    1.39 -        if (!HAS_PENDING_EXCEPTION && ik->declares_default_methods()) {
    1.40 -          ik->initialize(THREAD);
    1.41 -        }
    1.42 -        if (HAS_PENDING_EXCEPTION) {
    1.43 -          Handle e(THREAD, PENDING_EXCEPTION);
    1.44 -          CLEAR_PENDING_EXCEPTION;
    1.45 -          {
    1.46 -            EXCEPTION_MARK;
    1.47 -            // Locks object, set state, and notify all waiting threads
    1.48 -            this_oop->set_initialization_state_and_notify(
    1.49 -                initialization_error, THREAD);
    1.50 -
    1.51 -            // ignore any exception thrown, superclass initialization error is
    1.52 -            // thrown below
    1.53 -            CLEAR_PENDING_EXCEPTION;
    1.54 -          }
    1.55 -          THROW_OOP(e());
    1.56 -        }
    1.57 -      }
    1.58 +void InstanceKlass::initialize_super_interfaces(instanceKlassHandle this_k, TRAPS) {
    1.59 +  assert (this_k->has_default_methods(), "caller should have checked this");
    1.60 +  for (int i = 0; i < this_k->local_interfaces()->length(); ++i) {
    1.61 +    Klass* iface = this_k->local_interfaces()->at(i);
    1.62 +    InstanceKlass* ik = InstanceKlass::cast(iface);
    1.63 +
    1.64 +    // Initialization is depth first search ie. we start with top of the inheritance tree
    1.65 +    // has_default_methods drives searching superinterfaces since it
    1.66 +    // means has_default_methods in its superinterface hierarchy
    1.67 +    if (ik->has_default_methods()) {
    1.68 +      ik->initialize_super_interfaces(ik, CHECK);
    1.69 +    }
    1.70 +
    1.71 +    // Only initialize() interfaces that "declare" concrete methods.
    1.72 +    if (ik->should_be_initialized() && ik->declares_default_methods()) {
    1.73 +      ik->initialize(CHECK);
    1.74      }
    1.75    }
    1.76  }
    1.77 @@ -897,30 +886,36 @@
    1.78    }
    1.79  
    1.80    // Step 7
    1.81 -  Klass* super_klass = this_oop->super();
    1.82 -  if (super_klass != NULL && !this_oop->is_interface() && super_klass->should_be_initialized()) {
    1.83 -    super_klass->initialize(THREAD);
    1.84 -
    1.85 +  // Next, if C is a class rather than an interface, initialize its super class and super
    1.86 +  // interfaces.
    1.87 +  if (!this_oop->is_interface()) {
    1.88 +    Klass* super_klass = this_oop->super();
    1.89 +    if (super_klass != NULL && super_klass->should_be_initialized()) {
    1.90 +      super_klass->initialize(THREAD);
    1.91 +    }
    1.92 +    // If C implements any interfaces that declares a non-abstract, non-static method,
    1.93 +    // the initialization of C triggers initialization of its super interfaces.
    1.94 +    // Only need to recurse if has_default_methods which includes declaring and
    1.95 +    // inheriting default methods
    1.96 +    if (!HAS_PENDING_EXCEPTION && this_oop->has_default_methods()) {
    1.97 +      this_oop->initialize_super_interfaces(this_oop, THREAD);
    1.98 +    }
    1.99 +
   1.100 +    // If any exceptions, complete abruptly, throwing the same exception as above.
   1.101      if (HAS_PENDING_EXCEPTION) {
   1.102        Handle e(THREAD, PENDING_EXCEPTION);
   1.103        CLEAR_PENDING_EXCEPTION;
   1.104        {
   1.105          EXCEPTION_MARK;
   1.106 -        this_oop->set_initialization_state_and_notify(initialization_error, THREAD); // Locks object, set state, and notify all waiting threads
   1.107 -        CLEAR_PENDING_EXCEPTION;   // ignore any exception thrown, superclass initialization error is thrown below
   1.108 +        // Locks object, set state, and notify all waiting threads
   1.109 +        this_oop->set_initialization_state_and_notify(initialization_error, THREAD);
   1.110 +        CLEAR_PENDING_EXCEPTION;
   1.111        }
   1.112        DTRACE_CLASSINIT_PROBE_WAIT(super__failed, InstanceKlass::cast(this_oop()), -1,wait);
   1.113        THROW_OOP(e());
   1.114      }
   1.115    }
   1.116  
   1.117 -  // Recursively initialize any superinterfaces that declare default methods
   1.118 -  // Only need to recurse if has_default_methods which includes declaring and
   1.119 -  // inheriting default methods
   1.120 -  if (this_oop->has_default_methods()) {
   1.121 -    this_oop->initialize_super_interfaces(this_oop, CHECK);
   1.122 -  }
   1.123 -
   1.124    // Step 8
   1.125    {
   1.126      assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
   1.127 @@ -981,10 +976,15 @@
   1.128  
   1.129  void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) {
   1.130    oop init_lock = this_oop->init_lock();
   1.131 -  ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
   1.132 -  this_oop->set_init_state(state);
   1.133 -  this_oop->fence_and_clear_init_lock();
   1.134 -  ol.notify_all(CHECK);
   1.135 +  if (init_lock != NULL) {
   1.136 +    ObjectLocker ol(init_lock, THREAD);
   1.137 +    this_oop->set_init_state(state);
   1.138 +    this_oop->fence_and_clear_init_lock();
   1.139 +    ol.notify_all(CHECK);
   1.140 +  } else {
   1.141 +    assert(init_lock != NULL, "The initialization state should never be set twice");
   1.142 +    this_oop->set_init_state(state);
   1.143 +  }
   1.144  }
   1.145  
   1.146  // The embedded _implementor field can only record one implementor.
   1.147 @@ -1475,18 +1475,23 @@
   1.148  
   1.149  // find_method looks up the name/signature in the local methods array
   1.150  Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const {
   1.151 -  return find_method_impl(name, signature, false);
   1.152 +  return find_method_impl(name, signature, find_overpass, find_static, find_private);
   1.153  }
   1.154  
   1.155 -Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature, bool skipping_overpass) const {
   1.156 -  return InstanceKlass::find_method_impl(methods(), name, signature, skipping_overpass, false);
   1.157 +Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature,
   1.158 +                                        OverpassLookupMode overpass_mode,
   1.159 +                                        StaticLookupMode static_mode,
   1.160 +                                        PrivateLookupMode private_mode) const {
   1.161 +  return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode);
   1.162  }
   1.163  
   1.164  // find_instance_method looks up the name/signature in the local methods array
   1.165  // and skips over static methods
   1.166  Method* InstanceKlass::find_instance_method(
   1.167      Array<Method*>* methods, Symbol* name, Symbol* signature) {
   1.168 -  Method* meth = InstanceKlass::find_method_impl(methods, name, signature, false, true);
   1.169 +  Method* meth = InstanceKlass::find_method_impl(methods, name, signature,
   1.170 +                                                 find_overpass, skip_static, find_private);
   1.171 +  assert(((meth == NULL) || !meth->is_static()), "find_instance_method should have skipped statics");
   1.172    return meth;
   1.173  }
   1.174  
   1.175 @@ -1496,22 +1501,51 @@
   1.176      return InstanceKlass::find_instance_method(methods(), name, signature);
   1.177  }
   1.178  
   1.179 +// Find looks up the name/signature in the local methods array
   1.180 +// and filters on the overpass, static and private flags
   1.181 +// This returns the first one found
   1.182 +// note that the local methods array can have up to one overpass, one static
   1.183 +// and one instance (private or not) with the same name/signature
   1.184 +Method* InstanceKlass::find_local_method(Symbol* name, Symbol* signature,
   1.185 +                                        OverpassLookupMode overpass_mode,
   1.186 +                                        StaticLookupMode static_mode,
   1.187 +                                        PrivateLookupMode private_mode) const {
   1.188 +  return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode);
   1.189 +}
   1.190 +
   1.191 +// Find looks up the name/signature in the local methods array
   1.192 +// and filters on the overpass, static and private flags
   1.193 +// This returns the first one found
   1.194 +// note that the local methods array can have up to one overpass, one static
   1.195 +// and one instance (private or not) with the same name/signature
   1.196 +Method* InstanceKlass::find_local_method(Array<Method*>* methods,
   1.197 +                                        Symbol* name, Symbol* signature,
   1.198 +                                        OverpassLookupMode overpass_mode,
   1.199 +                                        StaticLookupMode static_mode,
   1.200 +                                        PrivateLookupMode private_mode) {
   1.201 +  return InstanceKlass::find_method_impl(methods, name, signature, overpass_mode, static_mode, private_mode);
   1.202 +}
   1.203 +
   1.204 +
   1.205  // find_method looks up the name/signature in the local methods array
   1.206  Method* InstanceKlass::find_method(
   1.207      Array<Method*>* methods, Symbol* name, Symbol* signature) {
   1.208 -  return InstanceKlass::find_method_impl(methods, name, signature, false, false);
   1.209 +  return InstanceKlass::find_method_impl(methods, name, signature, find_overpass, find_static, find_private);
   1.210  }
   1.211  
   1.212  Method* InstanceKlass::find_method_impl(
   1.213 -    Array<Method*>* methods, Symbol* name, Symbol* signature, bool skipping_overpass, bool skipping_static) {
   1.214 -  int hit = find_method_index(methods, name, signature, skipping_overpass, skipping_static);
   1.215 +    Array<Method*>* methods, Symbol* name, Symbol* signature,
   1.216 +    OverpassLookupMode overpass_mode, StaticLookupMode static_mode,
   1.217 +    PrivateLookupMode private_mode) {
   1.218 +  int hit = find_method_index(methods, name, signature, overpass_mode, static_mode, private_mode);
   1.219    return hit >= 0 ? methods->at(hit): NULL;
   1.220  }
   1.221  
   1.222 -bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static) {
   1.223 -    return (m->signature() == signature) &&
   1.224 +bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static, bool skipping_private) {
   1.225 +    return  ((m->signature() == signature) &&
   1.226              (!skipping_overpass || !m->is_overpass()) &&
   1.227 -            (!skipping_static || !m->is_static());
   1.228 +            (!skipping_static || !m->is_static()) &&
   1.229 +            (!skipping_private || !m->is_private()));
   1.230  }
   1.231  
   1.232  // Used directly for default_methods to find the index into the
   1.233 @@ -1521,15 +1555,25 @@
   1.234  // the search continues to find a potential non-overpass match.  This capability
   1.235  // is important during method resolution to prefer a static method, for example,
   1.236  // over an overpass method.
   1.237 +// There is the possibility in any _method's array to have the same name/signature
   1.238 +// for a static method, an overpass method and a local instance method
   1.239 +// To correctly catch a given method, the search criteria may need
   1.240 +// to explicitly skip the other two. For local instance methods, it
   1.241 +// is often necessary to skip private methods
   1.242  int InstanceKlass::find_method_index(
   1.243 -    Array<Method*>* methods, Symbol* name, Symbol* signature, bool skipping_overpass, bool skipping_static) {
   1.244 +    Array<Method*>* methods, Symbol* name, Symbol* signature,
   1.245 +    OverpassLookupMode overpass_mode, StaticLookupMode static_mode,
   1.246 +    PrivateLookupMode private_mode) {
   1.247 +  bool skipping_overpass = (overpass_mode == skip_overpass);
   1.248 +  bool skipping_static = (static_mode == skip_static);
   1.249 +  bool skipping_private = (private_mode == skip_private);
   1.250    int hit = binary_search(methods, name);
   1.251    if (hit != -1) {
   1.252      Method* m = methods->at(hit);
   1.253  
   1.254      // Do linear search to find matching signature.  First, quick check
   1.255      // for common case, ignoring overpasses if requested.
   1.256 -    if (method_matches(m, signature, skipping_overpass, skipping_static)) return hit;
   1.257 +    if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return hit;
   1.258  
   1.259      // search downwards through overloaded methods
   1.260      int i;
   1.261 @@ -1537,18 +1581,18 @@
   1.262          Method* m = methods->at(i);
   1.263          assert(m->is_method(), "must be method");
   1.264          if (m->name() != name) break;
   1.265 -        if (method_matches(m, signature, skipping_overpass, skipping_static)) return i;
   1.266 +        if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i;
   1.267      }
   1.268      // search upwards
   1.269      for (i = hit + 1; i < methods->length(); ++i) {
   1.270          Method* m = methods->at(i);
   1.271          assert(m->is_method(), "must be method");
   1.272          if (m->name() != name) break;
   1.273 -        if (method_matches(m, signature, skipping_overpass, skipping_static)) return i;
   1.274 +        if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i;
   1.275      }
   1.276      // not found
   1.277  #ifdef ASSERT
   1.278 -    int index = skipping_overpass || skipping_static ? -1 : linear_search(methods, name, signature);
   1.279 +    int index = (skipping_overpass || skipping_static || skipping_private) ? -1 : linear_search(methods, name, signature);
   1.280      assert(index == -1, err_msg("binary search should have found entry %d", index));
   1.281  #endif
   1.282    }
   1.283 @@ -1574,16 +1618,16 @@
   1.284  
   1.285  // uncached_lookup_method searches both the local class methods array and all
   1.286  // superclasses methods arrays, skipping any overpass methods in superclasses.
   1.287 -Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, MethodLookupMode mode) const {
   1.288 -  MethodLookupMode lookup_mode = mode;
   1.289 +Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const {
   1.290 +  OverpassLookupMode overpass_local_mode = overpass_mode;
   1.291    Klass* klass = const_cast<InstanceKlass*>(this);
   1.292    while (klass != NULL) {
   1.293 -    Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, (lookup_mode == skip_overpass));
   1.294 +    Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, overpass_local_mode, find_static, find_private);
   1.295      if (method != NULL) {
   1.296        return method;
   1.297      }
   1.298      klass = InstanceKlass::cast(klass)->super();
   1.299 -    lookup_mode = skip_overpass;   // Always ignore overpass methods in superclasses
   1.300 +    overpass_local_mode = skip_overpass;   // Always ignore overpass methods in superclasses
   1.301    }
   1.302    return NULL;
   1.303  }
   1.304 @@ -1613,7 +1657,7 @@
   1.305    }
   1.306    // Look up interfaces
   1.307    if (m == NULL) {
   1.308 -    m = lookup_method_in_all_interfaces(name, signature, normal);
   1.309 +    m = lookup_method_in_all_interfaces(name, signature, find_defaults);
   1.310    }
   1.311    return m;
   1.312  }
   1.313 @@ -1623,7 +1667,7 @@
   1.314  // They should only be found in the initial InterfaceMethodRef
   1.315  Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name,
   1.316                                                         Symbol* signature,
   1.317 -                                                       MethodLookupMode mode) const {
   1.318 +                                                       DefaultsLookupMode defaults_mode) const {
   1.319    Array<Klass*>* all_ifs = transitive_interfaces();
   1.320    int num_ifs = all_ifs->length();
   1.321    InstanceKlass *ik = NULL;
   1.322 @@ -1631,7 +1675,7 @@
   1.323      ik = InstanceKlass::cast(all_ifs->at(i));
   1.324      Method* m = ik->lookup_method(name, signature);
   1.325      if (m != NULL && m->is_public() && !m->is_static() &&
   1.326 -        ((mode != skip_defaults) || !m->is_default_method())) {
   1.327 +        ((defaults_mode != skip_defaults) || !m->is_default_method())) {
   1.328        return m;
   1.329      }
   1.330    }

mercurial