-
-
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.
- Loading branch information
Showing
6 changed files
with
112 additions
and
6 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 that value should be automatically constructed with current injector resources. | ||
*/ | ||
@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface AutoConstruct { | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
di/src/test/java/org/panda_lang/utilities/inject/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,64 @@ | ||
package org.panda_lang.utilities.inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
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); | ||
} | ||
|
||
} | ||
|
||
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 shouldRunPostConstructMethods() throws Throwable { | ||
Injector injector = DependencyInjection.createInjector(resources -> { | ||
resources.on(String.class).assignInstance("TestString"); | ||
}); | ||
|
||
injector.newInstanceWithFields(Repository.class); | ||
injector.newInstanceWithFields(Service.class); | ||
} | ||
|
||
} |