Example #1
0
 /** Peephole optimization: Remove sequences of [ALOAD, POP]. */
 private void peepHoleOptimization(MethodGenerator methodGen) {
   final String pattern = "`aload'`pop'`instruction'";
   final InstructionList il = methodGen.getInstructionList();
   final InstructionFinder find = new InstructionFinder(il);
   for (Iterator iter = find.search(pattern); iter.hasNext(); ) {
     InstructionHandle[] match = (InstructionHandle[]) iter.next();
     try {
       il.delete(match[0], match[1]);
     } catch (TargetLostException e) {
       // TODO: move target down into the list
     }
   }
 }
 public void run() {
   ClassGen nodeCG = hookHandler.getClassByNick("Node");
   for (ClassGen cG : classes) {
     ConstantPoolGen cpg = cG.getConstantPool();
     int nonstatic = 0;
     int nodeFields = 0;
     for (Field field : cG.getFields()) {
       if (field.isStatic()) continue;
       nonstatic++;
       if (field.getSignature().equals("L" + nodeCG.getClassName() + ";")) nodeFields++;
     }
     if (nonstatic != 2 || nodeFields != 2) continue;
     String fieldHeadName = "";
     for (Method m : cG.getMethods()) {
       if (m.getName().equals("<init>")) {
         InstructionFinder instructionFinder =
             new InstructionFinder(new InstructionList(m.getCode().getCode()));
         Iterator<InstructionHandle[]> iterator = instructionFinder.search("putfield");
         if (iterator.hasNext()) {
           fieldHeadName =
               ((FieldInstruction) iterator.next()[0].getInstruction()).getFieldName(cpg);
         }
       }
     }
     hookHandler.addClassNick("NodeList", cG);
     for (Field field : cG.getFields()) {
       if (field.isStatic()) continue;
       if (field.getName().equals(fieldHeadName)) {
         hookHandler.addFieldHook(
             "NodeList", cG, field, "headNode", TypeBuilder.createHookType("Node"));
       } else {
         hookHandler.addFieldHook(
             "NodeList", cG, field, "currentNode", TypeBuilder.createHookType("Node"));
       }
     }
   }
 }
  @Override
  public void run() {
    ClassGen nodeCG = hookHandler.getClassByNick("Node");
    ClassGen npcCG = hookHandler.getClassByNick("NPC");
    for (ClassGen cG : classes) {
      ConstantPoolGen cpg = cG.getConstantPool();
      if (!cG.getSuperclassName().equals(nodeCG.getClassName())) {
        continue;
      }

      int nonStaticFieldCount = 0;
      int npcFieldCount = 0;
      for (Field field : cG.getFields()) {
        if (field.isStatic()) {
          continue;
        }
        nonStaticFieldCount++;
        if (field.getType().toString().equals(npcCG.getClassName())) {
          npcFieldCount++;
        }
      }
      if (nonStaticFieldCount != 1 || npcFieldCount != 1) {
        continue;
      }

      hookHandler.addClassNick("NPCNode", cG);
      for (Field field : cG.getFields()) {
        if (field.isStatic()) {
          continue;
        }
        if (field.getType().toString().equals(npcCG.getClassName())) {
          hookHandler.addFieldHook("NPCNode", cG, field, "NPC", TypeBuilder.createHookType("NPC"));
        }
      }
    }
    ClassGen npcNodeCG = hookHandler.getClassByNick("NPCNode");
    for (ClassGen cG : classes) {
      for (Field field : cG.getFields()) {
        if (!field.isStatic()) {
          continue;
        }
        if (field.getType().getSignature().equals("[L" + npcNodeCG.getClassName() + ";")) {
          hookHandler.addClientHook(
              cG, field, "NPCNodes", TypeBuilder.createHookArrayType("NPCNode", 1));
        }
      }
    }
    ClassGen nodeCacheCG = hookHandler.getClassByNick("NodeCache");
    FieldHook fieldHook = hookHandler.getFieldHook("NPCNode", "NPC");
    for (ClassGen cG : classes) {
      ConstantPoolGen cpg = cG.getConstantPool();
      if (cpg.lookupClass(nodeCacheCG.getClassName()) == -1) {
        continue;
      }
      if (cpg.lookupFieldref(
              fieldHook.getClassName(),
              fieldHook.getFieldName(),
              fieldHook.getFieldType().getSignature())
          == -1) {
        continue;
      }
      for (Method method : cG.getMethods()) {
        if (!method.isStatic()) {
          continue;
        }
        if (!method.getReturnType().equals(Type.VOID)) {
          continue;
        }
        Type[] args = method.getArgumentTypes();
        if (args.length < 13 || args.length > 14) {
          continue;
        }
        if (TypeCounter.getObjectCount(args) != 0) {
          continue;
        }
        InstructionList iList = new InstructionList(method.getCode().getCode());
        InstructionFinder instructionFinder = new InstructionFinder(iList);
        for (Iterator<InstructionHandle[]> iterator = instructionFinder.search("getstatic");
            iterator.hasNext(); ) {
          InstructionHandle[] ih = iterator.next();
          FieldInstruction fieldInstruction = (FieldInstruction) ih[0].getInstruction();
          if (fieldInstruction.getFieldType(cpg).toString().equals(nodeCacheCG.getClassName())) {
            hookHandler.addClientHook(
                fieldInstruction, cpg, "NPCNodeCache", TypeBuilder.createHookType("NodeCache"));
          }
        }
      }
    }
  }