org.apache.bcel.generic.FieldGen Java Examples

The following examples show how to use org.apache.bcel.generic.FieldGen. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaBuilder.java    From luaj with MIT License 6 votes vote down vote up
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;
}
 
Example #2
Source File: id.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] argv) throws Exception {
    JavaClass clazz;

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

    final ClassGen cg = new ClassGen(clazz);

    for (final Method method : clazz.getMethods()) {
        final MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool());
        cg.replaceMethod(method, mg.getMethod());
    }

    for (final Field field : clazz.getFields()) {
        final FieldGen fg = new FieldGen(field, cg.getConstantPool());
        cg.replaceField(field, fg.getField());
    }

    cg.getJavaClass().dump(clazz.getClassName() + ".clazz");
}
 
Example #3
Source File: LDAPSSLSocketFactoryGenerator.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a static field _sslContext of type {@link SSLSocketFactory}.
 *
 * @param classGen
 * @param constantPoolGen
 */
private static void createSslContextStaticField(ClassGen classGen, ConstantPoolGen constantPoolGen)
{
    FieldGen fieldGen = new FieldGen(ACC_PRIVATE | ACC_STATIC,
                                     Type.getType(SSLSocketFactory.class),
                                     SSL_SOCKET_FACTORY_FIELD,
                                     constantPoolGen);
    classGen.addField(fieldGen.getField());
}
 
Example #4
Source File: JavaBuilder.java    From luaj with MIT License 5 votes vote down vote up
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;
}
 
Example #5
Source File: JavaBuilder.java    From luaj with MIT License 5 votes vote down vote up
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;
}
 
Example #6
Source File: JavaBuilder.java    From luaj with MIT License 4 votes vote down vote up
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];
}
 
Example #7
Source File: BCELPerfTest.java    From annotation-tools with MIT License 4 votes vote down vote up
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();
}
 
Example #8
Source File: BCELPerfTest.java    From annotation-tools with MIT License 4 votes vote down vote up
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();
}