Java Code Examples for javassist.bytecode.Bytecode#add()

The following examples show how to use javassist.bytecode.Bytecode#add() . 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: JavassistPerfTest.java    From annotation-tools with MIT License 5 votes vote down vote up
byte[] counterAdaptClass(final InputStream is, final String name)
        throws Exception
{
    CtClass cc = pool.makeClass(is);
    if (!cc.isInterface()) {
        cc.addField(new CtField(CtClass.intType, "_counter", cc));
    }
    CtMethod[] ms = cc.getDeclaredMethods();
    for (int j = 0; j < ms.length; ++j) {
        CtMethod m = ms[j];
        int modifiers = m.getModifiers();
        if (!Modifier.isStatic(modifiers)
                && !Modifier.isAbstract(modifiers)
                && !Modifier.isNative(modifiers))
        {
            if (!m.isEmpty()) {
                MethodInfo info = m.getMethodInfo();
                Bytecode bc = new Bytecode(info.getConstPool(), 1, 0);
                bc.addAload(0);
                bc.addAload(0);
                bc.addGetfield(cc, "_counter", "I");
                bc.add(Opcode.ICONST_1);
                bc.add(Opcode.IADD);
                bc.addPutfield(cc, "_counter", "I");
                CodeIterator iter = info.getCodeAttribute().iterator();
                iter.begin();
                iter.insert(bc.get());
            }
        }
    }
    return cc.toBytecode();
}
 
Example 2
Source File: JavassistPerfTest.java    From annotation-tools with MIT License 5 votes vote down vote up
byte[] counterAdaptClass(final InputStream is, final String name)
        throws Exception
{
    CtClass cc = pool.makeClass(is);
    if (!cc.isInterface()) {
        cc.addField(new CtField(CtClass.intType, "_counter", cc));
    }
    CtMethod[] ms = cc.getDeclaredMethods();
    for (int j = 0; j < ms.length; ++j) {
        CtMethod m = ms[j];
        int modifiers = m.getModifiers();
        if (!Modifier.isStatic(modifiers)
                && !Modifier.isAbstract(modifiers)
                && !Modifier.isNative(modifiers))
        {
            if (!m.isEmpty()) {
                MethodInfo info = m.getMethodInfo();
                Bytecode bc = new Bytecode(info.getConstPool(), 1, 0);
                bc.addAload(0);
                bc.addAload(0);
                bc.addGetfield(cc, "_counter", "I");
                bc.add(Opcode.ICONST_1);
                bc.add(Opcode.IADD);
                bc.addPutfield(cc, "_counter", "I");
                CodeIterator iter = info.getCodeAttribute().iterator();
                iter.begin();
                iter.insert(bc.get());
            }
        }
    }
    return cc.toBytecode();
}
 
Example 3
Source File: BulkAccessorFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void addGetter(ClassFile classfile, final Method[] getters) throws CannotCompileException {
	final ConstPool constPool = classfile.getConstPool();
	final int targetBeanConstPoolIndex = constPool.addClassInfo( this.targetBean.getName() );
	final String desc = GET_SETTER_DESC;
	final MethodInfo getterMethodInfo = new MethodInfo( constPool, GENERATED_GETTER_NAME, desc );

	final Bytecode code = new Bytecode( constPool, 6, 4 );
	/* | this | bean | args | raw bean | */
	if ( getters.length >= 0 ) {
		// aload_1 // load bean
		code.addAload( 1 );
		// checkcast // cast bean
		code.addCheckcast( this.targetBean.getName() );
		// astore_3 // store bean
		code.addAstore( 3 );
		for ( int i = 0; i < getters.length; ++i ) {
			if ( getters[i] != null ) {
				final Method getter = getters[i];
				// aload_2 // args
				code.addAload( 2 );
				// iconst_i // continue to aastore
				// growing stack is 1
				code.addIconst( i );
				final Class returnType = getter.getReturnType();
				int typeIndex = -1;
				if ( returnType.isPrimitive() ) {
					typeIndex = FactoryHelper.typeIndex( returnType );
					// new
					code.addNew( FactoryHelper.wrapperTypes[typeIndex] );
					// dup
					code.addOpcode( Opcode.DUP );
				}

				// aload_3 // load the raw bean
				code.addAload( 3 );
				final String getterSignature = RuntimeSupport.makeDescriptor( getter );
				final String getterName = getter.getName();
				if ( this.targetBean.isInterface() ) {
					// invokeinterface
					code.addInvokeinterface( targetBeanConstPoolIndex, getterName, getterSignature, 1 );
				}
				else {
					// invokevirtual
					code.addInvokevirtual( targetBeanConstPoolIndex, getterName, getterSignature );
				}

				if ( typeIndex >= 0 ) {
					// is a primitive type
					// invokespecial
					code.addInvokespecial(
							FactoryHelper.wrapperTypes[typeIndex],
							MethodInfo.nameInit,
							FactoryHelper.wrapperDesc[typeIndex]
					);
				}

				// aastore // args
				code.add( Opcode.AASTORE );
				code.growStack( -3 );
			}
		}
	}
	// return
	code.addOpcode( Opcode.RETURN );

	getterMethodInfo.setCodeAttribute( code.toCodeAttribute() );
	getterMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
	classfile.addMethod( getterMethodInfo );
}
 
Example 4
Source File: BulkAccessorFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void addGetter(ClassFile classfile, final Method[] getters) throws CannotCompileException {
	ConstPool cp = classfile.getConstPool();
	int target_type_index = cp.addClassInfo( this.targetBean.getName() );
	String desc = GET_SETTER_DESC;
	MethodInfo mi = new MethodInfo( cp, GENERATED_GETTER_NAME, desc );

	Bytecode code = new Bytecode( cp, 6, 4 );
	/* | this | bean | args | raw bean | */
	if ( getters.length >= 0 ) {
		// aload_1 // load bean
		code.addAload( 1 );
		// checkcast // cast bean
		code.addCheckcast( this.targetBean.getName() );
		// astore_3 // store bean
		code.addAstore( 3 );
		for ( int i = 0; i < getters.length; ++i ) {
			if ( getters[i] != null ) {
				Method getter = getters[i];
				// aload_2 // args
				code.addAload( 2 );
				// iconst_i // continue to aastore
				code.addIconst( i ); // growing stack is 1
				Class returnType = getter.getReturnType();
				int typeIndex = -1;
				if ( returnType.isPrimitive() ) {
					typeIndex = FactoryHelper.typeIndex( returnType );
					// new
					code.addNew( FactoryHelper.wrapperTypes[typeIndex] );
					// dup
					code.addOpcode( Opcode.DUP );
				}

				// aload_3 // load the raw bean
				code.addAload( 3 );
				String getter_desc = RuntimeSupport.makeDescriptor( getter );
				String getterName = getter.getName();
				if ( this.targetBean.isInterface() ) {
					// invokeinterface
					code.addInvokeinterface( target_type_index, getterName, getter_desc, 1 );
				}
				else {
					// invokevirtual
					code.addInvokevirtual( target_type_index, getterName, getter_desc );
				}

				if ( typeIndex >= 0 ) {       // is a primitive type
					// invokespecial
					code.addInvokespecial(
							FactoryHelper.wrapperTypes[typeIndex],
					        MethodInfo.nameInit,
					        FactoryHelper.wrapperDesc[typeIndex]
					);
				}

				// aastore // args
				code.add( Opcode.AASTORE );
				code.growStack( -3 );
			}
		}
	}
	// return
	code.addOpcode( Opcode.RETURN );

	mi.setCodeAttribute( code.toCodeAttribute() );
	mi.setAccessFlags( AccessFlag.PUBLIC );
	classfile.addMethod( mi );
}