Supplier alternative

Hey! I have some optional dependencies in my pom.xml (scope: provided). Now for each dependency I have a Hook class, which I would like to create an object of if the dependency is found. The following has bee confirmed to work:
public void register() {
if (isEnabled("MyDependency")) {
MyDependencyHook hook = new MyDependencyHook();
hookManager.setMyDependencyHook(hook);
}
}
public void register() {
if (isEnabled("MyDependency")) {
MyDependencyHook hook = new MyDependencyHook();
hookManager.setMyDependencyHook(hook);
}
}
While this works, it comes at the cost of having a bunch of code when handling multiple dependencies. So what I came up with instead is:
public void register() {
enable("MyDependency", MyDependencyHook::new, hookManager::setMyDependencyHook);
}

private <T> void enable(String name, Supplier<T> supplier, Consumer<T> consumer) {
if (!isEnabled(name)) return;

T instance = supplier.get();
consumer.accept(instance);
}
public void register() {
enable("MyDependency", MyDependencyHook::new, hookManager::setMyDependencyHook);
}

private <T> void enable(String name, Supplier<T> supplier, Consumer<T> consumer) {
if (!isEnabled(name)) return;

T instance = supplier.get();
consumer.accept(instance);
}
This cleans up my code base quite a bit, however, this will cause a java.lang.NoClassDefFoundError, which I believe is caused by the static reference of the hook? Can someone help me figure out an alternative? Any help is greatly appreciated^^
6 Replies
JavaBot
JavaBot10mo ago
This post has been reserved for your question.
Hey @Aze! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Aze
AzeOP10mo ago
It seems the function gets executed immediately, and the get method only returns the result. Is there an alternative that only executes the function on method call?
Kyo-chan
Kyo-chan10mo ago
Use reflection
JavaBot
JavaBot10mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Aze
AzeOP10mo ago
private <T> void enable(String name, Class<T> clazz, Consumer<T> consumer) {
if (isEnabled(name)) return;

try {
T instance = clazz.getDeclaredConstructor().newInstance();
consumer.accept(instance);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
private <T> void enable(String name, Class<T> clazz, Consumer<T> consumer) {
if (isEnabled(name)) return;

try {
T instance = clazz.getDeclaredConstructor().newInstance();
consumer.accept(instance);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
same issue
JavaBot
JavaBot10mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server