10/16: JAVA > メソッドやフィールドを調べるjava.lang.reflectパッケージ
Category: Web dev
Posted by: hajime osako
RADKRAFT: Java Reflection API
public String myReflection(String className) { String return_str = ""; try { int i,j; Class cls = Class.forName(className); Field[] fieldList = cls.getFields(); for (i=0;i<fieldList.length;i++) { Field fld = fieldList[i]; return_str += Modifier.toString(fld.getModifiers()); return_str += " " + fld.getType().getName(); return_str += " " + fld.getName() + ";\n"; } return_str += "\n"; Constructor[] ctorList = cls.getConstructors(); for (i=0;i<ctorList.length;i++) { Constructor ct = ctorList[i]; return_str += Modifier.toString(ct.getModifiers()); return_str += " " + ct.getDeclaringClass().getName(); Class[] cparamList = ct.getParameterTypes(); return_str += "("; for (j=0;j<cparamList.length;j++) { return_str += " " + cparamList[j].getName(); } return_str += ");\n"; } return_str += "\n"; Method[] methList = cls.getMethods(); for (i=0;i<methList.length;i++) { Method m = methList[i]; return_str += Modifier.toString(m.getModifiers()); return_str += " " + m.getReturnType().getName(); return_str += " " + m.getName(); Class[] mparamList = m.getParameterTypes(); return_str += "("; for (j=0;j<mparamList.length;j++) { return_str += " " + mparamList[j].getName(); } return_str += ");\n"; } return_str += "\n"; } catch (Exception e) { e.printStackTrace(); } return return_str; }