#22 mvc: reflection

This commit is contained in:
haerong22
2022-09-22 23:41:55 +09:00
parent 54edba1b02
commit 0c412100a2
4 changed files with 97 additions and 4 deletions

View File

@@ -0,0 +1,11 @@
package org.example.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Service {
}

View File

@@ -0,0 +1,30 @@
package org.example.model;
import java.util.Objects;
public class User {
private String userId;
private String name;
public User(String userId, String name) {
this.userId = userId;
this.name = name;
}
public boolean equalUser(User user) {
return this.equals(user);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return Objects.equals(userId, user.userId) && Objects.equals(name, user.name);
}
@Override
public int hashCode() {
return Objects.hash(userId, name);
}
}

View File

@@ -0,0 +1,7 @@
package org.example.service;
import org.example.annotation.Service;
@Service
public class HomeService {
}

View File

@@ -1,13 +1,22 @@
package org.example;
import org.assertj.core.api.Assertions;
import org.example.annotation.Controller;
import org.example.annotation.Service;
import org.example.model.User;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.*;
public class ReflectionTest {
@@ -15,11 +24,47 @@ public class ReflectionTest {
@Test
void controllerScan() {
Reflections reflections = new Reflections("org.example");
Set<Class<?>> beans = new HashSet<>();
beans.addAll(reflections.getTypesAnnotatedWith(Controller.class));
Set<Class<?>> beans = getTypeAnnotationWith(List.of(Controller.class, Service.class));
logger.debug("beans: [{}]", beans);
}
@Test
void showClass() {
Class<User> clazz = User.class;
logger.debug(clazz.getName());
logger.debug("User all declared fields: [{}]", Arrays.stream(clazz.getDeclaredFields()).collect(Collectors.toList()));
logger.debug("User all declared constructor: [{}]", Arrays.stream(clazz.getDeclaredConstructors()).collect(Collectors.toList()));
logger.debug("User all declared method: [{}]", Arrays.stream(clazz.getDeclaredMethods()).collect(Collectors.toList()));
}
@Test
void load() throws ClassNotFoundException {
Class<User> clazz = User.class;
User user = new User("1", "test");
Class<? extends User> clazz2 = user.getClass();
Class<?> clazz3 = Class.forName("org.example.model.User");
logger.debug("clazz: [{}]", clazz);
logger.debug("clazz2: [{}]", clazz2);
logger.debug("clazz3: [{}]", clazz3);
assertThat(clazz == clazz2).isTrue();
assertThat(clazz == clazz3).isTrue();
assertThat(clazz2 == clazz3).isTrue();
}
private static Set<Class<?>> getTypeAnnotationWith(List<Class<? extends Annotation>> annotations) {
Reflections reflections = new Reflections("org.example");
Set<Class<?>> beans = new HashSet<>();
annotations.forEach(annotation -> beans.addAll(reflections.getTypesAnnotatedWith(annotation)));
return beans;
}
}