code manipulation : reflection(간단한 DI 프레임워크 만들기)

This commit is contained in:
haerong22
2021-02-20 23:37:52 +09:00
parent 608cbe2ae2
commit 705a4dcbd3
10 changed files with 164 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>di-example</artifactId>
<version>1.0-SNAPSHOT</version>
<name>di-example</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>reflection-example</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,8 @@
package org.example;
public class AccountRepository {
public void save() {
System.out.println("Repo.save()");
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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 );
}
}

View File

@@ -0,0 +1,30 @@
package org.example.di;
import java.util.Arrays;
public class ContainerService {
public static <T> T getObject(Class<T> 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> T createInstance(Class<T> classType) {
try {
return classType.getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,9 @@
package org.example.di;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
}

View File

@@ -0,0 +1,4 @@
package org.example.di;
public class BookRepository {
}

View File

@@ -0,0 +1,7 @@
package org.example.di;
public class BookService {
@Inject
BookRepository bookRepository;
}

View File

@@ -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);
}
}