hotspot/src/share/vm/prims/unsafe.cpp
changeset 40923 10fe1c28b9f6
parent 40918 4b0a28b0ed28
child 41066 e8c366069761
equal deleted inserted replaced
40922:d9f1eaf18f9a 40923:10fe1c28b9f6
   781 } UNSAFE_END
   781 } UNSAFE_END
   782 
   782 
   783 
   783 
   784 // define a class but do not make it known to the class loader or system dictionary
   784 // define a class but do not make it known to the class loader or system dictionary
   785 // - host_class:  supplies context for linkage, access control, protection domain, and class loader
   785 // - host_class:  supplies context for linkage, access control, protection domain, and class loader
       
   786 //                if host_class is itself anonymous then it is replaced with its host class.
   786 // - data:  bytes of a class file, a raw memory address (length gives the number of bytes)
   787 // - data:  bytes of a class file, a raw memory address (length gives the number of bytes)
   787 // - cp_patches:  where non-null entries exist, they replace corresponding CP entries in data
   788 // - cp_patches:  where non-null entries exist, they replace corresponding CP entries in data
   788 
   789 
   789 // When you load an anonymous class U, it works as if you changed its name just before loading,
   790 // When you load an anonymous class U, it works as if you changed its name just before loading,
   790 // to a name that you will never use again.  Since the name is lost, no other class can directly
   791 // to a name that you will never use again.  Since the name is lost, no other class can directly
   791 // link to any member of U.  Just after U is loaded, the only way to use it is reflectively,
   792 // link to any member of U.  Just after U is loaded, the only way to use it is reflectively,
   792 // through java.lang.Class methods like Class.newInstance.
   793 // through java.lang.Class methods like Class.newInstance.
   793 
   794 
       
   795 // The package of an anonymous class must either match its host's class's package or be in the
       
   796 // unnamed package.  If it is in the unnamed package then it will be put in its host class's
       
   797 // package.
       
   798 //
       
   799 
   794 // Access checks for linkage sites within U continue to follow the same rules as for named classes.
   800 // Access checks for linkage sites within U continue to follow the same rules as for named classes.
   795 // The package of an anonymous class is given by the package qualifier on the name under which it was loaded.
       
   796 // An anonymous class also has special privileges to access any member of its host class.
   801 // An anonymous class also has special privileges to access any member of its host class.
   797 // This is the main reason why this loading operation is unsafe.  The purpose of this is to
   802 // This is the main reason why this loading operation is unsafe.  The purpose of this is to
   798 // allow language implementations to simulate "open classes"; a host class in effect gets
   803 // allow language implementations to simulate "open classes"; a host class in effect gets
   799 // new code when an anonymous class is loaded alongside it.  A less convenient but more
   804 // new code when an anonymous class is loaded alongside it.  A less convenient but more
   800 // standard way to do this is with reflection, which can also be set to ignore access
   805 // standard way to do this is with reflection, which can also be set to ignore access
   872     host_klass = InstanceKlass::cast(host_klass)->host_klass();
   877     host_klass = InstanceKlass::cast(host_klass)->host_klass();
   873   }
   878   }
   874 
   879 
   875   // Primitive types have NULL Klass* fields in their java.lang.Class instances.
   880   // Primitive types have NULL Klass* fields in their java.lang.Class instances.
   876   if (host_klass == NULL) {
   881   if (host_klass == NULL) {
   877     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
   882     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Host class is null");
   878   }
   883   }
       
   884 
       
   885   assert(host_klass->is_instance_klass(), "Host class must be an instance class");
   879 
   886 
   880   const char* host_source = host_klass->external_name();
   887   const char* host_source = host_klass->external_name();
   881   Handle      host_loader(THREAD, host_klass->class_loader());
   888   Handle      host_loader(THREAD, host_klass->class_loader());
   882   Handle      host_domain(THREAD, host_klass->protection_domain());
   889   Handle      host_domain(THREAD, host_klass->protection_domain());
   883 
   890 
   905   Symbol* no_class_name = NULL;
   912   Symbol* no_class_name = NULL;
   906   Klass* anonk = SystemDictionary::parse_stream(no_class_name,
   913   Klass* anonk = SystemDictionary::parse_stream(no_class_name,
   907                                                 host_loader,
   914                                                 host_loader,
   908                                                 host_domain,
   915                                                 host_domain,
   909                                                 &st,
   916                                                 &st,
   910                                                 host_klass,
   917                                                 InstanceKlass::cast(host_klass),
   911                                                 cp_patches,
   918                                                 cp_patches,
   912                                                 CHECK_NULL);
   919                                                 CHECK_NULL);
   913   if (anonk == NULL) {
   920   if (anonk == NULL) {
   914     return NULL;
   921     return NULL;
   915   }
   922   }