src/share/vm/runtime/reflection.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/runtime

src/share/vm/runtime/reflection.cpp

Print this page




  39 #include "prims/jvm.h"
  40 #include "prims/jvmtiExport.hpp"
  41 #include "runtime/arguments.hpp"
  42 #include "runtime/handles.inline.hpp"
  43 #include "runtime/javaCalls.hpp"
  44 #include "runtime/reflection.hpp"
  45 #include "runtime/reflectionUtils.hpp"
  46 #include "runtime/signature.hpp"
  47 #include "runtime/vframe.hpp"
  48 
  49 static void trace_class_resolution(Klass* to_class) {
  50   ResourceMark rm;
  51   int line_number = -1;
  52   const char * source_file = NULL;
  53   Klass* caller = NULL;
  54   JavaThread* jthread = JavaThread::current();
  55   if (jthread->has_last_Java_frame()) {
  56     vframeStream vfst(jthread);
  57     // skip over any frames belonging to java.lang.Class
  58     while (!vfst.at_end() &&
  59            vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class()) {
  60       vfst.next();
  61     }
  62     if (!vfst.at_end()) {
  63       // this frame is a likely suspect
  64       caller = vfst.method()->method_holder();
  65       line_number = vfst.method()->line_number_from_bci(vfst.bci());
  66       Symbol* s = vfst.method()->method_holder()->source_file_name();
  67       if (s != NULL) {
  68         source_file = s->as_C_string();
  69       }
  70     }
  71   }
  72   if (caller != NULL) {
  73     const char * from = caller->external_name();
  74     const char * to = to_class->external_name();
  75     // print in a single call to reduce interleaving between threads
  76     if (source_file != NULL) {
  77       tty->print("RESOLVE %s %s %s:%d (reflection)\n", from, to, source_file, line_number);
  78     } else {
  79       tty->print("RESOLVE %s %s (reflection)\n", from, to);


 850   // Ensure klass is initialized
 851   klass->initialize(CHECK_NULL);
 852 
 853   bool is_static = reflected_method->is_static();
 854   if (is_static) {
 855     // ignore receiver argument
 856     method = reflected_method;
 857     target_klass = klass;
 858   } else {
 859     // check for null receiver
 860     if (receiver.is_null()) {
 861       THROW_0(vmSymbols::java_lang_NullPointerException());
 862     }
 863     // Check class of receiver against class declaring method
 864     if (!receiver->is_a(klass())) {
 865       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
 866     }
 867     // target klass is receiver's klass
 868     target_klass = KlassHandle(THREAD, receiver->klass());
 869     // no need to resolve if method is private or <init>
 870     if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) {
 871       method = reflected_method;
 872     } else {
 873       // resolve based on the receiver
 874       if (reflected_method->method_holder()->is_interface()) {
 875         // resolve interface call
 876         //
 877         // Match resolution errors with those thrown due to reflection inlining
 878         // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
 879         method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
 880         if (HAS_PENDING_EXCEPTION) {
 881         // Method resolution threw an exception; wrap it in an InvocationTargetException
 882           oop resolution_exception = PENDING_EXCEPTION;
 883           CLEAR_PENDING_EXCEPTION;
 884           // JVMTI has already reported the pending exception
 885           // JVMTI internal flag reset is needed in order to report InvocationTargetException
 886           if (THREAD->is_Java_thread()) {
 887             JvmtiExport::clear_detected_exception((JavaThread*) THREAD);
 888           }
 889           JavaCallArguments args(Handle(THREAD, resolution_exception));
 890           THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),


1058     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1059   }
1060   methodHandle method(THREAD, m);
1061 
1062   return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1063 }
1064 
1065 
1066 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1067   oop mirror             = java_lang_reflect_Constructor::clazz(constructor_mirror);
1068   int slot               = java_lang_reflect_Constructor::slot(constructor_mirror);
1069   bool override          = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1070   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1071 
1072   instanceKlassHandle klass(THREAD, java_lang_Class::as_Klass(mirror));
1073   Method* m = klass->method_with_idnum(slot);
1074   if (m == NULL) {
1075     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1076   }
1077   methodHandle method(THREAD, m);
1078   assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1079 
1080   // Make sure klass gets initialize
1081   klass->initialize(CHECK_NULL);
1082 
1083   // Create new instance (the receiver)
1084   klass->check_valid_for_instantiation(false, CHECK_NULL);
1085   Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1086 
1087   // Ignore result from call and return receiver
1088   invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1089   return receiver();
1090 }


  39 #include "prims/jvm.h"
  40 #include "prims/jvmtiExport.hpp"
  41 #include "runtime/arguments.hpp"
  42 #include "runtime/handles.inline.hpp"
  43 #include "runtime/javaCalls.hpp"
  44 #include "runtime/reflection.hpp"
  45 #include "runtime/reflectionUtils.hpp"
  46 #include "runtime/signature.hpp"
  47 #include "runtime/vframe.hpp"
  48 
  49 static void trace_class_resolution(Klass* to_class) {
  50   ResourceMark rm;
  51   int line_number = -1;
  52   const char * source_file = NULL;
  53   Klass* caller = NULL;
  54   JavaThread* jthread = JavaThread::current();
  55   if (jthread->has_last_Java_frame()) {
  56     vframeStream vfst(jthread);
  57     // skip over any frames belonging to java.lang.Class
  58     while (!vfst.at_end() &&
  59            vfst.method()->method_holder()->name()->equals(vmSymbols::java_lang_Class())) {
  60       vfst.next();
  61     }
  62     if (!vfst.at_end()) {
  63       // this frame is a likely suspect
  64       caller = vfst.method()->method_holder();
  65       line_number = vfst.method()->line_number_from_bci(vfst.bci());
  66       Symbol* s = vfst.method()->method_holder()->source_file_name();
  67       if (s != NULL) {
  68         source_file = s->as_C_string();
  69       }
  70     }
  71   }
  72   if (caller != NULL) {
  73     const char * from = caller->external_name();
  74     const char * to = to_class->external_name();
  75     // print in a single call to reduce interleaving between threads
  76     if (source_file != NULL) {
  77       tty->print("RESOLVE %s %s %s:%d (reflection)\n", from, to, source_file, line_number);
  78     } else {
  79       tty->print("RESOLVE %s %s (reflection)\n", from, to);


 850   // Ensure klass is initialized
 851   klass->initialize(CHECK_NULL);
 852 
 853   bool is_static = reflected_method->is_static();
 854   if (is_static) {
 855     // ignore receiver argument
 856     method = reflected_method;
 857     target_klass = klass;
 858   } else {
 859     // check for null receiver
 860     if (receiver.is_null()) {
 861       THROW_0(vmSymbols::java_lang_NullPointerException());
 862     }
 863     // Check class of receiver against class declaring method
 864     if (!receiver->is_a(klass())) {
 865       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
 866     }
 867     // target klass is receiver's klass
 868     target_klass = KlassHandle(THREAD, receiver->klass());
 869     // no need to resolve if method is private or <init>
 870     if (reflected_method->is_private() || reflected_method->name()->equals(vmSymbols::object_initializer_name())) {
 871       method = reflected_method;
 872     } else {
 873       // resolve based on the receiver
 874       if (reflected_method->method_holder()->is_interface()) {
 875         // resolve interface call
 876         //
 877         // Match resolution errors with those thrown due to reflection inlining
 878         // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
 879         method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
 880         if (HAS_PENDING_EXCEPTION) {
 881         // Method resolution threw an exception; wrap it in an InvocationTargetException
 882           oop resolution_exception = PENDING_EXCEPTION;
 883           CLEAR_PENDING_EXCEPTION;
 884           // JVMTI has already reported the pending exception
 885           // JVMTI internal flag reset is needed in order to report InvocationTargetException
 886           if (THREAD->is_Java_thread()) {
 887             JvmtiExport::clear_detected_exception((JavaThread*) THREAD);
 888           }
 889           JavaCallArguments args(Handle(THREAD, resolution_exception));
 890           THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),


1058     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1059   }
1060   methodHandle method(THREAD, m);
1061 
1062   return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1063 }
1064 
1065 
1066 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1067   oop mirror             = java_lang_reflect_Constructor::clazz(constructor_mirror);
1068   int slot               = java_lang_reflect_Constructor::slot(constructor_mirror);
1069   bool override          = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1070   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1071 
1072   instanceKlassHandle klass(THREAD, java_lang_Class::as_Klass(mirror));
1073   Method* m = klass->method_with_idnum(slot);
1074   if (m == NULL) {
1075     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1076   }
1077   methodHandle method(THREAD, m);
1078   assert(method->name()->equals(vmSymbols::object_initializer_name()), "invalid constructor");
1079 
1080   // Make sure klass gets initialize
1081   klass->initialize(CHECK_NULL);
1082 
1083   // Create new instance (the receiver)
1084   klass->check_valid_for_instantiation(false, CHECK_NULL);
1085   Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1086 
1087   // Ignore result from call and return receiver
1088   invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1089   return receiver();
1090 }
src/share/vm/runtime/reflection.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File