Skip to content

Commit 7b0f67b

Browse files
committedFeb 25, 2025
fix #3 & #4, ResourcePackage has issues process level of path, and asserts has no effect.
1 parent 2c67a0e commit 7b0f67b

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed
 

‎gradle.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = 0.3.1
2-
snapshot = false
1+
version = 0.3.2
2+
snapshot = true
33

44
archive_name = resource-tools
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cc.sukazyo.restools;
2+
3+
import javax.annotation.Nonnull;
4+
5+
public class NoSuchResourceException extends RuntimeException {
6+
7+
public NoSuchResourceException (@Nonnull ClassLoader classLoader, @Nonnull String identifierFilePath) {
8+
super(String.format(
9+
"Cannot find resource \"%s\"! Such path does not exists in the class loader %s.",
10+
identifierFilePath, classLoader
11+
));
12+
}
13+
14+
}

‎src/main/java/cc/sukazyo/restools/ResourcePackage.java

+44-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cc.sukazyo.restools.impl.disk.DiskPackage;
44
import cc.sukazyo.restools.impl.jar.JarPackage;
5+
import cc.sukazyo.restools.utils.PathsHelper;
56

67
import javax.annotation.Nonnull;
78
import javax.annotation.Nullable;
@@ -32,10 +33,13 @@ class UnsupportedPackageTypeException extends Exception {}
3233
* @return A {@link ResourcePackage} that contains the resource.
3334
* @throws UnsupportedPackageTypeException If the resource location cannot be read by any
3435
* of the known implementations.
36+
* @throws NoSuchResourceException If the identifierFilePath is not exists (either directory
37+
* or file) in classloader's all the classpath.
3538
*
3639
* @since 0.3.0
3740
*/
38-
static ResourcePackage get (@Nonnull ClassLoader classLoader, @Nonnull String... identifierFilePath) throws UnsupportedPackageTypeException {
41+
static ResourcePackage get (@Nonnull ClassLoader classLoader, @Nonnull String... identifierFilePath)
42+
throws UnsupportedPackageTypeException, NoSuchResourceException {
3943

4044
try { return new DiskPackage(classLoader, identifierFilePath); } catch (UnsupportedPackageTypeException ignored) {}
4145
try { return new JarPackage(classLoader, identifierFilePath); } catch (UnsupportedPackageTypeException ignored) {}
@@ -44,6 +48,28 @@ static ResourcePackage get (@Nonnull ClassLoader classLoader, @Nonnull String...
4448

4549
}
4650

51+
/**
52+
* Get a resource package.
53+
* <p>
54+
* Different with the v0.2.x and older, the ResourcePackage always locates in the root of the
55+
* classpath, instead of the old behavior of the v0.2.x, which locates in the given path.
56+
*
57+
* @param classLoader the {@link ClassLoader} that is used to load the resource.
58+
* @param identifierFilePath Path of a resource, used to locate a resource package. The path
59+
* will be parsed to an array using {@link PathsHelper#parseString(String)}.
60+
* @return A {@link ResourcePackage} that contains the resource.
61+
* @throws UnsupportedPackageTypeException If the resource location cannot be read by any
62+
* of the known implementations.
63+
* @throws NoSuchResourceException If the identifierFilePath is not exists (either directory
64+
* or file) in classloader's all the classpath.
65+
*
66+
* @since 0.3.0
67+
*/
68+
static ResourcePackage get (@Nonnull ClassLoader classLoader, @Nonnull String identifierFilePath)
69+
throws UnsupportedPackageTypeException, NoSuchResourceException {
70+
return get(classLoader, PathsHelper.parseString(identifierFilePath));
71+
}
72+
4773
/**
4874
* Get the resource package using the current thread's {@link ClassLoader}.
4975
* <p>
@@ -54,10 +80,26 @@ static ResourcePackage get (@Nonnull ClassLoader classLoader, @Nonnull String...
5480
*
5581
* @since 0.3.0
5682
*/
57-
static ResourcePackage get (@Nonnull String... identifierFilePath) throws UnsupportedPackageTypeException {
83+
static ResourcePackage get (@Nonnull String... identifierFilePath)
84+
throws UnsupportedPackageTypeException, NoSuchResourceException {
5885
return get(Thread.currentThread().getContextClassLoader(), identifierFilePath);
5986
}
6087

88+
/**
89+
* Get the resource package using the current thread's {@link ClassLoader}.
90+
* <p>
91+
* This is an alias of {@link #get(ClassLoader, String)}, in most cases where there's no
92+
* custom classloader, this method is enough to work.
93+
*
94+
* @see #get(ClassLoader, String)
95+
*
96+
* @since 0.3.0
97+
*/
98+
static ResourcePackage get (@Nonnull String identifierFilePath)
99+
throws UnsupportedPackageTypeException, NoSuchResourceException {
100+
return get(PathsHelper.parseString(identifierFilePath));
101+
}
102+
61103
/**
62104
* Get the associated {@link ResourcePackage} that contains this {@link ResourceDirectory}.
63105
* <p>

‎src/main/java/cc/sukazyo/restools/impl/disk/DiskPackage.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.sukazyo.restools.impl.disk;
22

3+
import cc.sukazyo.restools.NoSuchResourceException;
34
import cc.sukazyo.restools.ResourcePackage;
45
import cc.sukazyo.restools.utils.PathsHelper;
56

@@ -16,12 +17,13 @@ public class DiskPackage implements ResourcePackage, IDiskDirectory, IDiskEntry
1617
public final Path packageRoot;
1718

1819
public DiskPackage (@Nonnull ClassLoader classLoader, @Nonnull String[] identifierFilePath)
19-
throws UnsupportedPackageTypeException {
20+
throws UnsupportedPackageTypeException, NoSuchResourceException {
2021

2122
final String resPath = PathsHelper.compile(identifierFilePath);
2223
final int resLevel = identifierFilePath.length;
2324
final URL resURL = classLoader.getResource(resPath);
24-
assert resURL != null : "Cannot find resource: " + resPath;
25+
if (resURL == null)
26+
throw new NoSuchResourceException(classLoader, resPath);
2527

2628
if (!"file".equals(resURL.getProtocol()))
2729
throw new UnsupportedPackageTypeException();
@@ -32,6 +34,7 @@ public DiskPackage (@Nonnull ClassLoader classLoader, @Nonnull String[] identifi
3234
} catch (URISyntaxException e) {
3335
throw new IllegalStateException("Cannot get the root dir of the required resource " + resPath + ".", e);
3436
}
37+
3538
this.packageRoot = PathsHelper.getParent(identifierRealPath, resLevel);
3639

3740
}

‎src/main/java/cc/sukazyo/restools/impl/jar/JarPackage.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.sukazyo.restools.impl.jar;
22

3+
import cc.sukazyo.restools.NoSuchResourceException;
34
import cc.sukazyo.restools.ResourcePackage;
45
import cc.sukazyo.restools.impl.jar.tree.IBranchNode;
56
import cc.sukazyo.restools.impl.jar.tree.NodeRoot;
@@ -21,11 +22,13 @@ public class JarPackage implements IJarDirectory, ResourcePackage {
2122
public final NodeRoot jarNodeRoot;
2223

2324
public JarPackage (@Nonnull ClassLoader classLoader, @Nonnull String[] identifierFilePath)
24-
throws UnsupportedPackageTypeException {
25+
throws UnsupportedPackageTypeException, NoSuchResourceException {
2526

2627
final String identifierStringPath = PathsHelper.compile(identifierFilePath);
2728
final URL identifierUrl = classLoader.getResource(identifierStringPath);
28-
assert identifierUrl != null : "Cannot find resource: " + PathsHelper.compile(identifierFilePath);
29+
if (identifierUrl == null)
30+
throw new NoSuchResourceException(classLoader, identifierStringPath);
31+
2932
if (!Objects.equals(identifierUrl.getProtocol(), "jar")) throw new UnsupportedPackageTypeException();
3033

3134
try {

0 commit comments

Comments
 (0)
Please sign in to comment.