-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add @autoconstruct * Bump version * Fux AutoConstruct targets * Fix targets again * Fix @autoconstruct on methods/constructors parameters * Improve javadocs of @autoconstruct * Improve auto construct bind
- Loading branch information
Showing
7 changed files
with
133 additions
and
9 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
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
15 changes: 15 additions & 0 deletions
15
di/src/main/java/org/panda_lang/utilities/inject/annotations/AutoConstruct.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,15 @@ | ||
package org.panda_lang.utilities.inject.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marks field/parameter that value instance should be constructed, if no other bind was found | ||
*/ | ||
@Target({ ElementType.FIELD, ElementType.PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface AutoConstruct { | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
di/src/test/java/org/panda_lang/utilities/inject/annotation/AutoConstructTest.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,81 @@ | ||
package org.panda_lang.utilities.inject.annotation; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.panda_lang.utilities.inject.DependencyInjection; | ||
import org.panda_lang.utilities.inject.Injector; | ||
import org.panda_lang.utilities.inject.annotations.AutoConstruct; | ||
import org.panda_lang.utilities.inject.annotations.Inject; | ||
import org.panda_lang.utilities.inject.annotations.PostConstruct; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
final class AutoConstructTest { | ||
|
||
static class Service { | ||
|
||
@AutoConstruct | ||
private Repository repository; | ||
|
||
@PostConstruct | ||
private void construct() { | ||
assertNotNull(this.repository); | ||
} | ||
|
||
public void testMethod(@AutoConstruct DataProvider dataProvider) { | ||
assertNotNull(dataProvider); | ||
} | ||
|
||
} | ||
|
||
static class Repository { | ||
|
||
@AutoConstruct | ||
private DataProvider dataProvider; | ||
|
||
@Inject | ||
private String testValue; | ||
|
||
@PostConstruct | ||
private void construct() { | ||
assertEquals("TestString", this.testValue); | ||
assertNotNull(this.dataProvider); | ||
} | ||
|
||
} | ||
|
||
static class DataProvider { | ||
|
||
@Inject | ||
private String testValue; | ||
|
||
@PostConstruct | ||
private void construct() { | ||
assertEquals("TestString", this.testValue); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
void shouldCreateInstances() throws Throwable { | ||
Injector injector = DependencyInjection.createInjector(resources -> { | ||
resources.on(String.class).assignInstance("TestString"); | ||
}); | ||
|
||
injector.newInstanceWithFields(DataProvider.class); | ||
injector.newInstanceWithFields(Repository.class); | ||
injector.newInstanceWithFields(Service.class); | ||
} | ||
|
||
@Test | ||
void shouldRunMethod() throws Throwable { | ||
Injector injector = DependencyInjection.createInjector(resources -> { | ||
resources.on(String.class).assignInstance("TestString"); | ||
}); | ||
|
||
Service service = injector.newInstanceWithFields(Service.class); | ||
injector.invokeMethod(Service.class.getMethod("testMethod", DataProvider.class), service); | ||
} | ||
|
||
} |
4 changes: 3 additions & 1 deletion
4
...g/utilities/inject/PostConstructTest.java → .../inject/annotation/PostConstructTest.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