diff --git a/src/main/java/cokr/xit/foundation/data/Property.java b/src/main/java/cokr/xit/foundation/data/Property.java index 13cb485..2a79ae5 100644 --- a/src/main/java/cokr/xit/foundation/data/Property.java +++ b/src/main/java/cokr/xit/foundation/data/Property.java @@ -1,5 +1,7 @@ package cokr.xit.foundation.data; +import java.beans.PropertyDescriptor; + import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.PropertyUtils; @@ -17,6 +19,10 @@ public class Property extends AbstractComponent { return instance; } + public static PropertyDescriptor descriptor(Object obj, String name) { + return get().getDescriptor(obj, name); + } + /**객체에서 지정하는 프로퍼티 값을 읽을 수 있는지 반환한다. * @param obj 객체 * @param name 프로퍼티 이름 @@ -44,6 +50,10 @@ public class Property extends AbstractComponent { return get().get(obj, name); } + public static Object value(Object obj, String name) { + return get().getValue(obj, name); + } + /**객체에 지정하는 프로퍼티 값을 쓴다. * @param obj 객체 * @param name 프로퍼티 이름 @@ -98,6 +108,16 @@ public class Property extends AbstractComponent { } } + public Object getValue(Object obj, String name) { + try { + return getDescriptor(obj, name) + .getReadMethod() + .invoke(obj); + } catch (Exception e) { + throw runtimeException(e); + } + } + /**객체에 지정하는 프로퍼티 값을 쓴다. * @param obj 객체 * @param name 프로퍼티 이름 @@ -110,4 +130,12 @@ public class Property extends AbstractComponent { throw runtimeException(e); } } + + public PropertyDescriptor getDescriptor(Object obj, String name) { + try { + return PropertyUtils.getPropertyDescriptor(obj, name); + } catch (Exception e) { + throw runtimeException(e); + } + } } \ No newline at end of file diff --git a/src/test/java/cokr/xit/foundation/data/PropertyTest.java b/src/test/java/cokr/xit/foundation/data/PropertyTest.java new file mode 100644 index 0000000..d72666e --- /dev/null +++ b/src/test/java/cokr/xit/foundation/data/PropertyTest.java @@ -0,0 +1,70 @@ +package cokr.xit.foundation.data; + +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import lombok.Getter; +import lombok.Setter; + +public class PropertyTest { + private TestObject testObject() { + TestObject obj = new TestObject(); + obj.str = "abc"; + obj.array = new String[]{"1", "2", "3"}; + obj.list = List.of("a", "b", "c"); + obj.map = Map.of("k1", "v1", "k2", "v2", "k3", "v3"); + return obj; + } + + @Test + void read() { + TestObject obj = testObject(); + + try { + String name = "str"; + Object val = Property.value(obj, name); + System.out.println(name + " = " + val); + + val = Property.value(obj, name = "array"); + System.out.println(name + " = " + val); + + val = Property.value(obj, name = "list"); + System.out.println(name + " = " + val); + + val = Property.value(obj, name = "map"); + System.out.println(name + " = " + val); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + void write() { + TestObject obj = new TestObject(); + + String name = "str"; + Object val = "abc"; + Property.write(obj, name, val); + System.out.println(name + ": " + Property.value(obj, name)); + + Property.write(obj, name = "array", val = new String[] {"1", "2", "3"}); + System.out.println(name + ": " + Property.value(obj, name)); + + Property.write(obj, name = "list", val = List.of("a", "b", "c")); + System.out.println(name + ": " + Property.value(obj, name)); + + Property.write(obj, name = "map", val = Map.of("k1", "v1", "k2", "v2", "k3", "v3")); + System.out.println(name + ": " + Property.value(obj, name)); + } + + @Getter + @Setter + public static class TestObject { + private String str; + private String[] array; + private List list; + private Map map; + } +} \ No newline at end of file