2009-09-16

자바 - Reflection으로 static 필드 정보 java.util.Map에 담기

자바에서 Reflection을 이용해서 특정 클래스의 static 필드 정보를 java.util.Map에 담는 방법이다.


다음은 java.sql.Types 클래스에 포함된 int 유형의 데이터베이스 칼럼 타입 정보를 처리하는 예제이다.

Map<String, Integer> typeMapping = new HashMap<String, Integer>();

Field[] fields = java.sql.Types.class.getDeclaredFields();
for (Field field : fields) {
  if (Modifier.isStatic(field.getModifiers()) && field.getType() == int.class) {
    typeMapping.put(field.getName(), (Integer) field.get(null));
  }
}


No comments: