src/share/vm/runtime/reflection.cpp

Print this page




 453 
 454 
 455 bool Reflection::verify_class_access(Klass* current_class, Klass* new_class, bool classloader_only) {
 456   // Verify that current_class can access new_class.  If the classloader_only
 457   // flag is set, we automatically allow any accesses in which current_class
 458   // doesn't have a classloader.
 459   if ((current_class == NULL) ||
 460       (current_class == new_class) ||
 461       (new_class->is_public()) ||
 462       is_same_class_package(current_class, new_class)) {
 463     return true;
 464   }
 465   // New (1.4) reflection implementation. Allow all accesses from
 466   // sun/reflect/MagicAccessorImpl subclasses to succeed trivially.
 467   if (   JDK_Version::is_gte_jdk14x_version()
 468       && UseNewReflection
 469       && current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
 470     return true;
 471   }
 472 
 473   // Also allow all accesses from
 474   // java/lang/invoke/MagicLambdaImpl subclasses to succeed trivially.
 475   if (current_class->is_subclass_of(SystemDictionary::lambda_MagicLambdaImpl_klass())) {
 476     return true;
 477   }
 478 
 479   return can_relax_access_check_for(current_class, new_class, classloader_only);
 480 }
 481 
 482 static bool under_host_klass(InstanceKlass* ik, Klass* host_klass) {
 483   DEBUG_ONLY(int inf_loop_check = 1000 * 1000 * 1000);
 484   for (;;) {
 485     Klass* hc = (Klass*) ik->host_klass();
 486     if (hc == NULL)        return false;
 487     if (hc == host_klass)  return true;
 488     ik = InstanceKlass::cast(hc);
 489 
 490     // There's no way to make a host class loop short of patching memory.
 491     // Therefore there cannot be a loop here unles there's another bug.
 492     // Still, let's check for it.
 493     assert(--inf_loop_check > 0, "no host_klass loop");
 494   }
 495 }
 496 
 497 bool Reflection::can_relax_access_check_for(
 498     Klass* accessor, Klass* accessee, bool classloader_only) {


 553             current_class->is_subclass_of(resolved_class) ||
 554             resolved_class->is_subclass_of(current_class)) {
 555           return true;
 556         }
 557       }
 558     }
 559   }
 560 
 561   if (!access.is_private() && is_same_class_package(current_class, field_class)) {
 562     return true;
 563   }
 564 
 565   // New (1.4) reflection implementation. Allow all accesses from
 566   // sun/reflect/MagicAccessorImpl subclasses to succeed trivially.
 567   if (   JDK_Version::is_gte_jdk14x_version()
 568       && UseNewReflection
 569       && current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
 570     return true;
 571   }
 572 
 573   // Also allow all accesses from
 574   // java/lang/invoke/MagicLambdaImpl subclasses to succeed trivially.
 575   if (current_class->is_subclass_of(SystemDictionary::lambda_MagicLambdaImpl_klass())) {
 576     return true;
 577   }
 578 
 579   return can_relax_access_check_for(
 580     current_class, field_class, classloader_only);
 581 }
 582 
 583 
 584 bool Reflection::is_same_class_package(Klass* class1, Klass* class2) {
 585   return InstanceKlass::cast(class1)->is_same_class_package(class2);
 586 }
 587 
 588 bool Reflection::is_same_package_member(Klass* class1, Klass* class2, TRAPS) {
 589   return InstanceKlass::cast(class1)->is_same_package_member(class2, THREAD);
 590 }
 591 
 592 
 593 // Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not,
 594 // throw an incompatible class change exception
 595 // If inner_is_member, require the inner to be a member of the outer.
 596 // If !inner_is_member, require the inner to be anonymous (a non-member).
 597 // Caller is responsible for figuring out in advance which case must be true.
 598 void Reflection::check_for_inner_class(instanceKlassHandle outer, instanceKlassHandle inner,




 453 
 454 
 455 bool Reflection::verify_class_access(Klass* current_class, Klass* new_class, bool classloader_only) {
 456   // Verify that current_class can access new_class.  If the classloader_only
 457   // flag is set, we automatically allow any accesses in which current_class
 458   // doesn't have a classloader.
 459   if ((current_class == NULL) ||
 460       (current_class == new_class) ||
 461       (new_class->is_public()) ||
 462       is_same_class_package(current_class, new_class)) {
 463     return true;
 464   }
 465   // New (1.4) reflection implementation. Allow all accesses from
 466   // sun/reflect/MagicAccessorImpl subclasses to succeed trivially.
 467   if (   JDK_Version::is_gte_jdk14x_version()
 468       && UseNewReflection
 469       && current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
 470     return true;
 471   }
 472 






 473   return can_relax_access_check_for(current_class, new_class, classloader_only);
 474 }
 475 
 476 static bool under_host_klass(InstanceKlass* ik, Klass* host_klass) {
 477   DEBUG_ONLY(int inf_loop_check = 1000 * 1000 * 1000);
 478   for (;;) {
 479     Klass* hc = (Klass*) ik->host_klass();
 480     if (hc == NULL)        return false;
 481     if (hc == host_klass)  return true;
 482     ik = InstanceKlass::cast(hc);
 483 
 484     // There's no way to make a host class loop short of patching memory.
 485     // Therefore there cannot be a loop here unles there's another bug.
 486     // Still, let's check for it.
 487     assert(--inf_loop_check > 0, "no host_klass loop");
 488   }
 489 }
 490 
 491 bool Reflection::can_relax_access_check_for(
 492     Klass* accessor, Klass* accessee, bool classloader_only) {


 547             current_class->is_subclass_of(resolved_class) ||
 548             resolved_class->is_subclass_of(current_class)) {
 549           return true;
 550         }
 551       }
 552     }
 553   }
 554 
 555   if (!access.is_private() && is_same_class_package(current_class, field_class)) {
 556     return true;
 557   }
 558 
 559   // New (1.4) reflection implementation. Allow all accesses from
 560   // sun/reflect/MagicAccessorImpl subclasses to succeed trivially.
 561   if (   JDK_Version::is_gte_jdk14x_version()
 562       && UseNewReflection
 563       && current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
 564     return true;
 565   }
 566 






 567   return can_relax_access_check_for(
 568     current_class, field_class, classloader_only);
 569 }
 570 
 571 
 572 bool Reflection::is_same_class_package(Klass* class1, Klass* class2) {
 573   return InstanceKlass::cast(class1)->is_same_class_package(class2);
 574 }
 575 
 576 bool Reflection::is_same_package_member(Klass* class1, Klass* class2, TRAPS) {
 577   return InstanceKlass::cast(class1)->is_same_package_member(class2, THREAD);
 578 }
 579 
 580 
 581 // Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not,
 582 // throw an incompatible class change exception
 583 // If inner_is_member, require the inner to be a member of the outer.
 584 // If !inner_is_member, require the inner to be anonymous (a non-member).
 585 // Caller is responsible for figuring out in advance which case must be true.
 586 void Reflection::check_for_inner_class(instanceKlassHandle outer, instanceKlassHandle inner,