示例#1
0
  public static void main(String[] argv) throws Exception {
    JavaClass clazz = null;

    if ((clazz = Repository.lookupClass(argv[0])) == null) {
      clazz = new ClassParser(argv[0]).parse(); // May throw IOException
    }

    ClassGen cg = new ClassGen(clazz);

    Method[] methods = clazz.getMethods();

    for (int i = 0; i < methods.length; i++) {
      MethodGen mg = new MethodGen(methods[i], cg.getClassName(), cg.getConstantPool());
      cg.replaceMethod(methods[i], mg.getMethod());
    }

    Field[] fields = clazz.getFields();

    for (int i = 0; i < fields.length; i++) {
      FieldGen fg = new FieldGen(fields[i], cg.getConstantPool());
      cg.replaceField(fields[i], fg.getField());
    }

    cg.getJavaClass().dump(clazz.getClassName() + ".clazz");
  }
示例#2
0
 private void createField(Element field) throws IllegalXMLVMException {
   String name = field.getAttributeValue("name");
   Type t = parseTypeString(field.getAttributeValue("type"));
   short flags = getAccessFlags(field);
   FieldGen f = new FieldGen(flags, t, name, _cp);
   _cg.addField(f.getField());
 }
示例#3
0
  private void createFields(Pair<String, String>[] fieldDefs) {
    FieldGen field;

    for (Pair<String, String> aField : fieldDefs) {
      field = new FieldGen(ACC_PUBLIC, new ObjectType(aField.first), aField.second, _cp);
      _cg.addField(field.getField());
    }
  }
  /* (non-Javadoc)
   * @see com.atosorigin.nl.brainfuck.compiler.v1.MemberCompiler#compile(com.atosorigin.nl.brainfuck.compiler.base.CompilerContext, java.lang.Object)
   */
  @Override
  public void compile(CompilerContext ctx, Object instr) {
    super.compile(ctx, instr);
    ConstantPoolGen cp = ctx.getConstantPool();

    ObjectType console = new ObjectType("jline.ConsoleReader");

    FieldGen consoleGen = new FieldGen(Constants.ACC_PRIVATE, console, "console", cp);
    ctx.getCg().addField(consoleGen.getField());
  }
  /**
   * Adds a join point member field.
   *
   * @param cp the ConstantPoolGen
   * @param cg the ClassGen
   * @param mg the MethodGen
   * @param methodSequence the methods sequence number
   */
  private void addJoinPointField(
      final ConstantPoolGen cp, final ClassGen cg, final MethodGen mg, final int methodSequence) {

    final String joinPoint = getJoinPointName(mg.getMethod(), methodSequence);

    if (cg.containsField(joinPoint) != null) {
      return;
    }

    final FieldGen field =
        new FieldGen(
            Constants.ACC_PRIVATE | Constants.ACC_FINAL,
            new ObjectType(TransformationUtil.THREAD_LOCAL_CLASS),
            joinPoint,
            cp);
    cg.addField(field.getField());
  }
示例#6
0
文件: BCEL.java 项目: tempbottle/ASM
 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();
 }