source: trunk/gsdl3/src/packages/javagdbm/java/au/com/pharos/meta/ShowClass.java@ 10737

Last change on this file since 10737 was 10737, checked in by kjdon, 19 years ago

Java Wrapper for GDBM, from Martin Pool. Original website gone, so added it all in here. I have modified the Makefiles to work in greenstone, and on macs, and added windows makefiles

  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1// This example is from the book "Java in a Nutshell, Second Edition".
2// Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
3// You may distribute this source code for non-commercial purposes only.
4// You may study, modify, and use this example for any purpose, as long as
5// this notice is retained. Note that this example is provided "as is",
6// WITHOUT WARRANTY of any kind either expressed or implied.
7
8import java.lang.reflect.*;
9
10/** A program that displays a class synopsis for the named class */
11public class ShowClass {
12 /** The main method. Print info about the named class */
13 public static void main(String[] args) throws ClassNotFoundException {
14 Class c = Class.forName(args[0]);
15 print_class(c);
16 }
17
18 /** Display the modifiers, name, superclass and interfaces of a class
19 * or interface. Then go and list all constructors, fields, and methods. */
20 public static void print_class(Class c)
21 {
22 // Print modifiers, type (class or interface), name and superclass.
23 if (c.isInterface()) {
24 // The modifiers will include the "interface" keyword here...
25 System.out.print(Modifier.toString(c.getModifiers()) + c.getName());
26 }
27 else
28 System.out.print(Modifier.toString(c.getModifiers()) + " class " +
29 c.getName() +
30 " extends " + c.getSuperclass().getName());
31
32 // Print interfaces or super-interfaces of the class or interface.
33 Class[] interfaces = c.getInterfaces();
34 if ((interfaces != null) && (interfaces.length > 0)) {
35 if (c.isInterface()) System.out.println(" extends ");
36 else System.out.print(" implements ");
37 for(int i = 0; i < interfaces.length; i++) {
38 if (i > 0) System.out.print(", ");
39 System.out.print(interfaces[i].getName());
40 }
41 }
42
43 System.out.println(" {"); // Begin class member listing.
44
45 // Now look up and display the members of the class.
46 System.out.println(" // Constructors");
47 Constructor[] constructors = c.getDeclaredConstructors();
48 for(int i = 0; i < constructors.length; i++) // Display constructors.
49 print_method_or_constructor(constructors[i]);
50
51 System.out.println(" // Fields");
52 Field[] fields = c.getDeclaredFields(); // Look up fields.
53 for(int i = 0; i < fields.length; i++) // Display them.
54 print_field(fields[i]);
55
56 System.out.println(" // Methods");
57 Method[] methods = c.getDeclaredMethods(); // Look up methods.
58 for(int i = 0; i < methods.length; i++) // Display them.
59 print_method_or_constructor(methods[i]);
60
61 System.out.println("}"); // End class member listing.
62 }
63
64 /** Return the name of an interface or primitive type, handling arrays. */
65 public static String typename(Class t) {
66 String brackets = "";
67 while(t.isArray()) {
68 brackets += "[]";
69 t = t.getComponentType();
70 }
71 return t.getName() + brackets;
72 }
73
74 /** Return a string version of modifiers, handling spaces nicely. */
75 public static String modifiers(int m) {
76 if (m == 0) return "";
77 else return Modifier.toString(m) + " ";
78 }
79
80 /** Print the modifiers, type, and name of a field */
81 public static void print_field(Field f) {
82 System.out.println(" " +
83 modifiers(f.getModifiers()) +
84 typename(f.getType()) + " " + f.getName() + ";");
85 }
86
87 /** Print the modifiers, return type, name, parameter types and exception
88 * type of a method or constructor. Note the use of the Member interface
89 * to allow this method to work with both Method and Constructor objects */
90 public static void print_method_or_constructor(Member member) {
91 Class returntype=null, parameters[], exceptions[];
92 if (member instanceof Method) {
93 Method m = (Method) member;
94 returntype = m.getReturnType();
95 parameters = m.getParameterTypes();
96 exceptions = m.getExceptionTypes();
97 } else {
98 Constructor c = (Constructor) member;
99 parameters = c.getParameterTypes();
100 exceptions = c.getExceptionTypes();
101 }
102
103 System.out.print(" " + modifiers(member.getModifiers()) +
104 ((returntype!=null)? typename(returntype)+" " : "") +
105 member.getName() + "(");
106 for(int i = 0; i < parameters.length; i++) {
107 if (i > 0) System.out.print(", ");
108 System.out.print(typename(parameters[i]));
109 }
110 System.out.print(")");
111 if (exceptions.length > 0) System.out.print(" throws ");
112 for(int i = 0; i < exceptions.length; i++) {
113 if (i > 0) System.out.print(", ");
114 System.out.print(typename(exceptions[i]));
115 }
116 System.out.println(";");
117 }
118}
Note: See TracBrowser for help on using the repository browser.