001: /*
002: * JavaClass.java Copyright (c) 2006,07 Swaroop Belur
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008:
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013:
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
017: *
018: */
019:
020: package net.sf.jdec.reflection;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024:
025: import net.sf.jdec.main.ConsoleLauncher;
026: import net.sf.jdec.config.Configuration;
027: import net.sf.jdec.constantpool.ClassDescription;
028:
029: public class JavaClass {
030:
031: private ClassDescription cd;
032:
033: private ArrayList allFields = new ArrayList();
034: private ArrayList allMethods = new ArrayList();
035: private ArrayList allConstructors = new ArrayList();
036: private ArrayList interfacesImplemented = new ArrayList();
037:
038: private String className = ""; // Represents The complete or Full name
039: private String simpleName = ""; // Represents The partial name
040: private boolean isInner = false;
041: private boolean hasInner = false;
042: private boolean isInterface = false;
043: private boolean isArray = false;
044: private boolean isPrimitive = false;
045: private Class OuterClass = null;
046: private Class InnerClass = null;
047: private java.lang.String super classname = "I am not initialized!!!";
048: private java.lang.String packageName = "Default Package";
049: private java.lang.String[] accessSpecifiers = null;
050:
051: /**
052: * Constructor
053: * @param name The name of the Class(Complete|Full Nmae)
054: * @return
055: */
056:
057: public JavaClass(String name) {
058: this .className = name;
059: }
060:
061: /***
062: * Constructor
063: * @param name Stands for partial name Or Simple Name
064: * @param pkg Stands For package to which the Class belongs
065: */
066:
067: public JavaClass(java.lang.String name, java.lang.String pkg) {
068: this .className = name;
069: this .packageName = pkg;
070:
071: }
072:
073: public java.lang.String getClassName() {
074: return className;
075: }
076:
077: public java.lang.String getSimpleName() {
078: if (className != null && className.indexOf(".") != -1) {
079: return className.substring(className.lastIndexOf(".") + 1);
080: } else
081: return className;
082: }
083:
084: public void setField(FieldMember f) {
085: allFields.add(f);
086: }
087:
088: public void setMethod(MethodMember m) {
089: allMethods.add(m);
090: }
091:
092: public void setConstructor(ConstructorMember c) {
093: allConstructors.add(c);
094: }
095:
096: public ArrayList getDecalaredFields() {
097: return allFields;
098: }
099:
100: public ArrayList getDecalaredMethods() {
101: return allMethods;
102: }
103:
104: public ArrayList getConstructors() {
105: return this .allConstructors;
106: }
107:
108: public ArrayList getALLMethods() {
109: ArrayList allfunctions = new ArrayList();
110:
111: if (!ConsoleLauncher.isShowconstfirst()) {
112: allfunctions.addAll(allMethods);
113: allfunctions.addAll(allConstructors);
114: } else {
115: allfunctions.addAll(allConstructors);
116: allfunctions.addAll(allMethods);
117: }
118: return allfunctions;
119:
120: }
121:
122: public void setAccessSpecifiers(ArrayList accessSpec) {
123: accessSpecifiers = new java.lang.String[accessSpec.size()];
124: for (int i = 0; i < accessSpec.size(); i++) {
125: java.lang.String access_spec = (java.lang.String) accessSpec
126: .get(i);
127: accessSpecifiers[i] = access_spec;
128: }
129: //this.accessSpecifiers=Util.parseAccessSpecifiers(al,Constants.CLASS_ACC);
130: }
131:
132: public void setSuperClassName(java.lang.String super Name) {
133: qualifiedSuperClassName = new StringBuffer(super Name);
134: if (super Name != "" && super Name != null
135: && super Name.trim().length() != 0) {
136: boolean ok = checkThisClassName(this .className);
137: if (!ok) {
138: this .super classname = super Name;
139: } else {
140: this .super classname = "";
141: }
142:
143: String si = Configuration.getShowImport();
144: if (si.equalsIgnoreCase("true")) {
145: String name = super classname;
146: if (name.indexOf("/") != -1)
147: name = name.replaceAll("/", ".");
148: ConsoleLauncher.addImportClass(name);
149: String simpleName = "";
150: int index = name.lastIndexOf(".");
151: if (index != -1) {
152: simpleName = name.substring(index + 1);
153: } else
154: simpleName = name;
155: this .super classname = simpleName;
156: }
157:
158: }
159: }
160:
161: private boolean checkThisClassName(String className) {
162:
163: if (className.equals("java/lang/Object")) {
164: return true;
165:
166: } else
167: return false;
168: }
169:
170: public void setPackageName(String pkgName) {
171: this .packageName = pkgName;
172:
173: }
174:
175: /***
176: * NOTE: Not used now
177: * @return
178: */
179: public String describeMe() {
180: StringBuffer whoami = new StringBuffer();
181: whoami.append("The ClassName of This Class is ..."
182: + this .getClassName());
183: whoami
184: .append("The packageName to which this Class Name belongs is..."
185: + this .getPackageName());
186: whoami
187: .append("******************************************************");
188: whoami
189: .append("Total Number of fields belonging To this Class..."
190: + this .allFields.size()
191: + "...[NOTE: THIS DOES NOT INCLUDE INHERITED FIELDS]");
192: whoami
193: .append("******************************************************\n\t");
194: whoami.append("Field Descriptions...");
195: Iterator fields = this .allFields.iterator();
196: while (fields.hasNext()) {
197: FieldMember f = (FieldMember) fields.next();
198:
199: whoami.append(f);
200: }
201: whoami
202: .append("******************************************************");
203: whoami
204: .append("Total Number of Methods belonging To this Class..."
205: + this .allMethods.size()
206: + "...[NOTE: THIS DOES NOT INCLUDE INHERITED Methods]");
207: whoami
208: .append("******************************************************\n\t");
209: whoami.append("Field Descriptions...");
210: Iterator methods = this .allMethods.iterator();
211: while (methods.hasNext()) {
212: MethodMember m = (MethodMember) methods.next();
213:
214: //Change this line later...
215: whoami.append(m);
216: }
217: return whoami.toString();
218: }
219:
220: /**
221: * @return Returns the packageName.
222: */
223: public java.lang.String getPackageName() {
224:
225: java.lang.String pkg = "";
226: if (className != null && className.indexOf(".") != -1) {
227:
228: pkg = className.substring(0, className.lastIndexOf("."));
229: return pkg;
230: }
231:
232: return packageName;
233: }
234:
235: /**
236: * @return Returns the isArray.
237: */
238: public boolean isArray() {
239: return isArray;
240: }
241:
242: /**
243: * @param isArray The isArray to set.
244: */
245: public void setArray(boolean isArray) {
246: this .isArray = isArray;
247: }
248:
249: /**
250: * @return Returns the isInterface.
251: */
252: public boolean isInterface() {
253: return isInterface;
254: }
255:
256: /**
257: * @param isInterface The isInterface to set.
258: */
259: public void setInterface(boolean isInterface) {
260: this .isInterface = isInterface;
261: }
262:
263: /**
264: * @return Returns the isInner.
265: */
266: public boolean isInner() {
267: return isInner;
268: }
269:
270: /**
271: * @param isInner The isInner to set.
272: */
273: public void setInner(boolean isInner) {
274: this .isInner = isInner;
275: }
276:
277: /**
278: * @return Returns the hasInner.
279: */
280: public boolean isHasInner() {
281: return hasInner;
282: }
283:
284: /**
285: * @param hasInner The hasInner to set.
286: */
287: public void setHasInner(boolean hasInner) {
288: this .hasInner = hasInner;
289: }
290:
291: /**
292: * @return Returns the outerClass.
293: */
294: public Class getOuterClass() {
295: return OuterClass;
296: }
297:
298: /**
299: * @param outerClass The outerClass to set.
300: */
301: public void setOuterClass(Class outerClass) {
302: OuterClass = outerClass;
303: }
304:
305: /**
306: * @return Returns the innerClass.
307: */
308: public Class getInnerClass() {
309: return InnerClass;
310: }
311:
312: /**
313: * @param innerClass The innerClass to set.
314: */
315: public void setInnerClass(Class innerClass) {
316: InnerClass = innerClass;
317: }
318:
319: StringBuffer qualifiedSuperClassName = null;
320:
321: /**
322: * @return Returns the superclassname.
323: */
324: public java.lang.String getSuperclassname() {
325:
326: String si = Configuration.getShowImport();
327:
328: if (si.equalsIgnoreCase("true")) {
329: String name = super classname;
330: if (name.indexOf("/") != -1)
331: name = name.replaceAll("/", ".");
332: ConsoleLauncher.addImportClass(name);
333: String simpleName = "";
334: int index = name.lastIndexOf(".");
335: if (index != -1) {
336: simpleName = name.substring(index + 1);
337: } else
338: simpleName = name;
339: return simpleName;
340: }
341: return super classname;
342: }
343:
344: /**
345: * @param superclassname The superclassname to set.
346: */
347: /**
348: * @return Returns the isPrimitive.
349: */
350: public boolean isPrimitive() {
351: return isPrimitive;
352: }
353:
354: /**
355: * @param isPrimitive The isPrimitive to set.
356: */
357: public void setPrimitive(boolean isPrimitive) {
358: this .isPrimitive = isPrimitive;
359: }
360:
361: public FieldMember getField(String fname) {
362: FieldMember f = null;
363: Iterator it = this .allFields.iterator();
364: while (it.hasNext()) {
365: FieldMember field = (FieldMember) it.next();
366: if (f.getName() == fname) {
367: f = field;
368: }
369: }
370:
371: return f;
372: }
373:
374: /**
375: * @return Returns the accessSpecifiers in a form of list .
376: */
377: public java.lang.String getAccessSpecifiers() {
378: if (accessSpecifiers != null && accessSpecifiers.length > 0) {
379: StringBuffer temp = new StringBuffer();
380: temp.append("[");
381: for (int i = 0; i < accessSpecifiers.length; i++) {
382: temp.append(accessSpecifiers[i]);
383: if (i != accessSpecifiers.length)
384: temp.append("\t");
385: }
386: temp.append("]");
387: return temp.toString();
388: } else
389: return "";
390: //return "Default Package Access";
391:
392: }
393:
394: public java.lang.String getUserFriendlyAccessSpecifiers() {
395: if (accessSpecifiers != null && accessSpecifiers.length > 0) {
396: StringBuffer temp = new StringBuffer();
397: // temp.append("[");
398: boolean interfacep = false;
399: for (int i = 0; i < accessSpecifiers.length; i++) {
400: if (accessSpecifiers[i].indexOf("super") == -1
401: && accessSpecifiers[i].indexOf("interface") == -1) {
402: temp.append(accessSpecifiers[i]);
403: if (i != accessSpecifiers.length)
404: temp.append(" ");
405: }
406: if (accessSpecifiers[i].indexOf("interface") != -1) {
407: interfacep = true;
408: }
409: }
410: if (interfacep) {
411: temp.append("interface");
412: }
413: //temp.append("]");
414: return temp.toString();
415: } else
416: return "";
417:
418: }
419:
420: public void setInterfacesImplemented(ArrayList interfaces) {
421: interfacesImplemented = interfaces;
422: }
423:
424: public ArrayList getInterfacesImplemented() {
425: return interfacesImplemented;
426: }
427:
428: public String getQualifiedSuperClassName() {
429: if (qualifiedSuperClassName != null)
430: return qualifiedSuperClassName.toString();
431: else
432: return "";
433: }
434:
435: public ClassDescription getCd() {
436: return cd;
437: }
438:
439: public void setCd(ClassDescription cd) {
440: this.cd = cd;
441: }
442: }
|