프로퍼티 읽기 추가

master
mjkhan21 1 week ago
parent 4ece563336
commit 24624103ef

@ -1,5 +1,7 @@
package cokr.xit.foundation.data; package cokr.xit.foundation.data;
import java.beans.PropertyDescriptor;
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.PropertyUtils;
@ -17,6 +19,10 @@ public class Property extends AbstractComponent {
return instance; return instance;
} }
public static PropertyDescriptor descriptor(Object obj, String name) {
return get().getDescriptor(obj, name);
}
/** . /** .
* @param obj * @param obj
* @param name * @param name
@ -44,6 +50,10 @@ public class Property extends AbstractComponent {
return get().get(obj, name); return get().get(obj, name);
} }
public static Object value(Object obj, String name) {
return get().getValue(obj, name);
}
/** . /** .
* @param obj * @param obj
* @param name * @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 obj
* @param name * @param name
@ -110,4 +130,12 @@ public class Property extends AbstractComponent {
throw runtimeException(e); throw runtimeException(e);
} }
} }
public PropertyDescriptor getDescriptor(Object obj, String name) {
try {
return PropertyUtils.getPropertyDescriptor(obj, name);
} catch (Exception e) {
throw runtimeException(e);
}
}
} }

@ -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<String> list;
private Map<String, String> map;
}
}
Loading…
Cancel
Save