001: /*
002: * ConstructorMember.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: import java.util.List;
025: import java.util.Map;
026:
027: import net.sf.jdec.util.Util;
028: import net.sf.jdec.config.Configuration;
029: import net.sf.jdec.constantpool.MethodInfo;
030: import net.sf.jdec.core.LocalVariableStructure;
031: import net.sf.jdec.core.ShortcutAnalyser;
032: import net.sf.jdec.blocks.TryBlock;
033: import net.sf.jdec.blocks.CatchBlock;
034: import net.sf.jdec.blocks.FinallyBlock;
035:
036: public class ConstructorMember extends Behaviour {
037:
038: private ShortcutAnalyser shortCutA = null;
039: private java.lang.String name = "";
040: private String params[] = null;
041: private int numberofparams = -1;
042: private int numberofexceptions = -1;
043: private String exceptions[] = null;
044: private boolean isDefault = false;
045: private byte[] code = null;
046: private java.lang.String declaringClass = null;
047: private java.lang.String[] accessSpecifier = null;
048: private ArrayList allTrys = new ArrayList();
049: private ArrayList createdTableList = new ArrayList();
050: private ArrayList allswitches = new ArrayList();
051: private ArrayList behaviourLoops = new ArrayList();
052: private LocalVariableStructure localVars = new LocalVariableStructure();
053:
054: public LocalVariableStructure getLocalVariables() {
055: return localVars;
056: }
057:
058: public void setMethodLocalVariables(
059: LocalVariableStructure methodLocalVariables) {
060: this .localVars = methodLocalVariables;
061: }
062:
063: public ArrayList getSynchronizedEntries() {
064: return synchronizedEntries;
065: }
066:
067: private ArrayList synchronizedEntries;
068:
069: public ConstructorMember(String name) {
070: this .name = name;
071: }
072:
073: public ConstructorMember(java.lang.String name,
074: java.lang.String Class) {
075: this .name = name;
076: this .declaringClass = Class;
077: }
078:
079: public java.lang.String[] getAccessSpecifier() {
080: return accessSpecifier;
081: }
082:
083: public void setAccessSpecifier(java.lang.String[] accessSpecifier) {
084: this .accessSpecifier = accessSpecifier;
085: }
086:
087: public byte[] getCode() {
088: return code;
089: }
090:
091: public void setCode(byte[] code) {
092: this .code = code;
093: }
094:
095: public java.lang.String getDeclaringClass() {
096: return declaringClass;
097: }
098:
099: public void setDeclaringClass(java.lang.String declaringClass) {
100: this .declaringClass = declaringClass;
101: }
102:
103: // Alternate method to use is getExceptionTypes
104: // Try to use the latter one so that Behaviour Type
105: // can be used when invoking.
106:
107: public java.lang.String[] getExceptions() {
108: return exceptions;
109: }
110:
111: public void setExceptionsTypes(java.lang.String[] exceptions) {
112: this .exceptions = exceptions;
113: setNumberofexceptions(exceptions.length);
114: }
115:
116: public boolean isDefault() {
117: return isDefault;
118: }
119:
120: public void setDefault(boolean isDefault) {
121: this .isDefault = isDefault;
122: }
123:
124: public java.lang.String getName() {
125: return name;
126: }
127:
128: public int getNumberofexceptions() {
129: return numberofexceptions;
130: }
131:
132: private void setNumberofexceptions(int numberofexceptions) {
133: this .numberofexceptions = numberofexceptions;
134: }
135:
136: public int getNumberofparamters() {
137: return numberofparams;
138: }
139:
140: private void setNumberofparams(int numberofparams) {
141: this .numberofparams = numberofparams;
142: }
143:
144: public String[] getParams() {
145: return params;
146: }
147:
148: public void setParams(String[] params) {
149: this .params = params;
150: setNumberofparams(params.length);
151: }
152:
153: public String toString() {
154: String temp = "";
155: temp += this .accessSpecifier + "\t" + this .name + "(";
156: for (int i = 0; i < this .getNumberofparamters(); i++) {
157: temp += this .getParams()[i];
158: if (i != this .getNumberofparamters())
159: temp += ",";
160: }
161: temp += ")";
162:
163: for (int i = 0; i < this .getNumberofexceptions(); i++) {
164: temp += this .getExceptions()[i];
165: if (i != this .getNumberofexceptions())
166: temp += ",";
167:
168: }
169:
170: temp += "{\n\t";
171: temp += this .getCode();
172: temp += "\n}";
173: return temp;
174: }
175:
176: public String getBehaviourName() {
177:
178: java.lang.String v = name;
179: StringBuffer bb = new StringBuffer("");
180: Util.checkForImport(v, bb);
181: return bb.toString();
182: }
183:
184: public java.lang.String getUserFriendlyMethodAccessors() {
185:
186: String temp = "";
187: for (int i = 0; i < accessSpecifier.length; i++) {
188: temp += accessSpecifier[i];
189: temp += " ";
190: }
191: return temp;
192: }
193:
194: public java.lang.String getReturnType() {
195: return "";
196: }
197:
198: public java.lang.String getUserFriendlyMethodParams() {
199: String temp = "";
200: if (params.length < 1) {
201: temp = "( )";
202: } else {
203: temp += "(";
204: for (int i = 0; i < params.length; i++) {
205: String tmp = "";
206: if (i > 0 && i < params.length)
207: temp += ",";
208: if (params[i].indexOf("[") != -1
209: || params[i].indexOf("L") != -1) {
210: tmp = parseObjectType(params[i]);
211: temp += tmp;
212: } else {
213: temp += params[i];
214: }
215: temp += " ";
216: }
217: temp += ")";
218: }
219: return temp;
220: }
221:
222: private static String parseObjectType(java.lang.String str) {
223: String type = "";
224: int ch;
225: int counter = 0;
226: String refName = "";
227: ch = str.indexOf("[");
228: if (ch != -1) {
229:
230: while (ch != -1) {
231: counter++;
232: ch = str.indexOf("[", ch + 1);
233: }
234: ch = str.indexOf("L");
235: if (ch != -1) {
236: refName = str.substring(ch + 1);
237: if (refName.indexOf(";") != -1)
238: refName = refName
239: .substring(0, refName.indexOf(";"));
240: }
241: if (ch == -1) {
242: refName = str.substring(str.lastIndexOf("[") + 1);
243: if (refName.indexOf(";") != -1)
244: refName = refName
245: .substring(0, refName.indexOf(";"));
246: }
247: type += refName + " ";
248: for (int count = 0; count < counter; count++) {
249: type += "[]";
250: }
251:
252: return type;
253: }
254:
255: else if (str.indexOf("L") != -1) {
256: ch = str.indexOf("L");
257: refName = str.substring(ch + 1);
258: if (refName.indexOf(";") != -1)
259: refName = refName.substring(0, refName.indexOf(";"));
260:
261: type += refName + "\t";
262:
263: return type;
264: } else {
265: return " ERROR";
266: }
267:
268: }
269:
270: public java.lang.String getStringifiedParameters() {
271: java.lang.String temp = "";
272:
273: List al = (List) java.util.Arrays.asList(params);
274: Iterator i = al.iterator();
275: if (i.hasNext() == false) {
276: temp += "()";
277: return temp;
278: } else {
279: temp += "(";
280: while (i.hasNext()) {
281: temp += (java.lang.String) i.next();
282: if (i.hasNext()) {
283: temp += ",";
284: }
285: }
286: temp += ")";
287: return temp;
288: }
289: }
290:
291: public String[] getMethodParams() {
292: return params;
293: }
294:
295: public String[] getMethodAccessors() {
296:
297: return accessSpecifier;
298: }
299:
300: public void setExceptionTableList(ArrayList tables) {
301: this .exceptionTables = tables;
302: }
303:
304: public ArrayList getExceptionTableList() {
305: return exceptionTables;
306: }
307:
308: public ArrayList getAllTriesForMethod() {
309: return allTrys;
310: }
311:
312: public void setAllTriesForMethod(ArrayList alltries) {
313:
314: allTrys = alltries;
315: }
316:
317: public void setCreatedTableList(ArrayList list) {
318: this .createdTableList = list;
319:
320: }
321:
322: public ArrayList getCreatedTableList() {
323: return createdTableList;
324:
325: }
326:
327: public String[] getExceptionTypes() {
328: return this .exceptions;
329: }
330:
331: public ArrayList getAllSwitchBlks() {
332: return allswitches;
333: }
334:
335: public void setSynchronizedTableEntries(ArrayList list) {
336: this .synchronizedEntries = list;
337: //private ArrayList synchronizedEntries;
338: }
339:
340: public void setAllswitches(ArrayList allswitches) {
341: this .allswitches = allswitches;
342: }
343:
344: public ArrayList getBehaviourLoops() {
345: return behaviourLoops;
346: }
347:
348: public void setBehaviourLoops(ArrayList indexInfiniteLoop) {
349: this .behaviourLoops = indexInfiniteLoop;
350: }
351:
352: public ArrayList getInstructionStartPositions() {
353: return this .starts;
354: }
355:
356: public void setInstrStartPos(ArrayList starts) {
357: this .starts = starts;
358: }
359:
360: private ArrayList starts = null;
361:
362: public java.lang.String removeDummyLabels() {
363: java.lang.String temp = getCodeStatements();
364: //if(getLabels()==null || getLabels().size()==0)return temp;
365: int i = -1;
366: int start = 0;
367: String copy = getCodeStatements();
368: i = copy.indexOf("#FORINDEX", start);
369: while (i != -1) {
370: int j = i + 9;
371: int hash = copy.indexOf("#", (i + 1));
372: java.lang.String num = copy.substring(j, hash);
373: java.lang.String dummy = copy.substring(i, (hash + 1));
374: int inum = Integer.parseInt(num);
375: boolean skip = false;
376: if (getLabels() != null && getLabels().size() != 0) {
377: java.lang.String lbl = (java.lang.String) getLabels()
378: .get(new Integer(inum));
379: if (lbl != null) {
380: copy = copy.replaceAll(dummy, lbl);
381: skip = true;
382: }
383: }
384: if (!skip) {
385: copy = copy.replaceAll("\n" + dummy + "\n", "");
386: }
387:
388: i = copy.indexOf("#FORINDEX", start);
389:
390: }
391: temp = copy;
392: temp = temp.replaceAll("\n\n", "\n");
393:
394: return temp;
395:
396: }
397:
398: public boolean isMethodConstructor() {
399: return true;
400: }
401:
402: public void createExceptionTableInStringifiedForm() {
403: java.lang.String detailed = Configuration
404: .getDetailedExceptionTableInfo();
405: if (detailed == null)
406: detailed = "false";
407: StringBuffer synch = new StringBuffer("");
408: StringBuffer str = new StringBuffer("");
409: ArrayList alltables = getExceptionTableList();
410: ArrayList alltries = getAllTriesForMethod();
411: if (alltables == null || alltables.size() == 0) {
412:
413: }
414: /*if(alltables.size() > 0)
415: {
416: str.append("TRY_CATCH_FINALLY BLOCK DETAILS...\n");
417: str.append("StartPC\tEndPC\tHandlerPC\tException Handler Name\n");
418:
419: str.append("Synchronized BLOCK DETAILS...\n");
420: str.append("StartPC\tEndPC\tHandlerPC\tException Handler Name\n");
421: } */
422: boolean showExcep = false;
423: boolean showSynch = false;
424: for (int z = 0; z < alltables.size(); z++) {
425:
426: MethodInfo.ExceptionTable curtab = (MethodInfo.ExceptionTable) alltables
427: .get(z);
428: int startpc = curtab.getStartPC();
429: int endpc = curtab.getEndPC();
430: int hpc = curtab.getStartOfHandler();
431: java.lang.String temp = " " + startpc + " ";
432: temp += endpc + " ";
433: temp += hpc + " ";
434: java.lang.String name = curtab.getExceptionName();
435: if (name.trim().equals("<any>")) {
436: name = "Any Handler(Special Handler)";
437: }
438: temp += " " + name + " ";
439: if (curtab.getMonitorEnterPosInCode() == -1) {
440: showExcep = true;
441: str.append(Util.formatDisassembledOutput(temp + "\n"));
442: } else {
443: showSynch = true;
444: synch
445: .append(Util.formatDisassembledOutput(temp
446: + "\n"));
447: }
448:
449: }
450:
451: if (showExcep) {
452: StringBuffer sb = new StringBuffer("");
453: sb
454: .append(Util
455: .formatDisassembledOutput("TRY_CATCH_FINALLY BLOCK DETAILS...\n"));
456: sb
457: .append(Util
458: .formatDisassembledOutput("----------------------------------------------------\n"));
459: sb
460: .append(Util
461: .formatDisassembledOutput("StartPC\tEndPC\tHandlerPC\tException Handler Name\n"));
462: sb
463: .append(Util
464: .formatDisassembledOutput("----------------------------------------------------\n"));
465: shortExceptionTableDetails = sb.toString() + "\n"
466: + str.toString() + "\n\n";
467: detailedExceptionTableDetails = sb.toString() + "\n"
468: + str.toString() + "\n\n";
469: }
470: if (showSynch) {
471: StringBuffer sb = new StringBuffer("");
472: sb
473: .append(Util
474: .formatDisassembledOutput("Synchronized BLOCK DETAILS...\n"));
475: sb
476: .append(Util
477: .formatDisassembledOutput("----------------------------------------------------\n"));
478: sb
479: .append(Util
480: .formatDisassembledOutput("StartPC\tEndPC\tHandlerPC\tException Handler Name\n"));
481: sb
482: .append(Util
483: .formatDisassembledOutput("----------------------------------------------------\n"));
484: synchTableDetails = sb.toString() + "\n" + synch.toString();
485: }
486: if (detailed.equalsIgnoreCase("true")) {
487: if (alltries != null && alltries.size() > 0) {
488: StringBuffer tries = new StringBuffer("");
489: tries
490: .append(Util
491: .formatDisassembledOutput("----------------------------------------------------\n"));
492: tries
493: .append(Util
494: .formatDisassembledOutput("Try Block Details\n"));
495: tries
496: .append(Util
497: .formatDisassembledOutput("----------------------------------------------------\n"));
498: for (int f = 0; f < alltries.size(); f++) {
499: tries
500: .append(Util
501: .formatDisassembledOutput("*** START OF TRY BLOCK ***\n\n"));
502: TryBlock tryb = (TryBlock) alltries.get(f);
503: int num = tryb.getNumberOfCatchBlks();
504: int start = tryb.getStart();
505: int end = tryb.getEnd();
506: tries.append((Util.formatDisassembledOutput(start
507: + " Is the Start of This Try Block\n")));
508: tries.append((Util.formatDisassembledOutput(end
509: + " Is the End of This Try Block\n")));
510: ArrayList catches = tryb.getAllCatchesForThisTry();
511: if (catches != null && catches.size() > 0) {
512: tries
513: .append((Util
514: .formatDisassembledOutput(num
515: + " Is the Number of Catch blocks In Method For This Try\n")));
516: for (int t = 0; t < catches.size(); t++) {
517: CatchBlock cblk = (CatchBlock) catches
518: .get(t);
519: int cs = cblk.getStart();
520: int ce = cblk.getEnd();
521: tries
522: .append(Util
523: .formatDisassembledOutput("\nCATCH BLOCK "
524: + (t + 1)
525: + " Details....\n\n"));
526: tries
527: .append((Util
528: .formatDisassembledOutput(cs
529: + " Is the Catch Start For This Try\n")));
530: tries
531: .append((Util
532: .formatDisassembledOutput(ce
533: + " Is the Catch End For This Try\n")));
534: }
535: }
536: if (tryb.hasFinallyBlk()) {
537: FinallyBlock block = tryb.getFinallyBlock();
538: if (block != null) {
539: int s = block.getStart();
540: int e = block.getEnd();
541: tries
542: .append(Util
543: .formatDisassembledOutput("\nFinally BLOCK Details....\n\n"));
544: tries
545: .append((Util
546: .formatDisassembledOutput(s
547: + "is the start of FinallY Block For This Try\n")));
548: tries
549: .append((Util
550: .formatDisassembledOutput(e
551: + "is the end of FinallY Block For This Try\n")));
552: }
553:
554: }
555:
556: tries
557: .append((Util
558: .formatDisassembledOutput("\n*** END OF TRY BLOCK ***\n\n")));
559:
560: }
561:
562: detailedExceptionTableDetails += (Util
563: .formatDisassembledOutput("\n\n"));
564: detailedExceptionTableDetails += tries.toString();
565:
566: }
567:
568: }
569:
570: }
571:
572: private java.lang.String shortExceptionTableDetails = "";
573: private java.lang.String detailedExceptionTableDetails = "";
574: private java.lang.String synchTableDetails = "";
575:
576: public String getShortExceptionTableDetails() {
577: return shortExceptionTableDetails;
578: }
579:
580: public String getDetailedExceptionTableDetails() {
581: return detailedExceptionTableDetails;
582: }
583:
584: public String getSynchTableDetails() {
585: return synchTableDetails;
586: }
587:
588: public void setShortCutAnalyser(ShortcutAnalyser sa) {
589: shortCutA = sa;
590: }
591:
592: public ShortcutAnalyser getShortCutAnalyser() {
593: return shortCutA;
594: }
595:
596: public void setClassRef(JavaClass classRef) {
597: this .classRef = classRef;
598: }
599:
600: public void setDatatypesForParams(Map datatypesForParams) {
601: this.datatypesForParams = datatypesForParams;
602: }
603:
604: }
|