001: /*
002: * IFBlock.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.blocks;
021:
022: import net.sf.jdec.main.ConsoleLauncher;
023: import net.sf.jdec.reflection.Behaviour;
024:
025: public class IFBlock implements Comparable {
026: private boolean hasIfBeenGenerated;
027: private boolean hasMatchingElseBeenGenerated;
028: private int ifCloseLineNumber;
029: private int elseCloseLineNumber;
030: private int startOfIf = -1;
031: private boolean elsebreakadded = false;
032: private boolean elsebreakinvalid = false;
033:
034: public boolean getDonotclose() {
035: return donotclose;
036: }
037:
038: public void setDonotclose(boolean donotclose) {
039: this .donotclose = donotclose;
040: }
041:
042: private boolean donotclose = false;
043:
044: public boolean IfHasBeenClosed() {
045: return ifHasBeenClosed;
046: }
047:
048: public void setIfHasBeenClosed(boolean ifHasBeenClosed) {
049: this .ifHasBeenClosed = ifHasBeenClosed;
050: }
051:
052: private boolean ifHasBeenClosed = false;
053: private boolean elseHasBeenClosed = false;
054:
055: public int getIfCloseFromByteCode() {
056: return ifCloseFromByteCode;
057: }
058:
059: public void setIfCloseFromByteCode(int ifCloseFromByteCode) {
060: this .ifCloseFromByteCode = ifCloseFromByteCode;
061: }
062:
063: private int ifCloseFromByteCode;
064:
065: public boolean isHasElse() {
066: return hasElse;
067: }
068:
069: public void setHasElse(boolean hasElse) {
070: this .hasElse = hasElse;
071: }
072:
073: private boolean hasElse = false;
074:
075: public void setIfStart(int start) {
076: startOfIf = start;
077: setIFEndFromByteCode(startOfIf);
078: }
079:
080: public int getIfStart() {
081: return startOfIf;
082: }
083:
084: public IFBlock() {
085: ifCloseLineNumber = -1;
086: elseCloseLineNumber = -1;
087: }
088:
089: public int getElseCloseLineNumber() {
090: return elseCloseLineNumber;
091: }
092:
093: public void setElseCloseLineNumber(int elseCloseLineNumber) {
094: this .elseCloseLineNumber = elseCloseLineNumber;
095: }
096:
097: public boolean hasIfBeenGenerated() {
098: return hasIfBeenGenerated;
099: }
100:
101: public void setHasIfBeenGenerated(boolean hasIfBeenGenerated) {
102: this .hasIfBeenGenerated = hasIfBeenGenerated;
103: }
104:
105: public boolean hasMatchingElseBeenGenerated() {
106: return hasMatchingElseBeenGenerated;
107: }
108:
109: public void setHasMatchingElseBeenGenerated(
110: boolean hasMatchingElseBeenGenerated) {
111: this .hasMatchingElseBeenGenerated = hasMatchingElseBeenGenerated;
112: }
113:
114: public int getIfCloseLineNumber() {
115: return ifCloseLineNumber;
116: }
117:
118: public void setIfCloseLineNumber(int ifCloseLineNumber) {
119: this .ifCloseLineNumber = ifCloseLineNumber;
120: }
121:
122: public int compareTo(Object o) {
123: if (o instanceof IFBlock) {
124: IFBlock IF = (IFBlock) o;
125: int start = IF.getIfStart();
126: if (this .startOfIf > start)
127: return 1;
128: else if (this .startOfIf < start)
129: return -1;
130: else
131: return 0;
132: } else {
133: return -1;
134: }
135: }
136:
137: private void setIFEndFromByteCode(int startOfIf) {
138: Behaviour b = ConsoleLauncher.getCurrentMethodBeingExecuted();
139: if (b != null) {
140: byte[] code = b.getCode();
141: int jump = getJumpAddress(code, startOfIf);
142: ifCloseFromByteCode = jump;
143: }
144: }
145:
146: private int getJumpAddress(byte[] info, int counter) {
147:
148: int b1 = info[++counter];
149: int b2 = info[++counter];
150: int z;
151: if (b1 < 0)
152: b1 = (256 + b1);
153: if (b2 < 0)
154: b2 = (256 + b2);
155:
156: int indexInst = ((((b1 << 8) | b2)) + (counter - 2));
157: if (indexInst > 65535)
158: indexInst = indexInst - 65536;
159: if (indexInst < 0)
160: indexInst = 256 + indexInst;
161: return indexInst;
162: }
163:
164: public boolean elseHasBeenClosed() {
165: return elseHasBeenClosed;
166: }
167:
168: public void setElseHasBeenClosed(boolean elseHasBeenClosed) {
169: this .elseHasBeenClosed = elseHasBeenClosed;
170: }
171:
172: public int hashCode() {
173: final int PRIME = 31;
174: int result = 1;
175: result = PRIME * result + startOfIf;
176: return result;
177: }
178:
179: public boolean equals(Object obj) {
180: if (this == obj)
181: return true;
182: if (obj == null)
183: return false;
184: if (getClass() != obj.getClass())
185: return false;
186: final IFBlock other = (IFBlock) obj;
187: if (startOfIf != other.startOfIf)
188: return false;
189: return true;
190: }
191:
192: public boolean isElsebreakadded() {
193: return elsebreakadded;
194: }
195:
196: public void setElsebreakadded(boolean elsebreakadded) {
197: this .elsebreakadded = elsebreakadded;
198: }
199:
200: public boolean isElsebreakinvalid() {
201: return elsebreakinvalid;
202: }
203:
204: public void setElsebreakinvalid(boolean elsebreakinvalid) {
205: this.elsebreakinvalid = elsebreakinvalid;
206: }
207:
208: }
|