< prev index next >

src/hotspot/share/runtime/reflection.cpp

Print this page

  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/moduleEntry.hpp"
  29 #include "classfile/packageEntry.hpp"
  30 #include "classfile/stringTable.hpp"
  31 #include "classfile/verifier.hpp"
  32 #include "classfile/vmClasses.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "interpreter/linkResolver.hpp"
  35 #include "logging/log.hpp"
  36 #include "memory/oopFactory.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.hpp"

  39 #include "oops/instanceKlass.inline.hpp"
  40 #include "oops/klass.inline.hpp"
  41 #include "oops/objArrayKlass.hpp"
  42 #include "oops/objArrayOop.inline.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "oops/typeArrayOop.inline.hpp"
  45 #include "prims/jvmtiExport.hpp"
  46 #include "runtime/fieldDescriptor.inline.hpp"
  47 #include "runtime/handles.inline.hpp"
  48 #include "runtime/javaCalls.hpp"
  49 #include "runtime/reflection.hpp"
  50 #include "runtime/reflectionUtils.hpp"
  51 #include "runtime/signature.hpp"
  52 #include "runtime/thread.inline.hpp"
  53 #include "runtime/vframe.inline.hpp"
  54 #include "utilities/formatBuffer.hpp"

  55 
  56 static void trace_class_resolution(oop mirror) {
  57   if (mirror == NULL || java_lang_Class::is_primitive(mirror)) {
  58     return;
  59   }
  60   Klass* to_class = java_lang_Class::as_Klass(mirror);
  61   ResourceMark rm;
  62   int line_number = -1;
  63   const char * source_file = NULL;
  64   Klass* caller = NULL;
  65   JavaThread* jthread = JavaThread::current();
  66   if (jthread->has_last_Java_frame()) {
  67     vframeStream vfst(jthread);
  68     // skip over any frames belonging to java.lang.Class
  69     while (!vfst.at_end() &&
  70            vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class()) {
  71       vfst.next();
  72     }
  73     if (!vfst.at_end()) {
  74       // this frame is a likely suspect

 330   else {
 331     return Universe::typeArrayKlassObj(type);
 332   }
 333 }
 334 
 335 arrayOop Reflection::reflect_new_array(oop element_mirror, jint length, TRAPS) {
 336   if (element_mirror == NULL) {
 337     THROW_0(vmSymbols::java_lang_NullPointerException());
 338   }
 339   if (length < 0) {
 340     THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
 341   }
 342   if (java_lang_Class::is_primitive(element_mirror)) {
 343     Klass* tak = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
 344     return TypeArrayKlass::cast(tak)->allocate(length, THREAD);
 345   } else {
 346     Klass* k = java_lang_Class::as_Klass(element_mirror);
 347     if (k->is_array_klass() && ArrayKlass::cast(k)->dimension() >= MAX_DIM) {
 348       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 349     }
 350     return oopFactory::new_objArray(k, length, THREAD);




 351   }
 352 }
 353 
 354 
 355 arrayOop Reflection::reflect_new_multi_array(oop element_mirror, typeArrayOop dim_array, TRAPS) {
 356   assert(dim_array->is_typeArray(), "just checking");
 357   assert(TypeArrayKlass::cast(dim_array->klass())->element_type() == T_INT, "just checking");
 358 
 359   if (element_mirror == NULL) {
 360     THROW_0(vmSymbols::java_lang_NullPointerException());
 361   }
 362 
 363   int len = dim_array->length();
 364   if (len <= 0 || len > MAX_DIM) {
 365     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 366   }
 367 
 368   jint dimensions[MAX_DIM];   // C array copy of intArrayOop
 369   for (int i = 0; i < len; i++) {
 370     int d = dim_array->int_at(i);
 371     if (d < 0) {
 372       THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", d));
 373     }
 374     dimensions[i] = d;
 375   }
 376 
 377   Klass* klass;
 378   int dim = len;
 379   if (java_lang_Class::is_primitive(element_mirror)) {
 380     klass = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
 381   } else {
 382     klass = java_lang_Class::as_Klass(element_mirror);
 383     if (klass->is_array_klass()) {
 384       int k_dim = ArrayKlass::cast(klass)->dimension();
 385       if (k_dim + len > MAX_DIM) {
 386         THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 387       }
 388       dim += k_dim;
 389     }
 390   }
 391   klass = klass->array_klass(dim, CHECK_NULL);




 392   oop obj = ArrayKlass::cast(klass)->multi_allocate(len, dimensions, CHECK_NULL);
 393   assert(obj->is_array(), "just checking");
 394   return arrayOop(obj);
 395 }
 396 
 397 
 398 static bool can_relax_access_check_for(const Klass* accessor,
 399                                        const Klass* accessee,
 400                                        bool classloader_only) {
 401 
 402   const InstanceKlass* accessor_ik = InstanceKlass::cast(accessor);
 403   const InstanceKlass* accessee_ik = InstanceKlass::cast(accessee);
 404 
 405   if (RelaxAccessControlCheck &&
 406     accessor_ik->major_version() < Verifier::NO_RELAX_ACCESS_CTRL_CHECK_VERSION &&
 407     accessee_ik->major_version() < Verifier::NO_RELAX_ACCESS_CTRL_CHECK_VERSION) {
 408     return classloader_only &&
 409       Verifier::relax_access_for(accessor_ik->class_loader()) &&
 410       accessor_ik->protection_domain() == accessee_ik->protection_domain() &&
 411       accessor_ik->class_loader() == accessee_ik->class_loader();

 741     }
 742     if (!ss.at_return_type()) {
 743       mirrors->obj_at_put(index++, mirror);
 744     } else if (return_type != NULL) {
 745       // Collect return type as well
 746       assert(ss.at_return_type(), "return type should be present");
 747       *return_type = mirror;
 748     }
 749   }
 750   assert(index == parameter_count, "invalid parameter count");
 751   return mirrors;
 752 }
 753 
 754 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
 755   return method->resolved_checked_exceptions(THREAD);
 756 }
 757 
 758 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
 759   ResolvingSignatureStream ss(signature, k, false);
 760   oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
 761   if (log_is_enabled(Debug, class, resolve)) {
 762     trace_class_resolution(nt);
 763   }
 764   return Handle(THREAD, nt);
 765 }
 766 
 767 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
 768   // Allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
 769   assert(!method()->is_initializer() ||
 770          (for_constant_pool_access && method()->is_static()),
 771          "should call new_constructor instead");
 772   InstanceKlass* holder = method->method_holder();
 773   int slot = method->method_idnum();
 774 
 775   Symbol*  signature  = method->signature();
 776   int parameter_count = ArgumentCount(signature).size();
 777   oop return_type_oop = NULL;
 778   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
 779   if (parameter_types.is_null() || return_type_oop == NULL) return NULL;
 780 
 781   Handle return_type(THREAD, return_type_oop);
 782 
 783   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 784   assert(!exception_types.is_null(), "cannot return null");
 785 
 786   Symbol*  method_name = method->name();
 787   oop name_oop = StringTable::intern(method_name, CHECK_NULL);
 788   Handle name = Handle(THREAD, name_oop);
 789   if (name == NULL) return NULL;
 790 

 799   java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
 800   java_lang_reflect_Method::set_exception_types(mh(), exception_types());
 801   java_lang_reflect_Method::set_modifiers(mh(), modifiers);
 802   java_lang_reflect_Method::set_override(mh(), false);
 803   if (method->generic_signature() != NULL) {
 804     Symbol*  gs = method->generic_signature();
 805     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 806     java_lang_reflect_Method::set_signature(mh(), sig());
 807   }
 808   typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
 809   java_lang_reflect_Method::set_annotations(mh(), an_oop);
 810   an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
 811   java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
 812   an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
 813   java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
 814   return mh();
 815 }
 816 
 817 
 818 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
 819   assert(method()->is_initializer(), "should call new_method instead");


 820 
 821   InstanceKlass* holder = method->method_holder();
 822   int slot = method->method_idnum();
 823 
 824   Symbol*  signature  = method->signature();
 825   int parameter_count = ArgumentCount(signature).size();
 826   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, NULL, CHECK_NULL);
 827   if (parameter_types.is_null()) return NULL;
 828 
 829   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 830   assert(!exception_types.is_null(), "cannot return null");
 831 
 832   const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
 833 
 834   Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
 835 
 836   java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
 837   java_lang_reflect_Constructor::set_slot(ch(), slot);
 838   java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
 839   java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());

 852 }
 853 
 854 
 855 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
 856   Symbol*  field_name = fd->name();
 857   oop name_oop = StringTable::intern(field_name, CHECK_NULL);
 858   Handle name = Handle(THREAD, name_oop);
 859   Symbol*  signature  = fd->signature();
 860   InstanceKlass* holder = fd->field_holder();
 861   Handle type = new_type(signature, holder, CHECK_NULL);
 862   Handle rh  = java_lang_reflect_Field::create(CHECK_NULL);
 863 
 864   java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
 865   java_lang_reflect_Field::set_slot(rh(), fd->index());
 866   java_lang_reflect_Field::set_name(rh(), name());
 867   java_lang_reflect_Field::set_type(rh(), type());
 868   if (fd->is_trusted_final()) {
 869     java_lang_reflect_Field::set_trusted_final(rh());
 870   }
 871   // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
 872   java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);




 873   java_lang_reflect_Field::set_override(rh(), false);
 874   if (fd->has_generic_signature()) {
 875     Symbol*  gs = fd->generic_signature();
 876     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 877     java_lang_reflect_Field::set_signature(rh(), sig());
 878   }
 879   typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
 880   java_lang_reflect_Field::set_annotations(rh(), an_oop);
 881   return rh();
 882 }
 883 
 884 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
 885                               int flags, TRAPS) {
 886 
 887   Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
 888 
 889   if(NULL != sym) {
 890     Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
 891     java_lang_reflect_Parameter::set_name(rh(), name());
 892   } else {

1123     if (rtype == T_BOOLEAN || rtype == T_BYTE || rtype == T_CHAR || rtype == T_SHORT) {
1124       narrow((jvalue*)result.get_value_addr(), rtype, CHECK_NULL);
1125     }
1126     return Reflection::box((jvalue*)result.get_value_addr(), rtype, THREAD);
1127   }
1128 }
1129 
1130 // This would be nicer if, say, java.lang.reflect.Method was a subclass
1131 // of java.lang.reflect.Constructor
1132 
1133 oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) {
1134   oop mirror             = java_lang_reflect_Method::clazz(method_mirror);
1135   int slot               = java_lang_reflect_Method::slot(method_mirror);
1136   bool override          = java_lang_reflect_Method::override(method_mirror) != 0;
1137   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror)));
1138 
1139   oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror);
1140   BasicType rtype;
1141   if (java_lang_Class::is_primitive(return_type_mirror)) {
1142     rtype = basic_type_mirror_to_basic_type(return_type_mirror);


1143   } else {
1144     rtype = T_OBJECT;
1145   }
1146 
1147   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1148   Method* m = klass->method_with_idnum(slot);
1149   if (m == NULL) {
1150     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1151   }
1152   methodHandle method(THREAD, m);
1153 
1154   return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1155 }
1156 
1157 
1158 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1159   oop mirror             = java_lang_reflect_Constructor::clazz(constructor_mirror);
1160   int slot               = java_lang_reflect_Constructor::slot(constructor_mirror);
1161   bool override          = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1162   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1163 
1164   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1165   Method* m = klass->method_with_idnum(slot);
1166   if (m == NULL) {
1167     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1168   }
1169   methodHandle method(THREAD, m);
1170   assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1171 
1172   // Make sure klass gets initialize
1173   klass->initialize(CHECK_NULL);
1174 
1175   // Create new instance (the receiver)
1176   klass->check_valid_for_instantiation(false, CHECK_NULL);
















1177   Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1178 
1179   // Ignore result from call and return receiver
1180   invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1181   return receiver();
1182 }

  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/moduleEntry.hpp"
  29 #include "classfile/packageEntry.hpp"
  30 #include "classfile/stringTable.hpp"
  31 #include "classfile/verifier.hpp"
  32 #include "classfile/vmClasses.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "interpreter/linkResolver.hpp"
  35 #include "logging/log.hpp"
  36 #include "memory/oopFactory.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.hpp"
  39 #include "oops/inlineKlass.inline.hpp"
  40 #include "oops/instanceKlass.inline.hpp"
  41 #include "oops/klass.inline.hpp"
  42 #include "oops/objArrayKlass.hpp"
  43 #include "oops/objArrayOop.inline.hpp"
  44 #include "oops/oop.inline.hpp"
  45 #include "oops/typeArrayOop.inline.hpp"
  46 #include "prims/jvmtiExport.hpp"
  47 #include "runtime/fieldDescriptor.inline.hpp"
  48 #include "runtime/handles.inline.hpp"
  49 #include "runtime/javaCalls.hpp"
  50 #include "runtime/reflection.hpp"
  51 #include "runtime/reflectionUtils.hpp"
  52 #include "runtime/signature.hpp"
  53 #include "runtime/thread.inline.hpp"
  54 #include "runtime/vframe.inline.hpp"
  55 #include "utilities/formatBuffer.hpp"
  56 #include "utilities/globalDefinitions.hpp"
  57 
  58 static void trace_class_resolution(oop mirror) {
  59   if (mirror == NULL || java_lang_Class::is_primitive(mirror)) {
  60     return;
  61   }
  62   Klass* to_class = java_lang_Class::as_Klass(mirror);
  63   ResourceMark rm;
  64   int line_number = -1;
  65   const char * source_file = NULL;
  66   Klass* caller = NULL;
  67   JavaThread* jthread = JavaThread::current();
  68   if (jthread->has_last_Java_frame()) {
  69     vframeStream vfst(jthread);
  70     // skip over any frames belonging to java.lang.Class
  71     while (!vfst.at_end() &&
  72            vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class()) {
  73       vfst.next();
  74     }
  75     if (!vfst.at_end()) {
  76       // this frame is a likely suspect

 332   else {
 333     return Universe::typeArrayKlassObj(type);
 334   }
 335 }
 336 
 337 arrayOop Reflection::reflect_new_array(oop element_mirror, jint length, TRAPS) {
 338   if (element_mirror == NULL) {
 339     THROW_0(vmSymbols::java_lang_NullPointerException());
 340   }
 341   if (length < 0) {
 342     THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
 343   }
 344   if (java_lang_Class::is_primitive(element_mirror)) {
 345     Klass* tak = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
 346     return TypeArrayKlass::cast(tak)->allocate(length, THREAD);
 347   } else {
 348     Klass* k = java_lang_Class::as_Klass(element_mirror);
 349     if (k->is_array_klass() && ArrayKlass::cast(k)->dimension() >= MAX_DIM) {
 350       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 351     }
 352     if (k->is_inline_klass() && java_lang_Class::is_secondary_mirror(element_mirror)) {
 353       return oopFactory::new_flatArray(k, length, THREAD);
 354     } else {
 355       return oopFactory::new_objArray(k, length, THREAD);
 356     }
 357   }
 358 }
 359 
 360 
 361 arrayOop Reflection::reflect_new_multi_array(oop element_mirror, typeArrayOop dim_array, TRAPS) {
 362   assert(dim_array->is_typeArray(), "just checking");
 363   assert(TypeArrayKlass::cast(dim_array->klass())->element_type() == T_INT, "just checking");
 364 
 365   if (element_mirror == NULL) {
 366     THROW_0(vmSymbols::java_lang_NullPointerException());
 367   }
 368 
 369   int len = dim_array->length();
 370   if (len <= 0 || len > MAX_DIM) {
 371     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 372   }
 373 
 374   jint dimensions[MAX_DIM];   // C array copy of intArrayOop
 375   for (int i = 0; i < len; i++) {
 376     int d = dim_array->int_at(i);
 377     if (d < 0) {
 378       THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", d));
 379     }
 380     dimensions[i] = d;
 381   }
 382 
 383   Klass* klass;
 384   int dim = len;
 385   if (java_lang_Class::is_primitive(element_mirror)) {
 386     klass = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
 387   } else {
 388     klass = java_lang_Class::as_Klass(element_mirror);
 389     if (klass->is_array_klass()) {
 390       int k_dim = ArrayKlass::cast(klass)->dimension();
 391       if (k_dim + len > MAX_DIM) {
 392         THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 393       }
 394       dim += k_dim;
 395     }
 396   }
 397   if (klass->is_inline_klass() && java_lang_Class::is_secondary_mirror(element_mirror)) {
 398     klass = InlineKlass::cast(klass)->null_free_inline_array_klass(dim, CHECK_NULL);
 399   } else {
 400     klass = klass->array_klass(dim, CHECK_NULL);
 401   }
 402   oop obj = ArrayKlass::cast(klass)->multi_allocate(len, dimensions, CHECK_NULL);
 403   assert(obj->is_array(), "just checking");
 404   return arrayOop(obj);
 405 }
 406 
 407 
 408 static bool can_relax_access_check_for(const Klass* accessor,
 409                                        const Klass* accessee,
 410                                        bool classloader_only) {
 411 
 412   const InstanceKlass* accessor_ik = InstanceKlass::cast(accessor);
 413   const InstanceKlass* accessee_ik = InstanceKlass::cast(accessee);
 414 
 415   if (RelaxAccessControlCheck &&
 416     accessor_ik->major_version() < Verifier::NO_RELAX_ACCESS_CTRL_CHECK_VERSION &&
 417     accessee_ik->major_version() < Verifier::NO_RELAX_ACCESS_CTRL_CHECK_VERSION) {
 418     return classloader_only &&
 419       Verifier::relax_access_for(accessor_ik->class_loader()) &&
 420       accessor_ik->protection_domain() == accessee_ik->protection_domain() &&
 421       accessor_ik->class_loader() == accessee_ik->class_loader();

 751     }
 752     if (!ss.at_return_type()) {
 753       mirrors->obj_at_put(index++, mirror);
 754     } else if (return_type != NULL) {
 755       // Collect return type as well
 756       assert(ss.at_return_type(), "return type should be present");
 757       *return_type = mirror;
 758     }
 759   }
 760   assert(index == parameter_count, "invalid parameter count");
 761   return mirrors;
 762 }
 763 
 764 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
 765   return method->resolved_checked_exceptions(THREAD);
 766 }
 767 
 768 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
 769   ResolvingSignatureStream ss(signature, k, false);
 770   oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);



 771   return Handle(THREAD, nt);
 772 }
 773 
 774 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
 775   // Allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
 776   assert(!method()->name()->starts_with('<') || for_constant_pool_access,

 777          "should call new_constructor instead");
 778   InstanceKlass* holder = method->method_holder();
 779   int slot = method->method_idnum();
 780 
 781   Symbol*  signature  = method->signature();
 782   int parameter_count = ArgumentCount(signature).size();
 783   oop return_type_oop = NULL;
 784   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
 785   if (parameter_types.is_null() || return_type_oop == NULL) return NULL;
 786 
 787   Handle return_type(THREAD, return_type_oop);
 788 
 789   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 790   assert(!exception_types.is_null(), "cannot return null");
 791 
 792   Symbol*  method_name = method->name();
 793   oop name_oop = StringTable::intern(method_name, CHECK_NULL);
 794   Handle name = Handle(THREAD, name_oop);
 795   if (name == NULL) return NULL;
 796 

 805   java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
 806   java_lang_reflect_Method::set_exception_types(mh(), exception_types());
 807   java_lang_reflect_Method::set_modifiers(mh(), modifiers);
 808   java_lang_reflect_Method::set_override(mh(), false);
 809   if (method->generic_signature() != NULL) {
 810     Symbol*  gs = method->generic_signature();
 811     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 812     java_lang_reflect_Method::set_signature(mh(), sig());
 813   }
 814   typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
 815   java_lang_reflect_Method::set_annotations(mh(), an_oop);
 816   an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
 817   java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
 818   an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
 819   java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
 820   return mh();
 821 }
 822 
 823 
 824 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
 825   assert(method()->is_object_constructor() ||
 826          method()->is_static_init_factory(),
 827          "should call new_method instead");
 828 
 829   InstanceKlass* holder = method->method_holder();
 830   int slot = method->method_idnum();
 831 
 832   Symbol*  signature  = method->signature();
 833   int parameter_count = ArgumentCount(signature).size();
 834   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, NULL, CHECK_NULL);
 835   if (parameter_types.is_null()) return NULL;
 836 
 837   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 838   assert(!exception_types.is_null(), "cannot return null");
 839 
 840   const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
 841 
 842   Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
 843 
 844   java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
 845   java_lang_reflect_Constructor::set_slot(ch(), slot);
 846   java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
 847   java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());

 860 }
 861 
 862 
 863 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
 864   Symbol*  field_name = fd->name();
 865   oop name_oop = StringTable::intern(field_name, CHECK_NULL);
 866   Handle name = Handle(THREAD, name_oop);
 867   Symbol*  signature  = fd->signature();
 868   InstanceKlass* holder = fd->field_holder();
 869   Handle type = new_type(signature, holder, CHECK_NULL);
 870   Handle rh  = java_lang_reflect_Field::create(CHECK_NULL);
 871 
 872   java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
 873   java_lang_reflect_Field::set_slot(rh(), fd->index());
 874   java_lang_reflect_Field::set_name(rh(), name());
 875   java_lang_reflect_Field::set_type(rh(), type());
 876   if (fd->is_trusted_final()) {
 877     java_lang_reflect_Field::set_trusted_final(rh());
 878   }
 879   // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
 880   int modifiers = fd->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS;
 881   if (fd->is_inlined()) {
 882     modifiers |= JVM_ACC_FIELD_INLINED;
 883   }
 884   java_lang_reflect_Field::set_modifiers(rh(), modifiers);
 885   java_lang_reflect_Field::set_override(rh(), false);
 886   if (fd->has_generic_signature()) {
 887     Symbol*  gs = fd->generic_signature();
 888     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 889     java_lang_reflect_Field::set_signature(rh(), sig());
 890   }
 891   typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
 892   java_lang_reflect_Field::set_annotations(rh(), an_oop);
 893   return rh();
 894 }
 895 
 896 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
 897                               int flags, TRAPS) {
 898 
 899   Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
 900 
 901   if(NULL != sym) {
 902     Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
 903     java_lang_reflect_Parameter::set_name(rh(), name());
 904   } else {

1135     if (rtype == T_BOOLEAN || rtype == T_BYTE || rtype == T_CHAR || rtype == T_SHORT) {
1136       narrow((jvalue*)result.get_value_addr(), rtype, CHECK_NULL);
1137     }
1138     return Reflection::box((jvalue*)result.get_value_addr(), rtype, THREAD);
1139   }
1140 }
1141 
1142 // This would be nicer if, say, java.lang.reflect.Method was a subclass
1143 // of java.lang.reflect.Constructor
1144 
1145 oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) {
1146   oop mirror             = java_lang_reflect_Method::clazz(method_mirror);
1147   int slot               = java_lang_reflect_Method::slot(method_mirror);
1148   bool override          = java_lang_reflect_Method::override(method_mirror) != 0;
1149   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror)));
1150 
1151   oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror);
1152   BasicType rtype;
1153   if (java_lang_Class::is_primitive(return_type_mirror)) {
1154     rtype = basic_type_mirror_to_basic_type(return_type_mirror);
1155   } else if (java_lang_Class::as_Klass(return_type_mirror)->is_inline_klass()) {
1156     rtype = java_lang_Class::is_primary_mirror(return_type_mirror) ? T_OBJECT : T_INLINE_TYPE;
1157   } else {
1158     rtype = T_OBJECT;
1159   }
1160 
1161   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1162   Method* m = klass->method_with_idnum(slot);
1163   if (m == NULL) {
1164     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1165   }
1166   methodHandle method(THREAD, m);
1167 
1168   return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1169 }
1170 
1171 
1172 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1173   oop mirror             = java_lang_reflect_Constructor::clazz(constructor_mirror);
1174   int slot               = java_lang_reflect_Constructor::slot(constructor_mirror);
1175   bool override          = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1176   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1177 
1178   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1179   Method* m = klass->method_with_idnum(slot);
1180   if (m == NULL) {
1181     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1182   }
1183   methodHandle method(THREAD, m);
1184   assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1185 
1186   // Make sure klass gets initialize
1187   klass->initialize(CHECK_NULL);
1188 
1189   // Create new instance (the receiver)
1190   klass->check_valid_for_instantiation(false, CHECK_NULL);
1191 
1192   // Special case for factory methods
1193   if (!method->signature()->is_void_method_signature()) {
1194     assert(klass->is_inline_klass(), "inline classes must use factory methods");
1195     Handle no_receiver; // null instead of receiver
1196     BasicType rtype;
1197     if (klass->is_hidden()) {
1198       rtype = T_OBJECT;
1199     } else {
1200       rtype = T_INLINE_TYPE;
1201     }
1202     return invoke(klass, method, no_receiver, override, ptypes, rtype, args, false, CHECK_NULL);
1203   }
1204 
1205   // main branch of code creates a non-inline object:
1206   assert(!klass->is_inline_klass(), "classic constructors are only for non-inline classes");
1207   Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1208 
1209   // Ignore result from call and return receiver
1210   invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1211   return receiver();
1212 }
< prev index next >