Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.

Commit e510ab4

Browse files
committed
chore: optimized main class grabbing
Co-authored-by: @ScaredDev
1 parent 6afc82f commit e510ab4

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'de.interguess'
7-
version '1.0-beta'
7+
version '1.0.1-beta'
88

99
repositories {
1010
mavenCentral()

src/main/java/de/interguess/javamoduleloader/module/Module.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.interguess.javamoduleloader.module;
22

3+
import de.interguess.javamoduleloader.exception.ModuleLoadException;
34
import org.jetbrains.annotations.NotNull;
45
import org.jetbrains.annotations.Nullable;
56

@@ -44,7 +45,7 @@ public interface Module<T> {
4445
* @param requiredSuperClass the required superclass of the main class
4546
* @param mainClass the name of the main class
4647
* @return the main class of the module
47-
* @throws ClassNotFoundException if the main class could not be found
48+
* @throws ModuleLoadException if there was an error loading or verifying the main class
4849
*/
49-
@NotNull Class<T> getMainClassByName(@NotNull Class<T> requiredSuperClass, @NotNull String mainClass) throws ClassNotFoundException;
50+
@NotNull Class<T> getMainClassByName(@NotNull Class<T> requiredSuperClass, @NotNull String mainClass) throws ModuleLoadException;
5051
}

src/main/java/de/interguess/javamoduleloader/module/SimpleModule.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ public SimpleModule(File file) throws ModuleLoadException {
5050
}
5151

5252
@Override
53-
public @NotNull Class<T> getMainClassByName(@NotNull Class<T> requiredSuperClass, @NotNull String mainClass) throws ClassNotFoundException {
54-
Class<?> clazz = classLoader.loadClass(mainClass);
53+
public @NotNull Class<T> getMainClassByName(@NotNull Class<T> requiredSuperClass, @NotNull String mainClass) throws ModuleLoadException {
54+
try {
55+
Class<?> clazz = classLoader.loadClass(mainClass);
56+
57+
if (!requiredSuperClass.isAssignableFrom(clazz)) {
58+
throw new ModuleLoadException("Class " + mainClass + " does not extend or implement " + requiredSuperClass.getName());
59+
}
5560

56-
if (clazz == null) {
57-
throw new ClassNotFoundException("Class not found: " + mainClass);
58-
} else if (!clazz.getSuperclass().equals(requiredSuperClass)) {
59-
throw new ClassNotFoundException("Class " + mainClass + " does not implement " + requiredSuperClass.getName());
60-
} else {
6161
return (Class<T>) clazz;
62+
} catch (ClassNotFoundException e) {
63+
throw new ModuleLoadException("Class not found: " + mainClass, e);
6264
}
6365
}
6466
}

0 commit comments

Comments
 (0)