Merge branch 'development'
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
projectGroup=ru.dmitriymx
|
||||
projectName=reflection-object
|
||||
projectVersion=1.0-BETA
|
||||
projectVersion=1.1-BETA
|
||||
@@ -2,6 +2,7 @@ package ru.dmitriymx.reflection;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
@@ -28,6 +29,10 @@ public class ReflectionField {
|
||||
return field.getName();
|
||||
}
|
||||
|
||||
public Class<?> type() {
|
||||
return field.getType();
|
||||
}
|
||||
|
||||
public ReflectionObject get() {
|
||||
try {
|
||||
if (!field.isAccessible()) {
|
||||
@@ -44,10 +49,29 @@ public class ReflectionField {
|
||||
return get().getOriginalObject(clazz);
|
||||
}
|
||||
|
||||
public ReflectionMethod getter() {
|
||||
final String methodName = "get" + formatNameForMethod();
|
||||
ReflectionMethod method = new ReflectionObject(object).method(methodName);
|
||||
if (method.returnType().equals(type())) {
|
||||
return method;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public ReflectionMethod setter() {
|
||||
final String methodName = "set" + formatNameForMethod();
|
||||
return new ReflectionObject(object).method(methodName, type());
|
||||
}
|
||||
|
||||
public boolean isStatic() {
|
||||
return Modifier.isStatic(field.getModifiers());
|
||||
}
|
||||
|
||||
public <T extends Annotation> T annotation(Class<T> annotationType) {
|
||||
return field.getAnnotation(annotationType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ReflectionField{" +
|
||||
@@ -56,4 +80,8 @@ public class ReflectionField {
|
||||
", isStatic=" + isStatic() +
|
||||
'}';
|
||||
}
|
||||
|
||||
private String formatNameForMethod() {
|
||||
return name().substring(0, 1).toUpperCase() + name().substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,10 @@ public class ReflectionMethod {
|
||||
return method.getName();
|
||||
}
|
||||
|
||||
public Class<?> returnType() {
|
||||
return method.getReturnType();
|
||||
}
|
||||
|
||||
public boolean isStatic() {
|
||||
return Modifier.isStatic(method.getModifiers());
|
||||
}
|
||||
|
||||
@@ -2,11 +2,14 @@ package ru.dmitriymx.reflection;
|
||||
|
||||
import lombok.ToString;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ToString
|
||||
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) {
|
||||
final Class clazz = object.getClass();
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ public class ReflectionObjectTest {
|
||||
new ReflectionObject(strObj).getOriginalObject(Integer.class);
|
||||
}
|
||||
|
||||
//region Field tests
|
||||
@Test
|
||||
public void fieldsList() {
|
||||
final List<String> expectedList = Arrays.asList("finalizedField", "simpleField");
|
||||
@@ -64,6 +65,44 @@ public class ReflectionObjectTest {
|
||||
refObj.field("field_not_exists");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void field_getter() {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
|
||||
ReflectionField refField = refObj.field("someIntField");
|
||||
assertNotNull(refField);
|
||||
|
||||
ReflectionMethod refGetter = refField.getter();
|
||||
assertNotNull(refGetter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void field_setter() {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
|
||||
ReflectionField refField = refObj.field("someIntField");
|
||||
assertNotNull(refField);
|
||||
|
||||
ReflectionMethod refSetter = refField.setter();
|
||||
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
|
||||
|
||||
//region Method tests
|
||||
@Test
|
||||
public void methodsList() {
|
||||
final List<String> expectedList = Arrays.asList("getSimpleField", "setSimpleField");
|
||||
@@ -105,4 +144,5 @@ public class ReflectionObjectTest {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
refObj.method("not_exists_method");
|
||||
}
|
||||
//endregion
|
||||
}
|
||||
12
src/test/java/ru/dmitriymx/reflection/SomeAnnotation.java
Normal file
12
src/test/java/ru/dmitriymx/reflection/SomeAnnotation.java
Normal 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 {
|
||||
|
||||
}
|
||||
@@ -6,6 +6,9 @@ class SomeObject {
|
||||
private final String finalizedField = "value123";
|
||||
private String simpleField = "defaultValue";
|
||||
|
||||
@SomeAnnotation
|
||||
private int someIntField;
|
||||
|
||||
public String getSimpleField() {
|
||||
return simpleField;
|
||||
}
|
||||
@@ -18,6 +21,14 @@ class SomeObject {
|
||||
simpleField = "123value";
|
||||
}
|
||||
|
||||
public int getSomeIntField() {
|
||||
return someIntField;
|
||||
}
|
||||
|
||||
public void setSomeIntField(int someIntField) {
|
||||
this.someIntField = someIntField;
|
||||
}
|
||||
|
||||
public static int getMagicNumber() {
|
||||
return MAGIC_NUMBER;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user