-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iss 32. add support for Provides annotation
- Loading branch information
Showing
9 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
core/src/main/java/ch/jalu/injector/annotations/Provides.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ch.jalu.injector.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marks that a method provides a certain type. The return value of the method | ||
* is used as the type. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface Provides { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
core/src/main/java/ch/jalu/injector/config/InjectorConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ch.jalu.injector.config; | ||
|
||
/** | ||
* Marker interface for an injector configuration class. | ||
*/ | ||
public interface InjectorConfiguration { | ||
} |
28 changes: 28 additions & 0 deletions
28
core/src/main/java/ch/jalu/injector/config/ProviderByMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ch.jalu.injector.config; | ||
|
||
import ch.jalu.injector.utils.InjectorUtils; | ||
import ch.jalu.injector.utils.ReflectionUtils; | ||
|
||
import javax.inject.Provider; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Wraps a method call as a Provider. | ||
*/ | ||
public class ProviderByMethod<T> implements Provider<T> { | ||
|
||
private final Method method; | ||
private final Object instance; | ||
|
||
public ProviderByMethod(Method method, Object instance) { | ||
InjectorUtils.checkArgument(method.getParameterTypes().length == 0, | ||
"Provider method may not take any arguments"); | ||
this.method = method; | ||
this.instance = instance; | ||
} | ||
|
||
@Override | ||
public T get() { | ||
return (T) ReflectionUtils.invokeMethod(method, instance); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
core/src/main/java/ch/jalu/injector/config/ProviderMethodsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ch.jalu.injector.config; | ||
|
||
import ch.jalu.injector.annotations.Provides; | ||
import ch.jalu.injector.exceptions.InjectorException; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import static ch.jalu.injector.utils.InjectorUtils.checkArgument; | ||
|
||
/** | ||
* Has methods for {@link Provides} methods. | ||
*/ | ||
public final class ProviderMethodsUtil { | ||
|
||
private ProviderMethodsUtil() { | ||
} | ||
|
||
public static boolean isProviderMethod(Method method) { | ||
if (!method.isAnnotationPresent(Provides.class)) { | ||
return false; | ||
} | ||
|
||
checkArgument(method.getParameterTypes().length == 0, "@Provides method '" + method | ||
+ "' is not a no-argument method"); | ||
return true; | ||
} | ||
|
||
public static Class<?> getProviderMethodType(Method method) { | ||
Class<?> returnType = method.getReturnType(); | ||
if (returnType.equals(Void.TYPE)) { | ||
throw new InjectorException("@Provides method '" + method + "' has void return type"); | ||
} | ||
return returnType; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
.../src/test/java/ch/jalu/injector/samples/configurations/InvalidInjectorConfigurations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ch.jalu.injector.samples.configurations; | ||
|
||
import ch.jalu.injector.annotations.Provides; | ||
import ch.jalu.injector.config.InjectorConfiguration; | ||
import ch.jalu.injector.samples.ProvidedClass; | ||
|
||
/** | ||
* Collection of invalid configurations. | ||
*/ | ||
public class InvalidInjectorConfigurations { | ||
|
||
public static final class ConfigurationWithVoidReturnType implements InjectorConfiguration { | ||
@Provides | ||
public ProvidedClass initProvidedClass() { | ||
return new ProvidedClass(""); | ||
} | ||
|
||
@Provides | ||
public void initBetaManager() { | ||
// invalid return type | ||
} | ||
} | ||
|
||
public static final class ConfigurationWithParameters implements InjectorConfiguration { | ||
@Provides | ||
public ProvidedClass initProvidedClass(boolean b) { | ||
return new ProvidedClass(""); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
core/src/test/java/ch/jalu/injector/samples/configurations/SampleConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ch.jalu.injector.samples.configurations; | ||
|
||
import ch.jalu.injector.annotations.Provides; | ||
import ch.jalu.injector.config.InjectorConfiguration; | ||
import ch.jalu.injector.samples.BetaManager; | ||
import ch.jalu.injector.samples.ProvidedClass; | ||
|
||
/** | ||
* Sample configuration class. | ||
*/ | ||
public class SampleConfiguration implements InjectorConfiguration { | ||
|
||
private BetaManager betaManager = new BetaManager(); | ||
private ProvidedClass providedClass = new ProvidedClass(""); | ||
|
||
@Provides | ||
public BetaManager initBetaManager() { | ||
return betaManager; | ||
} | ||
|
||
@Provides | ||
private ProvidedClass initProvidedClass() { | ||
return providedClass; | ||
} | ||
|
||
public ProvidedClass getProvidedClass() { | ||
return providedClass; | ||
} | ||
} |