Browse Source

Add functional style provider registration

spock
parent
commit
80ade86128
5 changed files with 34 additions and 0 deletions
  1. +6
    -0
      README.md
  2. +5
    -0
      src/main/java/org/perfectable/injection/Configuration.java
  3. +10
    -0
      src/main/java/org/perfectable/injection/registration/FunctionalProvider.java
  4. +9
    -0
      src/main/java/org/perfectable/injection/registration/ProviderRegistration.java
  5. +4
    -0
      src/main/java/org/perfectable/injection/registration/Registration.java

+ 6
- 0
README.md View File

@@ -43,6 +43,12 @@ Type constructed by provider:
registry.registerProvider(MyProvidedService.class, MyProvidedService::createCustom);
```

Also just by providing serializable functional interface:

```java
registry.registerProvider(MyProvidedService::createCustom);
```

Or external dependency fetching mechanism:

```java


+ 5
- 0
src/main/java/org/perfectable/injection/Configuration.java View File

@@ -2,6 +2,7 @@ package org.perfectable.injection;

import org.perfectable.injection.registration.Factory;
import org.perfectable.injection.registration.FunctionalFactory;
import org.perfectable.injection.registration.FunctionalProvider;
import org.perfectable.injection.registration.Registration;
import org.perfectable.injection.registration.Retriever;
import org.perfectable.injection.replacement.Replacement;
@@ -22,6 +23,10 @@ public interface Configuration {
register(Registration.type(type));
}

default <T> void registerProvider(FunctionalProvider<T> provider) {
register(Registration.provider(provider));
}

default <T> void registerProvider(Class<T> type, Provider<T> provider) {
register(Registration.provider(type, provider));
}


+ 10
- 0
src/main/java/org/perfectable/injection/registration/FunctionalProvider.java View File

@@ -0,0 +1,10 @@
package org.perfectable.injection.registration;

import org.perfectable.introspection.FunctionalReference;

import javax.inject.Provider;

@FunctionalInterface
public interface FunctionalProvider<T> extends Provider<T>, FunctionalReference {
// uses Provider method
}

+ 9
- 0
src/main/java/org/perfectable/injection/registration/ProviderRegistration.java View File

@@ -5,6 +5,7 @@ import org.perfectable.injection.Registry;
import org.perfectable.injection.replacement.Replacement;
import org.perfectable.injection.scope.Scope;
import org.perfectable.injection.scope.ScopeSelector;
import org.perfectable.introspection.FunctionalReference;

import java.lang.annotation.Annotation;
import java.util.Set;
@@ -18,6 +19,14 @@ public final class ProviderRegistration<T> implements Registration {
private final Class<T> createdClass;
private final Provider<T> provider;

public static <T> ProviderRegistration<T> create(FunctionalProvider<T> provider) {
requireNonNull(provider);
FunctionalReference.Introspection introspection = provider.introspect();
@SuppressWarnings("unchecked")
Class<T> createdClass = (Class<T>) introspection.resultType();
return create(createdClass, provider);
}

public static <T> ProviderRegistration<T> create(Class<T> createdClass, Provider<T> provider) {
requireNonNull(createdClass);
requireNonNull(provider);


+ 4
- 0
src/main/java/org/perfectable/injection/registration/Registration.java View File

@@ -19,6 +19,10 @@ public interface Registration {
return LoaderRegistration.create(type);
}

static <T> Registration provider(FunctionalProvider<T> provider) {
return ProviderRegistration.create(provider);
}

static <T> Registration provider(Class<T> type, Provider<T> provider) {
return ProviderRegistration.create(type, provider);
}


Loading…
Cancel
Save