Safer classpath building in ImplementationRegistry

This commit is contained in:
天クマ 2026-05-23 15:12:13 -03:00
commit 811b54517a

View file

@ -9,12 +9,13 @@ public class ImplementationRegistry {
} }
public <T> T getInstance(Class<T> apiClass, String target, String replacement) { public <T> T getInstance(Class<T> apiClass, String target, String replacement) {
String base = apiClass.getName();
String implName = String implName =
apiClass.getName() base.replaceFirst(
.replace( "org\\.adrianvictor\\.lib",
target, "org.adrianvictor.lib.impl." + classSuffix
replacement + classSuffix + "." );
);
try { try {
Class<?> implClass = Class.forName(implName); Class<?> implClass = Class.forName(implName);
@ -23,10 +24,7 @@ public class ImplementationRegistry {
implClass.getDeclaredConstructor().newInstance() implClass.getDeclaredConstructor().newInstance()
); );
} catch (ReflectiveOperationException e) { } catch (ReflectiveOperationException e) {
throw new IllegalStateException( throw new IllegalStateException("Cannot load " + implName, e);
"Cannot load " + implName,
e
);
} }
} }