반응형
방법을 찾다가 다음과 같은 코드를 만들게 됬다.
단 전제조건은 get/set 함수명은 앞3글자를 제외하고 필드명과 같다.
// 클래스에 해당하는 필드값을 리스트로 넘긴다
private List allFileds(Class claz){
List fields = new ArrayList()
Class parent = claz;
do {
fields.addAll(Arrays.asList(parent.getDeclaredFields()));
parent = parent.getSuperclass(); // 상속받은 클래스가 뭔지알아볼려는것
} whild (!parent.equals(Object.class))
return fields;
}
// 클래스에 해당하는 함수 Method 클래스를 넘긴다.
private Method classMethod(Class claz, String fieldName){
List arrMethods = allMethods(claz);
Method method = null;
for (Iterator iter = arrMethods.iterator(); iter.hasNext();){
Method field = (Method) iter.next();
String methodName = new String();
methodName = "get" + fieldName;
if(field.getName().toString().equals(methodName)){
method = field;
}
}
return method;
}
위의 함수를 사용해서 Method 객체를 가져온후 invoke 함수로 실행시킨다.
List arrFields = allFields(create.getClass())
for (Iterator iter = arrFields.iterator(); iter.hasNext()){
Field field = (Field)iter.next();
field.setAccessible(true);
// 필드값과 일치하는 메서드 호출
Method method = classMethod(create.getClass().field.getName())
// 함수 실행하게 된다, 파라미터가 있는 함수이면 null 대신 Object 배열로 넘기면된다.
String settingValue = (String)method.invoke(create, null);
}
반응형
댓글을 달아 주세요