/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ /** * eine Klasse zur Ausgabe von * Laufzeit-Typinformation von Objekten * und deren Klassen */ //-------------------- import java.lang.reflect.*; class ClassInfo { private Class c; //-------------------- // Konstruktoren fuer Objekte und Klassennamen public ClassInfo(Object obj) { this.c = obj.getClass(); } public ClassInfo(String className) throws ClassNotFoundException { this.c = Class.forName(className); } //-------------------- // die modifier public String getModifiers() { return Modifier.toString(c.getModifiers()); } //-------------------- // die Schnittstellen public String getInterfaces() { Class[] ifs = c.getInterfaces(); if (ifs.length == 0) { return ""; } else { String res = ""; res += ( c.isInterface() ? "\n extends " : "\n implements "); for ( int i = 0; i < ifs.length; ++i ) { if ( i != 0 ) { res += ", "; } res += ifs[i].getName(); } return res; } } //-------------------- // der Klassen-Kopf public String getClassHeader() { return getModifiers() + ( c.isInterface() ? "" : "\nclass" ) + " " + c.getName() + ( c.isInterface() ? "" : "\n extends " + c.getSuperclass().getName() ) + getInterfaces(); } //-------------------- // alle Felder, Konstruktoren und Methoden public String getClassMethods() { return getFields() + getConstructors() + getMethods(); } //-------------------- // alle Felder public String getFields() { Field[] fl = c.getDeclaredFields(); String res = ""; for ( int i = 0; i < fl.length; ++i ) { res += getField(fl[i]); } return res; } //-------------------- // alle Konstruktoren public String getConstructors() { Constructor[] cl = c.getDeclaredConstructors(); String res = ""; for ( int i = 0; i < cl.length; ++i ) { res += getConstructor(cl[i]); } return res; } //-------------------- // alle Methoden public String getMethods() { Method[] ml = c.getDeclaredMethods(); String res = ""; for ( int i = 0; i < ml.length; ++i ) { res += getMethod(ml[i]); } return res; } //-------------------- // ein Feld public String getField(Field f) { return "\n " + Modifier.toString(f.getModifiers()) + "\n " + getTypeName(f.getType()) + " " + f.getName() + ";\n"; } //-------------------- // ein Konstruktor public String getConstructor(Constructor c) { return "\n " + Modifier.toString(c.getModifiers()) + "\n " + c.getName() + getParamList(c.getParameterTypes()) + getExcList(c.getExceptionTypes()) + ";\n"; } //-------------------- // eine Methode public String getMethod(Method m) { return "\n " + Modifier.toString(m.getModifiers()) + "\n " + getTypeName(m.getReturnType()) + " " + m.getName() + getParamList(m.getParameterTypes()) + getExcList(m.getExceptionTypes()) + ";\n"; } //-------------------- // der Typname public String getTypeName(Class t) { String dim = ""; while ( t.isArray() ) { dim += "[]"; t = t.getComponentType(); } return t.getName() + dim; } //-------------------- // die Parameterliste public String getParamList(Class[] pl) { String res = "("; for ( int i = 0; i < pl.length; ++i ) { if ( i > 0 ) { res += ", "; } res += getTypeName(pl[i]) +" x" + i; } res += ")"; return res; } //-------------------- // die throws Liste public String getExcList(Class[] el) { if ( el.length == 0 ) { return ""; } else { String res = "\n throws "; for ( int i = 0; i < el.length; ++ i ) { if ( i != 0 ) { res += ", "; } res += getTypeName(el[i]); } return res; } } //-------------------- // die Hauptroutine public String getClassInfo() { return getClassHeader() + " {\n" + getClassMethods() + "\n}\n"; } }