Skip to content

Commit

Permalink
Guice resection
Browse files Browse the repository at this point in the history
Surgically removes malignant Guice code.

o.o.common.inject is a fork of Guice from many moons ago. It is used to
create type safe instances at runtime w/o the need for Factories. Most of
the functionality has been removed prior to forking OpenSearch. This
housecleaning commit removes stale code that is either not called or
unnecessarily called through the Injector logic path.

Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed Oct 25, 2023
1 parent a890e51 commit 4e2ffd0
Show file tree
Hide file tree
Showing 140 changed files with 568 additions and 6,841 deletions.
155 changes: 1 addition & 154 deletions server/src/main/java/org/opensearch/common/inject/AbstractModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@
package org.opensearch.common.inject;

import org.opensearch.common.inject.binder.AnnotatedBindingBuilder;
import org.opensearch.common.inject.binder.AnnotatedConstantBindingBuilder;
import org.opensearch.common.inject.binder.LinkedBindingBuilder;
import org.opensearch.common.inject.matcher.Matcher;
import org.opensearch.common.inject.spi.Message;
import org.opensearch.common.inject.spi.TypeConverter;
import org.opensearch.common.inject.spi.TypeListener;

import java.lang.annotation.Annotation;
import java.util.Objects;

/**
Expand Down Expand Up @@ -65,7 +58,7 @@ public abstract class AbstractModule implements Module {
Binder binder;

@Override
public final synchronized void configure(Binder builder) {
public final synchronized void configure(final Binder builder) {
if (this.binder != null) {
throw new IllegalStateException("Re-entry is not allowed.");
}
Expand All @@ -89,20 +82,6 @@ protected Binder binder() {
return binder;
}

/**
* @see Binder#bindScope(Class, Scope)
*/
protected void bindScope(Class<? extends Annotation> scopeAnnotation, Scope scope) {
binder.bindScope(scopeAnnotation, scope);
}

/**
* @see Binder#bind(Key)
*/
protected <T> LinkedBindingBuilder<T> bind(Key<T> key) {
return binder.bind(key);
}

/**
* @see Binder#bind(TypeLiteral)
*/
Expand All @@ -116,136 +95,4 @@ protected <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) {
protected <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz) {
return binder.bind(clazz);
}

/**
* @see Binder#bindConstant()
*/
protected AnnotatedConstantBindingBuilder bindConstant() {
return binder.bindConstant();
}

/**
* @see Binder#install(Module)
*/
protected void install(Module module) {
binder.install(module);
}

/**
* @see Binder#addError(String, Object[])
*/
protected void addError(String message, Object... arguments) {
binder.addError(message, arguments);
}

/**
* @see Binder#addError(Throwable)
*/
protected void addError(Throwable t) {
binder.addError(t);
}

/**
* @see Binder#addError(Message)
* @since 2.0
*/
protected void addError(Message message) {
binder.addError(message);
}

/**
* @see Binder#requestInjection(Object)
* @since 2.0
*/
protected void requestInjection(Object instance) {
binder.requestInjection(instance);
}

/**
* @see Binder#requestStaticInjection(Class[])
*/
protected void requestStaticInjection(Class<?>... types) {
binder.requestStaticInjection(types);
}

/**
* Adds a dependency from this module to {@code key}. When the injector is
* created, Guice will report an error if {@code key} cannot be injected.
* Note that this requirement may be satisfied by implicit binding, such as
* a public no-arguments constructor.
*
* @since 2.0
*/
protected void requireBinding(Key<?> key) {
binder.getProvider(key);
}

/**
* Adds a dependency from this module to {@code type}. When the injector is
* created, Guice will report an error if {@code type} cannot be injected.
* Note that this requirement may be satisfied by implicit binding, such as
* a public no-arguments constructor.
*
* @since 2.0
*/
protected void requireBinding(Class<?> type) {
binder.getProvider(type);
}

/**
* @see Binder#getProvider(Key)
* @since 2.0
*/
protected <T> Provider<T> getProvider(Key<T> key) {
return binder.getProvider(key);
}

/**
* @see Binder#getProvider(Class)
* @since 2.0
*/
protected <T> Provider<T> getProvider(Class<T> type) {
return binder.getProvider(type);
}

/**
* @see Binder#convertToTypes
* @since 2.0
*/
protected void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) {
binder.convertToTypes(typeMatcher, converter);
}

/**
* @see Binder#currentStage()
* @since 2.0
*/
protected Stage currentStage() {
return binder.currentStage();
}

/**
* @see Binder#getMembersInjector(Class)
* @since 2.0
*/
protected <T> MembersInjector<T> getMembersInjector(Class<T> type) {
return binder.getMembersInjector(type);
}

/**
* @see Binder#getMembersInjector(TypeLiteral)
* @since 2.0
*/
protected <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> type) {
return binder.getMembersInjector(type);
}

/**
* @see Binder#bindListener(org.opensearch.common.inject.matcher.Matcher,
* org.opensearch.common.inject.spi.TypeListener)
* @since 2.0
*/
protected void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener) {
binder.bindListener(typeMatcher, listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@
import org.opensearch.common.inject.internal.Errors;
import org.opensearch.common.inject.spi.Element;
import org.opensearch.common.inject.spi.ElementVisitor;
import org.opensearch.common.inject.spi.InjectionRequest;
import org.opensearch.common.inject.spi.MembersInjectorLookup;
import org.opensearch.common.inject.spi.Message;
import org.opensearch.common.inject.spi.PrivateElements;
import org.opensearch.common.inject.spi.ProviderLookup;
import org.opensearch.common.inject.spi.ScopeBinding;
import org.opensearch.common.inject.spi.StaticInjectionRequest;
import org.opensearch.common.inject.spi.TypeConverterBinding;
import org.opensearch.common.inject.spi.TypeListenerBinding;

import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -99,21 +93,6 @@ public Boolean visit(ScopeBinding scopeBinding) {
return false;
}

@Override
public Boolean visit(InjectionRequest<?> injectionRequest) {
return false;
}

@Override
public Boolean visit(StaticInjectionRequest staticInjectionRequest) {
return false;
}

@Override
public Boolean visit(TypeConverterBinding typeConverterBinding) {
return false;
}

@Override
public <T> Boolean visit(Binding<T> binding) {
return false;
Expand All @@ -123,19 +102,4 @@ public <T> Boolean visit(Binding<T> binding) {
public <T> Boolean visit(ProviderLookup<T> providerLookup) {
return false;
}

@Override
public Boolean visit(PrivateElements privateElements) {
return false;
}

@Override
public <T> Boolean visit(MembersInjectorLookup<T> lookup) {
return false;
}

@Override
public Boolean visit(TypeListenerBinding binding) {
return false;
}
}
111 changes: 2 additions & 109 deletions server/src/main/java/org/opensearch/common/inject/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@
package org.opensearch.common.inject;

import org.opensearch.common.inject.binder.AnnotatedBindingBuilder;
import org.opensearch.common.inject.binder.AnnotatedConstantBindingBuilder;
import org.opensearch.common.inject.binder.LinkedBindingBuilder;
import org.opensearch.common.inject.matcher.Matcher;
import org.opensearch.common.inject.spi.Message;
import org.opensearch.common.inject.spi.TypeConverter;
import org.opensearch.common.inject.spi.TypeListener;

import java.lang.annotation.Annotation;

Expand Down Expand Up @@ -153,9 +149,7 @@
* Sets up a constant binding. Constant injections must always be annotated.
* When a constant binding's value is a string, it is eligible for conversion to
* all primitive types, to {@link Enum#valueOf all enums}, and to
* {@link Class#forName class literals}. Conversions for other types can be
* configured using {@link #convertToTypes(Matcher, TypeConverter)
* convertToTypes()}.
* {@link Class#forName class literals}.
*
* <pre>
* {@literal @}Color("red") Color red; // A member variable (field)
Expand Down Expand Up @@ -189,8 +183,7 @@
* the problems at runtime, as soon as you try to create your Injector.
*
* <p>The other methods of Binder such as {@link #bindScope},
* {@link #install}, {@link #requestStaticInjection},
* {@link #addError} and {@link #currentStage} are not part of the Binding EDSL;
* {@link #install}, and {@link #addError} are not part of the Binding EDSL;
* you can learn how to use these in the usual way, from the method
* documentation.
*
Expand Down Expand Up @@ -222,48 +215,11 @@ public interface Binder {
*/
<T> AnnotatedBindingBuilder<T> bind(Class<T> type);

/**
* See the EDSL examples at {@link Binder}.
*/
AnnotatedConstantBindingBuilder bindConstant();

/**
* Upon successful creation, the {@link Injector} will inject instance fields
* and methods of the given object.
*
* @param type of instance
* @param instance for which members will be injected
* @since 2.0
*/
<T> void requestInjection(TypeLiteral<T> type, T instance);

/**
* Upon successful creation, the {@link Injector} will inject instance fields
* and methods of the given object.
*
* @param instance for which members will be injected
* @since 2.0
*/
void requestInjection(Object instance);

/**
* Upon successful creation, the {@link Injector} will inject static fields
* and methods in the given classes.
*
* @param types for which static members will be injected
*/
void requestStaticInjection(Class<?>... types);

/**
* Uses the given module to configure more bindings.
*/
void install(Module module);

/**
* Gets the current stage.
*/
Stage currentStage();

/**
* Records an error message which will be presented to the user at a later
* time. Unlike throwing an exception, this enable us to continue
Expand Down Expand Up @@ -298,58 +254,6 @@ public interface Binder {
*/
<T> Provider<T> getProvider(Key<T> key);

/**
* Returns the provider used to obtain instances for the given injection type.
* The returned provider will not be valid until the {@link Injector} has been
* created. The provider will throw an {@code IllegalStateException} if you
* try to use it beforehand.
*
* @since 2.0
*/
<T> Provider<T> getProvider(Class<T> type);

/**
* Returns the members injector used to inject dependencies into methods and fields on instances
* of the given type {@code T}. The returned members injector will not be valid until the main
* {@link Injector} has been created. The members injector will throw an {@code
* IllegalStateException} if you try to use it beforehand.
*
* @param typeLiteral type to get members injector for
* @since 2.0
*/
<T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral);

/**
* Returns the members injector used to inject dependencies into methods and fields on instances
* of the given type {@code T}. The returned members injector will not be valid until the main
* {@link Injector} has been created. The members injector will throw an {@code
* IllegalStateException} if you try to use it beforehand.
*
* @param type type to get members injector for
* @since 2.0
*/
<T> MembersInjector<T> getMembersInjector(Class<T> type);

/**
* Binds a type converter. The injector will use the given converter to
* convert string constants to matching types as needed.
*
* @param typeMatcher matches types the converter can handle
* @param converter converts values
* @since 2.0
*/
void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter);

/**
* Registers a listener for injectable types. Guice will notify the listener when it encounters
* injectable types matched by the given type matcher.
*
* @param typeMatcher that matches injectable types the listener should be notified of
* @param listener for injectable types matched by typeMatcher
* @since 2.0
*/
void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener);

/**
* Returns a binder that uses {@code source} as the reference location for
* configuration errors. This is typically a {@link StackTraceElement}
Expand All @@ -374,15 +278,4 @@ public interface Binder {
* @since 2.0
*/
Binder skipSources(Class<?>... classesToSkip);

/**
* Creates a new private child environment for bindings and other configuration. The returned
* binder can be used to add and configuration information in this environment. See {@link
* PrivateModule} for details.
*
* @return a binder that inherits configuration from this binder. Only exposed configuration on
* the returned binder will be visible to this binder.
* @since 2.0
*/
PrivateBinder newPrivateBinder();
}
Loading

0 comments on commit 4e2ffd0

Please sign in to comment.