//------------------------------------------------------------------------ // File Name: ClassInspector.java // Description: A progam for inspecting class hierarchies, // methods, instance data, etc. // // Runs and compiles under JDK 1.1 or later. // // Modification History // Date Developer Defect Tag Description // ------ --------- ------- ----------- ---------------------------------- // 070797 DOBecker Created. //------------------------------------------------------------------------ import java.lang.reflect.*; public class ClassInspector { // Constants public final static String version = "1.0"; public final static int RECURSECLASS = 1 << 0; public final static int INHERITED = 1 << 1; public final static int NOMODIFIERS = 1 << 2; public final static int CONSTRUCTORS = 1 << 3; public final static int METHODS = 1 << 4; public final static int PARAMETERS = 1 << 5; public final static int EXCEPTIONS = 1 << 6; public final static int FIELDS = 1 << 7; // Methods public static void main( String[] args ) { System.out.println( "\nClassInspector version " + version + " by Dan Becker" ); if (args.length == 0) { ClassInspector.printHelp(); return; } /* endif */ if ( !versionCheck() ) return; ClassInspector inspector = new ClassInspector(); inspector.handleArgs( args ); try { inspector.printType( Class.forName( inspector.startClass ), 0 ); } catch ( ClassNotFoundException e ) { System.err.println( e + "\nEnsure you use fully qualified package names (e.g. java.lang.Integer)" ); } /* endcatch */ } public static void printHelp() { System.out.println( " Inspect Java class information: constructors, methods, fields." ); System.out.println(); System.out.println( " Syntax: java ClassInspector classname [options]" ); System.out.println( " Example: java ClassInspector java.util.Vector -r -a" ); System.out.println( " Options:" ); System.out.println( " -r recurse super classes" ); System.out.println( " -i print inherited constructors/methods/fields" ); System.out.println( " -n print no constructor/method/field modifiers" ); System.out.println( " -c print constructors" ); System.out.println( " -m print methods" ); System.out.println( " -p print constructor,method parameters" ); System.out.println( " -e print constructor,method exceptions" ); System.out.println( " -f print fields" ); System.out.println( " -a print all constructors/methods/fields" ); } public static boolean versionCheck() { // Checks the Java system version. Returns true to continue, false to halt. String version = System.getProperty( "java.version" ); int index; if ( -1 != ( index = version.indexOf( "1.0" ) )) { System.out.println( "Class introspection not likely to work on Java version " + version ); return false; } /* endif */ return true; } public void printType( Class type, int depth ) { if (type==null) return; // Object's supertype is null // Calculate indent StringBuffer indent = new StringBuffer(); for (int i = 0; i < depth; i++ ) indent.append( " " ); String[] labels = ( depth == 0 ? basic : extended ); System.out.print( indent + labels[ type.isInterface() ? 1 : 0 ] + " " ); // Print class modifiers printModifiers( type.getModifiers() ); System.out.println( type.getName() ); // Print constructors if ( ( options & CONSTRUCTORS ) != 0 ) printConstructors( indent.toString(), type ); if ( ( options & METHODS ) != 0 ) printMethods( indent.toString(), type ); if ( ( options & FIELDS ) != 0 ) printFields( indent.toString(), type ); // Print the super classes to this class if ( ( options & RECURSECLASS ) != 0 ) printType( type.getSuperclass(), depth + 1 ); else if ( type.getSuperclass() != null ) System.out.println( indent + " " + extended[ 0 ] + " " + type.getSuperclass().getName() ); // Print interfaces this class implements. Class[] interfaces = type.getInterfaces(); for (int i = 0; i < interfaces.length; i++ ) { if ( ( options & RECURSECLASS ) != 0 ) printType( interfaces[ i ], depth + 1 ); else if ( type.getSuperclass() != null ) System.out.println( indent + " " + extended[ 1 ] + " " + interfaces[ i ].getName() ); } } public void handleArgs( String [] args ) { for (int i = 0; i < args.length; i ++ ) { if ( i == 0 ) startClass = args[ i ] ; else if ( args[ i ].equalsIgnoreCase( "-r" ) ) options |= RECURSECLASS; else if ( args[ i ].equalsIgnoreCase( "-i" ) ) options |= INHERITED; else if ( args[ i ].equalsIgnoreCase( "-n" ) ) options |= NOMODIFIERS; else if ( args[ i ].equalsIgnoreCase( "-c" ) ) options |= CONSTRUCTORS; else if ( args[ i ].equalsIgnoreCase( "-m" ) ) options |= METHODS; else if ( args[ i ].equalsIgnoreCase( "-p" ) ) options |= PARAMETERS; else if ( args[ i ].equalsIgnoreCase( "-e" ) ) options |= EXCEPTIONS; else if ( args[ i ].equalsIgnoreCase( "-f" ) ) options |= FIELDS; else if ( args[ i ].equalsIgnoreCase( "-a" ) ) options |= CONSTRUCTORS | METHODS | PARAMETERS | EXCEPTIONS | FIELDS; else System.out.println( "Inspector.handleArgs: unknown parameter " + args[ i ] + "." ); } } void printModifiers( int mods ) { if ( ( options & NOMODIFIERS ) == 0 ) if ( Modifier.toString( mods ).length() > 0 ) System.out.print( Modifier.toString( mods ) + " " ); } void printType( Class type ) { if ( type.isArray() ) System.out.print( type.getComponentType().getName() + " [] " ); else System.out.print( type.getName() + " " ); } void printParams( Class[] params ) { if (params.length == 0) System.out.println( "()" ); else { if ( ( options & PARAMETERS ) != 0 ) { System.out.print( "(" ); for ( int i = 0; i < params.length; i++ ) { if ( i < params.length - 1 ) System.out.print( params[ i ].getName() + "," ); else System.out.print( params[ i ].getName() ); } System.out.println( ")" ); } else System.out.println( "(" + params.length + " parameters)" ); } } void printExceptions( String indent, Class[] exps ) { if ( ( options & EXCEPTIONS ) != 0 ) for ( int i = 0; i < exps.length; i++ ) System.out.println( indent + " e" + i + ": throws " + exps[ i ].getName() ); } void printConstructors( String indent, Class type ) { Constructor[] myConstructors; if ( ( options & INHERITED ) != 0 ) myConstructors = type.getConstructors(); else myConstructors = type.getDeclaredConstructors(); System.out.println( indent + " " + myConstructors.length + " constructors:" ); for ( int i = 0; i < myConstructors.length; i++ ) { System.out.print( indent + " c" + i + ": " ); printModifiers( myConstructors[ i ].getModifiers() ); System.out.print( myConstructors[ i ].getName() ); printParams( myConstructors[ i ].getParameterTypes() ); printExceptions( indent, myConstructors[ i ].getExceptionTypes() ); } } void printMethods( String indent, Class type ) { Method[] myMethods; if ( ( options & INHERITED ) != 0 ) myMethods = type.getMethods(); else myMethods = type.getDeclaredMethods(); System.out.println( indent + " " + myMethods.length + " methods:" ); for ( int i = 0; i < myMethods.length; i++ ) { System.out.print( indent + " m" + i + ": " ); printModifiers( myMethods[ i ].getModifiers() ); printType( myMethods[ i ].getReturnType() ); System.out.print( myMethods[ i ].getName() ); printParams( myMethods[ i ].getParameterTypes() ); printExceptions( indent, myMethods[ i ].getExceptionTypes() ); } } void printFields( String indent, Class type ) { Field[] myFields; if ( ( options & INHERITED ) != 0 ) myFields = type.getFields(); else myFields = type.getDeclaredFields(); System.out.println( indent + " " + myFields.length + " fields:" ); for( int i = 0; i < myFields.length; i++ ) { System.out.print( indent + " f" + i + ": " ); printModifiers( myFields[ i ].getModifiers() ); printType( myFields[ i ].getType() ); System.out.println( myFields[ i ].getName() ); } } // Instances private int options = 0; private String startClass = new String( "" ); private final static String[] basic = { "class", "interface" }, extended = { "extends", "implements" }; }