< 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/javaThread.hpp"
  50 #include "runtime/reflection.hpp"
  51 #include "runtime/reflectionUtils.hpp"
  52 #include "runtime/signature.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();

 748     }
 749     if (!ss.at_return_type()) {
 750       mirrors->obj_at_put(index++, mirror);
 751     } else if (return_type != NULL) {
 752       // Collect return type as well
 753       assert(ss.at_return_type(), "return type should be present");
 754       *return_type = mirror;
 755     }
 756   }
 757   assert(index == parameter_count, "invalid parameter count");
 758   return mirrors;
 759 }
 760 
 761 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
 762   return method->resolved_checked_exceptions(THREAD);
 763 }
 764 
 765 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
 766   ResolvingSignatureStream ss(signature, k, false);
 767   oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
 768   if (log_is_enabled(Debug, class, resolve)) {
 769     trace_class_resolution(nt);
 770   }
 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()->is_initializer() ||
 777          (for_constant_pool_access && method()->is_static()),
 778          "should call new_constructor instead");
 779   InstanceKlass* holder = method->method_holder();
 780   int slot = method->method_idnum();
 781 
 782   Symbol*  signature  = method->signature();
 783   int parameter_count = ArgumentCount(signature).size();
 784   oop return_type_oop = NULL;
 785   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
 786   if (parameter_types.is_null() || return_type_oop == NULL) return NULL;
 787 
 788   Handle return_type(THREAD, return_type_oop);
 789 
 790   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 791   assert(!exception_types.is_null(), "cannot return null");
 792 
 793   Symbol*  method_name = method->name();
 794   oop name_oop = StringTable::intern(method_name, CHECK_NULL);
 795   Handle name = Handle(THREAD, name_oop);
 796   if (name == NULL) return NULL;
 797 

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


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

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

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

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


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
















1184   Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1185 
1186   // Ignore result from call and return receiver
1187   invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1188   return receiver();
1189 }

  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/javaThread.hpp"
  51 #include "runtime/reflection.hpp"
  52 #include "runtime/reflectionUtils.hpp"
  53 #include "runtime/signature.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_valueArray(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)->value_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();

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



 778   return Handle(THREAD, nt);
 779 }
 780 
 781 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
 782   // Allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
 783   assert(!method()->name()->starts_with('<') || for_constant_pool_access,

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

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

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

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