1
2
3
4
5
6
7
8
9import java.lang.reflect.*;
10
11class ClassInfo {
12
13 private
14 Class c;
15
16
17
18
19 public
20 ClassInfo(Object obj) {
21 this.c = obj.getClass();
22 }
23
24 public
25 ClassInfo(String className)
26 throws ClassNotFoundException
27 {
28 this.c = Class.forName(className);
29 }
30
31
32
33
34 public
35 String getModifiers() {
36 return Modifier.toString(c.getModifiers());
37 }
38
39
40
41
42 public
43 String getInterfaces() {
44 Class[] ifs = c.getInterfaces();
45 if (ifs.length == 0) {
46 return "";
47 } else {
48 String res = "";
49 res +=
50 ( c.isInterface()
51 ? "\n extends "
52 : "\n implements ");
53
54 for ( int i = 0;
55 i < ifs.length;
56 ++i ) {
57 if ( i != 0 ) {
58 res += ", ";
59 }
60 res += ifs[i].getName();
61 }
62 return res;
63 }
64 }
65
66
67
68
69 public
70 String getClassHeader() {
71 return
72 getModifiers()
73 +
74 ( c.isInterface()
75 ? ""
76 : "\nclass"
77 )
78 + " "
79 + c.getName()
80 +
81 ( c.isInterface()
82 ? ""
83 : "\n extends " + c.getSuperclass().getName()
84 )
85 + getInterfaces();
86 }
87
88
89
90
91 public
92 String getClassMethods() {
93 return
94 getFields()
95 + getConstructors()
96 + getMethods();
97 }
98
99
100
101
102 public
103 String getFields() {
104 Field[] fl = c.getDeclaredFields();
105 String res = "";
106
107 for ( int i = 0;
108 i < fl.length;
109 ++i ) {
110 res += getField(fl[i]);
111 }
112 return res;
113 }
114
115
116
117
118 public
119 String getConstructors() {
120 Constructor[] cl = c.getDeclaredConstructors();
121 String res = "";
122
123 for ( int i = 0;
124 i < cl.length;
125 ++i ) {
126 res += getConstructor(cl[i]);
127 }
128 return res;
129 }
130
131
132
133
134 public
135 String getMethods() {
136 Method[] ml = c.getDeclaredMethods();
137 String res = "";
138
139 for ( int i = 0;
140 i < ml.length;
141 ++i ) {
142 res += getMethod(ml[i]);
143 }
144 return res;
145 }
146
147
148
149
150 public
151 String getField(Field f) {
152 return
153 "\n "
154 + Modifier.toString(f.getModifiers())
155 + "\n "
156 + getTypeName(f.getType())
157 + " "
158 + f.getName()
159 + ";\n";
160 }
161
162
163
164
165 public
166 String getConstructor(Constructor c) {
167 return
168 "\n "
169 + Modifier.toString(c.getModifiers())
170 + "\n "
171 + c.getName()
172 + getParamList(c.getParameterTypes())
173 + getExcList(c.getExceptionTypes())
174 + ";\n";
175 }
176
177
178
179
180 public
181 String getMethod(Method m) {
182 return
183 "\n "
184 + Modifier.toString(m.getModifiers())
185 + "\n "
186 + getTypeName(m.getReturnType())
187 + " "
188 + m.getName()
189 + getParamList(m.getParameterTypes())
190 + getExcList(m.getExceptionTypes())
191 + ";\n";
192 }
193
194
195
196
197 public
198 String getTypeName(Class t) {
199 String dim = "";
200 while ( t.isArray() ) {
201 dim += "[]";
202 t = t.getComponentType();
203 }
204 return t.getName() + dim;
205 }
206
207
208
209
210 public
211 String getParamList(Class[] pl) {
212 String res = "(";
213
214 for ( int i = 0;
215 i < pl.length;
216 ++i ) {
217 if ( i > 0 ) {
218 res += ", ";
219 }
220 res += getTypeName(pl[i]) +" x" + i;
221 }
222
223 res += ")";
224 return res;
225 }
226
227
228
229
230 public
231 String getExcList(Class[] el) {
232 if ( el.length == 0 ) {
233 return "";
234 } else {
235 String res = "\n throws ";
236
237 for ( int i = 0;
238 i < el.length;
239 ++ i ) {
240 if ( i != 0 ) {
241 res += ", ";
242 }
243 res += getTypeName(el[i]);
244 }
245
246 return res;
247 }
248 }
249
250
251
252
253 public
254 String getClassInfo() {
255 return
256 getClassHeader()
257 + " {\n"
258 + getClassMethods()
259 + "\n}\n";
260 }
261}