< prev index next >

src/hotspot/share/classfile/classLoader.cpp

Print this page

1027 
1028 // Called by the boot classloader to load classes
1029 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1030   assert(name != nullptr, "invariant");
1031 
1032   ResourceMark rm(THREAD);
1033   HandleMark hm(THREAD);
1034 
1035   const char* const class_name = name->as_C_string();
1036 
1037   EventMarkClassLoading m("Loading class %s", class_name);
1038 
1039   const char* const file_name = file_name_for_class_name(class_name,
1040                                                          name->utf8_length());
1041   assert(file_name != nullptr, "invariant");
1042 
1043   // Lookup stream for parsing .class file
1044   ClassFileStream* stream = nullptr;
1045   s2 classpath_index = 0;
1046   ClassPathEntry* e = nullptr;

1047 
1048   // If search_append_only is true, boot loader visibility boundaries are
1049   // set to be _first_append_entry to the end. This includes:
1050   //   [-Xbootclasspath/a]; [jvmti appended entries]
1051   //
1052   // If search_append_only is false, boot loader visibility boundaries are
1053   // set to be the --patch-module entries plus the base piece. This includes:
1054   //   [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1055   //
1056 
1057   // Load Attempt #1: --patch-module
1058   // Determine the class' defining module.  If it appears in the _patch_mod_entries,
1059   // attempt to load the class from those locations specific to the module.
1060   // Specifications to --patch-module can contain a partial number of classes
1061   // that are part of the overall module definition.  So if a particular class is not
1062   // found within its module specification, the search should continue to Load Attempt #2.
1063   // Note: The --patch-module entries are never searched if the boot loader's
1064   //       visibility boundary is limited to only searching the append entries.
1065   if (_patch_mod_entries != nullptr && !search_append_only) {
1066     assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1067     stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);














1068   }
1069 
1070   // Load Attempt #2: [jimage | exploded build]
1071   if (!search_append_only && (nullptr == stream)) {
1072     if (has_jrt_entry()) {
1073       e = _jrt_entry;
1074       stream = _jrt_entry->open_stream(THREAD, file_name);
1075     } else {
1076       // Exploded build - attempt to locate class in its defining module's location.
1077       assert(_exploded_entries != nullptr, "No exploded build entries present");
1078       assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1079       stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1080     }
1081   }
1082 
1083   // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1084   if (search_append_only && (nullptr == stream)) {
1085     // For the boot loader append path search, the starting classpath_index
1086     // for the appended piece is always 1 to account for either the
1087     // _jrt_entry or the _exploded_entries.

1096       }
1097       e = e->next();
1098       ++classpath_index;
1099     }
1100   }
1101 
1102   if (nullptr == stream) {
1103     return nullptr;
1104   }
1105 
1106   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1107   Handle protection_domain;
1108   ClassLoadInfo cl_info(protection_domain);
1109 
1110   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1111                                                            name,
1112                                                            loader_data,
1113                                                            cl_info,
1114                                                            CHECK_NULL);
1115   result->set_classpath_index(classpath_index);



1116   return result;
1117 }
1118 
1119 #if INCLUDE_CDS
1120 static const char* skip_uri_protocol(const char* source) {
1121   if (strncmp(source, "file:", 5) == 0) {
1122     // file: protocol path could start with file:/ or file:///
1123     // locate the char after all the forward slashes
1124     int offset = 5;
1125     while (*(source + offset) == '/') {
1126         offset++;
1127     }
1128     source += offset;
1129   // for non-windows platforms, move back one char as the path begins with a '/'
1130 #ifndef _WINDOWS
1131     source -= 1;
1132 #endif
1133   } else if (strncmp(source, "jrt:/", 5) == 0) {
1134     source += 5;
1135   }

1172   for (size_t i = 0; i < strlen(uri); ++i) {
1173     char decoded = decode_percent_encoded(uri, i);
1174     path[path_index++] = decoded;
1175   }
1176   path[path_index] = '\0';
1177   return path;
1178 }
1179 
1180 // Record the shared classpath index and loader type for classes loaded
1181 // by the builtin loaders at dump time.
1182 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1183                                 const ClassFileStream* stream, bool redefined) {
1184   assert(CDSConfig::is_dumping_archive(), "sanity");
1185   assert(stream != nullptr, "sanity");
1186 
1187   if (ik->is_hidden()) {
1188     record_hidden_class(ik);
1189     return;
1190   }
1191 




1192   oop loader = ik->class_loader();
1193   char* src = (char*)stream->source();
1194   if (src == nullptr) {
1195     if (loader == nullptr) {
1196       // JFR classes
1197       ik->set_shared_classpath_index(0);
1198     }
1199     return;
1200   }
1201 
1202   if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1203     // A class loaded by a user-defined classloader.
1204     assert(ik->shared_classpath_index() < 0, "not assigned yet");
1205     ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1206     SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1207     return;
1208   }
1209 
1210   assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1211 

1027 
1028 // Called by the boot classloader to load classes
1029 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1030   assert(name != nullptr, "invariant");
1031 
1032   ResourceMark rm(THREAD);
1033   HandleMark hm(THREAD);
1034 
1035   const char* const class_name = name->as_C_string();
1036 
1037   EventMarkClassLoading m("Loading class %s", class_name);
1038 
1039   const char* const file_name = file_name_for_class_name(class_name,
1040                                                          name->utf8_length());
1041   assert(file_name != nullptr, "invariant");
1042 
1043   // Lookup stream for parsing .class file
1044   ClassFileStream* stream = nullptr;
1045   s2 classpath_index = 0;
1046   ClassPathEntry* e = nullptr;
1047   bool is_patched = false;
1048 
1049   // If search_append_only is true, boot loader visibility boundaries are
1050   // set to be _first_append_entry to the end. This includes:
1051   //   [-Xbootclasspath/a]; [jvmti appended entries]
1052   //
1053   // If search_append_only is false, boot loader visibility boundaries are
1054   // set to be the --patch-module entries plus the base piece. This includes:
1055   //   [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1056   //
1057 
1058   // Load Attempt #1: --patch-module
1059   // Determine the class' defining module.  If it appears in the _patch_mod_entries,
1060   // attempt to load the class from those locations specific to the module.
1061   // Specifications to --patch-module can contain a partial number of classes
1062   // that are part of the overall module definition.  So if a particular class is not
1063   // found within its module specification, the search should continue to Load Attempt #2.
1064   // Note: The --patch-module entries are never searched if the boot loader's
1065   //       visibility boundary is limited to only searching the append entries.
1066   if (_patch_mod_entries != nullptr && !search_append_only) {
1067     // At CDS dump time, the --patch-module entries are ignored. That means a
1068     // class is still loaded from the runtime image even if it might
1069     // appear in the _patch_mod_entries. The runtime shared class visibility
1070     // check will determine if a shared class is visible based on the runtime
1071     // environment, including the runtime --patch-module setting.
1072     if (!CDSConfig::is_valhalla_preview()) {
1073       // Dynamic dumping requires UseSharedSpaces to be enabled. Since --patch-module
1074       // is not supported with UseSharedSpaces, we can never come here during dynamic dumping.
1075       assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1076     }
1077     if (CDSConfig::is_valhalla_preview() || !CDSConfig::is_dumping_static_archive()) {
1078       stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1079       if (stream != nullptr) {
1080         is_patched = true;
1081       }
1082     }
1083   }
1084 
1085   // Load Attempt #2: [jimage | exploded build]
1086   if (!search_append_only && (nullptr == stream)) {
1087     if (has_jrt_entry()) {
1088       e = _jrt_entry;
1089       stream = _jrt_entry->open_stream(THREAD, file_name);
1090     } else {
1091       // Exploded build - attempt to locate class in its defining module's location.
1092       assert(_exploded_entries != nullptr, "No exploded build entries present");
1093       assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1094       stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1095     }
1096   }
1097 
1098   // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1099   if (search_append_only && (nullptr == stream)) {
1100     // For the boot loader append path search, the starting classpath_index
1101     // for the appended piece is always 1 to account for either the
1102     // _jrt_entry or the _exploded_entries.

1111       }
1112       e = e->next();
1113       ++classpath_index;
1114     }
1115   }
1116 
1117   if (nullptr == stream) {
1118     return nullptr;
1119   }
1120 
1121   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1122   Handle protection_domain;
1123   ClassLoadInfo cl_info(protection_domain);
1124 
1125   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1126                                                            name,
1127                                                            loader_data,
1128                                                            cl_info,
1129                                                            CHECK_NULL);
1130   result->set_classpath_index(classpath_index);
1131   if (is_patched) {
1132     result->set_shared_classpath_index(0);
1133   }
1134   return result;
1135 }
1136 
1137 #if INCLUDE_CDS
1138 static const char* skip_uri_protocol(const char* source) {
1139   if (strncmp(source, "file:", 5) == 0) {
1140     // file: protocol path could start with file:/ or file:///
1141     // locate the char after all the forward slashes
1142     int offset = 5;
1143     while (*(source + offset) == '/') {
1144         offset++;
1145     }
1146     source += offset;
1147   // for non-windows platforms, move back one char as the path begins with a '/'
1148 #ifndef _WINDOWS
1149     source -= 1;
1150 #endif
1151   } else if (strncmp(source, "jrt:/", 5) == 0) {
1152     source += 5;
1153   }

1190   for (size_t i = 0; i < strlen(uri); ++i) {
1191     char decoded = decode_percent_encoded(uri, i);
1192     path[path_index++] = decoded;
1193   }
1194   path[path_index] = '\0';
1195   return path;
1196 }
1197 
1198 // Record the shared classpath index and loader type for classes loaded
1199 // by the builtin loaders at dump time.
1200 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1201                                 const ClassFileStream* stream, bool redefined) {
1202   assert(CDSConfig::is_dumping_archive(), "sanity");
1203   assert(stream != nullptr, "sanity");
1204 
1205   if (ik->is_hidden()) {
1206     record_hidden_class(ik);
1207     return;
1208   }
1209 
1210   if (ik->shared_classpath_index() == 0 && ik->defined_by_boot_loader()) {
1211     return;
1212   }
1213 
1214   oop loader = ik->class_loader();
1215   char* src = (char*)stream->source();
1216   if (src == nullptr) {
1217     if (loader == nullptr) {
1218       // JFR classes
1219       ik->set_shared_classpath_index(0);
1220     }
1221     return;
1222   }
1223 
1224   if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1225     // A class loaded by a user-defined classloader.
1226     assert(ik->shared_classpath_index() < 0, "not assigned yet");
1227     ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1228     SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1229     return;
1230   }
1231 
1232   assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1233 
< prev index next >