Revert "Add API for creating auto-registering services."

This reverts commit b2171717ad.
This commit is contained in:
Jeremy Wood 2023-03-24 23:32:14 -04:00
parent 4aa1782cfd
commit 376fc8db18
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
5 changed files with 0 additions and 148 deletions

View File

@ -1,69 +0,0 @@
package com.onarandombox.MultiverseCore.inject.registration;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import org.glassfish.hk2.api.Filter;
import org.glassfish.hk2.api.InstanceLifecycleEvent;
import org.glassfish.hk2.api.InstanceLifecycleEventType;
import org.glassfish.hk2.api.InstanceLifecycleListener;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
/**
* Provided as a base class for {@link InstanceLifecycleListener} implementations that automatically register instances
* of a given type.
* <br/>
* This will cause any instance of the given type to be registered as a service unless it is annotated with
* {@link DoNotRegister}. What registration means is up to the implementation.
* <br/>
* Note: Implementations should be annotated with {@link Service} and utilize constructor injection with {@link Inject}
* to ensure that the filter provider is properly injected.
*
* @param <T> The type of instances to automatically register.
*/
public abstract class AbstractAutoRegistration<T> implements InstanceLifecycleListener {
private final Provider<RegistrationFilter> filterProvider;
private final Class<T> autoRegisteredType;
protected AbstractAutoRegistration(
@NotNull Provider<RegistrationFilter> filterProvider,
@NotNull Class<T> autoRegisteredType
) {
this.filterProvider = filterProvider;
this.autoRegisteredType = autoRegisteredType;
}
private RegistrationFilter getRegistrationFilter() {
return filterProvider.get();
}
@Override
public Filter getFilter() {
return null;
}
@Override
public void lifecycleEvent(InstanceLifecycleEvent lifecycleEvent) {
if (lifecycleEvent.getEventType() != InstanceLifecycleEventType.POST_PRODUCTION) {
return;
}
var potentialInstance = lifecycleEvent.getLifecycleObject();
if (shouldRegister(potentialInstance)) {
register(autoRegisteredType.cast(potentialInstance));
}
}
private boolean shouldRegister(Object instance) {
return autoRegisteredType.isInstance(instance) && getRegistrationFilter().shouldRegister(instance);
}
/**
* Called when the given instance should be registered. What registration means is up to the implementation.
* If the instance's class is annotated with {@link DoNotRegister}, this method will not be called for instances of
* that type.
*
* @param instance The instance to register.
*/
protected abstract void register(T instance);
}

View File

@ -1,34 +0,0 @@
package com.onarandombox.MultiverseCore.inject.registration;
import jakarta.inject.Singleton;
import org.glassfish.hk2.api.InstanceLifecycleListener;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
/**
* A binder that binds an implementation of {@link InstanceLifecycleListener} to the {@link InstanceLifecycleListener}
* contract. Additionally, it performs the necessary bindings to support the {@link DoNotRegister} annotation.
*/
public class AutoRegistrationBinder extends AbstractBinder {
/**
* Creates a new {@link AutoRegistrationBinder}.
*
* @param lifecycleListenerClass The implementation of {@link InstanceLifecycleListener} to bind
* @return The new {@link AutoRegistrationBinder}
*/
public static AutoRegistrationBinder with(Class<? extends InstanceLifecycleListener> lifecycleListenerClass) {
return new AutoRegistrationBinder(lifecycleListenerClass);
}
private final Class<? extends InstanceLifecycleListener> lifecycleListenerClass;
private AutoRegistrationBinder(Class<? extends InstanceLifecycleListener> lifecycleListenerClass) {
this.lifecycleListenerClass = lifecycleListenerClass;
}
@Override
protected void configure() {
bind(DoNotRegisterRegistrationFilter.class).in(Singleton.class).to(RegistrationFilter.class).ranked(-1);
bind(lifecycleListenerClass).in(Singleton.class).to(InstanceLifecycleListener.class);
}
}

View File

@ -1,19 +0,0 @@
package com.onarandombox.MultiverseCore.inject.registration;
import org.bukkit.event.Listener;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* An annotation that can be used to prevent a class that would normally be automatically registered from being
* registered automatically.
* <br/>
* For example, any {@link Listener} would normally be registered automatically, but if it is annotated with
* {@code @DoNotRegister}, it will not be registered automatically.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface DoNotRegister { }

View File

@ -1,12 +0,0 @@
package com.onarandombox.MultiverseCore.inject.registration;
import org.jvnet.hk2.annotations.Service;
@Service
class DoNotRegisterRegistrationFilter implements RegistrationFilter {
@Override
public boolean shouldRegister(Object object) {
return object.getClass().getAnnotation(DoNotRegister.class) == null;
}
}

View File

@ -1,14 +0,0 @@
package com.onarandombox.MultiverseCore.inject.registration;
import org.jvnet.hk2.annotations.Contract;
/**
* The base contract for automatic registration filters.
* <br/>
* This is primarily for internal use only. See {@link DoNotRegister} for a practical application.
*/
@Contract
public interface RegistrationFilter {
boolean shouldRegister(Object object);
}