Skip to content

Commit

Permalink
Updates SystemClassLoader to also search framework bundles for resour…
Browse files Browse the repository at this point in the history
…ces.

PiperOrigin-RevId: 531574419
  • Loading branch information
tomball authored and copybara-github committed May 12, 2023
1 parent 07de550 commit 1a4f6cb
Showing 1 changed file with 50 additions and 11 deletions.
61 changes: 50 additions & 11 deletions jre_emul/Classes/java_lang_SystemClassLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,38 @@ JNIEXPORT jclass Java_java_lang_SystemClassLoader_findClass(
return [IOSClass forName:name];
}

// Returns a URL for a specified resource in a bundle, or nil if not found.
static JavaNetURL *getResourceURLFromBundle(NSBundle *bundle, jstring name) {
NSURL *nativeURL = [bundle URLForResource:name withExtension:nil];
if (nativeURL) {
return create_JavaNetURL_initWithNSString_([nativeURL description]);
}
return nil;
}

JNIEXPORT jobject Java_java_lang_SystemClassLoader_findResource(
JNIEnv *env, jobject obj, jstring name) {
if (!name) {
return nil;
}
NSBundle *bundle = [NSBundle mainBundle];
NSURL *nativeURL = [bundle URLForResource:name withExtension:nil];
if (nativeURL) {
return create_JavaNetURL_initWithNSString_([nativeURL description]);

JavaNetURL *url = getResourceURLFromBundle([NSBundle mainBundle], name);
if (url) {
return url;
}

IOSByteArray *data = GetLinkedResource(name);
return data ? CreateResourceURL(name, data) : nil;
if (data) {
return CreateResourceURL(name, data);
}

for (NSBundle *bundle in [NSBundle allFrameworks]) {
JavaNetURL *url = getResourceURLFromBundle(bundle, name);
if (url) {
return url;
}
}
return nil;
}

JNIEXPORT jobject Java_java_lang_SystemClassLoader_findResources(
Expand Down Expand Up @@ -114,19 +134,38 @@ JNIEXPORT jobject Java_java_lang_SystemClassLoader_findResources(
return JavaUtilCollections_enumerationWithJavaUtilCollection_(urls);
}

// Returns an input stream for a specified resource in a bundle, or nil if not found.
static JavaIoBufferedInputStream *getResourceFromBundle(NSBundle *bundle, jstring name) {
NSString *path = [bundle pathForResource:name ofType:nil];
if (path) {
return create_JavaIoBufferedInputStream_initWithJavaIoInputStream_(
create_JavaIoFileInputStream_initWithNSString_(path));
}
return nil;
}

JNIEXPORT jobject Java_java_lang_SystemClassLoader_getResourceAsStream(
JNIEnv *env, jobject obj, jstring name) {
if (!name) {
return nil;
}
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:name ofType:nil];
if (path) {
return create_JavaIoBufferedInputStream_initWithJavaIoInputStream_(
create_JavaIoFileInputStream_initWithNSString_(path));
JavaIoBufferedInputStream *stream = getResourceFromBundle([NSBundle mainBundle], name);
if (stream) {
return stream;
}

// No iOS resource available, check for linked resource.
IOSByteArray *data = GetLinkedResource(name);
return data ? create_JavaIoByteArrayInputStream_initWithByteArray_(data) : nil;
if (data) {
return create_JavaIoByteArrayInputStream_initWithByteArray_(data);
}

// Check framework bundles for resource.
for (NSBundle *bundle in [NSBundle allFrameworks]) {
stream = getResourceFromBundle(bundle, name);
if (stream) {
return stream;
}
}
return nil;
}

0 comments on commit 1a4f6cb

Please sign in to comment.