/* ReflectionTest.java * * An example of the reflection properties of Java. This * example is derived from Example 5-4 in Core Java, Volume I. * Performs the same basic functions as javap (seems faster!). * * Bruce M. Bolden * March 10, 1998 * http://www.cs.uidaho.edu/~bruceb/ * */ import java.lang.reflect.*; public class ReflectionTest { public static void main(String[] args) { int nArgs = args.length; if( nArgs < 1 ) { ShowUsage(); System.exit(-1); } // Process all classes for( int i = 0 ; i < nArgs ; ++i ) { if( i > 0 ) System.out.println( "\n\n" ); String name = args[i]; processClass( name ); } } /** Process a single class name */ public static void processClass( String name ) { try { Class cl = Class.forName(name); Class sc = cl.getSuperclass(); System.out.print("class " + name); if (sc != null && !sc.equals(Object.class)) System.out.print("\n extends " + sc.getName()); if( sc != null && !sc.equals(Object.class) ) { System.out.print("\n implements " ); printInterfaces(cl); } System.out.print("\n{\n"); System.out.println( "\t// Constructors" ); printConstructors(cl); System.out.println(); System.out.println( "\t// Methods" ); printMethods(cl); System.out.println(); System.out.println( "\t// Fields" ); printFields(cl); System.out.println("}"); } catch(ClassNotFoundException e) { System.out.println("Class not found."); } } /** Show Interfaces implemented by the class */ public static void printInterfaces(Class cl) { Class[] impClasses = cl.getInterfaces(); for (int i = 0; i < impClasses.length; i++) { Class c = impClasses[i]; String name = c.getName(); if( i > 0 ) System.out.print(", " + name ); else System.out.print(" " + name ); } } /** Show Constructors implemented by the class */ public static void printConstructors(Class cl) { Constructor[] constructors = cl.getDeclaredConstructors(); for (int i = 0; i < constructors.length; i++) { Constructor c = constructors[i]; Class[] paramTypes = c.getParameterTypes(); String name = cl.getName(); System.out.print(Modifier.toString(c.getModifiers())); System.out.print(" " + name + "("); for (int j = 0; j < paramTypes.length; j++) { if (j > 0) System.out.print(", "); System.out.print(paramTypes[j].getName()); } System.out.println(");"); } } /** Show Methods implemented by the class */ public static void printMethods(Class cl) { Method[] methods = cl.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { Method m = methods[i]; Class retType = m.getReturnType(); Class[] paramTypes = m.getParameterTypes(); String name = m.getName(); System.out.print(Modifier.toString(m.getModifiers())); System.out.print(" " + retType.getName() + " " + name + "("); for (int j = 0; j < paramTypes.length; j++) { if (j > 0) System.out.print(", "); System.out.print(paramTypes[j].getName()); } System.out.println(");"); } } /** Show Fields implemented by the class */ public static void printFields(Class cl) { Field[] fields = cl.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; Class type = f.getType(); String name = f.getName(); System.out.print(Modifier.toString(f.getModifiers())); System.out.println(" " + type.getName() + " " + name + ";"); } } /** Show expected argument list */ static void ShowUsage() { System.out.println( "Usage: java ReflectionTest className[s]" ); } } /* * Cay S. Horstmann & Gary Cornell, Core Java * Published By Sun Microsystems Press/Prentice-Hall * Copyright (C) 1997 Sun Microsystems Inc. * All Rights Reserved. * * Permission to use, copy, modify, and distribute this * software and its documentation for NON-COMMERCIAL purposes * and without fee is hereby granted provided that this * copyright notice appears in all copies. * * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */