#22 mvc: reflection
This commit is contained in:
@@ -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 {
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.example.service;
|
||||
|
||||
import org.example.annotation.Service;
|
||||
|
||||
@Service
|
||||
public class HomeService {
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user