Skip to content

Commit 3a33f84

Browse files
authored
Catch NCDF instead of using Class.forName (#932)
1 parent d53002d commit 3a33f84

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
import java.util.function.Consumer;
1717
import java.util.function.Supplier;
1818

19-
import jakarta.inject.Provider;
2019
import org.jspecify.annotations.Nullable;
2120

2221
import io.avaje.applog.AppLog;
22+
import io.avaje.config.Configuration;
2323
import io.avaje.inject.spi.AvajeModule;
2424
import io.avaje.inject.spi.Builder;
2525
import io.avaje.inject.spi.ClosePair;
2626
import io.avaje.inject.spi.ConfigPropertyPlugin;
2727
import io.avaje.inject.spi.EnrichBean;
2828
import io.avaje.inject.spi.ModuleOrdering;
2929
import io.avaje.inject.spi.SuppliedBean;
30+
import jakarta.inject.Provider;
3031

3132
/** Build a bean scope with options for shutdown hook and supplying test doubles. */
3233
final class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {
@@ -220,16 +221,19 @@ private ConfigPropertyPlugin defaultPropertyPlugin() {
220221
return detectAvajeConfig() ? new DConfigProps() : new DSystemProps();
221222
}
222223

223-
private boolean detectAvajeConfig() {
224-
if (ModuleLayer.boot().findModule("io.avaje.config").isPresent()) {
225-
return true;
226-
}
227-
try {
228-
Class.forName("io.avaje.config.Configuration", false, classLoader);
229-
return true;
230-
} catch (final ClassNotFoundException e) {
231-
return false;
232-
}
224+
@SuppressWarnings("ConstantValue")
225+
private static boolean detectAvajeConfig() {
226+
var modules = ModuleLayer.boot();
227+
return modules
228+
.findModule("io.avaje.inject")
229+
.map(m -> modules.findModule("io.avaje.config").isPresent())
230+
.orElseGet(() -> {
231+
try {
232+
return Configuration.class != null;
233+
} catch (NoClassDefFoundError e) {
234+
return false;
235+
}
236+
});
233237
}
234238

235239
private void initProfiles() {

inject/src/main/java/io/avaje/inject/spi/ConfigPropertyPlugin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
* Plugin interface which contains the application properties used for wiring. Used with
99
* {@link io.avaje.inject.RequiresProperty} and {@link io.avaje.inject.Profile}.
1010
*
11-
* <p>The plugin is loaded via ServiceLoader and defaults to an implementation that uses
12-
* {@link System#getProperty(String)} and {@link System#getenv(String)}.
11+
* @see InjectExtension
1312
*/
1413
@NullMarked
1514
public interface ConfigPropertyPlugin extends InjectExtension {

0 commit comments

Comments
 (0)