001: /*
002: * LocalVariable.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: package net.sf.jdec.core;
020:
021: import net.sf.jdec.commonutil.Objects;
022: import net.sf.jdec.config.Configuration;
023: import net.sf.jdec.constantpool.ClassDescription;
024: import net.sf.jdec.main.ConsoleLauncher;
025: import net.sf.jdec.settings.SettingsStore;
026:
027: /**
028: *
029: * @author swaroop belur (belurs)
030: *
031: */
032: public class LocalVariable implements Comparable {
033:
034: private int loadStorePosUsedToCreateVraiable = -1;
035:
036: private java.lang.String passedDataTypeWhileCreatingWithOutMinusG = "";
037:
038: private boolean nameAlreadyReset = false;
039:
040: private boolean methodParameterVariable = false;
041:
042: private java.lang.String methodName = "";
043:
044: private java.lang.String dataType = "";
045:
046: private java.lang.String varName = "";
047:
048: private java.lang.String tempVarName = null;
049:
050: private int indexPos = -1;
051:
052: private boolean declarationGenerated = false;
053:
054: private boolean isVisited = false;
055:
056: private boolean isPrimitive = false;
057:
058: private boolean isBoolean = false;
059:
060: private boolean isWithinBlock = false;
061:
062: private int blockIndex = -1;
063:
064: // Used to indicate that variable's info
065: // was not present in code
066: // Code was eithe compiled with -g:none OR for some reason this
067: // Variables's info was not present in compiled code
068: private boolean wasCreated = false;
069:
070: boolean primitive = false;
071:
072: int numberOfBrackets = -1;
073:
074: private boolean iamprimitive = false;
075:
076: private int blockStart = -1;
077:
078: private int blockEnd = -1;
079:
080: public boolean wasCreated() {
081: return wasCreated;
082: }
083:
084: public void setWasCreated(boolean wasCreated) {
085: this .wasCreated = wasCreated;
086: }
087:
088: public LocalVariable() {
089: super ();
090:
091: }
092:
093: public LocalVariable(java.lang.String mname,
094: java.lang.String vname, int index) {
095: this .methodName = mname;
096: this .varName = vname;
097: this .indexPos = index;
098: }
099:
100: public LocalVariable(java.lang.String methodName,
101: java.lang.String dataType, java.lang.String varName,
102: int index) {
103: this .methodName = methodName;
104: this .dataType = parse(dataType);
105: this .varName = varName;
106: indexPos = index;
107: String dt = this .dataType;
108:
109: // For Imports
110: if (!iamprimitive) {
111: String si = Configuration.getShowImport();
112: if (si.equalsIgnoreCase("true")) {
113: String name = dt;
114: if (name.indexOf("/") != -1)
115: name = name.replaceAll("/", ".");
116: ConsoleLauncher.addImportClass(name);
117: String simpleName = "";
118: int in = name.lastIndexOf(".");
119: if (in != -1) {
120: simpleName = name.substring(in + 1);
121: } else
122: simpleName = name;
123: dt = simpleName;
124: } else {
125: dt = this .dataType;
126: }
127: this .dataType = dt;
128: }
129:
130: }
131:
132: public LocalVariable(java.lang.String methodName,
133: java.lang.String dataType, java.lang.String varName,
134: int index, boolean declarationGenerated) {
135: this .methodName = methodName;
136: this .dataType = parse(dataType);
137: this .varName = varName;
138: indexPos = index;
139: this .declarationGenerated = declarationGenerated;
140:
141: if (!iamprimitive) {
142: String dt = this .dataType;
143: // For Imports
144: String si = Configuration.getShowImport();
145: if (si.equalsIgnoreCase("true")) {
146: String name = dt;
147: if (name.indexOf("/") != -1)
148: name = name.replaceAll("/", ".");
149: ConsoleLauncher.addImportClass(name);
150: String simpleName = "";
151: int in = name.lastIndexOf(".");
152: if (in != -1) {
153: simpleName = name.substring(in + 1);
154: } else
155: simpleName = name;
156: dt = simpleName;
157: } else {
158: dt = this .dataType;
159: }
160: this .dataType = dt;
161: }
162:
163: }
164:
165: private java.lang.String parse(java.lang.String input) {
166: java.lang.String type = "";
167: java.lang.String brackets = "";
168: if (input.equals("I")) {
169: // System.out.println(type);
170: type = "int";
171: iamprimitive = true;
172: } else if (input.equals("B")) {
173: type = "byte";
174: iamprimitive = true;
175: } else if (input.equals("C")) {
176: type = "char";
177: iamprimitive = true;
178: } else if (input.equals("S")) {
179: type = "short";
180: iamprimitive = true;
181: } else if (input.equals("Z")) {
182: type = "boolean";
183: iamprimitive = true;
184: } else if (input.equals("F")) {
185: type = "float";
186: iamprimitive = true;
187: } else if (input.equals("D")) {
188: type = "double";
189: iamprimitive = true;
190: } else if (input.equals("J")) {
191: type = "long";
192: iamprimitive = true;
193: } else if (input.startsWith("L")) {
194: type = input.substring(1);
195: if (type.indexOf(";") != -1)
196: type = type.substring(0, type.indexOf(";"));
197: } else if (input.startsWith("[")) {
198: // System.out.println(type);
199: int lastBracket = input.lastIndexOf("[");
200: int objectType = input.indexOf("L");
201: java.lang.String className = "";
202: if (objectType != -1)
203: className = input.substring(objectType + 1);
204: else
205: className = input.substring(lastBracket + 1);
206: if (className.indexOf(";") != -1)
207: className = className.substring(0, className
208: .indexOf(";"));
209: boolean b = AmIPrimitive(className);
210: if (b == true) {
211: primitive = true;
212: numberOfBrackets = lastBracket + 1;
213: type = parse(className);
214: } else {
215: java.lang.String temp = "";
216: for (int c = 0; c < lastBracket + 1; c++)
217: temp += "[]";
218: type = className + " " + temp;
219: }
220:
221: }
222: if (primitive) {
223: for (int s = 0; s < numberOfBrackets; s++)
224: brackets += "[]";
225: primitive = false;
226: }
227: return type + " " + brackets;
228:
229: }
230:
231: public boolean isPrimitive() {
232: return isPrimitive;
233: }
234:
235: public boolean isBoolean() {
236: return isBoolean;
237: }
238:
239: private boolean AmIPrimitive(String className) {
240: AmIBoolean(className);
241: if (className.equals("I") || className.equals("B")
242: || className.equals("C") || className.equals("S")
243: || className.equals("F") || className.equals("D")
244: || className.equals("J") || className.equals("Z")) {
245: isPrimitive = true;
246: return true;
247: } else {
248: isPrimitive = false;
249: return false;
250: }
251:
252: }
253:
254: private boolean AmIBoolean(String className) {
255: if (className.equals("Z")) {
256: isBoolean = true;
257: return true;
258: } else {
259: isBoolean = false;
260: return false;
261: }
262: }
263:
264: public java.lang.String toString() {
265: java.lang.String desc = "[";
266: desc += this .dataType + " " + this .varName + " "
267: + this .blockStart + " " + this .blockEnd + " "
268: + this .blockIndex + " " + this .indexPos
269: + "[--Index Pos--]";
270: desc += "]";
271: return desc;
272:
273: }
274:
275: public java.lang.String getDataType() {
276: return dataType.trim();
277: }
278:
279: public void setDataType(java.lang.String passedDataType) {
280:
281: /***********************************************************************
282: * Enhancement Request for 1.2.1 Changes for showing better local var
283: * names
284: *
285: */
286: // Start
287: boolean check = true;
288: if (passedDataType != null && passedDataType.indexOf(".") != -1) {
289: int lastdot = passedDataType.lastIndexOf(".");
290: if (lastdot != -1
291: && (lastdot + 1) < passedDataType.length()) {
292: String tmp = passedDataType.substring(lastdot + 1);
293: if (this .dataType != null && tmp != null
294: && tmp.trim().equals(this .dataType.trim())) {
295: check = false;
296: }
297: }
298:
299: }
300: if (check && passedDataType != null
301: && passedDataType.equals(this .dataType) == false
302: && nameAlreadyReset) {
303: nameAlreadyReset = false;
304: }
305:
306: String name = null;
307: boolean resetname = true;
308: if (varName != null && varName.trim().indexOf("this") != -1) {
309: resetname = false;
310: }
311: ClassDescription cd = ClassDescription.ref;
312: if (!cd.isClassCompiledWithMinusG() && !nameAlreadyReset
313: && resetname) {
314: StringBuffer classn = new StringBuffer();
315: String pkg = getPkgFromType(passedDataType, classn);
316:
317: String prefix = SettingsStore.getVariablesettings()
318: .getPrefixName(pkg, classn.toString(),
319: passedDataType);
320: if (prefix != null) {
321: int num = SettingsStore.getVariablesettings()
322: .getPrefixOccurence(prefix);
323: setVarName(prefix + num);
324: nameAlreadyReset = true;
325: }
326: }
327: // End change
328:
329: String si = Configuration.getShowImport();
330:
331: String typename = passedDataType;
332: if (typename.indexOf("/") != -1)
333: typename = name.replaceAll("/", ".");
334: String simpleName = "";
335: int index = typename.lastIndexOf(".");
336: if (index != -1) {
337: simpleName = typename.substring(index + 1);
338: } else
339: simpleName = typename;
340: StringBuffer fulltype = new StringBuffer();
341: if (si.equalsIgnoreCase("true")
342: && !LocalVariableHelper
343: .simpleTypeAlreadyAddedToImports(simpleName,
344: fulltype)) {
345: ConsoleLauncher.addImportClass(typename);
346: this .dataType = simpleName;
347: } else {
348: if (fulltype.toString().equals(passedDataType)
349: && si.equalsIgnoreCase("true")) {
350: ConsoleLauncher.addImportClass(typename);
351: this .dataType = simpleName;
352: } else {
353: this .dataType = passedDataType;
354: }
355:
356: }
357:
358: }
359:
360: private String getPkgFromType(String dataType,
361: StringBuffer className) {
362: if (Objects.isEmpty(dataType))
363: return null;
364: int dot = dataType.lastIndexOf(".");
365: if (dot == -1) {
366: dot = dataType.lastIndexOf("/");
367: }
368: if (dot == -1) {
369: return null;
370: }
371: className.append(dataType.substring(dot + 1));
372: return dataType.substring(0, dot);
373: }
374:
375: public int getIndexPos() {
376: return indexPos;
377: }
378:
379: public void setIndexPos(int indexPos) {
380: this .indexPos = indexPos;
381: }
382:
383: public java.lang.String getMethodName() {
384: return methodName;
385: }
386:
387: public void setMethodName(java.lang.String methodName) {
388: this .methodName = methodName;
389: }
390:
391: public java.lang.String getVarName() {
392: return varName;
393: }
394:
395: public void setVarName(java.lang.String varName) {
396: this .varName = varName;
397: }
398:
399: public boolean isDeclarationGenerated() {
400: return declarationGenerated;
401: }
402:
403: public void setDeclarationGenerated(boolean declarationGenerated) {
404: this .declarationGenerated = declarationGenerated;
405: }
406:
407: public boolean isVisited() {
408: return isVisited;
409: }
410:
411: public void setVisited(boolean isVisited) {
412: this .isVisited = isVisited;
413: }
414:
415: public boolean isWithinBlock() {
416: return isWithinBlock;
417: }
418:
419: public void setWithinBlock(boolean isWithinBlock) {
420: this .isWithinBlock = isWithinBlock;
421: }
422:
423: public int getBlockIndex() {
424: return blockIndex;
425: }
426:
427: public void setBlockIndex(int blockIndex) {
428: this .blockIndex = blockIndex;
429: }
430:
431: public int getBlockEnd() {
432: return blockEnd;
433: }
434:
435: public void setBlockEnd(int blockEnd) {
436: this .blockEnd = blockEnd;
437: }
438:
439: public int getBlockStart() {
440: return blockStart;
441: }
442:
443: public void setBlockStart(int blockStart) {
444: this .blockStart = blockStart;
445: }
446:
447: public void setPrimitive(boolean p) {
448: this .primitive = p;
449:
450: }
451:
452: public int getLoadOrStorePosUsedToCreateVraiable() {
453: return loadStorePosUsedToCreateVraiable;
454: }
455:
456: public void setLoadORStorePosUsedToCreateVraiable(
457: int loadPosUsedToCreateVraiable) {
458: this .loadStorePosUsedToCreateVraiable = loadPosUsedToCreateVraiable;
459: }
460:
461: public int compareTo(Object o) {
462:
463: int cmp = -1;
464: if (o instanceof LocalVariable) {
465: LocalVariable local = (LocalVariable) o;
466: int index = local.getIndexPos();
467: int this Index = this .getIndexPos();
468: if (this Index < index)
469: return -1;
470: if (this Index > index)
471: return 1;
472: else
473: return 0;
474: } else
475: return -1;
476:
477: }
478:
479: public String getTempVarName() {
480: return tempVarName;
481: }
482:
483: public void setTempVarName(String tempVarName) {
484: this .tempVarName = tempVarName;
485: }
486:
487: public String getPassedDataTypeWhileCreatingWithOutMinusG() {
488: return passedDataTypeWhileCreatingWithOutMinusG;
489: }
490:
491: public void setPassedDataTypeWhileCreatingWithOutMinusG(
492: String passedDataTypeWhileCreatingWithOutMinusG) {
493: this .passedDataTypeWhileCreatingWithOutMinusG = passedDataTypeWhileCreatingWithOutMinusG;
494: }
495:
496: public boolean isMethodParameterVariable() {
497: return methodParameterVariable;
498: }
499:
500: public void setMethodParameterVariable(boolean isMethodParam) {
501: this.methodParameterVariable = isMethodParam;
502: }
503:
504: }
|