Esempio n. 1
0
  public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final InstructionList il = methodGen.getInstructionList();

    _left.translate(classGen, methodGen);
    _right.translate(classGen, methodGen);

    switch (_op) {
      case PLUS:
        il.append(_type.ADD());
        break;
      case MINUS:
        il.append(_type.SUB());
        break;
      case TIMES:
        il.append(_type.MUL());
        break;
      case DIV:
        il.append(_type.DIV());
        break;
      case MOD:
        il.append(_type.REM());
        break;
      default:
        ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_BINARY_OP_ERR, this);
        getParser().reportError(Constants.ERROR, msg);
    }
  }
Esempio n. 2
0
  /**
   * Convenience method.
   *
   * <p>Add an empty constructor to this class that does nothing but calling super().
   *
   * @param access rights for constructor
   */
  public void addEmptyConstructor(int access_flags) {
    InstructionList il = new InstructionList();
    il.append(InstructionConstants.THIS); // Push `this'
    il.append(new INVOKESPECIAL(cp.addMethodref(super_class_name, "<init>", "()V")));
    il.append(InstructionConstants.RETURN);

    MethodGen mg =
        new MethodGen(access_flags, Type.VOID, Type.NO_ARGS, null, "<init>", class_name, il, cp);
    mg.setMaxStack(1);
    addMethod(mg.getMethod());
  }
Esempio n. 3
0
 private void createCode(Element code) throws IllegalXMLVMException {
   List<Element> instructions = code.getChildren();
   for (Element inst : instructions) {
     String name = inst.getName();
     String opcMethodName =
         "createInstruction" + name.substring(0, 1).toUpperCase() + name.substring(1);
     Class appClazz;
     Method opcMeth;
     Class[] paramTypes = {Element.class};
     Object[] params = {inst};
     appClazz = this.getClass();
     Object newInstr = null;
     try {
       opcMeth = appClazz.getMethod(opcMethodName, paramTypes);
       newInstr = opcMeth.invoke(this, params);
     } catch (NoSuchMethodException ex) {
       throw new IllegalXMLVMException(
           "Illegal instruction 1, unable to find method "
               + opcMethodName
               + " for '"
               + name
               + "'");
     } catch (InvocationTargetException ex) {
       ex.printStackTrace();
       throw new IllegalXMLVMException("Illegal instruction 2 '" + name + "'");
     } catch (IllegalAccessException ex) {
       throw new IllegalXMLVMException("Illegal instruction 3 '" + name + "'");
     }
     if (newInstr != null) {
       InstructionHandle ih = null;
       if (newInstr instanceof BranchInstruction) ih = il.append((BranchInstruction) newInstr);
       else if (newInstr instanceof CompoundInstruction)
         ih = il.append((CompoundInstruction) newInstr);
       else if (newInstr instanceof Instruction) ih = il.append((Instruction) newInstr);
       instructionHandlerManager.registerInstructionHandle(ih);
     }
   }
 }
Esempio n. 4
0
 byte[] counterAdaptClass(final InputStream is, final String name) throws Exception {
   JavaClass jc = new ClassParser(is, name + ".class").parse();
   ClassGen cg = new ClassGen(jc);
   String cName = cg.getClassName();
   ConstantPoolGen cp = cg.getConstantPool();
   if (!cg.isInterface()) {
     FieldGen fg = new FieldGen(ACC_PUBLIC, Type.getType("I"), "_counter", cp);
     cg.addField(fg.getField());
   }
   Method[] ms = cg.getMethods();
   for (int j = 0; j < ms.length; ++j) {
     MethodGen mg = new MethodGen(ms[j], cg.getClassName(), cp);
     if (!mg.getName().equals("<init>") && !mg.isStatic() && !mg.isAbstract() && !mg.isNative()) {
       if (mg.getInstructionList() != null) {
         InstructionList il = new InstructionList();
         il.append(new ALOAD(0));
         il.append(new ALOAD(0));
         il.append(new GETFIELD(cp.addFieldref(name, "_counter", "I")));
         il.append(new ICONST(1));
         il.append(new IADD());
         il.append(new PUTFIELD(cp.addFieldref(name, "_counter", "I")));
         mg.getInstructionList().insert(il);
         mg.setMaxStack(Math.max(mg.getMaxStack(), 2));
         boolean lv = ms[j].getLocalVariableTable() == null;
         boolean ln = ms[j].getLineNumberTable() == null;
         if (lv) {
           mg.removeLocalVariables();
         }
         if (ln) {
           mg.removeLineNumbers();
         }
         cg.replaceMethod(ms[j], mg.getMethod());
       }
     }
   }
   return cg.getJavaClass().getBytes();
 }
Esempio n. 5
0
  /**
   * Transforms invoke instructions that match the specified list for this class to call the
   * specified static call instead.
   */
  private InstructionList xform_inst(MethodGen mg, Instruction inst) {

    switch (inst.getOpcode()) {
      case Constants.INVOKESTATIC:
        {
          InstructionList il = new InstructionList();
          INVOKESTATIC is = (INVOKESTATIC) inst;
          String cname = is.getClassName(pgen);
          String mname = is.getMethodName(pgen);
          Type[] args = is.getArgumentTypes(pgen);
          MethodDef orig = new MethodDef(cname + "." + mname, args);
          MethodInfo call = method_map.get(orig);
          if (call != null) {
            call.cnt++;
            String classname = call.method_class;
            String methodname = mname;
            debug_map.log(
                "%s.%s: Replacing method %s.%s (%s) with %s.%s%n",
                mg.getClassName(),
                mg.getName(),
                cname,
                mname,
                UtilMDE.join(args, ", "),
                classname,
                methodname);
            il.append(
                ifact.createInvoke(
                    classname, methodname, is.getReturnType(pgen), args, Constants.INVOKESTATIC));
          }
          return (il);
        }

      case Constants.INVOKEVIRTUAL:
        {
          InstructionList il = new InstructionList();
          INVOKEVIRTUAL iv = (INVOKEVIRTUAL) inst;
          String cname = iv.getClassName(pgen);
          String mname = iv.getMethodName(pgen);
          Type[] args = iv.getArgumentTypes(pgen);
          Type instance_type = iv.getReferenceType(pgen);
          Type[] new_args = BCELUtil.insert_type(instance_type, args);
          MethodDef orig = new MethodDef(cname + "." + mname, args);
          if (debug_class) System.out.printf("looking for %s in map %s%n", orig, method_map);
          MethodInfo call = method_map.get(orig);
          if (call != null) {
            call.cnt++;
            String classname = call.method_class;
            String methodname = mname;
            debug_map.log(
                "Replacing method %s.%s (%s) with %s.%s%n",
                cname, mname, ArraysMDE.toString(args), classname, methodname);
            il.append(
                ifact.createInvoke(
                    classname,
                    methodname,
                    iv.getReturnType(pgen),
                    new_args,
                    Constants.INVOKESTATIC));
          }
          return (il);
        }

      default:
        return (null);
    }
  }