hotspot/src/share/vm/oops/instanceKlass.cpp
changeset 36508 5f9eee6b383b
parent 36300 5b47f168b948
child 36812 4f96f15e4a46
equal deleted inserted replaced
36507:c80f6ecb0bb3 36508:5f9eee6b383b
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
  1970   // --> see ArrayKlass::complete_create_array_klass()
  1970   // --> see ArrayKlass::complete_create_array_klass()
  1971   k->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
  1971   k->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
  1972 }
  1972 }
  1973 
  1973 
  1974 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
  1974 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
       
  1975   instanceKlassHandle ik(THREAD, this);
       
  1976   ik->set_package(loader_data, CHECK);
  1975   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
  1977   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
  1976   instanceKlassHandle ik(THREAD, this);
       
  1977 
  1978 
  1978   Array<Method*>* methods = ik->methods();
  1979   Array<Method*>* methods = ik->methods();
  1979   int num_methods = methods->length();
  1980   int num_methods = methods->length();
  1980   for (int index2 = 0; index2 < num_methods; ++index2) {
  1981   for (int index2 = 0; index2 < num_methods; ++index2) {
  1981     methodHandle m(THREAD, methods->at(index2));
  1982     methodHandle m(THREAD, methods->at(index2));
  2176   dest[dest_index++] = ';';
  2177   dest[dest_index++] = ';';
  2177   dest[dest_index] = '\0';
  2178   dest[dest_index] = '\0';
  2178   return dest;
  2179   return dest;
  2179 }
  2180 }
  2180 
  2181 
  2181 // different verisons of is_same_class_package
  2182 const jbyte* InstanceKlass::package_from_name(const Symbol* name, int& length) {
       
  2183   ResourceMark rm;
       
  2184   length = 0;
       
  2185   if (name == NULL) {
       
  2186     return NULL;
       
  2187   } else {
       
  2188     const jbyte* base_name = name->base();
       
  2189     const jbyte* last_slash = UTF8::strrchr(base_name, name->utf8_length(), '/');
       
  2190 
       
  2191     if (last_slash == NULL) {
       
  2192       // No package name
       
  2193       return NULL;
       
  2194     } else {
       
  2195       // Skip over '['s
       
  2196       if (*base_name == '[') {
       
  2197         do {
       
  2198           base_name++;
       
  2199         } while (*base_name == '[');
       
  2200         if (*base_name != 'L') {
       
  2201           // Fully qualified class names should not contain a 'L'.
       
  2202           // Set length to -1 to indicate that the package name
       
  2203           // could not be obtained due to an error condition.
       
  2204           // In this situtation, is_same_class_package returns false.
       
  2205           length = -1;
       
  2206           return NULL;
       
  2207         }
       
  2208       }
       
  2209 
       
  2210       // Found the package name, look it up in the symbol table.
       
  2211       length = last_slash - base_name;
       
  2212       assert(length > 0, "Bad length for package name");
       
  2213       return base_name;
       
  2214     }
       
  2215   }
       
  2216 }
       
  2217 
       
  2218 ModuleEntry* InstanceKlass::module() const {
       
  2219   if (!in_unnamed_package()) {
       
  2220     return _package_entry->module();
       
  2221   }
       
  2222   const Klass* host = host_klass();
       
  2223   if (host == NULL) {
       
  2224     return class_loader_data()->modules()->unnamed_module();
       
  2225   }
       
  2226   return host->class_loader_data()->modules()->unnamed_module();
       
  2227 }
       
  2228 
       
  2229 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
       
  2230   int length = 0;
       
  2231   const jbyte* base_name = package_from_name(name(), length);
       
  2232 
       
  2233   if (base_name != NULL && loader_data != NULL) {
       
  2234     TempNewSymbol pkg_name = SymbolTable::new_symbol((const char*)base_name, length, CHECK);
       
  2235 
       
  2236     // Find in class loader's package entry table.
       
  2237     _package_entry = loader_data->packages()->lookup_only(pkg_name);
       
  2238 
       
  2239     // If the package name is not found in the loader's package
       
  2240     // entry table, it is an indication that the package has not
       
  2241     // been defined. Consider it defined within the unnamed module.
       
  2242     if (_package_entry == NULL) {
       
  2243       ResourceMark rm;
       
  2244 
       
  2245       if (!ModuleEntryTable::javabase_defined()) {
       
  2246         // Before java.base is defined during bootstrapping, define all packages in
       
  2247         // the java.base module.  If a non-java.base package is erroneously placed
       
  2248         // in the java.base module it will be caught later when java.base
       
  2249         // is defined by ModuleEntryTable::verify_javabase_packages check.
       
  2250         assert(ModuleEntryTable::javabase_module() != NULL, "java.base module is NULL");
       
  2251         _package_entry = loader_data->packages()->lookup(pkg_name, ModuleEntryTable::javabase_module());
       
  2252       } else {
       
  2253         assert(loader_data->modules()->unnamed_module() != NULL, "unnamed module is NULL");
       
  2254         _package_entry = loader_data->packages()->lookup(pkg_name,
       
  2255                                                          loader_data->modules()->unnamed_module());
       
  2256       }
       
  2257 
       
  2258       // A package should have been successfully created
       
  2259       assert(_package_entry != NULL, "Package entry for class %s not found, loader %s",
       
  2260              name()->as_C_string(), loader_data->loader_name());
       
  2261     }
       
  2262 
       
  2263     if (log_is_enabled(Debug, modules)) {
       
  2264       ResourceMark rm;
       
  2265       ModuleEntry* m = _package_entry->module();
       
  2266       log_trace(modules)("Setting package: class: %s, package: %s, loader: %s, module: %s",
       
  2267                          external_name(),
       
  2268                          pkg_name->as_C_string(),
       
  2269                          loader_data->loader_name(),
       
  2270                          (m->is_named() ? m->name()->as_C_string() : UNNAMED_MODULE));
       
  2271     }
       
  2272   } else {
       
  2273     ResourceMark rm;
       
  2274     log_trace(modules)("Setting package: class: %s, package: unnamed, loader: %s, module: %s",
       
  2275                        external_name(),
       
  2276                        (loader_data != NULL) ? loader_data->loader_name() : "NULL",
       
  2277                        UNNAMED_MODULE);
       
  2278   }
       
  2279 }
       
  2280 
       
  2281 
       
  2282 // different versions of is_same_class_package
       
  2283 
  2182 bool InstanceKlass::is_same_class_package(const Klass* class2) const {
  2284 bool InstanceKlass::is_same_class_package(const Klass* class2) const {
  2183   const Klass* const class1 = (const Klass* const)this;
  2285   oop classloader1 = this->class_loader();
  2184   oop classloader1 = InstanceKlass::cast(class1)->class_loader();
  2286   PackageEntry* classpkg1 = this->package();
  2185   const Symbol* const classname1 = class1->name();
       
  2186 
       
  2187   if (class2->is_objArray_klass()) {
  2287   if (class2->is_objArray_klass()) {
  2188     class2 = ObjArrayKlass::cast(class2)->bottom_klass();
  2288     class2 = ObjArrayKlass::cast(class2)->bottom_klass();
  2189   }
  2289   }
       
  2290 
  2190   oop classloader2;
  2291   oop classloader2;
       
  2292   PackageEntry* classpkg2;
  2191   if (class2->is_instance_klass()) {
  2293   if (class2->is_instance_klass()) {
  2192     classloader2 = InstanceKlass::cast(class2)->class_loader();
  2294     classloader2 = class2->class_loader();
       
  2295     classpkg2 = InstanceKlass::cast(class2)->package();
  2193   } else {
  2296   } else {
  2194     assert(class2->is_typeArray_klass(), "should be type array");
  2297     assert(class2->is_typeArray_klass(), "should be type array");
  2195     classloader2 = NULL;
  2298     classloader2 = NULL;
  2196   }
  2299     classpkg2 = NULL;
  2197   const Symbol* classname2 = class2->name();
  2300   }
  2198 
  2301 
  2199   return InstanceKlass::is_same_class_package(classloader1, classname1,
  2302   // Same package is determined by comparing class loader
  2200                                               classloader2, classname2);
  2303   // and package entries. Both must be the same. This rule
       
  2304   // applies even to classes that are defined in the unnamed
       
  2305   // package, they still must have the same class loader.
       
  2306   if ((classloader1 == classloader2) && (classpkg1 == classpkg2)) {
       
  2307     return true;
       
  2308   }
       
  2309 
       
  2310   return false;
  2201 }
  2311 }
  2202 
  2312 
  2203 bool InstanceKlass::is_same_class_package(oop other_class_loader,
  2313 bool InstanceKlass::is_same_class_package(oop other_class_loader,
  2204                                           const Symbol* other_class_name) const {
  2314                                           const Symbol* other_class_name) const {
  2205   oop this_class_loader = class_loader();
  2315   oop this_class_loader = class_loader();
  2223     ResourceMark rm;
  2333     ResourceMark rm;
  2224 
  2334 
  2225     // The Symbol*'s are in UTF8 encoding. Since we only need to check explicitly
  2335     // The Symbol*'s are in UTF8 encoding. Since we only need to check explicitly
  2226     // for ASCII characters ('/', 'L', '['), we can keep them in UTF8 encoding.
  2336     // for ASCII characters ('/', 'L', '['), we can keep them in UTF8 encoding.
  2227     // Otherwise, we just compare jbyte values between the strings.
  2337     // Otherwise, we just compare jbyte values between the strings.
  2228     const jbyte *name1 = class_name1->base();
  2338     int length1 = 0;
  2229     const jbyte *name2 = class_name2->base();
  2339     int length2 = 0;
  2230 
  2340     const jbyte *name1 = package_from_name(class_name1, length1);
  2231     const jbyte *last_slash1 = UTF8::strrchr(name1, class_name1->utf8_length(), '/');
  2341     const jbyte *name2 = package_from_name(class_name2, length2);
  2232     const jbyte *last_slash2 = UTF8::strrchr(name2, class_name2->utf8_length(), '/');
  2342 
  2233 
  2343     if ((length1 < 0) || (length2 < 0)) {
  2234     if ((last_slash1 == NULL) || (last_slash2 == NULL)) {
  2344       // error occurred parsing package name.
       
  2345       return false;
       
  2346     }
       
  2347 
       
  2348     if ((name1 == NULL) || (name2 == NULL)) {
  2235       // One of the two doesn't have a package.  Only return true
  2349       // One of the two doesn't have a package.  Only return true
  2236       // if the other one also doesn't have a package.
  2350       // if the other one also doesn't have a package.
  2237       return last_slash1 == last_slash2;
  2351       return name1 == name2;
  2238     } else {
  2352     }
  2239       // Skip over '['s
  2353 
  2240       if (*name1 == '[') {
  2354     // Check that package part is identical
  2241         do {
  2355     return UTF8::equal(name1, length1, name2, length2);
  2242           name1++;
       
  2243         } while (*name1 == '[');
       
  2244         if (*name1 != 'L') {
       
  2245           // Something is terribly wrong.  Shouldn't be here.
       
  2246           return false;
       
  2247         }
       
  2248       }
       
  2249       if (*name2 == '[') {
       
  2250         do {
       
  2251           name2++;
       
  2252         } while (*name2 == '[');
       
  2253         if (*name2 != 'L') {
       
  2254           // Something is terribly wrong.  Shouldn't be here.
       
  2255           return false;
       
  2256         }
       
  2257       }
       
  2258 
       
  2259       // Check that package part is identical
       
  2260       int length1 = last_slash1 - name1;
       
  2261       int length2 = last_slash2 - name2;
       
  2262 
       
  2263       return UTF8::equal(name1, length1, name2, length2);
       
  2264     }
       
  2265   }
  2356   }
  2266 }
  2357 }
  2267 
  2358 
  2268 // Returns true iff super_method can be overridden by a method in targetclassname
  2359 // Returns true iff super_method can be overridden by a method in targetclassname
  2269 // See JSL 3rd edition 8.4.6.1
  2360 // See JSL 3rd edition 8.4.6.1
  2298                                                 TRAPS) {
  2389                                                 TRAPS) {
  2299   if (class2 == class1) return true;
  2390   if (class2 == class1) return true;
  2300   if (!class2->is_instance_klass())  return false;
  2391   if (!class2->is_instance_klass())  return false;
  2301 
  2392 
  2302   // must be in same package before we try anything else
  2393   // must be in same package before we try anything else
  2303   if (!class1->is_same_class_package(class2->class_loader(), class2->name()))
  2394   if (!class1->is_same_class_package(class2))
  2304     return false;
  2395     return false;
  2305 
  2396 
  2306   // As long as there is an outer1.getEnclosingClass,
  2397   // As long as there is an outer1.getEnclosingClass,
  2307   // shift the search outward.
  2398   // shift the search outward.
  2308   const InstanceKlass* outer1 = class1;
  2399   const InstanceKlass* outer1 = class1;
  2906   return external_name();
  2997   return external_name();
  2907 }
  2998 }
  2908 
  2999 
  2909 void InstanceKlass::print_loading_log(LogLevel::type type,
  3000 void InstanceKlass::print_loading_log(LogLevel::type type,
  2910                                       ClassLoaderData* loader_data,
  3001                                       ClassLoaderData* loader_data,
       
  3002                                       const char* module_name,
  2911                                       const ClassFileStream* cfs) const {
  3003                                       const ClassFileStream* cfs) const {
  2912   ResourceMark rm;
  3004   ResourceMark rm;
  2913   outputStream* log;
  3005   outputStream* log;
  2914 
  3006 
  2915   assert(type == LogLevel::Info || type == LogLevel::Debug, "sanity");
  3007   assert(type == LogLevel::Info || type == LogLevel::Debug, "sanity");
  2926   log->print("%s", external_name());
  3018   log->print("%s", external_name());
  2927 
  3019 
  2928   // Source
  3020   // Source
  2929   if (cfs != NULL) {
  3021   if (cfs != NULL) {
  2930     if (cfs->source() != NULL) {
  3022     if (cfs->source() != NULL) {
  2931       log->print(" source: %s", cfs->source());
  3023       if (module_name != NULL) {
       
  3024         log->print(" source: jrt:/%s", module_name);
       
  3025       } else {
       
  3026         log->print(" source: %s", cfs->source());
       
  3027       }
  2932     } else if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
  3028     } else if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
  2933       Thread* THREAD = Thread::current();
  3029       Thread* THREAD = Thread::current();
  2934       Klass* caller =
  3030       Klass* caller =
  2935             THREAD->is_Java_thread()
  3031             THREAD->is_Java_thread()
  2936                 ? ((JavaThread*)THREAD)->security_get_caller_class(1)
  3032                 ? ((JavaThread*)THREAD)->security_get_caller_class(1)