Package net.sf.rej.java.instruction

Examples of net.sf.rej.java.instruction.Instruction


      Code code = codeAttr.getCode();
      DecompilationContext dc = code.createDecompilationContext();
      List instructions = code.getInstructions();
      dc.setPosition(0);
      for (int j = 0; j < instructions.size(); j++) {
        Instruction instruction = (Instruction) instructions.get(j);

        if (instruction instanceof Label) {
          LabelRow lr = new LabelRow((Label) instruction, mdr);
          lr.setParentCode(code);
          list.add(lr);
View Full Code Here


      LabelRow lrB = (LabelRow) erB;
      return lrA.getLabel().getId().equals(lrB.getLabel().getId());
    } else if (erA instanceof CodeRow) {
      CodeRow crA = (CodeRow) erA;
      CodeRow crB = (CodeRow) erB;
      Instruction instA = crA.getInstruction();
      Instruction instB = crB.getInstruction();
      boolean opCodesEqual = (instA.getOpcode() == instB.getOpcode());
      if (!opCodesEqual) return false;
           
      return instA.getParameters().getString(crA.getDecompilationContext()).equals(instB.getParameters().getString(crB.getDecompilationContext()));
    } else {
      throw new AssertionError("Invalid object type: " + erA.getClass());
    }
  }
View Full Code Here

    }
    InstructionCopier instructionCopier = new InstructionCopier();
    for (Instruction inst : this.newCode.getInstructions()) {
      if (inst instanceof Label) continue;
     
      Instruction copy = instructionCopier.copyInstruction(inst, this.newCode.createDecompilationContext().getConstantPool(), this.cf.getPool());
      this.addedInstructions.add(copy);
      int index = list.indexOf(inst);
      list.set(index, copy);
      List<Label> labels = inst.getLabels();
      List<Label> copyLabels = copy.getLabels();
      for (int i=0; i < labels.size(); i++) {
        Label label = labels.get(i);
        int labelIndex = list.indexOf(label);
        if (labelIndex != -1) {
          list.set(labelIndex, copyLabels.get(i));
View Full Code Here

import net.sf.rej.java.instruction.Label;
import net.sf.rej.java.instruction.Parameters;

public class InstructionCopier {
  public Instruction copyInstruction(Instruction inst, ConstantPool sourcePool, ConstantPool destinationPool) {
    Instruction copy = InstructionSet.getInstance().getInstruction(inst.getOpcode());
    Parameters params = inst.getParameters();
    Parameters copyParams = new Parameters();
    for(int i=0; i < params.getCount(); i++) {
          copyParams.addParam(params.getType(i));
            switch (params.getType(i)) {
            case TYPE_ARRAYTYPE:
              copyParams.addValue(params.getObject(i));
              break;
            case TYPE_LOCAL_VARIABLE:
            case TYPE_LOCAL_VARIABLE_READONLY:
            case TYPE_LOCAL_VARIABLE_WIDE:
              // TODO: should be done differently?
              copyParams.addValue(params.getObject(i));
              break;
            case TYPE_CONSTANT_WIDE:
            case TYPE_CONSTANT_READONLY:
            case TYPE_CONSTANT:
              copyParams.addValue(params.getObject(i));
              break;
            case TYPE_CONSTANT_POOL_CLASS: {
              ClassInfo ci = (ClassInfo) sourcePool.get(params.getInt(i));
                int index = destinationPool.optionalAddClassRef(ci.getName());
                copyParams.addValue(index);
                break;
            }
            case TYPE_CONSTANT_POOL_CONSTANT: {
              ConstantPoolInfo cpi = sourcePool.get(params.getInt(i));
              switch (cpi.getType()) {
              case ConstantPoolInfo.DOUBLE: {
                DoubleInfo di = (DoubleInfo)cpi;
                DoubleInfo diCopy = new DoubleInfo(di.getHighBytes(), di.getLowBytes(), destinationPool);
                int index = destinationPool.optionalAdd(diCopy);
                copyParams.addValue(index);
                break;
              }
              case ConstantPoolInfo.FLOAT: {
                FloatInfo fi = (FloatInfo)cpi;
                FloatInfo fiCopy = new FloatInfo(fi.getBytes(), destinationPool);
                int index = destinationPool.optionalAdd(fiCopy);
                copyParams.addValue(index);
                break;                   
              }
              case ConstantPoolInfo.INTEGER: {
                IntegerInfo ii = (IntegerInfo)cpi;
                IntegerInfo iiCopy = new IntegerInfo(ii.getIntValue(), destinationPool);
                int index = destinationPool.optionalAdd(iiCopy);
                copyParams.addValue(index);
                break;                                       
              }
              case ConstantPoolInfo.LONG: {
                LongInfo li = (LongInfo)cpi;
                LongInfo liCopy = new LongInfo(li.getLongValue(), destinationPool);
                int index = destinationPool.optionalAdd(liCopy);
                copyParams.addValue(index);
                break;                     
              }
              case ConstantPoolInfo.STRING: {
                StringInfo si = (StringInfo)cpi;
                int index = destinationPool.optionalAddString(si.getString());
                copyParams.addValue(index);
                break;                                       
               
              }
              }
             
              break;
            }
            case TYPE_CONSTANT_POOL_FIELD_REF: {
              RefInfo ri = (RefInfo) sourcePool.get(params.getInt(i));
              int index = destinationPool.optionalAddFieldRef(ri.getClassName(), ri.getTargetName(), ri.getMethodType());
            copyParams.addValue(index);
              break;
            }
            case TYPE_CONSTANT_POOL_METHOD_REF: {
              RefInfo ri = (RefInfo) sourcePool.get(params.getInt(i));
              int index = destinationPool.optionalAddMethodRef(ri.getClassName(), ri.getTargetName(), ri.getMethodType());
            copyParams.addValue(index);
              break;
            }
            case TYPE_LABEL:
              Label label = (Label) params.getObject(i);
              copyParams.addValue(new Label(label.getPosition()));
              break;
            case TYPE_SWITCH:
              // TODO: Switch
              break;
            }
    }
   
    copy.setParameters(copyParams);
    return copy;
  }
View Full Code Here

        List<Label> labels = new ArrayList<Label>();
        while (parser.hasMore()) {
            int opcode = parser.peekByte();

            try {
                Instruction instruction = set.getInstruction(opcode);
                if (instruction == null) {
                    this.errors.add("Unknown opcode at position " + dc.getPosition() + " trying to recover.");
                    continue;
                }
                int size = instruction.getSize(dc);
                byte[] data = parser.getBytes(size);
                instruction.setData(data, dc);
                List<Label> al = instruction.getLabels();
                if (al != null && al.size() > 0) {
                    labels.addAll(al);
                }

                this.code.add(instruction);
View Full Code Here

        for (Label label : labels ) {
            if (this.code.contains(label)) continue;
           
            DecompilationContext dc = createDecompilationContext();
            for (int j = 0; j < this.code.size(); j++) {
                Instruction instruction = this.code.get(j);
                if (dc.getPosition() == label.getPosition()) {
                    this.code.add(j, label);
                    continue labelloop;
                }
                dc.incrementPosition(instruction);
View Full Code Here

    public void addInstructionAtPC(int pc, Instruction instruction) {
      DecompilationContext dc = createDecompilationContext();
        int pos = -1;
        for (int i = 0; i < this.code.size(); i++) {
            Instruction otherInst = this.code.get(i);
            if (dc.getPosition() == pc) {
                pos = i; // get the last match, so insert will be after labels
            } else if (dc.getPosition() > pc) {
                break;
            }
View Full Code Here

                    break;
            }

            sd.setOffset(0);

            Instruction inst = cr.getInstruction();
            DecompilationContext dc = cr.getDecompilationContext();
            LocalVariableTableAttribute lvs = dc.getLocalVariableTable();
            ConstantPool pool = dc.getConstantPool();
          sd.drawIndent();
          sd.drawIndent();
            sd.drawInstruction(inst.getMnemonic());
            Parameters params = inst.getParameters();
            for (int i = 0; i < params.getCount(); i++) {
                try {
                    switch (params.getType(i)) {
                        case TYPE_LOCAL_VARIABLE:
                        case TYPE_LOCAL_VARIABLE_WIDE:
                        case TYPE_LOCAL_VARIABLE_READONLY:
                            if (lvs == null) {
                                sd.drawDefault(" " + params.getInt(i));
                            } else {
                                LocalVariable lv = lvs.getLocalVariable(params.getInt(i), cr.getPosition());
                                if (lv == null) {
                                    sd.drawDefault(" " + params.getInt(i));
                                } else {
                                    sd.drawDefault(" " + lv.getName());
                                }
                            }
                            break;
                        case TYPE_CONSTANT_POOL_METHOD_REF: {
                          int index = params.getInt(i);
                            ConstantPoolInfo cpi = pool.get(index);
                            renderMethodRef(sd, ia, (RefInfo) cpi, index);
                            break;
                        }
                        case TYPE_CONSTANT_POOL_FIELD_REF: {
                          int index = params.getInt(i);
                            ConstantPoolInfo cpi = pool.get(index);
                            renderFieldRef(sd, ia, (RefInfo) cpi, index);
                            break;
                        }
                        case TYPE_CONSTANT_POOL_CLASS: {
                          int index = params.getInt(i);
                            ConstantPoolInfo cpi = pool.get(index);
                            renderClassRef(sd, ia, (ClassInfo) cpi, index);
                            break;
                        }
                        case TYPE_CONSTANT_POOL_CONSTANT: {
                           int index = params.getInt(i);
                           ConstantPoolInfo cpi = pool.get(index);
                           renderConstant(sd, cpi, index);
                           break;
                        }
                        case TYPE_LABEL: {
                            Label label = (Label) params.getObject(i);
                            sd.drawDefault(" " + label.getId());
                            break;
                        }
                        case TYPE_SWITCH: {
                          @SuppressWarnings("unchecked")
                            Map<Integer, Label> offsets = (Map<Integer, Label>) params.getObject(i);
                            for (Entry<Integer, Label> entry : offsets.entrySet()) {
                                sd.drawDefault(" " + entry.getKey() + "=" + entry.getValue().getId());
                            }
                            break;
                        }
                        case TYPE_CONSTANT:
                        case TYPE_CONSTANT_READONLY:
                        case TYPE_CONSTANT_WIDE: {
                            sd.drawDefault(" " + params.getInt(i));
                            break;
                        }
                        case TYPE_ARRAYTYPE: {
                            sd.drawDefault(" " + _newarray.getTypeName(params.getInt(i)));
                            break;
                        }
                    }
                } catch (Exception ee) {
                  logger.warning("Error rendering instruction. Instruction = " + inst.getMnemonic() + " params = " + params);
                  StringWriter sw = new StringWriter();
                  ee.printStackTrace(new PrintWriter(sw));
                    logger.warning(sw.toString());
                }
View Full Code Here

            GroupAction group = new GroupAction();
            if (code == null) {
              group.add(new CreateCodeAttributeAction(mdr.getMethod().getAttributes(), this.cf.getPool()));
            }
            List choosers = editor.getChoosers();
            Instruction instruction = editor.getInstruction();
            try {
              instruction = instruction.createNewInstance();
            } catch(Exception e) {
              SystemFacade.getInstance().handleException(e);
            }

        InsertInstructionAction iia = new InsertInstructionAction(instruction, pc, mdr.getMethod());
View Full Code Here

            for (EditorRow er : mdr.getCodeRows()) {
              if (er instanceof LabelRow)
                continue;
              CodeRow cr = (CodeRow) er;

              Instruction inst = cr.getInstruction();

              Instruction copy = instructionCopier
                  .copyInstruction(inst, cr
                      .getDecompilationContext()
                      .getConstantPool(), newPool);
             
              if (copy == null) {
                throw new AssertionError("Copied instruction is null for instruction: " + inst);
              }
              int index = list.indexOf(inst);
              list.set(index, copy);
              List<Label> labels = inst.getLabels();
              List<Label> copyLabels = copy.getLabels();
              for (int i = 0; i < labels.size(); i++) {
                if (copyLabels.get(i) == null) {
                  throw new AssertionError("Copied label is null for instruction: " + inst + " : " + copyLabels);
                }
                Label label = labels.get(i);
                int labelIndex = list.indexOf(label);
                if (labelIndex != -1) {
                  list.set(labelIndex, copyLabels.get(i));
                } else {
                  list.add(copyLabels.get(i));
                }
              }

            }
            codeBlock.add(0, list);
            transferrable.setCode(codeBlock);
          }
          transferrable.setExceptions(exceptionNames);
          transferables.add(transferrable);
        }

      } else if (obj instanceof FieldDefRow) {
        FieldDefRow fdr = (FieldDefRow) obj;
        Field field = fdr.getField();
        // TODO: Attributes are not being copied

        TransferrableField transferrable = new TransferrableField();
        transferrable.setFieldName(field.getName());
        transferrable.setDescriptor(field.getDescriptor());
        transferrable.setAccessFlags(field.getAccessFlags());
        transferables.add(transferrable);
      } else if (obj instanceof CodeRow || obj instanceof LabelRow) {
        code.add((EditorRow) obj);
      }
    }

    if (!transferables.isEmpty()) {
      return transferables;
    } else {
      List<Instruction> list = new ArrayList<Instruction>();
      for (EditorRow er : code) {
        if (er instanceof CodeRow) {
          list.add(((CodeRow) er).getInstruction());
        } else if (er instanceof LabelRow) {
          list.add(((LabelRow) er).getLabel());
        } else {
          throw new AssertionError(
              "Object of invalid class (not CodeRow and not LabelRow) in list: "
                  + er.getClass());
        }
      }

      ConstantPool newPool = new ConstantPool();
      Code codeBlock = new Code(newPool);
      InstructionCopier instructionCopier = new InstructionCopier();
      for (EditorRow er : code) {
        if (er instanceof LabelRow)
          continue;
        CodeRow cr = (CodeRow) er;
        Instruction inst = cr.getInstruction();

        Instruction copy = instructionCopier.copyInstruction(inst, cr
            .getDecompilationContext().getConstantPool(), newPool);
        int index = list.indexOf(inst);
        list.set(index, copy);
        List<Label> labels = inst.getLabels();
        List<Label> copyLabels = copy.getLabels();
        for (int i = 0; i < labels.size(); i++) {
          Label label = labels.get(i);
          int labelIndex = list.indexOf(label);
          if (labelIndex != -1) {
            list.set(labelIndex, copyLabels.get(i));
View Full Code Here

TOP

Related Classes of net.sf.rej.java.instruction.Instruction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.