-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
256 additions
and
189 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
171 changes: 0 additions & 171 deletions
171
toothpick-runtime/src/main/java/toothpick/Configuration.java
This file was deleted.
Oops, something went wrong.
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
105 changes: 105 additions & 0 deletions
105
toothpick-runtime/src/main/java/toothpick/configuration/Configuration.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,105 @@ | ||
package toothpick.configuration; | ||
|
||
import toothpick.Factory; | ||
import toothpick.MemberInjector; | ||
import toothpick.config.Binding; | ||
|
||
/** | ||
* Strategy pattern that allows to change various behaviors of Toothpick. | ||
* The default configuration is {@link #production()} and {@link #reflection()}. | ||
* A custom configuration can be created and used by toothpick, | ||
* it is even possible to use a composition of the built-in configurations. | ||
*/ | ||
public class Configuration { | ||
|
||
public static Configuration instance = new Configuration(); | ||
|
||
private ReflectionConfiguration reflectionConfiguration = new ReflectionOnConfiguration(); | ||
private RuntimeCheckConfiguration runtimeCheckConfiguration = new RuntimeCheckOffConfiguration(); | ||
|
||
/** | ||
* Allows to pass custom configurations. | ||
* | ||
* @param configuration the configuration to use | ||
*/ | ||
public static void setConfiguration(Configuration configuration) { | ||
instance = configuration; | ||
} | ||
|
||
/** | ||
* Performs many runtime checks. This configuration | ||
* reduces performance. It should be used only during development. | ||
* The checks performed are: | ||
* <ul> | ||
* <li>cycle detection: check that not 2 classes depend on each other. Note that if of them uses a Lazy instance | ||
* of the other or a Producer, then there is no such cycle.</li> | ||
* <li>illegal binding detection: check no scope annotated class is used as the target of a binding.</li> | ||
* </ul> | ||
* | ||
* @return a development configuration. | ||
*/ | ||
public static Configuration development() { | ||
final Configuration configuration = new Configuration(); | ||
configuration.runtimeCheckConfiguration = new RuntimeCheckOnConfiguration(); | ||
return configuration; | ||
} | ||
|
||
/** | ||
* Performs no runtime checks. This configuration | ||
* is faster than {@link #development()}. | ||
* It can be used in production. | ||
* | ||
* @return a production configuration. | ||
*/ | ||
public static Configuration production() { | ||
final Configuration configuration = new Configuration(); | ||
configuration.runtimeCheckConfiguration = new RuntimeCheckOffConfiguration(); | ||
return configuration; | ||
} | ||
|
||
/** | ||
* Set reflection mode. | ||
* It is slower than {@link #reflectionFree()} but it does not | ||
* need any additional setup of the annotation processors. | ||
* It can be used in production or development. | ||
* | ||
* @return a configuration set up to use reflection. | ||
*/ | ||
public Configuration reflection() { | ||
this.reflectionConfiguration = new ReflectionOnConfiguration(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Set reflection free mode. | ||
* It is faster than {@link #reflection()} but it needs | ||
* some additional setup of the annotation processors. | ||
* It can be used in production or development. | ||
* | ||
* @return an optimized reflection free configuration. | ||
*/ | ||
public Configuration reflectionFree() { | ||
this.reflectionConfiguration = new ReflectionOffConfiguration(); | ||
return this; | ||
} | ||
|
||
public void checkIllegalBinding(Binding binding) { | ||
runtimeCheckConfiguration.checkIllegalBinding(binding); | ||
} | ||
|
||
public void checkCyclesStart(Class clazz) { | ||
runtimeCheckConfiguration.checkCyclesStart(clazz); | ||
} | ||
|
||
public void checkCyclesEnd(Class clazz) { | ||
runtimeCheckConfiguration.checkCyclesEnd(clazz); | ||
} | ||
|
||
public <T> Factory<T> getFactory(Class<T> clazz) { | ||
return reflectionConfiguration.getFactory(clazz); | ||
} | ||
|
||
public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) { | ||
return reflectionConfiguration.getMemberInjector(clazz); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../toothpick/CyclicDependencyException.java → ...figuration/CyclicDependencyException.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package toothpick; | ||
package toothpick.configuration; | ||
|
||
import java.util.List; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...va/toothpick/IllegalBindingException.java → ...onfiguration/IllegalBindingException.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package toothpick; | ||
package toothpick.configuration; | ||
|
||
/** | ||
* Thrown when a binding is illegal. | ||
|
9 changes: 9 additions & 0 deletions
9
toothpick-runtime/src/main/java/toothpick/configuration/ReflectionConfiguration.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,9 @@ | ||
package toothpick.configuration; | ||
|
||
import toothpick.Factory; | ||
import toothpick.MemberInjector; | ||
|
||
interface ReflectionConfiguration { | ||
<T> Factory<T> getFactory(Class<T> clazz); | ||
<T> MemberInjector<T> getMemberInjector(Class<T> clazz); | ||
} |
18 changes: 18 additions & 0 deletions
18
toothpick-runtime/src/main/java/toothpick/configuration/ReflectionOffConfiguration.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,18 @@ | ||
package toothpick.configuration; | ||
|
||
import toothpick.Factory; | ||
import toothpick.MemberInjector; | ||
import toothpick.registries.FactoryRegistryLocator; | ||
import toothpick.registries.MemberInjectorRegistryLocator; | ||
|
||
class ReflectionOffConfiguration implements ReflectionConfiguration { | ||
@Override | ||
public <T> Factory<T> getFactory(Class<T> clazz) { | ||
return FactoryRegistryLocator.getFactoryUsingRegistries(clazz); | ||
} | ||
|
||
@Override | ||
public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) { | ||
return MemberInjectorRegistryLocator.getMemberInjectorUsingRegistries(clazz); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
toothpick-runtime/src/main/java/toothpick/configuration/ReflectionOnConfiguration.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,18 @@ | ||
package toothpick.configuration; | ||
|
||
import toothpick.Factory; | ||
import toothpick.MemberInjector; | ||
import toothpick.registries.FactoryRegistryLocator; | ||
import toothpick.registries.MemberInjectorRegistryLocator; | ||
|
||
class ReflectionOnConfiguration implements ReflectionConfiguration { | ||
@Override | ||
public <T> Factory<T> getFactory(Class<T> clazz) { | ||
return FactoryRegistryLocator.getFactoryUsingReflection(clazz); | ||
} | ||
|
||
@Override | ||
public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) { | ||
return MemberInjectorRegistryLocator.getMemberInjectorUsingReflection(clazz); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckConfiguration.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,9 @@ | ||
package toothpick.configuration; | ||
|
||
import toothpick.config.Binding; | ||
|
||
interface RuntimeCheckConfiguration { | ||
void checkIllegalBinding(Binding binding); | ||
void checkCyclesStart(Class clazz); | ||
void checkCyclesEnd(Class clazz); | ||
} |
17 changes: 17 additions & 0 deletions
17
toothpick-runtime/src/main/java/toothpick/configuration/RuntimeCheckOffConfiguration.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,17 @@ | ||
package toothpick.configuration; | ||
|
||
import toothpick.config.Binding; | ||
|
||
class RuntimeCheckOffConfiguration implements RuntimeCheckConfiguration { | ||
@Override | ||
public void checkIllegalBinding(Binding binding) { | ||
} | ||
|
||
@Override | ||
public void checkCyclesStart(Class clazz) { | ||
} | ||
|
||
@Override | ||
public void checkCyclesEnd(Class clazz) { | ||
} | ||
} |
Oops, something went wrong.