hotspot/src/share/vm/oops/methodOop.cpp

Print this page
rev 611 : Merge
   1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)methodOop.cpp        1.314 08/11/24 12:22:56 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  


 416   // %%% Should return true for private methods also,
 417   // since there is no way to override them.
 418   return is_final() || Klass::cast(method_holder())->is_final();
 419 }
 420 
 421 
 422 bool methodOopDesc::is_strict_method() const {
 423   return is_strict();
 424 }
 425 
 426 
 427 bool methodOopDesc::can_be_statically_bound() const {
 428   if (is_final_method())  return true;
 429   return vtable_index() == nonvirtual_vtable_index;
 430 }
 431 
 432 
 433 bool methodOopDesc::is_accessor() const {
 434   if (code_size() != 5) return false;
 435   if (size_of_parameters() != 1) return false;
 436   if (Bytecodes::java_code_at(code_base()+0) != Bytecodes::_aload_0 ) return false;
 437   if (Bytecodes::java_code_at(code_base()+1) != Bytecodes::_getfield) return false;
 438   Bytecodes::Code ret_bc = Bytecodes::java_code_at(code_base()+4);
 439   if (Bytecodes::java_code_at(code_base()+4) != Bytecodes::_areturn &&
 440       Bytecodes::java_code_at(code_base()+4) != Bytecodes::_ireturn ) return false;
 441   return true;
 442 }
 443 
 444 
 445 bool methodOopDesc::is_initializer() const {
 446   return name() == vmSymbols::object_initializer_name() || name() == vmSymbols::class_initializer_name();
 447 }
 448 
 449 
 450 objArrayHandle methodOopDesc::resolved_checked_exceptions_impl(methodOop this_oop, TRAPS) {
 451   int length = this_oop->checked_exceptions_length();
 452   if (length == 0) {  // common case
 453     return objArrayHandle(THREAD, Universe::the_empty_class_klass_array());
 454   } else {
 455     methodHandle h_this(THREAD, this_oop);
 456     objArrayOop m_oop = oopFactory::new_objArray(SystemDictionary::class_klass(), length, CHECK_(objArrayHandle()));
 457     objArrayHandle mirrors (THREAD, m_oop);
 458     for (int i = 0; i < length; i++) {
 459       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 460       klassOop k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));


 658     set_native_function(
 659       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
 660       !native_bind_event_is_interesting);
 661   }
 662 
 663   // Setup compiler entrypoint.  This is made eagerly, so we do not need
 664   // special handling of vtables.  An alternative is to make adapters more
 665   // lazily by calling make_adapter() from from_compiled_entry() for the
 666   // normal calls.  For vtable calls life gets more complicated.  When a
 667   // call-site goes mega-morphic we need adapters in all methods which can be
 668   // called from the vtable.  We need adapters on such methods that get loaded
 669   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
 670   // problem we'll make these lazily later.
 671   (void) make_adapters(h_method, CHECK);
 672 
 673   // ONLY USE the h_method now as make_adapter may have blocked
 674 
 675 }
 676 
 677 address methodOopDesc::make_adapters(methodHandle mh, TRAPS) { 
 678   // If running -Xint we need no adapters.
 679   if (Arguments::mode() == Arguments::_int) return NULL;
 680 
 681   // Adapters for compiled code are made eagerly here.  They are fairly
 682   // small (generally < 100 bytes) and quick to make (and cached and shared)
 683   // so making them eagerly shouldn't be too expensive.
 684   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
 685   if (adapter == NULL ) {
 686     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 687   }
 688 
 689   mh->set_adapter_entry(adapter);
 690   mh->_from_compiled_entry = adapter->get_c2i_entry();
 691   return adapter->get_c2i_entry();
 692 }
 693 
 694 // The verified_code_entry() must be called when a invoke is resolved
 695 // on this method.
 696 
 697 // It returns the compiled code entry point, after asserting not null.
 698 // This function is called after potential safepoints so that nmethod
 699 // or adapter that it points to is still live and valid.
 700 // This function must not hit a safepoint!


 874     }
 875   }
 876 
 877   // return intrinsic id if any
 878   return vmIntrinsics::find_id(klass_id, name_id, sig_id, flags);
 879 }
 880 
 881 
 882 // These two methods are static since a GC may move the methodOopDesc
 883 bool methodOopDesc::load_signature_classes(methodHandle m, TRAPS) {
 884   bool sig_is_loaded = true;
 885   Handle class_loader(THREAD, instanceKlass::cast(m->method_holder())->class_loader());
 886   Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain());
 887   symbolHandle signature(THREAD, m->signature());
 888   for(SignatureStream ss(signature); !ss.is_done(); ss.next()) {
 889     if (ss.is_object()) {
 890       symbolOop sym = ss.as_symbol(CHECK_(false));
 891       symbolHandle name (THREAD, sym);
 892       klassOop klass = SystemDictionary::resolve_or_null(name, class_loader,
 893                                              protection_domain, THREAD);
 894       // We are loading classes eagerly. If a ClassNotFoundException was generated,
 895       // be sure to ignore it.
 896       if (HAS_PENDING_EXCEPTION) {
 897         if (PENDING_EXCEPTION->is_a(SystemDictionary::classNotFoundException_klass())) {

 898           CLEAR_PENDING_EXCEPTION;
 899         } else {
 900           return false;
 901         }
 902       }
 903       if( klass == NULL) { sig_is_loaded = false; }
 904     }
 905   }
 906   return sig_is_loaded;
 907 }
 908 
 909 bool methodOopDesc::has_unloaded_classes_in_signature(methodHandle m, TRAPS) {
 910   Handle class_loader(THREAD, instanceKlass::cast(m->method_holder())->class_loader());
 911   Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain());
 912   symbolHandle signature(THREAD, m->signature());
 913   for(SignatureStream ss(signature); !ss.is_done(); ss.next()) {
 914     if (ss.type() == T_OBJECT) {
 915       symbolHandle name(THREAD, ss.as_symbol_or_null());
 916       if (name() == NULL) return true;   
 917       klassOop klass = SystemDictionary::find(name, class_loader, protection_domain, THREAD);


 940   }
 941 
 942   // Prevent qsort from reordering a previous valid sort by
 943   // considering the address of the methodOops if two methods
 944   // would otherwise compare as equal.  Required to preserve
 945   // optimal access order in the shared archive.  Slower than
 946   // method_compare, only used for shared archive creation.
 947   static int method_compare_idempotent(methodOop* a, methodOop* b) {
 948     int i = method_compare(a, b);
 949     if (i != 0) return i;
 950     return ( a < b ? -1 : (a == b ? 0 : 1));
 951   }
 952 
 953   typedef int (*compareFn)(const void*, const void*);
 954 }
 955 
 956 
 957 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array
 958 static void reorder_based_on_method_index(objArrayOop methods,
 959                                           objArrayOop annotations,
 960                                           oop* temp_array) {
 961   if (annotations == NULL) {
 962     return;
 963   }
 964 
 965   int length = methods->length();
 966   int i;
 967   // Copy to temp array
 968   memcpy(temp_array, annotations->obj_at_addr(0), length * sizeof(oop));



 969 
 970   // Copy back using old method indices
 971   for (i = 0; i < length; i++) {
 972     methodOop m = (methodOop) methods->obj_at(i);
 973     annotations->obj_at_put(i, temp_array[m->method_idnum()]);
 974   }
 975 }
 976 
 977 
 978 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array
 979 void methodOopDesc::sort_methods(objArrayOop methods,
 980                                  objArrayOop methods_annotations,
 981                                  objArrayOop methods_parameter_annotations,
 982                                  objArrayOop methods_default_annotations,
 983                                  bool idempotent) {
 984   int length = methods->length();
 985   if (length > 1) {
 986     bool do_annotations = false;
 987     if (methods_annotations != NULL ||
 988         methods_parameter_annotations != NULL ||
 989         methods_default_annotations != NULL) {
 990       do_annotations = true;
 991     }
 992     if (do_annotations) {
 993       // Remember current method ordering so we can reorder annotations
 994       for (int i = 0; i < length; i++) {
 995         methodOop m = (methodOop) methods->obj_at(i);
 996         m->set_method_idnum(i);
 997       }
 998     }
 999 
1000     // Use a simple bubble sort for small number of methods since
1001     // qsort requires a functional pointer call for each comparison.
1002     if (length < 8) {
1003       bool sorted = true;
1004       for (int i=length-1; i>0; i--) {
1005         for (int j=0; j<i; j++) {
1006           methodOop m1 = (methodOop)methods->obj_at(j);
1007           methodOop m2 = (methodOop)methods->obj_at(j+1);
1008           if ((uintptr_t)m1->name() > (uintptr_t)m2->name()) {
1009             methods->obj_at_put(j, m2);
1010             methods->obj_at_put(j+1, m1);
1011             sorted = false;
1012           }
1013         }
1014         if (sorted) break;
1015         sorted = true;
1016       }
1017     } else {



1018       compareFn compare = (compareFn) (idempotent ? method_compare_idempotent : method_compare);
1019       qsort(methods->obj_at_addr(0), length, oopSize, compare);
1020     }
1021     
1022     // Sort annotations if necessary
1023     assert(methods_annotations == NULL           || methods_annotations->length() == methods->length(), "");
1024     assert(methods_parameter_annotations == NULL || methods_parameter_annotations->length() == methods->length(), "");
1025     assert(methods_default_annotations == NULL   || methods_default_annotations->length() == methods->length(), "");
1026     if (do_annotations) {

1027       // Allocate temporary storage
1028       oop* temp_array = NEW_RESOURCE_ARRAY(oop, length);
1029       reorder_based_on_method_index(methods, methods_annotations, temp_array);
1030       reorder_based_on_method_index(methods, methods_parameter_annotations, temp_array);
1031       reorder_based_on_method_index(methods, methods_default_annotations, temp_array);
1032     }
1033 
1034     // Reset method ordering
1035     for (int i = 0; i < length; i++) {
1036       methodOop m = (methodOop) methods->obj_at(i);
1037       m->set_method_idnum(i);
1038     }
1039   }
1040 }
1041 
1042 
1043 //-----------------------------------------------------------------------------------
1044 // Non-product code
1045 
1046 #ifndef PRODUCT
1047 class SignatureTypePrinter : public SignatureTypeNames {
1048  private:


   1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)methodOop.cpp        1.314 08/11/24 12:22:56 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  


 416   // %%% Should return true for private methods also,
 417   // since there is no way to override them.
 418   return is_final() || Klass::cast(method_holder())->is_final();
 419 }
 420 
 421 
 422 bool methodOopDesc::is_strict_method() const {
 423   return is_strict();
 424 }
 425 
 426 
 427 bool methodOopDesc::can_be_statically_bound() const {
 428   if (is_final_method())  return true;
 429   return vtable_index() == nonvirtual_vtable_index;
 430 }
 431 
 432 
 433 bool methodOopDesc::is_accessor() const {
 434   if (code_size() != 5) return false;
 435   if (size_of_parameters() != 1) return false;
 436   methodOop m = (methodOop)this;  // pass to code_at() to avoid method_from_bcp
 437   if (Bytecodes::java_code_at(code_base()+0, m) != Bytecodes::_aload_0 ) return false;
 438   if (Bytecodes::java_code_at(code_base()+1, m) != Bytecodes::_getfield) return false;
 439   if (Bytecodes::java_code_at(code_base()+4, m) != Bytecodes::_areturn &&
 440       Bytecodes::java_code_at(code_base()+4, m) != Bytecodes::_ireturn ) return false;
 441   return true;
 442 }
 443 
 444 
 445 bool methodOopDesc::is_initializer() const {
 446   return name() == vmSymbols::object_initializer_name() || name() == vmSymbols::class_initializer_name();
 447 }
 448 
 449 
 450 objArrayHandle methodOopDesc::resolved_checked_exceptions_impl(methodOop this_oop, TRAPS) {
 451   int length = this_oop->checked_exceptions_length();
 452   if (length == 0) {  // common case
 453     return objArrayHandle(THREAD, Universe::the_empty_class_klass_array());
 454   } else {
 455     methodHandle h_this(THREAD, this_oop);
 456     objArrayOop m_oop = oopFactory::new_objArray(SystemDictionary::class_klass(), length, CHECK_(objArrayHandle()));
 457     objArrayHandle mirrors (THREAD, m_oop);
 458     for (int i = 0; i < length; i++) {
 459       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 460       klassOop k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));


 658     set_native_function(
 659       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
 660       !native_bind_event_is_interesting);
 661   }
 662 
 663   // Setup compiler entrypoint.  This is made eagerly, so we do not need
 664   // special handling of vtables.  An alternative is to make adapters more
 665   // lazily by calling make_adapter() from from_compiled_entry() for the
 666   // normal calls.  For vtable calls life gets more complicated.  When a
 667   // call-site goes mega-morphic we need adapters in all methods which can be
 668   // called from the vtable.  We need adapters on such methods that get loaded
 669   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
 670   // problem we'll make these lazily later.
 671   (void) make_adapters(h_method, CHECK);
 672 
 673   // ONLY USE the h_method now as make_adapter may have blocked
 674 
 675 }
 676 
 677 address methodOopDesc::make_adapters(methodHandle mh, TRAPS) {



 678   // Adapters for compiled code are made eagerly here.  They are fairly
 679   // small (generally < 100 bytes) and quick to make (and cached and shared)
 680   // so making them eagerly shouldn't be too expensive.
 681   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
 682   if (adapter == NULL ) {
 683     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 684   }
 685 
 686   mh->set_adapter_entry(adapter);
 687   mh->_from_compiled_entry = adapter->get_c2i_entry();
 688   return adapter->get_c2i_entry();
 689 }
 690 
 691 // The verified_code_entry() must be called when a invoke is resolved
 692 // on this method.
 693 
 694 // It returns the compiled code entry point, after asserting not null.
 695 // This function is called after potential safepoints so that nmethod
 696 // or adapter that it points to is still live and valid.
 697 // This function must not hit a safepoint!


 871     }
 872   }
 873 
 874   // return intrinsic id if any
 875   return vmIntrinsics::find_id(klass_id, name_id, sig_id, flags);
 876 }
 877 
 878 
 879 // These two methods are static since a GC may move the methodOopDesc
 880 bool methodOopDesc::load_signature_classes(methodHandle m, TRAPS) {
 881   bool sig_is_loaded = true;
 882   Handle class_loader(THREAD, instanceKlass::cast(m->method_holder())->class_loader());
 883   Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain());
 884   symbolHandle signature(THREAD, m->signature());
 885   for(SignatureStream ss(signature); !ss.is_done(); ss.next()) {
 886     if (ss.is_object()) {
 887       symbolOop sym = ss.as_symbol(CHECK_(false));
 888       symbolHandle name (THREAD, sym);
 889       klassOop klass = SystemDictionary::resolve_or_null(name, class_loader,
 890                                              protection_domain, THREAD);
 891       // We are loading classes eagerly. If a ClassNotFoundException or
 892       // a LinkageError was generated, be sure to ignore it.
 893       if (HAS_PENDING_EXCEPTION) {
 894         if (PENDING_EXCEPTION->is_a(SystemDictionary::classNotFoundException_klass()) ||
 895             PENDING_EXCEPTION->is_a(SystemDictionary::linkageError_klass())) {
 896           CLEAR_PENDING_EXCEPTION;
 897         } else {
 898           return false;
 899         }
 900       }
 901       if( klass == NULL) { sig_is_loaded = false; }
 902     }
 903   }
 904   return sig_is_loaded;
 905 }
 906 
 907 bool methodOopDesc::has_unloaded_classes_in_signature(methodHandle m, TRAPS) {
 908   Handle class_loader(THREAD, instanceKlass::cast(m->method_holder())->class_loader());
 909   Handle protection_domain(THREAD, Klass::cast(m->method_holder())->protection_domain());
 910   symbolHandle signature(THREAD, m->signature());
 911   for(SignatureStream ss(signature); !ss.is_done(); ss.next()) {
 912     if (ss.type() == T_OBJECT) {
 913       symbolHandle name(THREAD, ss.as_symbol_or_null());
 914       if (name() == NULL) return true;   
 915       klassOop klass = SystemDictionary::find(name, class_loader, protection_domain, THREAD);


 938   }
 939 
 940   // Prevent qsort from reordering a previous valid sort by
 941   // considering the address of the methodOops if two methods
 942   // would otherwise compare as equal.  Required to preserve
 943   // optimal access order in the shared archive.  Slower than
 944   // method_compare, only used for shared archive creation.
 945   static int method_compare_idempotent(methodOop* a, methodOop* b) {
 946     int i = method_compare(a, b);
 947     if (i != 0) return i;
 948     return ( a < b ? -1 : (a == b ? 0 : 1));
 949   }
 950 
 951   typedef int (*compareFn)(const void*, const void*);
 952 }
 953 
 954 
 955 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array
 956 static void reorder_based_on_method_index(objArrayOop methods,
 957                                           objArrayOop annotations,
 958                                           GrowableArray<oop>* temp_array) {
 959   if (annotations == NULL) {
 960     return;
 961   }
 962 
 963   int length = methods->length();
 964   int i;
 965   // Copy to temp array
 966   temp_array->clear();
 967   for (i = 0; i < length; i++) {
 968     temp_array->append(annotations->obj_at(i));
 969   }
 970 
 971   // Copy back using old method indices
 972   for (i = 0; i < length; i++) {
 973     methodOop m = (methodOop) methods->obj_at(i);
 974     annotations->obj_at_put(i, temp_array->at(m->method_idnum()));
 975   }
 976 }
 977 
 978 
 979 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array
 980 void methodOopDesc::sort_methods(objArrayOop methods,
 981                                  objArrayOop methods_annotations,
 982                                  objArrayOop methods_parameter_annotations,
 983                                  objArrayOop methods_default_annotations,
 984                                  bool idempotent) {
 985   int length = methods->length();
 986   if (length > 1) {
 987     bool do_annotations = false;
 988     if (methods_annotations != NULL ||
 989         methods_parameter_annotations != NULL ||
 990         methods_default_annotations != NULL) {
 991       do_annotations = true;
 992     }
 993     if (do_annotations) {
 994       // Remember current method ordering so we can reorder annotations
 995       for (int i = 0; i < length; i++) {
 996         methodOop m = (methodOop) methods->obj_at(i);
 997         m->set_method_idnum(i);
 998       }
 999     }
1000 
1001     // Use a simple bubble sort for small number of methods since
1002     // qsort requires a functional pointer call for each comparison.
1003     if (UseCompressedOops || length < 8) {
1004       bool sorted = true;
1005       for (int i=length-1; i>0; i--) {
1006         for (int j=0; j<i; j++) {
1007           methodOop m1 = (methodOop)methods->obj_at(j);
1008           methodOop m2 = (methodOop)methods->obj_at(j+1);
1009           if ((uintptr_t)m1->name() > (uintptr_t)m2->name()) {
1010             methods->obj_at_put(j, m2);
1011             methods->obj_at_put(j+1, m1);
1012             sorted = false;
1013           }
1014         }
1015         if (sorted) break;
1016           sorted = true;
1017       }
1018     } else {
1019       // XXX This doesn't work for UseCompressedOops because the compare fn
1020       // will have to decode the methodOop anyway making it not much faster
1021       // than above.
1022       compareFn compare = (compareFn) (idempotent ? method_compare_idempotent : method_compare);
1023       qsort(methods->base(), length, heapOopSize, compare);
1024     }
1025     
1026     // Sort annotations if necessary
1027     assert(methods_annotations == NULL           || methods_annotations->length() == methods->length(), "");
1028     assert(methods_parameter_annotations == NULL || methods_parameter_annotations->length() == methods->length(), "");
1029     assert(methods_default_annotations == NULL   || methods_default_annotations->length() == methods->length(), "");
1030     if (do_annotations) {
1031       ResourceMark rm;
1032       // Allocate temporary storage
1033       GrowableArray<oop>* temp_array = new GrowableArray<oop>(length);
1034       reorder_based_on_method_index(methods, methods_annotations, temp_array);
1035       reorder_based_on_method_index(methods, methods_parameter_annotations, temp_array);
1036       reorder_based_on_method_index(methods, methods_default_annotations, temp_array);
1037     }
1038 
1039     // Reset method ordering
1040     for (int i = 0; i < length; i++) {
1041       methodOop m = (methodOop) methods->obj_at(i);
1042       m->set_method_idnum(i);
1043     }
1044   }
1045 }
1046 
1047 
1048 //-----------------------------------------------------------------------------------
1049 // Non-product code
1050 
1051 #ifndef PRODUCT
1052 class SignatureTypePrinter : public SignatureTypeNames {
1053  private: