001: package net.sf.jdec.jvminstructions.commands;
002:
003: import net.sf.jdec.config.Configuration;
004: import net.sf.jdec.constantpool.ClassDescription;
005: import net.sf.jdec.constantpool.ClassInfo;
006: import net.sf.jdec.core.DecompilerHelper;
007: import net.sf.jdec.core.GlobalVariableStore;
008: import net.sf.jdec.core.LocalVariableHelper;
009: import net.sf.jdec.core.Operand;
010: import net.sf.jdec.core.OperandStack;
011: import net.sf.jdec.main.ConsoleLauncher;
012: import net.sf.jdec.reflection.Behaviour;
013: import net.sf.jdec.util.Constants;
014: import net.sf.jdec.util.Util;
015:
016: import java.util.Stack;
017:
018: public class NewCommand extends AbstractInstructionCommand {
019:
020: public NewCommand(Behaviour context) {
021: super (context);
022:
023: }
024:
025: public int getSkipBytes() {
026: return 2;
027: }
028:
029: public void execute() {
030:
031: boolean nextisnew = true;
032: Stack newfoundstack = GlobalVariableStore.getNewfoundstack();
033: boolean newfound = GlobalVariableStore.isNewfound();
034: byte[] info = getCode();
035: int currentForIndex = getCurrentInstPosInCode();
036: nextisnew = DecompilerHelper.isNewFollowedByNew(info,
037: currentForIndex); // returns
038: // false
039: // for invoke
040: Behaviour behavior = getContext();
041: boolean prevNew = (newfound()) ? true : false;
042: newfound = true;
043: int i = getCurrentInstPosInCode();
044: int newpos = i;
045: int classIndex = getGenericFinder().getOffset(i);
046: i += 2;
047: ClassDescription cd = getContext().getClassRef().getCd();
048: ClassInfo cinfo = cd.getClassInfoAtCPoolPosition(classIndex);
049: Operand op = new Operand();
050: java.lang.String type = cd
051: .getUTF8String(cinfo.getUtf8pointer())
052: .replace('/', '.');
053: java.lang.String qType = type;
054: DecompilerHelper
055: .registerInnerClassIfAny(type.replace('.', '/'));
056: op.setOperandType(Constants.IS_OBJECT_REF);
057: java.lang.String Reference = "JdecGenerated" + i;
058: java.lang.String newTemp = "";
059: OperandStack opStack = getStack();
060: boolean sz = DecompilerHelper.checkForSizeOfArrayTimesStack();
061: StringBuffer fulltype = new StringBuffer();
062: if (Configuration.getShowImport().equals("false")
063: && (!nextisnew && !sz && !prevNew))
064: newTemp = type + " " + Reference + " = new " + type.trim()
065: + "(";
066: if (Configuration.getShowImport().equals("true")
067: && (!nextisnew && !sz && !prevNew)) {
068: java.lang.String fullName = type;
069: java.lang.String simpleName = "";
070: int lastdot = fullName.lastIndexOf(".");
071: boolean addim = false;
072: if (lastdot != -1) {
073: simpleName = fullName.substring(lastdot + 1);
074: type = simpleName;
075: addim = true;
076: boolean present = LocalVariableHelper
077: .simpleTypeAlreadyAddedToImports(simpleName,
078: fulltype);
079: if (present) {
080: if (fulltype.toString().trim().equals(
081: fullName.trim())) {
082: addim = true;
083: } else {
084: addim = false;
085: type = fullName;
086: }
087: }
088: }
089: if (addim) {
090: ConsoleLauncher.addImportClass(fullName);
091: }
092: newTemp = type + " " + Reference + " = new " + type.trim()
093: + "(";
094: }
095: if (Configuration.getShowImport().equals("false")
096: && (nextisnew || sz || prevNew))
097: newTemp = "new " + type.trim() + "(";
098: if (Configuration.getShowImport().equals("true")
099: && (nextisnew || sz || prevNew)) {
100: java.lang.String fullName = type;
101: java.lang.String simpleName = "";
102: int lastdot = fullName.lastIndexOf(".");
103: if (lastdot != -1) {
104: boolean addim = false;
105: simpleName = fullName.substring(lastdot + 1);
106: type = simpleName;
107: addim = true;
108: fulltype = new StringBuffer();
109: boolean present = LocalVariableHelper
110: .simpleTypeAlreadyAddedToImports(simpleName,
111: fulltype);
112: if (present) {
113: if (fulltype.toString().trim().equals(
114: fullName.trim())) {
115: addim = true;
116: } else {
117: addim = false;
118: type = fullName;
119: }
120: }
121: if (addim) {
122: ConsoleLauncher.addImportClass(fullName);
123: }
124: }
125: newTemp = " new " + type.trim() + "(";
126: }
127: newTemp = newTemp.trim();
128: if (!DecompilerHelper.checkForSizeOfArrayTimesStack()) {
129: boolean newinternarycond = false;// checkIFNewIsPartOFTernaryCond(currentForIndex);
130: if (!prevNew)// && !checkForSizeOfArrayTimesStack()) // Removed
131: // && !nextisnew from condition
132: {
133: if (newTemp.indexOf("=") == -1)
134: newTemp = type + " " + Reference + " = " + newTemp;
135: behavior.appendToBuffer("\n"
136: + Util.formatDecompiledStatement(newTemp)); // uncommented
137: op.setOperandValue(Reference);
138: } else if (newinternarycond && prevNew) {
139: op.setOperandValue(newTemp);
140: } else {
141: op.setOperandValue(newTemp);
142: }
143: op.setClassType(qType);
144: opStack.push(op);
145: } else {
146: int brk = newTemp.indexOf("(");
147: // doNotPop=true;
148:
149: if (brk != -1) {
150: newTemp = newTemp.substring(0, brk);
151: }
152: Util.forceStartSpace = false;
153: /*
154: * if(newfound()==false)
155: * codeStatements+=Util.formatDecompiledStatement(newTemp); else{
156: */
157: op = createOperand(newTemp + "(");
158: opStack.push(op);
159: // }
160:
161: /*
162: * if(arraytimesstack.size() > 0) {
163: * arraytimespush=Integer.parseInt(arraytimesstack.pop().toString());
164: * arraytimespush--; if(arraytimespush==0) { } else {
165: * arraytimesstack.push(""+arraytimespush); } }
166: */
167:
168: }
169: /*
170: * r=checkIFLoadInstIsPartOFTernaryCond(currentForIndex); if(r) {
171: * if(opStack.size() > 0) { java.lang.String
172: * str1=opStack.getTopOfStack().getOperandValue(); java.lang.String
173: * str2=opStack.getTopOfStack().getOperandValue(); java.lang.String
174: * str3=str2+str1; Operand d2=createOperand(str3); opStack.push(d2); } }
175: */
176: newfoundstack.push("new");
177: GlobalVariableStore.setNewfound(newfound);
178:
179: }
180:
181: }
|