if (descriptor[0] == '[') {
return CreateArrayClass(self, descriptor, class_loader);
} else if (class_loader.Get() == nullptr) {
+ // The boot class loader, search the boot class path.
ClassPathEntry pair = FindInClassPath(descriptor, boot_class_path_);
if (pair.second != nullptr) {
StackHandleScope<1> hs(self);
return DefineClass(descriptor, NullHandle<mirror::ClassLoader>(), *pair.first, *pair.second);
+ } else {
+ // The boot class loader is searched ahead of the application class loader, failures are
+ // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to
+ // trigger the chaining with a proper stack trace.
+ mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError();
+ self->SetException(ThrowLocation(), pre_allocated);
+ return nullptr;
}
} else if (Runtime::Current()->UseCompileTimeClassPath()) {
// First try with the bootstrap class loader.
return EnsureResolved(self, descriptor, klass);
}
}
- // If the lookup failed search the boot class path. We don't perform a recursive call to avoid a NoClassDefFoundError being allocated.
+ // If the lookup failed search the boot class path. We don't perform a recursive call to avoid
+ // a NoClassDefFoundError being allocated.
ClassPathEntry pair = FindInClassPath(descriptor, boot_class_path_);
if (pair.second != nullptr) {
StackHandleScope<1> hs(self);
ScopedLocalRef<jobject> class_name_object(soa.Env(),
soa.Env()->NewStringUTF(class_name_string.c_str()));
if (class_name_object.get() == nullptr) {
+ DCHECK(self->IsExceptionPending()); // OOME.
return nullptr;
}
CHECK(class_loader_object.get() != nullptr);
} else if (result.get() == nullptr) {
// broken loader - throw NPE to be compatible with Dalvik
ThrowNullPointerException(nullptr, StringPrintf("ClassLoader.loadClass returned null for %s",
- class_name_string.c_str()).c_str());
+ class_name_string.c_str()).c_str());
return nullptr;
} else {
// success, return mirror::Class*
CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
std::unique_ptr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
- if (options.get() == NULL) {
+ if (options.get() == nullptr) {
LOG(ERROR) << "Failed to parse options";
return false;
}
// ClassLinker needs an attached thread, but we can't fully attach a thread without creating
// objects. We can't supply a thread group yet; it will be fixed later. Since we are the main
// thread, we do not get a java peer.
- Thread* self = Thread::Attach("main", false, NULL, false);
+ Thread* self = Thread::Attach("main", false, nullptr, false);
CHECK_EQ(self->GetThreadId(), ThreadList::kMainThreadId);
- CHECK(self != NULL);
+ CHECK(self != nullptr);
// Set us to runnable so tools using a runtime can allocate and GC by default
self->TransitionFromSuspendedToRunnable();
}
}
} else {
- CHECK(options->boot_class_path_ != NULL);
+ CHECK(options->boot_class_path_ != nullptr);
CHECK_NE(options->boot_class_path_->size(), 0U);
class_linker_->InitWithoutImage(*options->boot_class_path_);
}
- CHECK(class_linker_ != NULL);
+ CHECK(class_linker_ != nullptr);
verifier::MethodVerifier::Init();
method_trace_ = options->method_trace_;
pre_allocated_OutOfMemoryError_ = GcRoot<mirror::Throwable>(self->GetException(NULL));
self->ClearException();
+ // Pre-allocate a NoClassDefFoundError for the common case of failing to find a system class
+ // ahead of checking the application's class loader.
+ self->ThrowNewException(ThrowLocation(), "Ljava/lang/NoClassDefFoundError;",
+ "Class not found using the boot class loader; no stack available");
+ pre_allocated_NoClassDefFoundError_ = GcRoot<mirror::Throwable>(self->GetException(NULL));
+ self->ClearException();
+
// Look for a native bridge.
native_bridge_library_filename_ = options->native_bridge_library_filename_;
android::SetupNativeBridge(native_bridge_library_filename_.c_str(), &native_bridge_art_callbacks_);
mirror::Throwable* Runtime::GetPreAllocatedOutOfMemoryError() {
mirror::Throwable* oome = pre_allocated_OutOfMemoryError_.Read();
- if (oome == NULL) {
+ if (oome == nullptr) {
LOG(ERROR) << "Failed to return pre-allocated OOME";
}
return oome;
}
+mirror::Throwable* Runtime::GetPreAllocatedNoClassDefFoundError() {
+ mirror::Throwable* ncdfe = pre_allocated_NoClassDefFoundError_.Read();
+ if (ncdfe == nullptr) {
+ LOG(ERROR) << "Failed to return pre-allocated NoClassDefFoundError";
+ }
+ return ncdfe;
+}
+
void Runtime::VisitConstantRoots(RootCallback* callback, void* arg) {
// Visit the classes held as static in mirror classes, these can be visited concurrently and only
// need to be visited once per GC since they never change.
}
resolution_method_.VisitRoot(callback, arg, 0, kRootVMInternal);
DCHECK(!resolution_method_.IsNull());
+ if (!pre_allocated_NoClassDefFoundError_.IsNull()) {
+ pre_allocated_NoClassDefFoundError_.VisitRoot(callback, arg, 0, kRootVMInternal);
+ DCHECK(!pre_allocated_NoClassDefFoundError_.IsNull());
+ }
if (HasImtConflictMethod()) {
imt_conflict_method_.VisitRoot(callback, arg, 0, kRootVMInternal);
}