0

добавлены методы поиска полей по аннотации

This commit is contained in:
2020-04-17 18:29:46 +03:00
parent a16fed9785
commit 62b93916f9
5 changed files with 45 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package ru.dmitriymx.reflection;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@@ -67,6 +68,10 @@ public class ReflectionField {
return Modifier.isStatic(field.getModifiers()); return Modifier.isStatic(field.getModifiers());
} }
public <T extends Annotation> T annotation(Class<T> annotationType) {
return field.getAnnotation(annotationType);
}
@Override @Override
public String toString() { public String toString() {
return "ReflectionField{" + return "ReflectionField{" +

View File

@@ -2,11 +2,14 @@ package ru.dmitriymx.reflection;
import lombok.ToString; import lombok.ToString;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ToString @ToString
public class ReflectionObject { public class ReflectionObject {
@@ -45,6 +48,15 @@ public class ReflectionObject {
} }
} }
public List<ReflectionField> fieldsWithAnnotation(final Class<? extends Annotation> annotationType) {
final Class clazz = object.getClass();
return Stream.of(clazz.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(annotationType))
.map(field -> new ReflectionField(object, field))
.collect(Collectors.toList());
}
public ReflectionField field(String name) { public ReflectionField field(String name) {
final Class clazz = object.getClass(); final Class clazz = object.getClass();

View File

@@ -86,6 +86,20 @@ public class ReflectionObjectTest {
ReflectionMethod refSetter = refField.setter(); ReflectionMethod refSetter = refField.setter();
assertNotNull(refSetter); assertNotNull(refSetter);
} }
@Test
public void fieldsWithAnnotation() {
ReflectionObject refObj = new ReflectionObject(new SomeObject());
List<ReflectionField> list = refObj.fieldsWithAnnotation(SomeAnnotation.class);
assertEquals(1, list.size());
ReflectionField refField = list.get(0);
assertEquals("someIntField", refField.name());
SomeAnnotation annotation = refField.annotation(SomeAnnotation.class);
assertNotNull(annotation);
}
//endregion //endregion
//region Method tests //region Method tests

View File

@@ -0,0 +1,12 @@
package ru.dmitriymx.reflection;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SomeAnnotation {
}

View File

@@ -5,6 +5,8 @@ class SomeObject {
private static final int MAGIC_NUMBER = 33; private static final int MAGIC_NUMBER = 33;
private final String finalizedField = "value123"; private final String finalizedField = "value123";
private String simpleField = "defaultValue"; private String simpleField = "defaultValue";
@SomeAnnotation
private int someIntField; private int someIntField;
public String getSimpleField() { public String getSimpleField() {