Merge branch 'development'
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
projectGroup=ru.dmitriymx
|
projectGroup=ru.dmitriymx
|
||||||
projectName=reflection-object
|
projectName=reflection-object
|
||||||
projectVersion=1.0-BETA
|
projectVersion=1.1-BETA
|
||||||
@@ -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;
|
||||||
|
|
||||||
@@ -28,6 +29,10 @@ public class ReflectionField {
|
|||||||
return field.getName();
|
return field.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Class<?> type() {
|
||||||
|
return field.getType();
|
||||||
|
}
|
||||||
|
|
||||||
public ReflectionObject get() {
|
public ReflectionObject get() {
|
||||||
try {
|
try {
|
||||||
if (!field.isAccessible()) {
|
if (!field.isAccessible()) {
|
||||||
@@ -44,10 +49,29 @@ public class ReflectionField {
|
|||||||
return get().getOriginalObject(clazz);
|
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() {
|
public boolean isStatic() {
|
||||||
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{" +
|
||||||
@@ -56,4 +80,8 @@ public class ReflectionField {
|
|||||||
", isStatic=" + isStatic() +
|
", isStatic=" + isStatic() +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String formatNameForMethod() {
|
||||||
|
return name().substring(0, 1).toUpperCase() + name().substring(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ public class ReflectionMethod {
|
|||||||
return method.getName();
|
return method.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Class<?> returnType() {
|
||||||
|
return method.getReturnType();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isStatic() {
|
public boolean isStatic() {
|
||||||
return Modifier.isStatic(method.getModifiers());
|
return Modifier.isStatic(method.getModifiers());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ public class ReflectionObjectTest {
|
|||||||
new ReflectionObject(strObj).getOriginalObject(Integer.class);
|
new ReflectionObject(strObj).getOriginalObject(Integer.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//region Field tests
|
||||||
@Test
|
@Test
|
||||||
public void fieldsList() {
|
public void fieldsList() {
|
||||||
final List<String> expectedList = Arrays.asList("finalizedField", "simpleField");
|
final List<String> expectedList = Arrays.asList("finalizedField", "simpleField");
|
||||||
@@ -64,6 +65,44 @@ public class ReflectionObjectTest {
|
|||||||
refObj.field("field_not_exists");
|
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
|
@Test
|
||||||
public void methodsList() {
|
public void methodsList() {
|
||||||
final List<String> expectedList = Arrays.asList("getSimpleField", "setSimpleField");
|
final List<String> expectedList = Arrays.asList("getSimpleField", "setSimpleField");
|
||||||
@@ -105,4 +144,5 @@ public class ReflectionObjectTest {
|
|||||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||||
refObj.method("not_exists_method");
|
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 final String finalizedField = "value123";
|
||||||
private String simpleField = "defaultValue";
|
private String simpleField = "defaultValue";
|
||||||
|
|
||||||
|
@SomeAnnotation
|
||||||
|
private int someIntField;
|
||||||
|
|
||||||
public String getSimpleField() {
|
public String getSimpleField() {
|
||||||
return simpleField;
|
return simpleField;
|
||||||
}
|
}
|
||||||
@@ -18,6 +21,14 @@ class SomeObject {
|
|||||||
simpleField = "123value";
|
simpleField = "123value";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getSomeIntField() {
|
||||||
|
return someIntField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSomeIntField(int someIntField) {
|
||||||
|
this.someIntField = someIntField;
|
||||||
|
}
|
||||||
|
|
||||||
public static int getMagicNumber() {
|
public static int getMagicNumber() {
|
||||||
return MAGIC_NUMBER;
|
return MAGIC_NUMBER;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user