View Javadoc

1   package com.insanityengine.ghia.util;
2   
3   import java.lang.reflect.*;
4   
5   /***
6    *
7    * <P>
8    * </P>
9    *
10   * @author BrianHammond
11   *
12   * $Header: /usr/local/cvsroot/ghia/src/java/com/insanityengine/ghia/util/DocReflect.java,v 1.6 2005/03/19 17:50:02 brian Exp $
13   *
14   */
15  
16  public class DocReflect {
17  
18  	/***
19  	 * 
20  	 * The main method rox!
21  	 * 
22  	 * @param argv the array of String
23  	 * 
24  	 */
25  	public final static void main( String argv[] ) {
26  		for ( int i = 0 ; i < argv.length ; ++i ) {
27  			DocReflect.reflectOn( argv[ i ] );
28  		}
29  
30  	}
31  
32  	public DocReflect() {}
33  
34  	/***
35  	 * 
36  	 * The reflectOn method rox!
37  	 * 
38  	 * @param className the String
39  	 * 
40  	 */
41  	public final static void reflectOn( String className ) {
42  		Class theClass = forName( className );
43  		if ( null != theClass ) {
44  			publicMethods( theClass );
45  		} else {
46  			System.err.println( "No class for " + className );
47  		}
48  	}
49  
50  	/***
51  	 * 
52  	 * The forName method rox!
53  	 * 
54  	 * @param className the String
55  	 * 
56  	 * @return the Class
57  	 * 
58  	 */
59  	public final static Class forName( String className ) {
60  		Class lass = null;
61  		try {
62  			lass = Class.forName( className );
63  		} catch ( Exception e ) {
64  		}
65  		return lass;
66  	}
67  
68  	/***
69  	 * 
70  	 * The publicMethods method rox!
71  	 * 
72  	 * @param theClass the Class
73  	 * 
74  	 */
75  	public final static void publicMethods( Class theClass ) {
76  		
77  		Method [] methods = theClass.getMethods();
78  		if ( null != methods ) {
79  
80  			String currentPackage = theClass.getName();
81  			currentPackage = currentPackage.substring( 0, currentPackage.lastIndexOf( '.' ) + 1 );
82  
83  			for ( int i = 0 ; i < methods.length ; ++i ) {
84  				if ( theClass == methods[ i ].getDeclaringClass() ) {
85  					publicMethod( currentPackage, methods[ i ] );
86  				}
87  			}
88  		}
89  	}
90  
91  	/***
92  	 * 
93  	 * The publicMethod method rox!
94  	 * 
95  	 * @param currentPackage the String
96  	 * @param method the java.lang.reflect.Method
97  	 * 
98  	 */
99  	public final static void publicMethod( String currentPackage, Method method ) {
100 
101 		System.out.println( "\t/**" );
102 		System.out.println( commentPrefix );
103 		
104 		if ( null != method ) {
105 			System.out.println( commentPrefix + "The " + method.getName() + " method rox!" );
106 			parameters( currentPackage, method.getParameterTypes() );
107 			returnType( currentPackage, method.getReturnType() );
108 		}
109 		System.out.println( commentPrefix );
110 		System.out.println( "\t */" );
111 		System.out.println();
112 	}	
113 
114 	/***
115 	 * 
116 	 * The parameters method rox!
117 	 * 
118 	 * @param currentPackage the String
119 	 * @param params the array of Class
120 	 * 
121 	 */
122 	public final static void parameters( String currentPackage, Class [] params ) {
123 		if ( null != params && 0 != params.length ) {
124 			System.out.println( commentPrefix );
125 			for ( int i = 0 ; i < params.length ; ++i ) {
126 				
127 				String type = params[ i ].getName();
128 				String array = "";
129 
130 				if ( 0 == type.indexOf( "[L" ) ) {
131 					type = type.substring( 2, type.length() - 1 );
132 					array = "array of ";
133 				}
134 
135 				type = removeFrom( "java.lang.", type );
136 				type = removeFrom( currentPackage, type );
137 					
138 				String name = "the" + type.substring( type.lastIndexOf( '.' ) + 1 );
139 				
140 				System.out.println( commentPrefix + "@param " + name + " the " + array + type ); 
141 			}
142 		}
143 	}
144 
145 	/***
146 	 * 
147 	 * The returnType method rox!
148 	 * 
149 	 * @param currentPackage the String
150 	 * @param rtype the Class
151 	 * 
152 	 */
153 	public final static void returnType( String currentPackage, Class rtype ) {
154 		String type = rtype.getName();
155 		if ( !type.equals( "void" ) ) {
156 
157 			System.out.println( commentPrefix );
158 		
159 			String array = "the ";
160 			
161 			if ( 0 == type.indexOf( "[L" ) ) {
162 				type = type.substring( 2, type.length() - 1 );
163 				array = "array of ";
164 			}
165 
166 			type = removeFrom( "java.lang.", type );
167 			type = removeFrom( currentPackage, type );
168 
169 			String name = "the" + type.substring( type.lastIndexOf( '.' ) + 1 );
170 
171 			System.out.println( commentPrefix + "@return " + array + type ); 
172 		}
173 	}
174 
175 	/***
176 	 * 
177 	 * The removeFrom method rox!
178 	 * 
179 	 * @param base the String
180 	 * @param str the String
181 	 * 
182 	 * @return the String
183 	 * 
184 	 */
185 	public final static String removeFrom( String base, String str ) {
186 		String s = str;
187 		if ( 0 == str.indexOf( base ) ) {
188 			s = str.substring( base.length() );
189 			if ( -1 != s.indexOf( '.' ) ) s = str;
190 		}
191 		return s;
192 	}
193 
194 	// private 
195 
196 	private final static String commentPrefix = "\t * ";
197 	
198 };
199 
200 /***
201  *
202  * $Log: DocReflect.java,v $
203  * Revision 1.6  2005/03/19 17:50:02  brian
204  * repackaging
205  *
206  * Revision 1.5  2004/09/01 01:10:42  brian
207  * fix class level javadoc placement
208  *
209  * Revision 1.4  2004/09/01 00:11:06  brian
210  * author, log and header stuff
211  *
212  *
213  */