001: package net.sf.jdec.jvminstructions.commands;
002:
003: import net.sf.jdec.blockhelpers.IFHelper;
004: import net.sf.jdec.constantpool.ClassDescription;
005: import net.sf.jdec.constantpool.ClassInfo;
006: import net.sf.jdec.constantpool.NameAndType;
007: import net.sf.jdec.reflection.Behaviour;
008: import net.sf.jdec.util.Constants;
009: import net.sf.jdec.config.Configuration;
010: import net.sf.jdec.core.JvmOpCodes;
011: import net.sf.jdec.core.Operand;
012: import net.sf.jdec.core.OperandStack;
013: import net.sf.jdec.core.ShortcutAnalyser;
014: import net.sf.jdec.main.ConsoleLauncher;
015:
016: public class InstanceOfCommand extends AbstractInstructionCommand {
017:
018: public InstanceOfCommand(Behaviour context) {
019: super (context);
020:
021: }
022:
023: public int getSkipBytes() {
024: return 2;
025: }
026:
027: public void execute() {
028: OperandStack opStack = getStack();
029: Operand op = (Operand) opStack.pop();
030: Operand op1 = new Operand();
031: op1.setCategory(Constants.CATEGORY1);
032: op1.setOperandType(Constants.IS_CONSTANT_STRING);
033: int i = getCurrentInstPosInCode();
034: int currentForIndex = getCurrentInstPosInCode();
035: int classIndex = getGenericFinder().getOffset(i);
036: i += 2;
037: ClassDescription cd = getContext().getClassRef().getCd();
038: ClassInfo constCInfo = cd
039: .getClassInfoAtCPoolPosition(classIndex);
040: NameAndType ninfo = cd
041: .getNameAndTypeAtCPoolPosition(classIndex);
042: String type = cd.getUTF8String(constCInfo.getUtf8pointer())
043: .replace('/', '.');
044: if (Configuration.getShowImport().equals("true")) {
045: java.lang.String fullName = type;
046: java.lang.String simpleName = "";
047: int lastdot = fullName.lastIndexOf(".");
048: if (lastdot != -1) {
049: simpleName = fullName.substring(lastdot + 1);
050: type = simpleName;
051: ConsoleLauncher.addImportClass(fullName);
052: }
053: }
054: op1.setOperandValue(op.getOperandValue() + " instanceof "
055: + type);
056: byte[] info = getCode();
057: ShortcutAnalyser sanalyser = getContext().getShortCutAnalyser();
058: boolean sh = IFHelper.isIFShortcutORComp(info,
059: (currentForIndex + 3));
060: int j = currentForIndex + 3;
061: boolean invert = sanalyser.isLastIfInChain(j);
062: // TestCase: enumeration.java
063:
064: if (!invert) {
065: java.lang.String connector = sanalyser.getConnector(j);
066: if (connector != null
067: && connector.trim().equals(ShortcutAnalyser.AND)) {
068: invert = true;
069: }
070: }
071:
072: if (!sh) {
073:
074: if (getGenericFinder().isThisInstrStart(
075: (currentForIndex + 3))) {
076: if (getBranchFinder().isInstructionIF(
077: (currentForIndex + 3))) {
078:
079: int ifclose = IFHelper.getIfCloseNumberForThisIF(
080: info, (currentForIndex + 3));
081: ifclose = ifclose - 3;
082: if (getGenericFinder().isThisInstrStart(ifclose)) {
083: if (getBranchFinder().isInstructionIF(ifclose)
084: && ifclose != j) {
085: sh = true;
086: }
087: }
088: }
089: }
090: }
091:
092: int firstIf = sanalyser.getFirstIfInChain(j);
093:
094: switch (info[(currentForIndex + 3)]) {
095: case JvmOpCodes.IFEQ:
096: if (!sh)
097: op1.setOperandValue("(" + op1.getOperandValue()
098: + ")!=false");
099: else {
100: if (!invert)
101: op1.setOperandValue("(" + op1.getOperandValue()
102: + ")==false");
103: else {
104: if (firstIf != j)
105: op1.setOperandValue("(" + op1.getOperandValue()
106: + ")==true");
107: else
108: op1.setOperandValue("(" + op1.getOperandValue()
109: + ")==false");
110: }
111:
112: }
113: break;
114: case JvmOpCodes.IFNE:
115: if (!sh)
116: op1.setOperandValue("(" + op1.getOperandValue()
117: + ")!=true");
118: else {
119: if (!invert) {
120: if (firstIf == j)
121: op1.setOperandValue("(" + op1.getOperandValue()
122: + ")==false");
123: else
124: op1.setOperandValue("(" + op1.getOperandValue()
125: + ")==true");
126: } else {
127: if (firstIf != j)
128: op1.setOperandValue("(" + op1.getOperandValue()
129: + ")==false"); //f
130: else
131: op1.setOperandValue("(" + op1.getOperandValue()
132: + ")==false");
133: }
134: }
135: break;
136: }
137:
138: boolean r = false;// checkIFLoadInstIsPartOFTernaryCond(currentForIndex);
139: if (r) {
140: if (opStack.size() > 0) {
141: java.lang.String str = opStack.getTopOfStack()
142: .getOperandValue();
143: str = str + op1.getOperandValue();
144: op1.setOperandValue(str);
145: }
146: }
147: opStack.push(op1);
148:
149: }
150:
151: }
|