Java 类org.apache.bcel.generic.FieldGen 实例源码

项目:root4j    文件:ProxyBuilder.java   
private void generateFields(RootClass k, ConstantPoolGen cp, ClassGen cg)
{
   RootMember[] members = k.getMembers();
   for (int i = 0; i < members.length; i++)
   {
      if (cg.containsField(members[i].getName()) == null)
      {
         Type type = ((BasicMember) members[i]).getJavaType();
         if (type != null)
         {
            FieldGen fg = new FieldGen(ACC_PRIVATE, type, members[i].getName(), cp);
            cg.addField(fg.getField());
         }
      }
   }
}
项目:luaj_xowa    文件:JavaBuilder.java   
private String createLuaStringField(LuaString value) {
    String name = PREFIX_CONSTANT+constants.size();
    FieldGen fg = new FieldGen(Constants.ACC_STATIC | Constants.ACC_FINAL, 
            TYPE_LUAVALUE, name, cp);
    cg.addField(fg.getField());
    LuaString ls = value.checkstring();
    if ( ls.isValidUtf8() ) {
        init.append(new PUSH(cp, value.tojstring()));
        init.append(factory.createInvoke(STR_LUASTRING, "valueOf",
                TYPE_LUASTRING, ARG_TYPES_STRING, Constants.INVOKESTATIC));
    } else {
        char[] c = new char[ls.m_length];
        for ( int j=0; j<ls.m_length; j++ ) 
            c[j] = (char) (0xff & (int) (ls.m_bytes[ls.m_offset+j]));
        init.append(new PUSH(cp, new String(c)));
        init.append(factory.createInvoke(STR_STRING, "toCharArray",
                TYPE_CHARARRAY, Type.NO_ARGS,
                Constants.INVOKEVIRTUAL));
        init.append(factory.createInvoke(STR_LUASTRING, "valueOf",
                TYPE_LUASTRING, ARG_TYPES_CHARARRAY,
                Constants.INVOKESTATIC));
    }
    init.append(factory.createPutStatic(classname, name, TYPE_LUAVALUE));           
    return name;
}
项目:luaj_xowa    文件:JavaBuilder.java   
private String createLuaStringField(LuaString value) {
    String name = PREFIX_CONSTANT+constants.size();
    FieldGen fg = new FieldGen(Constants.ACC_STATIC | Constants.ACC_FINAL, 
            TYPE_LUAVALUE, name, cp);
    cg.addField(fg.getField());
    LuaString ls = value.checkstring();
    if ( ls.isValidUtf8() ) {
        init.append(new PUSH(cp, value.tojstring()));
        init.append(factory.createInvoke(STR_LUASTRING, "valueOf",
                TYPE_LUASTRING, ARG_TYPES_STRING, Constants.INVOKESTATIC));
    } else {
        char[] c = new char[ls.m_length];
        for ( int j=0; j<ls.m_length; j++ ) 
            c[j] = (char) (0xff & (int) (ls.m_bytes[ls.m_offset+j]));
        init.append(new PUSH(cp, new String(c)));
        init.append(factory.createInvoke(STR_STRING, "toCharArray",
                TYPE_CHARARRAY, Type.NO_ARGS,
                Constants.INVOKEVIRTUAL));
        init.append(factory.createInvoke(STR_LUASTRING, "valueOf",
                TYPE_LUASTRING, ARG_TYPES_CHARARRAY,
                Constants.INVOKESTATIC));
    }
    init.append(factory.createPutStatic(classname, name, TYPE_LUAVALUE));           
    return name;
}
项目:eclectic    文件:ClosureDefJVMGen.java   
private MethodGen createCallMethod(ClassGen closureClass, ClosureScope scope, GenScope parentScope) {
    InstructionList il = new InstructionList();
    MethodGen mg = new MethodGen(Constants.ACC_PUBLIC,// access flags
            Type.OBJECT, // return type
            getParameterTypes(), getParameterNames(), // arg
            // names
            "call", closureClass.getClassName(), // method, class
            il, closureClass.getConstantPool());

    // TODO: Do it directly in the constructor!!
    scope.processMethod(mg);

    GenUtil.generate(this, closureClass, mg, scope, true);

    // Create proper fields in the class, they will be set when the
    // instance is created
    Set<Variable> o = scope.getOuterVariables().get();
    for (Variable variable : o) {
        closureClass.addField(new FieldGen(Constants.ACC_PUBLIC, Type.OBJECT, variable.getName(), closureClass.getConstantPool()).getField());
    }

    return mg;
}
项目:root4j    文件:Clones2Builder.java   
private void generateStaticFields(RootClass k, ConstantPoolGen cp, ClassGen cg)
{
   RootMember[] members = k.getMembers();
   for (int i = 0; i < members.length; i++)
   {
      BasicMember member = (BasicMember) members[i];
      TLeaf leaf = (TLeaf) lMap.get(member);
      if (leaf != null)
      {
         Type type = new ObjectType(leaf.getClass().getName());
         FieldGen fg = new FieldGen(ACC_PUBLIC | ACC_STATIC, type, member.getName() + "Leaf", cp);
         cg.addField(fg.getField());
      }
   }
}
项目:JRemoting    文件:BcelStubGenerator.java   
/**
 * @param encodedFieldName the encoded field name
 */
protected void addField(String encodedFieldName) {
    if (!internalFieldRepresentingClasses.contains(encodedFieldName)) {
        //System.out.println("method."+method.getName()+".addingfield["
        //  + _encodedFieldName + "]");
        FieldGen field = new FieldGen(Constants.ACC_STATIC, new ObjectType("java.lang.Class"), encodedFieldName, constantsPool);
        classGen.addField(field.getField());
        internalFieldRepresentingClasses.add(encodedFieldName);
    }

}
项目:cn1    文件:JavaByteCodeOutputProcess.java   
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, constantPoolGen);
    classGen.addField(f.getField());
}
项目:luaj_xowa    文件:JavaBuilder.java   
private String createLuaIntegerField(int value) {
    String name = PREFIX_CONSTANT+constants.size();
    FieldGen fg = new FieldGen(Constants.ACC_STATIC | Constants.ACC_FINAL, 
            TYPE_LUAVALUE, name, cp);
    cg.addField(fg.getField());
    init.append(new PUSH(cp, value));
    init.append(factory.createInvoke(STR_LUAVALUE, "valueOf",
            TYPE_LUAINTEGER, ARG_TYPES_INT, Constants.INVOKESTATIC));
    init.append(factory.createPutStatic(classname, name, TYPE_LUAVALUE));
    return name;
}
项目:luaj_xowa    文件:JavaBuilder.java   
private String createLuaDoubleField(double value) {
    String name = PREFIX_CONSTANT+constants.size();
    FieldGen fg = new FieldGen(Constants.ACC_STATIC | Constants.ACC_FINAL, 
            TYPE_LUAVALUE, name, cp);
    cg.addField(fg.getField());
    init.append(new PUSH(cp, value));
    init.append(factory.createInvoke(STR_LUAVALUE, "valueOf",
            TYPE_LUANUMBER, ARG_TYPES_DOUBLE, Constants.INVOKESTATIC));
    init.append(factory.createPutStatic(classname, name, TYPE_LUAVALUE));           
    return name;
}
项目:luaj_xowa    文件:JavaBuilder.java   
private String createLuaIntegerField(int value) {
    String name = PREFIX_CONSTANT+constants.size();
    FieldGen fg = new FieldGen(Constants.ACC_STATIC | Constants.ACC_FINAL, 
            TYPE_LUAVALUE, name, cp);
    cg.addField(fg.getField());
    init.append(new PUSH(cp, value));
    init.append(factory.createInvoke(STR_LUAVALUE, "valueOf",
            TYPE_LUAINTEGER, ARG_TYPES_INT, Constants.INVOKESTATIC));
    init.append(factory.createPutStatic(classname, name, TYPE_LUAVALUE));
    return name;
}
项目:luaj_xowa    文件:JavaBuilder.java   
private String createLuaDoubleField(double value) {
    String name = PREFIX_CONSTANT+constants.size();
    FieldGen fg = new FieldGen(Constants.ACC_STATIC | Constants.ACC_FINAL, 
            TYPE_LUAVALUE, name, cp);
    cg.addField(fg.getField());
    init.append(new PUSH(cp, value));
    init.append(factory.createInvoke(STR_LUAVALUE, "valueOf",
            TYPE_LUANUMBER, ARG_TYPES_DOUBLE, Constants.INVOKESTATIC));
    init.append(factory.createPutStatic(classname, name, TYPE_LUAVALUE));           
    return name;
}
项目:autodao    文件:DAOAnalysisSimple.java   
@Override
protected void setUpClassfile() {
    ObjectType type = new ObjectType(EM_TYPE);
    FieldGen fieldGen = new FieldGen(ACC_PRIVATE, type, "em", _cp);
    _cg.addField(fieldGen.getField());

    createInheritedConstructor(EntityManager.class);
    createGetEntityManager();
}
项目:root4j    文件:Clone2Builder.java   
public JavaClass build(GenericRootClass klass)
{
   optimize = (klass.getStreamerInfo().getBits() & (1 << 12)) == 0;

   String className = nameMangler.mangleFullClassName(getStem(),klass.getClassName());
   String clonesClassName = nameMangler.mangleFullClassName("org.dianahep.root4j.clones2",klass.getClassName());
   ClassGen cg = new ClassGen(className, "org/dianahep/root4j/core/Clone2", "<generated>", ACC_PUBLIC | ACC_SUPER, new String[]
      {
         nameMangler.mangleInterfaceName(klass.getClassName())
      });
   ConstantPoolGen cp = cg.getConstantPool();
   InstructionList il = new InstructionList();
   InstructionFactory factory = new InstructionFactory(cg);

   cg.addEmptyConstructor(ACC_PUBLIC);

   // Build the complete list of superclasses
   List sup = new ArrayList();
   RootClass[] superClasses = klass.getSuperClasses();
   iterativelyAdd(sup, superClasses);
   sup.add(klass);

   // Create the fields
   cg.addField(new FieldGen(ACC_PRIVATE, Type.INT, "index", cp).getField());
   cg.addField(new FieldGen(ACC_PRIVATE, new ObjectType(clonesClassName), "clones", cp).getField());

   //Generate the setData method
   MethodGen mg = new MethodGen(ACC_PUBLIC, Type.VOID, new Type[]
      {
         Type.INT, new ObjectType("org.dianahep.root4j.core.Clones2")
      }, new String[] { "index", "clones" }, "setData", className, il, cp);
   il.append(InstructionConstants.ALOAD_0);
   il.append(InstructionConstants.ILOAD_1);
   il.append(factory.createPutField(className, "index", Type.INT));
   il.append(InstructionConstants.ALOAD_0);
   il.append(InstructionConstants.ALOAD_2);
   il.append(factory.createCast(new ObjectType("org.dianahep.root4j.core.Clones2"), new ObjectType(clonesClassName)));
   il.append(factory.createPutField(className, "clones", new ObjectType(clonesClassName)));
   il.append(InstructionConstants.RETURN);
   mg.setMaxStack();
   mg.setMaxLocals();
   cg.addMethod(mg.getMethod());
   il.dispose();

   // Generate the accessor methods
   for (Iterator i = sup.iterator(); i.hasNext();)
      generateMethods((RootClass) i.next(), cp, il, factory, cg, className, clonesClassName);

   return cg.getJavaClass();
}
项目:root4j    文件:CloneBuilder.java   
public JavaClass build(GenericRootClass klass)
{
   optimize = (klass.getStreamerInfo().getBits() & (1 << 12)) == 0;

   String className =  nameMangler.mangleFullClassName(getStem(),klass.getClassName());
   String clonesClassName = nameMangler.mangleFullClassName("org.dianahep.root4j.clones",klass.getClassName());
   ClassGen cg = new ClassGen(className, "org/dianahep/root4j/core/Clone", "<generated>", ACC_PUBLIC | ACC_SUPER, new String[]
      {
         nameMangler.mangleInterfaceName(klass.getClassName())
      });
   ConstantPoolGen cp = cg.getConstantPool();
   InstructionList il = new InstructionList();
   InstructionFactory factory = new InstructionFactory(cg);

   cg.addEmptyConstructor(ACC_PUBLIC);

   // Build the complete list of superclasses
   List sup = new ArrayList();
   RootClass[] superClasses = klass.getSuperClasses();
   iterativelyAdd(sup, superClasses);
   sup.add(klass);

   // Create the fields
   cg.addField(new FieldGen(ACC_PRIVATE, Type.INT, "index", cp).getField());
   cg.addField(new FieldGen(ACC_PRIVATE, new ObjectType(clonesClassName), "clones", cp).getField());

   //Generate the setData method
   MethodGen mg = new MethodGen(ACC_PUBLIC, Type.VOID, new Type[]
      {
         Type.INT, new ObjectType("org.dianahep.root4j.core.Clones")
      }, new String[] { "index", "clones" }, "setData", className, il, cp);
   il.append(InstructionConstants.ALOAD_0);
   il.append(InstructionConstants.ILOAD_1);
   il.append(factory.createPutField(className, "index", Type.INT));
   il.append(InstructionConstants.ALOAD_0);
   il.append(InstructionConstants.ALOAD_2);
   il.append(factory.createCast(new ObjectType("org.dianahep.root4j.core.Clones"), new ObjectType(clonesClassName)));
   il.append(factory.createPutField(className, "clones", new ObjectType(clonesClassName)));
   il.append(InstructionConstants.RETURN);
   mg.setMaxStack();
   mg.setMaxLocals();
   cg.addMethod(mg.getMethod());
   il.dispose();

   // Generate the accessor methods
   for (Iterator i = sup.iterator(); i.hasNext();)
      generateMethods((RootClass) i.next(), cp, il, factory, cg, className, clonesClassName);

   return cg.getJavaClass();
}
项目:annotation-tools    文件:BCELPerfTest.java   
byte[] counterAdaptClass(final InputStream is, final String name)
        throws Exception
{
    JavaClass jc = new ClassParser(is, name + ".class").parse();
    ClassGen cg = new ClassGen(jc);
    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();
}
项目:luaj_xowa    文件:JavaBuilder.java   
public JavaBuilder(ProtoInfo pi, String classname, String filename) {
    this.pi = pi;
    this.p = pi.prototype;
    this.classname = classname;

    // what class to inherit from
    superclassType = p.numparams;
    if ( p.is_vararg != 0 || superclassType >= SUPERTYPE_VARARGS )
        superclassType = SUPERTYPE_VARARGS;
    for ( int i=0, n=p.code.length; i<n; i++ ) {
        int inst = p.code[i];
        int o = Lua.GET_OPCODE(inst);
        if ( (o == Lua.OP_TAILCALL) ||
             ((o == Lua.OP_RETURN) && (Lua.GETARG_B(inst) < 1 || Lua.GETARG_B(inst) > 2)) ) {
            superclassType = SUPERTYPE_VARARGS;
            break;
        }
    }

    // create class generator
    cg = new ClassGen(classname, SUPER_NAME_N[superclassType], filename,
            Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
    cp = cg.getConstantPool(); // cg creates constant pool

    // main instruction lists
    factory = new InstructionFactory(cg);
    init = new InstructionList();
    main = new InstructionList();

    // create the fields
    for ( int i=0; i<p.upvalues.length; i++ ) {
        boolean isrw = pi.isReadWriteUpvalue( pi.upvals[i] ); 
        Type uptype = isrw? (Type) TYPE_LOCALUPVALUE: (Type) TYPE_LUAVALUE;
        FieldGen fg = new FieldGen(0, uptype, upvalueName(i), cp);
        cg.addField(fg.getField());
    }

    // create the method
    mg = new MethodGen( Constants.ACC_PUBLIC | Constants.ACC_FINAL, // access flags
            RETURN_TYPE_N[superclassType], // return type
            ARG_TYPES_N[superclassType], // argument types
            ARG_NAMES_N[superclassType], // arg names
            METH_NAME_N[superclassType], 
            STR_LUAVALUE, // method, defining class
            main, cp);

    // initialize the values in the slots
    initializeSlots();  

    // initialize branching
    int nc = p.code.length;
    targets = new int[nc];
    branches = new BranchInstruction[nc];
    branchDestHandles = new InstructionHandle[nc];
    lastInstrHandles = new InstructionHandle[nc];
}
项目:luaj_xowa    文件:JavaBuilder.java   
public JavaBuilder(ProtoInfo pi, String classname, String filename) {
    this.pi = pi;
    this.p = pi.prototype;
    this.classname = classname;

    // what class to inherit from
    superclassType = p.numparams;
    if ( p.is_vararg != 0 || superclassType >= SUPERTYPE_VARARGS )
        superclassType = SUPERTYPE_VARARGS;
    for ( int i=0, n=p.code.length; i<n; i++ ) {
        int inst = p.code[i];
        int o = Lua.GET_OPCODE(inst);
        if ( (o == Lua.OP_TAILCALL) ||
             ((o == Lua.OP_RETURN) && (Lua.GETARG_B(inst) < 1 || Lua.GETARG_B(inst) > 2)) ) {
            superclassType = SUPERTYPE_VARARGS;
            break;
        }
    }

    // create class generator
    cg = new ClassGen(classname, SUPER_NAME_N[superclassType], filename,
            Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
    cp = cg.getConstantPool(); // cg creates constant pool

    // main instruction lists
    factory = new InstructionFactory(cg);
    init = new InstructionList();
    main = new InstructionList();

    // create the fields
    for ( int i=0; i<p.upvalues.length; i++ ) {
        boolean isrw = pi.isReadWriteUpvalue( pi.upvals[i] ); 
        Type uptype = isrw? (Type) TYPE_LOCALUPVALUE: (Type) TYPE_LUAVALUE;
        FieldGen fg = new FieldGen(0, uptype, upvalueName(i), cp);
        cg.addField(fg.getField());
    }

    // create the method
    mg = new MethodGen( Constants.ACC_PUBLIC | Constants.ACC_FINAL, // access flags
            RETURN_TYPE_N[superclassType], // return type
            ARG_TYPES_N[superclassType], // argument types
            ARG_NAMES_N[superclassType], // arg names
            METH_NAME_N[superclassType], 
            STR_LUAVALUE, // method, defining class
            main, cp);

    // initialize the values in the slots
    initializeSlots();  

    // initialize branching
    int nc = p.code.length;
    targets = new int[nc];
    branches = new BranchInstruction[nc];
    branchDestHandles = new InstructionHandle[nc];
    lastInstrHandles = new InstructionHandle[nc];
}
项目:eclectic    文件:CommonGen.java   
public static void generateQueuesFields(Collection<Queue> qs, GenScope scope, ClassGen cg) {
    for (Queue q : qs) {
        QueueJVMGen jvmQ = (QueueJVMGen) q;         
        cg.addField(new FieldGen(Constants.ACC_PUBLIC, jvmQ.getType(), jvmQ.getFieldName(), cg.getConstantPool()).getField());          
    }
}
项目:eclectic    文件:QoolTransformationJVMGen.java   
private String generateObserverClass(ModelDefinition model, ObserverDescription observer, TransformationContext context) {
    final String className = context.getRuntimeClassName() + "$" + observer.getName();
    final String transformationFieldName = "transformation_";
    ClassGen cg = new ClassGen(className, observer.getObserverClass(),
            context.getFilename(), Constants.ACC_PUBLIC | Constants.ACC_SUPER,
            new String[] {});

    cg.addField(new FieldGen(Constants.ACC_PUBLIC, DefaultTypes.QoolTransformation, 
            transformationFieldName, cg.getConstantPool()).getField());         

    // constructor
    InstructionList constructorIl = new InstructionList();
    InstructionFactory constructorIfact = new InstructionFactory(cg);
    MethodGen constructor = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, 
            new Type[] { DefaultTypes.QoolTransformation } , null, "<init>",
               cg.getClassName(), constructorIl, cg.getConstantPool());

       constructorIl.append(InstructionConstants.THIS); // Push `this'
       constructorIl.append(new INVOKESPECIAL(cg.getConstantPool().addMethodref(cg.getSuperclassName(), "<init>", "()V")));

    constructorIl.append(InstructionFactory.THIS);
    constructorIl.append(new ALOAD(1));
    constructorIl.append( constructorIfact.createPutField(cg.getClassName(), transformationFieldName, DefaultTypes.QoolTransformation) );       
       constructorIl.append(InstructionFactory.RETURN);
    constructor.setMaxLocals();
    constructor.setMaxStack();
       cg.addMethod(constructor.getMethod());
       // end-of constructor


    EList<UpdateMethod> methods = observer.getUpdateMethods();
    for (UpdateMethod updateMethod : methods) {
        EList<String> ptypes = updateMethod.getParameterTypes();
        InstructionList il = new InstructionList();
        InstructionFactory ifact = new InstructionFactory(cg);
        // TODO: Assuming void return type
        // TODO: Assuming object types

        Type[] types = new Type[ptypes.size()];
        int i = 0;
        for(String strType : ptypes) {
            types[i++] = new ObjectType(strType);
        }
        MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, types, null, updateMethod.getName(),
                cg.getClassName(), il, cg.getConstantPool());

        // TODO: assuming only 1 interest parameter
        assert(updateMethod.getInterest().size() == 1);
        il.append(InstructionConstants.THIS); 
        il.append(ifact.createGetField(className, transformationFieldName, DefaultTypes.QoolTransformation));
        il.append(ifact.createConstant(model.getName()));
        il.append(new ALOAD(updateMethod.getInterest().get(0) + 1));
        il.append(ifact.createInvoke(DefaultTypes.QoolTransformation.getClassName(), "addObjectToQueues",
                Type.VOID, new Type[] { Type.STRING, Type.OBJECT }, Constants.INVOKEVIRTUAL));
        il.append(InstructionFactory.RETURN);
        mg.setMaxLocals();
        mg.setMaxStack();           
        cg.addMethod(mg.getMethod());
    }

    context.addExtraClass(cg.getJavaClass());

    return className;
}