Add check for invalid version strings

This commit is contained in:
JeremyStar™ 2024-08-17 14:09:27 +02:00
parent e8ec0ed530
commit db52346168
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 18 additions and 1 deletions

View file

@ -321,6 +321,8 @@ public final class Engine extends SubsystemClass {
//noinspection DataFlowIssue // the crash call will prevent a NullPointerException //noinspection DataFlowIssue // the crash call will prevent a NullPointerException
subsystemsMutable.add(new DependencySubsystemVector(initializedClass.getDependencyVector(), initializedClass)); subsystemsMutable.add(new DependencySubsystemVector(initializedClass.getDependencyVector(), initializedClass));
} catch (Exception exception) { } catch (Exception exception) {
if (exception.getClass() == IllegalStateException.class && exception.getMessage().startsWith("The version string is invalid: "))
logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Invalid version string: " + exception.getMessage().replace("The version string is invalid: ", ""));
logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Method invocation error", exception); logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Method invocation error", exception);
} }
} }

View file

@ -22,6 +22,7 @@ package de.staropensource.sosengine.base.classes;
import de.staropensource.sosengine.base.Engine; import de.staropensource.sosengine.base.Engine;
import de.staropensource.sosengine.base.annotations.EngineSubsystem; import de.staropensource.sosengine.base.annotations.EngineSubsystem;
import de.staropensource.sosengine.base.annotations.EventListener; import de.staropensource.sosengine.base.annotations.EventListener;
import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException;
import de.staropensource.sosengine.base.internal.events.InternalEngineShutdownEvent; import de.staropensource.sosengine.base.internal.events.InternalEngineShutdownEvent;
import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.logging.LoggerInstance;
import de.staropensource.sosengine.base.types.DependencyVector; import de.staropensource.sosengine.base.types.DependencyVector;
@ -73,7 +74,7 @@ public abstract class SubsystemClass {
* @see DependencyVector * @see DependencyVector
* @since v1-alpha1 * @since v1-alpha1
*/ */
public abstract @NotNull DependencyVector getDependencyVector(); public abstract @NotNull DependencyVector getDependencyVector() throws InvalidVersionStringException;
/** /**
* Called on engine shutdown. * Called on engine shutdown.

View file

@ -20,11 +20,13 @@
package de.staropensource.sosengine.base.types; package de.staropensource.sosengine.base.types;
import de.staropensource.sosengine.base.classes.VersioningSystem; import de.staropensource.sosengine.base.classes.VersioningSystem;
import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException;
import de.staropensource.sosengine.base.utility.DependencyResolver; import de.staropensource.sosengine.base.utility.DependencyResolver;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -198,6 +200,18 @@ public class DependencyVector {
throw new IllegalStateException("The versioning system is unset"); throw new IllegalStateException("The versioning system is unset");
if (version == null || version.isBlank()) if (version == null || version.isBlank())
throw new IllegalStateException("The version is unset"); throw new IllegalStateException("The version is unset");
// Check if version string is valid
try {
versioningSystem.getDeclaredConstructor(String.class).newInstance(version);
} catch (InvocationTargetException exception) {
if (exception.getTargetException().getClass() == InvalidVersionStringException.class)
throw new IllegalStateException("The version string is invalid: " + exception.getTargetException().getMessage());
else
throw new IllegalStateException("Version string validation failed: Constructor threw " + exception.getTargetException().getClass().getName() + ": " + exception.getTargetException().getMessage());
} catch (Exception exception) {
throw new IllegalStateException("Version string validation failed: Threw " + exception.getClass().getName() + ": " + exception.getMessage());
}
} }
/** /**