예를 들면
public class Car {
private String model;
public String owner;
public static final int FOO = 123;
public static final int BAR = 456;
이런식으로 숫자값에 대한 정의만 난무한 class 가 있고, 이에 대해서 변수명을 출력할 일이 있다면 (주로 디버깅) 일일이 switch 문이나 if else 를 만드는 일은 곤욕스러운 일이다.
자바에서는
아래와 같이 getDeclaredFields 함수를 이용해서 동적으로 123 의 값을 가지고 "FOO" 를 출력하는 일이 가능하다.
onLine compile test : https://www.compilejava.net/
//*******************************************************************
// NOTE: please read the 'More Info' tab to the right for shortcuts.
//*******************************************************************
import java.lang.Math; // headers MUST be above the first class
import java.util.*;
import java.lang.reflect.*;
public class Car {
private String model;
public String owner;
public static final int FOO = 123;
public static final int BAR = 456;
public Car() {
}
public Car(String model) {
this.model = model;
}
public void setModel(String model) {
this.model = model;
}
public String getModel() {
return model;
}
private void setOwner(String owner) {
this.owner = owner;
}
public String getOwner() {
return owner;
}
}
// end of Car
public class ReflectionExample {
private static Map<Integer, String> constantNames = null;
public static String getConstantName(int constVal) throws Exception {
Class clazz = Class.forName("Car");
if (constantNames == null) {
Map<Integer, String> cNames = new HashMap<Integer, String>();
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields) {
if(int.class == field.getType() && ((field.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) != 0)) {
System.out.println(field.getType().getSimpleName() + " " + field.getName());
cNames.put((Integer)field.get(null), field.getName());
}
}
constantNames = cNames;
}
return constantNames.get(constVal);
}
public static void main(String[] args) throws Exception {
Class clazz = Class.forName("Car");
System.out.println("[find field]");
System.out.println(getConstantName(456));
System.out.println();
System.out.println("[class name]");
System.out.println(clazz.getName());
System.out.println();
System.out.println("[creator]");
Constructor[] constructors = clazz.getDeclaredConstructors();
for(Constructor constructor : constructors) {
System.out.print(constructor.getName() + "(");
Class[] parameters = constructor.getParameterTypes();
printParameters(parameters);
System.out.println(")");
}
System.out.println();
//System.out.println("field info");
//Field[] fields = clazz.getDeclaredFields();
//for(Field field : fields) {
// System.out.println(field.getType().getSimpleName() + " " + field.getName());
//}
//System.out.println();
System.out.println("method info");
Method[] methods = clazz.getDeclaredMethods();
for(Method method : methods) {
System.out.print(method.getName() + "(");
Class[] parameters = method.getParameterTypes();
printParameters(parameters);
System.out.println(")");
}
}
private static void printParameters(Class[] parameters) {
for(int i=0; i<parameters.length; i++) {
System.out.print(parameters[i].getName());
if(i<(parameters.length-1)) {
System.out.print(",");
}
}
}
}
'Programming > JAVA' 카테고리의 다른 글
JAVA 입출력 (0) | 2018.02.02 |
---|