From 705a4dcbd33b55a10d98a02157abf4f65eba3a4c Mon Sep 17 00:00:00 2001 From: haerong22 Date: Sat, 20 Feb 2021 23:37:52 +0900 Subject: [PATCH] =?UTF-8?q?code=20manipulation=20:=20reflection(=EA=B0=84?= =?UTF-8?q?=EB=8B=A8=ED=95=9C=20DI=20=ED=94=84=EB=A0=88=EC=9E=84=EC=9B=8C?= =?UTF-8?q?=ED=81=AC=20=EB=A7=8C=EB=93=A4=EA=B8=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code-manipulation/di-example/pom.xml | 36 +++++++++++++++++++ .../java/org/example/AccountRepository.java | 8 +++++ .../main/java/org/example/AccountService.java | 14 ++++++++ .../src/main/java/org/example/App.java | 15 ++++++++ .../src/test/java/org/example/AppTest.java | 20 +++++++++++ .../java/org/example/di/ContainerService.java | 30 ++++++++++++++++ .../src/main/java/org/example/di/Inject.java | 9 +++++ .../java/org/example/di/BookRepository.java | 4 +++ .../test/java/org/example/di/BookService.java | 7 ++++ .../org/example/di/ContainerServiceTest.java | 21 +++++++++++ 10 files changed, 164 insertions(+) create mode 100644 code-manipulation/di-example/pom.xml create mode 100644 code-manipulation/di-example/src/main/java/org/example/AccountRepository.java create mode 100644 code-manipulation/di-example/src/main/java/org/example/AccountService.java create mode 100644 code-manipulation/di-example/src/main/java/org/example/App.java create mode 100644 code-manipulation/di-example/src/test/java/org/example/AppTest.java create mode 100644 code-manipulation/reflection-example/src/main/java/org/example/di/ContainerService.java create mode 100644 code-manipulation/reflection-example/src/main/java/org/example/di/Inject.java create mode 100644 code-manipulation/reflection-example/src/test/java/org/example/di/BookRepository.java create mode 100644 code-manipulation/reflection-example/src/test/java/org/example/di/BookService.java create mode 100644 code-manipulation/reflection-example/src/test/java/org/example/di/ContainerServiceTest.java diff --git a/code-manipulation/di-example/pom.xml b/code-manipulation/di-example/pom.xml new file mode 100644 index 00000000..8643fce1 --- /dev/null +++ b/code-manipulation/di-example/pom.xml @@ -0,0 +1,36 @@ + + + + 4.0.0 + + org.example + di-example + 1.0-SNAPSHOT + + di-example + + http://www.example.com + + + UTF-8 + 11 + 11 + + + + + junit + junit + 4.11 + test + + + + org.example + reflection-example + 1.0-SNAPSHOT + + + + diff --git a/code-manipulation/di-example/src/main/java/org/example/AccountRepository.java b/code-manipulation/di-example/src/main/java/org/example/AccountRepository.java new file mode 100644 index 00000000..91072935 --- /dev/null +++ b/code-manipulation/di-example/src/main/java/org/example/AccountRepository.java @@ -0,0 +1,8 @@ +package org.example; + +public class AccountRepository { + + public void save() { + System.out.println("Repo.save()"); + } +} diff --git a/code-manipulation/di-example/src/main/java/org/example/AccountService.java b/code-manipulation/di-example/src/main/java/org/example/AccountService.java new file mode 100644 index 00000000..171c2998 --- /dev/null +++ b/code-manipulation/di-example/src/main/java/org/example/AccountService.java @@ -0,0 +1,14 @@ +package org.example; + +import org.example.di.Inject; + +public class AccountService { + + @Inject + AccountRepository accountRepository; + + public void join() { + System.out.println("Service.join()"); + accountRepository.save(); + } +} diff --git a/code-manipulation/di-example/src/main/java/org/example/App.java b/code-manipulation/di-example/src/main/java/org/example/App.java new file mode 100644 index 00000000..cd0e2589 --- /dev/null +++ b/code-manipulation/di-example/src/main/java/org/example/App.java @@ -0,0 +1,15 @@ +package org.example; + +import org.example.di.ContainerService; + +/** + * Hello world! + * + */ +public class App { + public static void main( String[] args ) { + + AccountService accountService = ContainerService.getObject(AccountService.class); + accountService.join(); + } +} diff --git a/code-manipulation/di-example/src/test/java/org/example/AppTest.java b/code-manipulation/di-example/src/test/java/org/example/AppTest.java new file mode 100644 index 00000000..6a1d2d79 --- /dev/null +++ b/code-manipulation/di-example/src/test/java/org/example/AppTest.java @@ -0,0 +1,20 @@ +package org.example; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +} diff --git a/code-manipulation/reflection-example/src/main/java/org/example/di/ContainerService.java b/code-manipulation/reflection-example/src/main/java/org/example/di/ContainerService.java new file mode 100644 index 00000000..a6b8e47c --- /dev/null +++ b/code-manipulation/reflection-example/src/main/java/org/example/di/ContainerService.java @@ -0,0 +1,30 @@ +package org.example.di; + +import java.util.Arrays; + +public class ContainerService { + + public static T getObject(Class classType) { + T instance = createInstance(classType); + Arrays.stream(classType.getDeclaredFields()).forEach(field -> { + if (field.getAnnotation(Inject.class) != null) { + Object fieldInstance = createInstance(field.getType()); + field.setAccessible(true); + try { + field.set(instance, fieldInstance); + } catch (IllegalAccessException e) { + throw new RuntimeException(); + } + } + }); + return instance; + } + + private static T createInstance(Class classType) { + try { + return classType.getConstructor().newInstance(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/code-manipulation/reflection-example/src/main/java/org/example/di/Inject.java b/code-manipulation/reflection-example/src/main/java/org/example/di/Inject.java new file mode 100644 index 00000000..da5e3603 --- /dev/null +++ b/code-manipulation/reflection-example/src/main/java/org/example/di/Inject.java @@ -0,0 +1,9 @@ +package org.example.di; + + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface Inject { +} diff --git a/code-manipulation/reflection-example/src/test/java/org/example/di/BookRepository.java b/code-manipulation/reflection-example/src/test/java/org/example/di/BookRepository.java new file mode 100644 index 00000000..94569f3d --- /dev/null +++ b/code-manipulation/reflection-example/src/test/java/org/example/di/BookRepository.java @@ -0,0 +1,4 @@ +package org.example.di; + +public class BookRepository { +} diff --git a/code-manipulation/reflection-example/src/test/java/org/example/di/BookService.java b/code-manipulation/reflection-example/src/test/java/org/example/di/BookService.java new file mode 100644 index 00000000..a251afdb --- /dev/null +++ b/code-manipulation/reflection-example/src/test/java/org/example/di/BookService.java @@ -0,0 +1,7 @@ +package org.example.di; + +public class BookService { + + @Inject + BookRepository bookRepository; +} diff --git a/code-manipulation/reflection-example/src/test/java/org/example/di/ContainerServiceTest.java b/code-manipulation/reflection-example/src/test/java/org/example/di/ContainerServiceTest.java new file mode 100644 index 00000000..7841cf3b --- /dev/null +++ b/code-manipulation/reflection-example/src/test/java/org/example/di/ContainerServiceTest.java @@ -0,0 +1,21 @@ +package org.example.di; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ContainerServiceTest { + + @Test + public void getObject_BookRepository() { + BookRepository bookRepository = ContainerService.getObject(BookRepository.class); + assertNotNull(bookRepository); + } + + @Test + public void getObject_BookService() { + BookService bookService = ContainerService.getObject(BookService.class); + assertNotNull(bookService); + assertNotNull(bookService.bookRepository); + } +} \ No newline at end of file