0001: /*
0002: * MethodInfo.java Copyright (c) 2006,07 Swaroop Belur
0003: *
0004: * This program is free software; you can redistribute it and/or
0005: * modify it under the terms of the GNU General Public License
0006: * as published by the Free Software Foundation; either version 2
0007: * of the License, or (at your option) any later version.
0008:
0009: * This program is distributed in the hope that it will be useful,
0010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012: * GNU General Public License for more details.
0013:
0014: * You should have received a copy of the GNU General Public License
0015: * along with this program; if not, write to the Free Software
0016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017: *
0018: */
0019:
0020: package net.sf.jdec.constantpool;
0021:
0022: import net.sf.jdec.blocks.Switch;
0023: import net.sf.jdec.config.Configuration;
0024: import net.sf.jdec.core.JvmOpCodes;
0025: import net.sf.jdec.core.LocalVariableStructure;
0026: import net.sf.jdec.core.ShortcutAnalyser;
0027: import net.sf.jdec.main.ConsoleLauncher;
0028: import net.sf.jdec.util.Constants;
0029: import net.sf.jdec.util.Util;
0030:
0031: import java.util.*;
0032:
0033: public class MethodInfo {
0034:
0035: private ShortcutAnalyser shortCutA = null;
0036:
0037: private LocalVariableStructure structure;
0038:
0039: private java.lang.String[] accessSpecifiers = null;
0040:
0041: private ArrayList parameters = new ArrayList();
0042:
0043: private java.lang.String returnType = "";
0044:
0045: private java.lang.String methodName = "";
0046:
0047: private ArrayList methodType = new ArrayList();
0048:
0049: private java.lang.String belongsToClass = "";
0050:
0051: private boolean returnTypeIsArray = false;
0052:
0053: private int returnTypeArrayDimension = -1;
0054:
0055: private ArrayList exceptions = new ArrayList();
0056:
0057: private byte[] code = null;
0058:
0059: private boolean isConstructor = false;
0060:
0061: private java.lang.String key = "";
0062:
0063: private boolean isParameterArray = false;
0064:
0065: private boolean isExceptionTableSet = false;
0066:
0067: private ArrayList allTryBlocksForMethod = new ArrayList();
0068:
0069: private ArrayList exceptionTablesCreated = new ArrayList();
0070:
0071: private ArrayList allswitchesformethod = new ArrayList();
0072:
0073: private ArrayList behaviourLoops = new ArrayList();
0074:
0075: public ShortcutAnalyser getShortCutAanalyser() {
0076: return shortCutA;
0077: }
0078:
0079: public void setShortCutAnalyser(ShortcutAnalyser shortCutA) {
0080: this .shortCutA = shortCutA;
0081: }
0082:
0083: public ArrayList getInststartpos() {
0084: return inststartpos;
0085: }
0086:
0087: public void setInststartpos(ArrayList inststartpos) {
0088: this .inststartpos = inststartpos;
0089: }
0090:
0091: private ArrayList inststartpos = new ArrayList();
0092:
0093: /*
0094: * public ArrayList getExceptions() { return exceptions; }
0095: */
0096:
0097: public void setExceptions(ArrayList exceptions) {
0098: this .exceptions = exceptions;
0099: }
0100:
0101: public void addExceptions(java.lang.String exceptionStr) {
0102: if (Configuration.getShowImport().equalsIgnoreCase("true")) {
0103: java.lang.String simplename = "";
0104: java.lang.String fullName = exceptionStr;
0105: int lastSlash = fullName.lastIndexOf("/");
0106: if (lastSlash == -1) {
0107: lastSlash = fullName.lastIndexOf(".");
0108: }
0109: if (lastSlash != -1) {
0110: simplename = fullName.substring(lastSlash + 1);
0111: } else
0112: simplename = fullName;
0113: fullName = fullName.replace('/', '.');
0114: ConsoleLauncher.addImportClass(fullName);
0115: exceptions.add(simplename);
0116:
0117: } else
0118: exceptions.add(exceptionStr);
0119: }
0120:
0121: public java.lang.String[] getExceptions() {
0122: java.lang.String[] exception_arr = new java.lang.String[exceptions
0123: .size()];
0124: int index = 0;
0125: Iterator i = exceptions.iterator();
0126: while (i.hasNext()) {
0127: exception_arr[index++] = (java.lang.String) i.next();
0128: }
0129: // System.out.println();
0130: return exception_arr;
0131: }
0132:
0133: public int getReturnTypeArrayDimension() {
0134: return returnTypeArrayDimension;
0135: }
0136:
0137: public void setReturnTypeArrayDimension(int returnTypeArrayDimension) {
0138: this .returnTypeArrayDimension = returnTypeArrayDimension;
0139: }
0140:
0141: public boolean isReturnTypeIsArray() {
0142: return returnTypeIsArray;
0143: }
0144:
0145: public void setReturnTypeIsArray(boolean returnTypeIsArray) {
0146: this .returnTypeIsArray = returnTypeIsArray;
0147: }
0148:
0149: public void setAccessSpecifiers(java.lang.String[] accessSpecifiers) {
0150: this .accessSpecifiers = accessSpecifiers;
0151: }
0152:
0153: public java.lang.String getBelongsToClass() {
0154: return belongsToClass;
0155: }
0156:
0157: public void setBelongsToClass(java.lang.String belongsToClass) {
0158: this .belongsToClass = belongsToClass;
0159: }
0160:
0161: public java.lang.String[] getAccessSpecifiers() {
0162: return accessSpecifiers;
0163: }
0164:
0165: public void setAccessSpecifiers(int al) {
0166: this .accessSpecifiers = Util.parseAccessSpecifiers(new Integer(
0167: al), Constants.METHOD_ACC);
0168: }
0169:
0170: public java.lang.String getMethodName() {
0171: return methodName;
0172: }
0173:
0174: public void setMethodName(java.lang.String methodName) {
0175: this .methodName = methodName;
0176: }
0177:
0178: public ArrayList getMethodType() {
0179: return methodType;
0180: }
0181:
0182: public void setMethodType(ArrayList methodType) {
0183: this .methodType = methodType;
0184: }
0185:
0186: public void addMethodType(java.lang.String methodType) {
0187: this .methodType.add(methodType);
0188: }
0189:
0190: /*
0191: * public ArrayList getParameters() { return parameters; }
0192: */
0193:
0194: public java.lang.String[] getParameters() {
0195: java.lang.String[] parameters_arr = new java.lang.String[parameters
0196: .size()];
0197: int index = 0;
0198: Iterator i = parameters.iterator();
0199: while (i.hasNext()) {
0200: parameters_arr[index++] = (java.lang.String) i.next();
0201: }
0202: return parameters_arr;
0203: }
0204:
0205: /**
0206: * Used While populating localVariabletable
0207: *
0208: * @param parameter
0209: */
0210:
0211: public java.lang.String getStringifiedParameters() {
0212: java.lang.String temp = "";
0213:
0214: Iterator i = parameters.iterator();
0215: if (i.hasNext() == false) {
0216: temp += "()";
0217: return temp;
0218: } else {
0219: temp += "(";
0220: while (i.hasNext()) {
0221: temp += (java.lang.String) i.next();
0222: if (i.hasNext())
0223: temp += ",";
0224: }
0225: temp += ")";
0226: return temp;
0227: }
0228: }
0229:
0230: public void addParameters(java.lang.String parameter) {
0231: this .parameters.add(parameter);
0232: }
0233:
0234: public void setParameters(ArrayList parameters) {
0235: this .parameters = parameters;
0236: }
0237:
0238: public java.lang.String getReturnType() {
0239:
0240: boolean isArray = this .isReturnTypeIsArray();
0241: java.lang.String temp = "";
0242: if (isArray == true) {
0243: for (int count = 1; count <= this
0244: .getReturnTypeArrayDimension(); count++) {
0245: temp += "[]";
0246: }
0247: return returnType + temp;
0248:
0249: } else {
0250: return returnType;
0251: }
0252: }
0253:
0254: public void setReturnType(java.lang.String returnType) {
0255: this .returnType = returnType;
0256: }
0257:
0258: public void parseDescriptor(java.lang.String descriptor) {
0259: if (methodName.equalsIgnoreCase("<clinit>")) {
0260: methodName = "static";
0261: descriptor = "";
0262: accessSpecifiers = new java.lang.String[] { "" };
0263: return;
0264: }
0265: int charIndex = 0;
0266: java.lang.String parameterString = descriptor.substring(1,
0267: descriptor.lastIndexOf(")"));
0268: java.lang.String returnString = descriptor.substring(descriptor
0269: .lastIndexOf(")") + 1);
0270: if (returnString.trim().charAt(0) == 'L'
0271: || returnString.trim().charAt(0) == '[')
0272: this .setReturnTypeAsObjectOrArrayType(true);
0273: else
0274: this .setReturnTypeAsObjectOrArrayType(false);
0275:
0276: java.lang.String arrString = "";
0277: // while(charIndex < parameterString.length())
0278: while (parameterString.length() > 0) {
0279: if (parameterString.startsWith("L")) {
0280: java.lang.String objectString = parameterString
0281: .substring(0, parameterString.indexOf(";"));
0282: objectString = objectString.replace('/', '.');
0283: parameters.add(objectString);
0284: charIndex = charIndex + (objectString.length() + 2);
0285: parameterString = parameterString
0286: .substring(parameterString.indexOf(";") + 1);
0287: } else {
0288: char parameterChar = parameterString.charAt(0);
0289: if (parameterChar == '[') {
0290: arrString = "";
0291: while (parameterString.startsWith("[")) {
0292: arrString += "[";
0293: parameterString = parameterString.substring(1,
0294: parameterString.length());
0295: isParameterArray = true;
0296: }
0297: java.lang.String objectString = "";
0298: if (parameterString.charAt(0) == 'L') {
0299: objectString = parameterString.substring(0,
0300: parameterString.indexOf(";"));
0301: parameterString = parameterString
0302: .substring(parameterString.indexOf(";") + 1);
0303: objectString = objectString.replace('/', '.');
0304: parameters.add(arrString + objectString);
0305: isParameterArray = false;
0306: }
0307: /*
0308: * while(objectString.startsWith("[")) { arrString += "[";
0309: * objectString =
0310: * objectString.substring(1,objectString.length()); }
0311: * objectString = objectString.substring(1); objectString =
0312: * objectString.replace('/','.');
0313: * parameters.add(objectString); charIndex = charIndex +
0314: * (objectString.length() + 2);
0315: * parameters.add(arrString+objectString);
0316: */
0317:
0318: } else {
0319: if (parameterChar == 'I') {
0320: if (isParameterArray) {
0321: parameters.add(arrString + "int");
0322: isParameterArray = false;
0323: } else {
0324: parameters.add("int");
0325: }
0326: }
0327: if (parameterChar == 'B') {
0328: if (isParameterArray) {
0329: parameters.add(arrString + "byte");
0330: isParameterArray = false;
0331: } else {
0332: parameters.add("byte");
0333: }
0334: }
0335: if (parameterChar == 'C') {
0336: if (isParameterArray) {
0337: parameters.add(arrString + "char");
0338: isParameterArray = false;
0339: } else {
0340: parameters.add("char");
0341: }
0342: }
0343: if (parameterChar == 'D') {
0344: if (isParameterArray) {
0345: parameters.add(arrString + "double");
0346: isParameterArray = false;
0347: } else {
0348: parameters.add("double");
0349: }
0350: }
0351: if (parameterChar == 'F') {
0352: if (isParameterArray) {
0353: parameters.add(arrString + "float");
0354: isParameterArray = false;
0355: } else {
0356: parameters.add("float");
0357: }
0358: }
0359: if (parameterChar == 'J') {
0360: if (isParameterArray) {
0361: parameters.add(arrString + "long");
0362: isParameterArray = false;
0363: } else {
0364: parameters.add("long");
0365: }
0366: }
0367: if (parameterChar == 'S') {
0368: if (isParameterArray) {
0369: parameters.add(arrString + "short");
0370: isParameterArray = false;
0371: } else {
0372: parameters.add("short");
0373: }
0374: }
0375: if (parameterChar == 'Z') {
0376: if (isParameterArray) {
0377: parameters.add(arrString + "boolean");
0378: isParameterArray = false;
0379: } else {
0380: parameters.add("boolean");
0381: }
0382: }
0383: parameterString = parameterString.substring(1);
0384: }
0385:
0386: }
0387: }
0388:
0389: if (returnString.indexOf(";") != -1) {
0390: returnString = returnString.substring(0, returnString
0391: .indexOf(";"));
0392: }
0393:
0394: while (returnString.length() > 0) {
0395: // System.out.println();
0396: if (returnString.startsWith("L")) {
0397: // System.out.println(returnString + " "+returnType.length());
0398: returnType = returnString.substring(1, returnString
0399: .length());
0400: returnType = returnType.replace('/', '.');
0401: returnString = "";
0402: } else {
0403: if (returnString.equals("I")) {
0404: returnType = "int";
0405: returnString = "";
0406: }
0407: if (returnString.equals("B")) {
0408: returnType = "byte";
0409: returnString = "";
0410: }
0411: if (returnString.equals("C")) {
0412: returnType = "char";
0413: returnString = "";
0414: }
0415: if (returnString.equals("D")) {
0416: returnType = "double";
0417: returnString = "";
0418: }
0419: if (returnString.equals("F")) {
0420: returnType = "float";
0421: returnString = "";
0422: }
0423: if (returnString.equals("J")) {
0424: returnType = "long";
0425: returnString = "";
0426: }
0427: if (returnString.equals("S")) {
0428: returnType = "short";
0429: returnString = "";
0430: }
0431: if (returnString.equals("Z")) {
0432: returnType = "boolean";
0433: returnString = "";
0434: }
0435: if (returnString.equals("V")) {
0436: returnType = "void";
0437: returnString = "";
0438: }
0439: if (returnString.startsWith("[")) {
0440: returnTypeIsArray = true;
0441: returnTypeArrayDimension = returnString
0442: .lastIndexOf("[") + 1;
0443: if (returnString.indexOf("L") != -1) {
0444: returnType = returnString
0445: .substring(returnString
0446: .lastIndexOf("[") + 2);
0447: returnType = returnType.replace('/', '.');
0448: returnString = "";
0449: // returnString =returnType;
0450: } else {
0451: returnString = returnString
0452: .substring(returnString
0453: .lastIndexOf("[") + 1);
0454: // returnString = "";
0455:
0456: }
0457: }
0458: }
0459: }
0460:
0461: }
0462:
0463: public void setCode(byte[] code) {
0464: this .code = code;
0465: }
0466:
0467: public byte[] getCode() {
0468: return this .code;
0469: }
0470:
0471: public boolean isConstructor() {
0472: return isConstructor;
0473: }
0474:
0475: public void setConstructor(boolean isConstructor) {
0476: this .isConstructor = isConstructor;
0477: }
0478:
0479: public java.lang.String getKey() {
0480: return key;
0481: }
0482:
0483: public void setKey(java.lang.String key) {
0484: this .key = key;
0485: }
0486:
0487: /***************************************************************************
0488: * Call this method when handling code attribute of methodInfo class
0489: *
0490: * @param p
0491: * true or false. True is only set for Try Catch Handlers
0492: */
0493:
0494: public void setExceptionTablePresent(boolean p) {
0495: this .isExceptionTableSet = p;
0496: }
0497:
0498: /***************************************************************************
0499: * True is returned only when a try catch Block is present NOT when there is
0500: * only synchronized statement
0501: *
0502: */
0503:
0504: public boolean isExceptionTableSet() {
0505: return this .isExceptionTableSet;
0506: }
0507:
0508: public class ExceptionTable {
0509:
0510: private boolean wasSynchClosed = false;
0511:
0512: /***********************************************************************
0513: *
0514: * Fields to represent Data as got from the code and according to the
0515: * spec
0516: *
0517: */
0518:
0519: private int startPc = -1;
0520:
0521: private int endPc = -1;
0522:
0523: private int handlerPc = -1;
0524:
0525: private int catchType = -1;
0526:
0527: /***********************************************************************
0528: * Interpretation for handing Try/Catch/Finally TODO: Need to modify if
0529: * necessary to handle monitorEnter and MonitorExit blocks
0530: */
0531:
0532: private int startOfGaurdRegion = -1;
0533:
0534: private int endOfGuardRegion = -1;
0535:
0536: private int startOfHandlerForGuardRegion = -1;
0537:
0538: private int endOfHandlerForGuardRegion = -1;
0539:
0540: private java.lang.String typeOfGuard = "";
0541:
0542: // exceptionName can ne <any> or String denoting an Exception Class Like
0543: // java/lang/Exception
0544: private java.lang.String exceptionName = "";
0545:
0546: // nextTable Populate for time being
0547: // TODO: May not be necesary to have...
0548: private ExceptionTable nextTable = null;
0549:
0550: // Used to denote catch or finally
0551: // TODO: check for monitor later on
0552:
0553: java.lang.String handlerType = "";
0554:
0555: ExceptionTable(int spc, int epc, int hpc, int ctype) {
0556: startPc = spc;
0557: endPc = epc;
0558: handlerPc = hpc;
0559: catchType = ctype;
0560: }
0561:
0562: ExceptionTable(int spc, int epc, int hpc, java.lang.String eName) {
0563: startPc = spc;
0564: endPc = epc;
0565: handlerPc = hpc;
0566: exceptionName = eName;
0567: }
0568:
0569: public int getStartPC() {
0570: return startPc;
0571: }
0572:
0573: public int getEndPC() {
0574: return endPc;
0575: }
0576:
0577: public int getStartOfHandler() {
0578: return handlerPc;
0579: }
0580:
0581: public int getCatchType() {
0582: return catchType;
0583: }
0584:
0585: public java.lang.String getExceptionName() {
0586: return exceptionName;
0587: }
0588:
0589: public void setExceptionName(java.lang.String eName) {
0590: exceptionName = eName;
0591: }
0592:
0593: public ExceptionTable getNextTable() {
0594: return nextTable;
0595: }
0596:
0597: public void setNextTable(ExceptionTable nextTable) {
0598: this .nextTable = nextTable;
0599: }
0600:
0601: public java.lang.String getHandlerType() {
0602: return handlerType;
0603: }
0604:
0605: public void setHandlerType(java.lang.String handlerType) {
0606: this .handlerType = handlerType;
0607: }
0608:
0609: /***********************************************************************
0610: * Important Methods
0611: *
0612: */
0613:
0614: public int getStartOfGuardRegion() {
0615: if (startOfGaurdRegion == -1) {
0616: startOfGaurdRegion = getStartPC();
0617: }
0618: return startOfGaurdRegion;
0619: }
0620:
0621: public int getEndOfGuardRegion() {
0622: if (endOfGuardRegion == -1) {
0623: endOfGuardRegion = getEndPC();
0624: }
0625: return endOfGuardRegion;
0626: }
0627:
0628: public void setEndOfGuardRegion(int end) {
0629: endOfGuardRegion = end;
0630: }
0631:
0632: public void setStartOfHandlerForGuardRegion(
0633: int startOfHandlerForGuardRegion) {
0634: this .startOfHandlerForGuardRegion = startOfHandlerForGuardRegion;
0635: }
0636:
0637: public int getStartOfHandlerForGuardRegion() {
0638: if (startOfHandlerForGuardRegion == -1) {
0639: startOfHandlerForGuardRegion = getStartOfHandler();
0640: }
0641: return startOfHandlerForGuardRegion;
0642: }
0643:
0644: /***********************************************************************
0645: *
0646: * This method returns the type of Guard Region Guard Region can be try
0647: * block or catch Block Necessary in order to print either "try{ or
0648: * print "catch(<Exception Type>){
0649: *
0650: * @return Either "try" or "catch"
0651: *
0652: */
0653:
0654: // NOTE: This is only for case where finally is Handler
0655: // Because we do know whether the Guard Region is of Try or Catch
0656: // TODO : Need to check what Happens when we have synchronized block
0657: // IMPORTANT
0658: // NOTE: Assumption : All ExceptionTables have already Been populated
0659: public java.lang.String getTypeOfGuardRegion() {
0660: return typeOfGuard;
0661: }
0662:
0663: public void setTypeOfGuardRegion(java.lang.String type) {
0664: typeOfGuard = type;
0665: }
0666:
0667: public int getEndOfHandlerForGuardRegion() {
0668: return endOfHandlerForGuardRegion;
0669: }
0670:
0671: public void setEndOfHandlerForGuardRegion(
0672: int endOfHandlerForGuardRegion) {
0673: this .endOfHandlerForGuardRegion = endOfHandlerForGuardRegion;
0674: }
0675:
0676: public java.lang.String getTypeOfHandlerForGuardRegion() {
0677: if (exceptionName == null
0678: || exceptionName.trim().length() == 0) {
0679: exceptionName = getExceptionName();
0680: }
0681: if (exceptionName != null
0682: && exceptionName.trim().length() != 0) {
0683: if (exceptionName.equals("<any>")) {
0684: setTypeOfHandlerForGuardRegion("FinallyBlock");
0685: return "FinallyBlock";
0686: } else {
0687: setTypeOfHandlerForGuardRegion("CatchBlock");
0688: return "CatchBlock";
0689: }
0690: } else {
0691: setTypeOfHandlerForGuardRegion("UNKNOWN HANDLER");
0692: return "UNKNOWN HANDLER";
0693: }
0694:
0695: }
0696:
0697: public boolean equals(Object o) {
0698: if (!(o instanceof ExceptionTable))
0699: return false;
0700: else {
0701: ExceptionTable table = (ExceptionTable) o;
0702: int guardStart = table.getStartOfGuardRegion();
0703: int myGuardStart = getStartOfGuardRegion();
0704: int guardEnd = table.getEndOfGuardRegion();
0705: int myGuardEnd = getEndOfGuardRegion();
0706: int handlerStart = table
0707: .getStartOfHandlerForGuardRegion();
0708: int myHandlerStart = getStartOfHandlerForGuardRegion();
0709: java.lang.String ExceptionName = table
0710: .getExceptionName();
0711: java.lang.String myExceptionName = getExceptionName();
0712:
0713: // Check
0714: if (guardStart == myGuardStart
0715: && guardEnd == myGuardEnd
0716: && handlerStart == myHandlerStart
0717: && ExceptionName.equals(myExceptionName)) {
0718: return true;
0719: }
0720:
0721: else {
0722: return false;
0723: }
0724: }
0725:
0726: }
0727:
0728: public java.lang.String getHandlerBlockName() {
0729: return handlerBlockName;
0730: }
0731:
0732: private java.lang.String handlerBlockName = "";
0733:
0734: public void setTypeOfHandlerForGuardRegion(java.lang.String type) {
0735: handlerBlockName = type;
0736: }
0737:
0738: // belurs:
0739:
0740: /**
0741: * The following two attributes of ExceptionTable are only meant for
0742: * Synchronized Blocks.
0743: */
0744: private int monitorEnterPosInCode = -1;
0745:
0746: private int synchEnd = -1;
0747:
0748: public int getMonitorEnterPosInCode() {
0749: return monitorEnterPosInCode;
0750: }
0751:
0752: public void setMonitorEnterPosInCode(int monnitorEnterPosInCode) {
0753: this .monitorEnterPosInCode = monnitorEnterPosInCode;
0754: }
0755:
0756: public int getSynchEnd() {
0757: if (synchEnd == -1)
0758: return endPc;
0759: return synchEnd;
0760: }
0761:
0762: public void setSynchEnd(int synchEnd) {
0763: byte[] code = getCode();
0764: if (code != null && getInststartpos() != null
0765: && getInststartpos().size() > 0 && code.length > 0) {
0766: List list = getInststartpos();
0767:
0768: if (list.contains(new Integer(synchEnd + 2))
0769: && list.contains(new Integer(synchEnd + 1))) {
0770: int i = synchEnd + 2;
0771: if (code[i - 1] == JvmOpCodes.ARETURN) {
0772: synchEnd = synchEnd + 2;
0773: }
0774: if (code[i - 1] == JvmOpCodes.IRETURN) {
0775: synchEnd = synchEnd + 2;
0776: }
0777: if (code[i - 1] == JvmOpCodes.LRETURN) {
0778: synchEnd = synchEnd + 2;
0779: }
0780: if (code[i - 1] == JvmOpCodes.FRETURN) {
0781: synchEnd = synchEnd + 2;
0782: }
0783: if (code[i - 1] == JvmOpCodes.DRETURN) {
0784: synchEnd = synchEnd + 2;
0785: }
0786:
0787: }
0788: }
0789: this .synchEnd = synchEnd;
0790: }
0791:
0792: public boolean isWasSynchClosed() {
0793: return wasSynchClosed;
0794: }
0795:
0796: public void setWasSynchClosed(boolean wasSynchClosed) {
0797: this .wasSynchClosed = wasSynchClosed;
0798: }
0799:
0800: }
0801:
0802: public boolean containsStatic() {
0803: boolean flag = false;
0804:
0805: for (int indx = 0; indx < accessSpecifiers.length; indx++) {
0806: if (accessSpecifiers[indx].equals("static")) {
0807: flag = true;
0808: }
0809: }
0810:
0811: return flag;
0812: }
0813:
0814: public void storeAllExcepptionTableRefs(ArrayList al) {
0815: this .AllExceptionTables = al;
0816: }
0817:
0818: public ArrayList getAllExceptionTables() {
0819: return AllExceptionTables;
0820: }
0821:
0822: private ArrayList AllExceptionTables = null;
0823:
0824: public int getParametersArrayLength() {
0825: return parameters.size();
0826: }
0827:
0828: /***************************************************************************
0829: * Some util methods Related to ExceptionTable Written to avoid cluttering
0830: * the code in elsewhere in many places with the same piece of code
0831: *
0832: */
0833:
0834: // EXCEPTIONTABLE UTILITY METHOD
0835: // List may itself contain many arraylists
0836: public ArrayList getExceptionTablesSortedByStartHandlerPC() {
0837: ArrayList completeList = new ArrayList();
0838: Iterator it = AllExceptionTables.iterator();
0839: while (it.hasNext()) {
0840: ArrayList list = new ArrayList();
0841: ArrayList prevList = null;
0842: ExceptionTable table = (ExceptionTable) it.next();
0843: int hpc = table.getStartOfHandlerForGuardRegion();
0844:
0845: // Now we Iterate again to capture the set of tables
0846: // having the handler PC value=to hpc
0847:
0848: for (int s = 0; s < AllExceptionTables.size(); s++) {
0849:
0850: ExceptionTable tab = (ExceptionTable) AllExceptionTables
0851: .get(s);
0852: if (tab.getStartOfHandlerForGuardRegion() == hpc)
0853: list.add(tab);
0854:
0855: }
0856:
0857: Iterator compIt = completeList.iterator();
0858: if (compIt.hasNext()) {
0859: boolean p = false;
0860: while (compIt.hasNext()) {
0861: ArrayList list1 = (ArrayList) compIt.next();
0862: boolean identical = compare(list1, list);
0863: if (identical == false) {
0864: p = false;
0865: continue;
0866: } else if (identical == true) {
0867: p = true;
0868: break;
0869: }
0870: }
0871: if (p == false)
0872: completeList.add(list);
0873:
0874: } else
0875: completeList.add(list);
0876: }
0877:
0878: return completeList;
0879: }
0880:
0881: // EXCEPTIONTABLE UTILITY METHOD
0882: public ExceptionTable getExceptionTableForSmallestStartOfGuard(
0883: ArrayList list) {
0884: ExceptionTable table = null;
0885: if (list.size() > 0) {
0886: Iterator it = list.iterator();
0887: int smallestStartOfGuard = ((ExceptionTable) list.get(0))
0888: .getStartOfGuardRegion();
0889: table = (ExceptionTable) list.get(0);
0890: while (it.hasNext()) {
0891: ExceptionTable tab = (ExceptionTable) it.next();
0892: int temp = tab.getStartOfGuardRegion();
0893: if (temp < smallestStartOfGuard) {
0894: smallestStartOfGuard = temp;
0895: table = tab;
0896: }
0897: }
0898: }
0899: return table;
0900:
0901: }
0902:
0903: // EXCEPTIONTABLE UTILITY METHOD
0904: public ArrayList getSortedExceptionTablesForEndOfGuard(
0905: ArrayList input) {
0906: ArrayList output = new ArrayList();
0907: HashMap map = new HashMap();
0908: int counter = 0;
0909: if (input.size() > 0) {
0910: int ends[] = new int[input.size()];
0911: Iterator it = input.iterator();
0912: while (it.hasNext()) {
0913: ExceptionTable tab = (ExceptionTable) it.next();
0914: int temp = tab.getEndOfGuardRegion();
0915: map.put(tab, new Integer(temp));
0916: ends[counter] = temp;
0917: counter++;
0918: }
0919: Arrays.sort(ends);
0920: for (int s = 0; s < ends.length; s++) {
0921: int current = ends[s];
0922: Iterator mapIt = map.entrySet().iterator();
0923: while (mapIt.hasNext()) {
0924: Map.Entry entry = (Map.Entry) mapIt.next();
0925: ExceptionTable key = (ExceptionTable) entry
0926: .getKey();
0927: Integer val = (Integer) entry.getValue();
0928: if (val.intValue() == current) {
0929: output.add(key);
0930: break;
0931: }
0932: }
0933:
0934: }
0935:
0936: }
0937:
0938: return output;
0939: }
0940:
0941: // EXCEPTIONTABLE UTILITY METHOD
0942: public ArrayList getSortedExceptionTablesForStartOfGuard(
0943: ArrayList input) {
0944: ArrayList output = new ArrayList();
0945: HashMap map = new HashMap();
0946: int counter = 0;
0947: if (input.size() > 0) {
0948: int starts[] = new int[input.size()];
0949: Iterator it = input.iterator();
0950: while (it.hasNext()) {
0951: ExceptionTable tab = (ExceptionTable) it.next();
0952: int temp = tab.getStartOfGuardRegion();
0953: map.put(tab, new Integer(temp));
0954: starts[counter] = temp;
0955: counter++;
0956: }
0957: Arrays.sort(starts);
0958: for (int s = 0; s < starts.length; s++) {
0959: int current = starts[s];
0960: Iterator mapIt = map.entrySet().iterator();
0961: while (mapIt.hasNext()) {
0962: Map.Entry entry = (Map.Entry) mapIt.next();
0963: ExceptionTable key = (ExceptionTable) entry
0964: .getKey();
0965: Integer val = (Integer) entry.getValue();
0966: if (val.intValue() == current) {
0967: output.add(key);
0968: break;
0969: }
0970: }
0971:
0972: }
0973:
0974: }
0975:
0976: return output;
0977: }
0978:
0979: // EXCEPTIONTABLE UTILITY METHOD
0980: private boolean compare(ArrayList list1, ArrayList list2) {
0981: boolean identical = false;
0982: if (list1.size() == list2.size()) {
0983: Iterator it1 = list1.iterator();
0984: Iterator it2 = list2.iterator();
0985: while (it1.hasNext() && it2.hasNext()) {
0986: ExceptionTable table1 = (ExceptionTable) it1.next();
0987: ExceptionTable table2 = (ExceptionTable) it2.next();
0988: if (table1.equals(table2)) {
0989: identical = true;
0990: continue;
0991: } else {
0992: identical = false;
0993: break;
0994: }
0995: }
0996:
0997: }
0998: return identical;
0999:
1000: }
1001:
1002: public ArrayList getAllTablesWhoseGuardsAreTries() {
1003: ArrayList list = new ArrayList();
1004: Iterator it = AllExceptionTables.iterator();
1005: while (it.hasNext()) {
1006: ExceptionTable table = (ExceptionTable) it.next();
1007: int ii = 1;
1008: if (table.getTypeOfGuardRegion().equals("try")) {
1009: boolean invalid = checkForInvalidTryBlock(table,
1010: AllExceptionTables); // Check 1
1011: if (!invalid) // Check 2
1012: {
1013: int this EndPc = table.getEndPC();
1014: for (int x = 0; x < list.size(); x++) {
1015: ExceptionTable tab = (ExceptionTable) list
1016: .get(x);
1017: int endg = tab.getEndOfGuardRegion();
1018: if (endg == this EndPc) {
1019: int gst = tab.getStartOfGuardRegion();
1020: int this Gst = table.getStartOfGuardRegion();
1021: if (this Gst > gst) {
1022: invalid = true;
1023: break;
1024: }
1025:
1026: }
1027:
1028: }
1029: }
1030: if (!invalid)
1031: list.add(table);
1032: else
1033: table.setTypeOfGuardRegion("");
1034: }
1035: }
1036: return (list.size() > 0) ? list : null;
1037: }
1038:
1039: public ArrayList getTablesWithGuardRange(int start, int end) {
1040: ArrayList tabList = new ArrayList();
1041: Iterator it = AllExceptionTables.iterator();
1042: while (it.hasNext()) {
1043: ExceptionTable t = (ExceptionTable) it.next();
1044: if (t.getStartOfGuardRegion() == start
1045: && t.getEndOfGuardRegion() == end) {
1046: tabList.add(t);
1047: }
1048: }
1049: if (tabList.size() > 0)
1050: return tabList;
1051: else
1052: return null;
1053: }
1054:
1055: public ArrayList getCreatedTablesWithGuardRange(int start, int end) {
1056: ArrayList tabList = new ArrayList();
1057: Iterator it = getCreatedTableList().iterator();
1058: while (it.hasNext()) {
1059: ExceptionTable t = (ExceptionTable) it.next();
1060: if (t.getStartOfGuardRegion() == start
1061: && t.getEndOfGuardRegion() == end) {
1062: tabList.add(t);
1063: }
1064: }
1065: if (tabList.size() > 0)
1066: return tabList;
1067: else
1068: return null;
1069: }
1070:
1071: public int getNumberOfCatchesForTry(ArrayList al) {
1072: int number = 0;
1073: Iterator it = al.iterator();
1074: while (it.hasNext()) {
1075: ExceptionTable table = (ExceptionTable) it.next();
1076: if (table.getExceptionName().equals("<any>") == false) {
1077: number++;
1078: }
1079: }
1080: return number;
1081:
1082: }
1083:
1084: public ArrayList sortTableListByStartOfHanlder(ArrayList al) {
1085: ArrayList sorted = new ArrayList();
1086: Iterator it = al.iterator();
1087: ExceptionTable first = null;
1088: Hashtable ht = new Hashtable();
1089: int allstarts[] = new int[al.size()];
1090: int counter = 0;
1091: if (al.size() > 0) {
1092: while (it.hasNext()) {
1093: ExceptionTable table = (ExceptionTable) it.next();
1094: int start = table.getStartOfHandlerForGuardRegion();
1095: ht.put(table, new Integer(start));
1096: allstarts[counter] = start;
1097: counter++;
1098:
1099: }
1100: Arrays.sort(allstarts);
1101:
1102: for (int k = 0; k < allstarts.length; k++) {
1103: int s = allstarts[k];
1104: Iterator htIt = ht.entrySet().iterator();
1105: while (htIt.hasNext()) {
1106: Map.Entry entry = (Map.Entry) htIt.next();
1107: ExceptionTable tb = ((ExceptionTable) entry
1108: .getKey());
1109: Integer val = (Integer) entry.getValue();
1110: if (val.intValue() == s)
1111: sorted.add(tb);
1112: }
1113: }
1114:
1115: }
1116: return (al.size() > 0) ? sorted : null;
1117:
1118: }
1119:
1120: public boolean doesTryHaveFinally(ArrayList al) {
1121: boolean hasFianlly = false;
1122: Iterator it = al.iterator();
1123: while (it.hasNext()) {
1124: ExceptionTable table = (ExceptionTable) it.next();
1125: if (table.getExceptionName().equals("<any>") == true) {
1126: hasFianlly = true;
1127: break;
1128: }
1129: }
1130: return hasFianlly;
1131:
1132: }
1133:
1134: public ExceptionTable getCatchBlk(int i) {
1135: ExceptionTable t = null;
1136: for (int s = 0; s < AllExceptionTables.size(); s++) {
1137: ExceptionTable table = (ExceptionTable) AllExceptionTables
1138: .get(s);
1139: if (table.getStartOfGuardRegion() == i
1140: && table.getExceptionName().equals("<any>") == true) {
1141:
1142: t = table;
1143: break;
1144:
1145: }
1146:
1147: }
1148: return t;
1149:
1150: }
1151:
1152: public ArrayList getAllTryBlocksForMethod() {
1153: return allTryBlocksForMethod;
1154: }
1155:
1156: public void setAllTryBlocksForMethod(ArrayList allTryBlocksForMethod) {
1157: this .allTryBlocksForMethod = allTryBlocksForMethod;
1158: }
1159:
1160: public ExceptionTable getExceptionTableWithBiggestStartOfGuard(
1161: ArrayList subList) {
1162:
1163: ExceptionTable table = null;
1164: if (subList != null && subList.size() > 0) {
1165: ExceptionTable t = (ExceptionTable) subList.get(0);
1166: int start = t.getStartOfGuardRegion();
1167: for (int c = 1; c < subList.size(); c++) {
1168: ExceptionTable t1 = (ExceptionTable) subList.get(c);
1169: if (t1.getStartOfGuardRegion() > start) {
1170: start = t1.getStartOfGuardRegion();
1171: table = t1;
1172: }
1173: }
1174: }
1175: return table;
1176: }
1177:
1178: public ArrayList getTableListWithGuardsSpecified(int startOfGuard,
1179: int endOfGuard) {
1180:
1181: ArrayList list = new ArrayList();
1182: Iterator all = this .AllExceptionTables.iterator();
1183: while (all.hasNext()) {
1184: ExceptionTable t = (ExceptionTable) all.next();
1185: if (t.getStartOfGuardRegion() == startOfGuard
1186: && t.getEndOfGuardRegion() == endOfGuard) {
1187: list.add(t);
1188: }
1189: }
1190:
1191: return (list.size() > 0) ? list : null;
1192:
1193: }
1194:
1195: public void addNewExceptionTable(ExceptionTable t) {
1196: exceptionTablesCreated.add(t);
1197: }
1198:
1199: public ArrayList getCreatedTableList() {
1200: return (exceptionTablesCreated.size() > 0) ? exceptionTablesCreated
1201: : null;
1202:
1203: }
1204:
1205: public ArrayList getAllCreatedTablesWhoseGuardsAreTries() {
1206: ArrayList temp = new ArrayList();
1207: ArrayList created = this .getCreatedTableList();
1208: Iterator it = created.iterator();
1209: while (it.hasNext()) {
1210: ExceptionTable table = (ExceptionTable) it.next();
1211: if (table.getTypeOfGuardRegion().equals("try"))
1212: temp.add(table);
1213: }
1214:
1215: return temp;
1216: }
1217:
1218: public ArrayList getTablesWithFinally() {
1219: ArrayList temp = new ArrayList();
1220: Iterator t = AllExceptionTables.iterator();
1221: while (t.hasNext()) {
1222: ExceptionTable t1 = (ExceptionTable) t.next();
1223: if (t1.getExceptionName().equals("<any>"))
1224: temp.add(t1);
1225: }
1226: return (temp.size() > 0) ? temp : null;
1227: }
1228:
1229: public ArrayList getTryTablesWithPCRange(int start, int end) {
1230: ArrayList temp = new ArrayList();
1231: Iterator t = AllExceptionTables.iterator();
1232: while (t.hasNext()) {
1233: ExceptionTable t1 = (ExceptionTable) t.next();
1234: if (t1.getExceptionName().equals("<any>") == false
1235: && t1.getStartPC() == start && t1.getEndPC() == end)
1236: temp.add(t1);
1237: }
1238: return (temp.size() > 0) ? temp : null;
1239:
1240: }
1241:
1242: public void addSwitchBlock(Switch switchblk) {
1243: if (switchblk != null)
1244: allswitchesformethod.add(switchblk);
1245: }
1246:
1247: public ArrayList getAllSwitchBlksForMethod() {
1248: if (allswitchesformethod.size() > 0)
1249: return allswitchesformethod;
1250: else
1251: return null;
1252: }
1253:
1254: private void setReturnTypeAsObjectOrArrayType(boolean b) {
1255: returnTypeAsObjectType = b;
1256: }
1257:
1258: public boolean isReturnTypeAsObjectType() {
1259: return returnTypeAsObjectType;
1260: }
1261:
1262: private boolean returnTypeAsObjectType;
1263:
1264: public void setTablesForSynchronizedBlks(
1265: ArrayList synchronizedTables) {
1266: this .synchronizedTables = synchronizedTables;
1267: }
1268:
1269: public ArrayList getSynchronizedTables() {
1270: return synchronizedTables;
1271: }
1272:
1273: private ArrayList synchronizedTables;
1274:
1275: public ArrayList getBehaviourLoops() {
1276: return behaviourLoops;
1277: }
1278:
1279: public void setLoops(ArrayList loop) {
1280: if (loop != null)
1281: this .behaviourLoops = loop;
1282: }
1283:
1284: public ArrayList getTablesWithGuardStart(int start) {
1285: ArrayList temp = new ArrayList();
1286: ArrayList alltables = this .getAllExceptionTables();
1287: for (int s = 0; s < alltables.size(); s++) {
1288: ExceptionTable table = (ExceptionTable) alltables.get(s);
1289: if (table.getStartOfGuardRegion() == start) {
1290: temp.add(table);
1291: }
1292: }
1293:
1294: return temp;
1295: }
1296:
1297: public LocalVariableStructure getStructure() {
1298: return structure;
1299: }
1300:
1301: public void setStructure(LocalVariableStructure structure) {
1302: this .structure = structure;
1303: }
1304:
1305: private boolean checkForInvalidTryBlock(ExceptionTable table,
1306: ArrayList list) {
1307:
1308: for (int z = 0; z < list.size(); z++) {
1309: ExceptionTable t = (ExceptionTable) list.get(z);
1310: if (t != table) {
1311: java.lang.String type = t.getHandlerBlockName();
1312: if (type != null && type.indexOf("Catch") != -1) {
1313: int cstart = t.getStartOfHandler();
1314: int tend = t.getEndPC();
1315: int this Start = table.getStartPC();
1316: // java.lang.String thisType=table.getHandlerBlockName();
1317: if (this Start > tend && this Start < cstart
1318: && cstart == table.getStartOfHandler()) {
1319: return true;
1320:
1321: }
1322:
1323: }
1324: }
1325: }
1326: return false;
1327:
1328: }
1329:
1330: }
|