当前位置: 首页>>代码示例>>Java>>正文


Java CodeAttribute.setAttribute方法代码示例

本文整理汇总了Java中javassist.bytecode.CodeAttribute.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java CodeAttribute.setAttribute方法的具体用法?Java CodeAttribute.setAttribute怎么用?Java CodeAttribute.setAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javassist.bytecode.CodeAttribute的用法示例。


在下文中一共展示了CodeAttribute.setAttribute方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: transformInvokevirtualsIntoPutAndGetfields

import javassist.bytecode.CodeAttribute; //导入方法依赖的package包/类
private void transformInvokevirtualsIntoPutAndGetfields(ClassFile classfile) throws CannotCompileException, BadBytecode {
	for ( Object o : classfile.getMethods() ) {
		final MethodInfo methodInfo = (MethodInfo) o;
		final String methodName = methodInfo.getName();
		if ( methodName.startsWith( EACH_READ_METHOD_PREFIX )
				|| methodName.startsWith( EACH_WRITE_METHOD_PREFIX )
				|| methodName.equals( GETFIELDHANDLER_METHOD_NAME )
				|| methodName.equals( SETFIELDHANDLER_METHOD_NAME ) ) {
			continue;
		}

		final CodeAttribute codeAttr = methodInfo.getCodeAttribute();
		if ( codeAttr == null ) {
			continue;
		}

		final CodeIterator iter = codeAttr.iterator();
		while ( iter.hasNext() ) {
			int pos = iter.next();
			pos = transformInvokevirtualsIntoGetfields( classfile, iter, pos );
			transformInvokevirtualsIntoPutfields( classfile, iter, pos );
		}
		final StackMapTable smt = MapMaker.make( classPool, methodInfo );
		codeAttr.setAttribute( smt );
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:27,代码来源:FieldTransformer.java

示例2: addGetFieldHandlerMethod

import javassist.bytecode.CodeAttribute; //导入方法依赖的package包/类
private void addGetFieldHandlerMethod(ClassFile classfile) throws CannotCompileException, BadBytecode {
	final ConstPool constPool = classfile.getConstPool();
	final int thisClassInfo = constPool.getThisClassInfo();
	final MethodInfo getterMethodInfo = new MethodInfo(
			constPool,
			GETFIELDHANDLER_METHOD_NAME,
			GETFIELDHANDLER_METHOD_DESCRIPTOR
	);

	/* local variable | this | */
	final Bytecode code = new Bytecode( constPool, 2, 1 );
	// aload_0 // load this
	code.addAload( 0 );
	// getfield // get field "$JAVASSIST_CALLBACK" defined already
	code.addOpcode( Opcode.GETFIELD );
	final int fieldIndex = constPool.addFieldrefInfo( thisClassInfo, HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR );
	code.addIndex( fieldIndex );
	// areturn // return the value of the field
	code.addOpcode( Opcode.ARETURN );
	getterMethodInfo.setCodeAttribute( code.toCodeAttribute() );
	getterMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
	final CodeAttribute codeAttribute = getterMethodInfo.getCodeAttribute();
	if ( codeAttribute != null ) {
		final StackMapTable smt = MapMaker.make( classPool, getterMethodInfo );
		codeAttribute.setAttribute( smt );
	}
	classfile.addMethod( getterMethodInfo );
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:29,代码来源:FieldTransformer.java

示例3: addSetFieldHandlerMethod

import javassist.bytecode.CodeAttribute; //导入方法依赖的package包/类
private void addSetFieldHandlerMethod(ClassFile classfile) throws CannotCompileException, BadBytecode {
	final ConstPool constPool = classfile.getConstPool();
	final int thisClassInfo = constPool.getThisClassInfo();
	final MethodInfo methodInfo = new MethodInfo(
			constPool,
			SETFIELDHANDLER_METHOD_NAME,
			SETFIELDHANDLER_METHOD_DESCRIPTOR
	);

	/* local variables | this | callback | */
	final Bytecode code = new Bytecode(constPool, 3, 3);
	// aload_0 : load this
	code.addAload( 0 );
	// aload_1 : load callback
	code.addAload( 1 );
	// putfield // put field "$JAVASSIST_CALLBACK" defined already
	code.addOpcode( Opcode.PUTFIELD );
	final int fieldIndex = constPool.addFieldrefInfo( thisClassInfo, HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR );
	code.addIndex( fieldIndex );
	// return
	code.addOpcode( Opcode.RETURN );
	methodInfo.setCodeAttribute( code.toCodeAttribute() );
	methodInfo.setAccessFlags( AccessFlag.PUBLIC );
	final CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
	if ( codeAttribute != null ) {
		final StackMapTable smt = MapMaker.make( classPool, methodInfo );
		codeAttribute.setAttribute( smt );
	}
	classfile.addMethod( methodInfo );
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:31,代码来源:FieldTransformer.java

示例4: addReadMethod

import javassist.bytecode.CodeAttribute; //导入方法依赖的package包/类
private void addReadMethod(ClassFile classfile, FieldInfo finfo) throws CannotCompileException, BadBytecode {
	final ConstPool constPool = classfile.getConstPool();
	final int thisClassInfo = constPool.getThisClassInfo();
	final String readMethodDescriptor = "()" + finfo.getDescriptor();
	final MethodInfo readMethodInfo = new MethodInfo(
			constPool,
			EACH_READ_METHOD_PREFIX + finfo.getName(),
			readMethodDescriptor
	);

	/* local variables | target obj | each oldvalue | */
	final Bytecode code = new Bytecode(constPool, 5, 3);
	// aload_0
	code.addAload( 0 );
	// getfield // get each field
	code.addOpcode( Opcode.GETFIELD );
	final int baseFieldIndex = constPool.addFieldrefInfo( thisClassInfo, finfo.getName(), finfo.getDescriptor() );
	code.addIndex( baseFieldIndex );
	// aload_0
	code.addAload( 0 );
	// invokeinterface : invoke Enabled.getInterceptFieldCallback()
	final int enabledClassIndex = constPool.addClassInfo( FIELD_HANDLED_TYPE_NAME );
	code.addInvokeinterface(
			enabledClassIndex,
			GETFIELDHANDLER_METHOD_NAME,
			GETFIELDHANDLER_METHOD_DESCRIPTOR,
			1
	);
	// ifnonnull
	code.addOpcode( Opcode.IFNONNULL );
	code.addIndex( 4 );
	// *return // each type
	addTypeDependDataReturn( code, finfo.getDescriptor() );
	// *store_1 // each type
	addTypeDependDataStore( code, finfo.getDescriptor(), 1 );
	// aload_0
	code.addAload( 0 );
	// invokeinterface // invoke Enabled.getInterceptFieldCallback()
	code.addInvokeinterface(
			enabledClassIndex,
			GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
			1
	);
	// aload_0
	code.addAload( 0 );
	// ldc // name of the field
	code.addLdc( finfo.getName() );
	// *load_1 // each type
	addTypeDependDataLoad( code, finfo.getDescriptor(), 1 );
	// invokeinterface // invoke Callback.read*() // each type
	addInvokeFieldHandlerMethod(
			classfile, code, finfo.getDescriptor(),
			true
	);
	// *return // each type
	addTypeDependDataReturn( code, finfo.getDescriptor() );

	readMethodInfo.setCodeAttribute( code.toCodeAttribute() );
	readMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
	final CodeAttribute codeAttribute = readMethodInfo.getCodeAttribute();
	if ( codeAttribute != null ) {
		final StackMapTable smt = MapMaker.make( classPool, readMethodInfo );
		codeAttribute.setAttribute( smt );
	}
	classfile.addMethod( readMethodInfo );
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:67,代码来源:FieldTransformer.java

示例5: addWriteMethod

import javassist.bytecode.CodeAttribute; //导入方法依赖的package包/类
private void addWriteMethod(ClassFile classfile, FieldInfo finfo) throws CannotCompileException, BadBytecode {
	final ConstPool constPool = classfile.getConstPool();
	final int thisClassInfo = constPool.getThisClassInfo();
	final String writeMethodDescriptor = "(" + finfo.getDescriptor() + ")V";
	final MethodInfo writeMethodInfo = new MethodInfo(
			constPool,
			EACH_WRITE_METHOD_PREFIX+ finfo.getName(),
			writeMethodDescriptor
	);

	/* local variables | target obj | each oldvalue | */
	final Bytecode code = new Bytecode(constPool, 6, 3);
	// aload_0
	code.addAload( 0 );
	// invokeinterface : enabled.getInterceptFieldCallback()
	final int enabledClassIndex = constPool.addClassInfo( FIELD_HANDLED_TYPE_NAME );
	code.addInvokeinterface(
			enabledClassIndex,
			GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
			1
	);
	// ifnonnull (label1)
	code.addOpcode( Opcode.IFNONNULL );
	code.addIndex( 9 );
	// aload_0
	code.addAload( 0 );
	// *load_1
	addTypeDependDataLoad( code, finfo.getDescriptor(), 1 );
	// putfield
	code.addOpcode( Opcode.PUTFIELD );
	final int baseFieldIndex = constPool.addFieldrefInfo( thisClassInfo, finfo.getName(), finfo.getDescriptor() );
	code.addIndex( baseFieldIndex );
	code.growStack( -Descriptor.dataSize( finfo.getDescriptor() ) );
	// return ;
	code.addOpcode( Opcode.RETURN );
	// aload_0
	code.addAload( 0 );
	// dup
	code.addOpcode( Opcode.DUP );
	// invokeinterface // enabled.getInterceptFieldCallback()
	code.addInvokeinterface(
			enabledClassIndex,
			GETFIELDHANDLER_METHOD_NAME,
			GETFIELDHANDLER_METHOD_DESCRIPTOR,
			1
	);
	// aload_0
	code.addAload( 0 );
	// ldc // field name
	code.addLdc( finfo.getName() );
	// aload_0
	code.addAload( 0 );
	// getfield // old value of the field
	code.addOpcode( Opcode.GETFIELD );
	code.addIndex( baseFieldIndex );
	code.growStack( Descriptor.dataSize( finfo.getDescriptor() ) - 1 );
	// *load_1
	addTypeDependDataLoad( code, finfo.getDescriptor(), 1 );
	// invokeinterface // callback.write*(..)
	addInvokeFieldHandlerMethod( classfile, code, finfo.getDescriptor(), false );
	// putfield // new value of the field
	code.addOpcode( Opcode.PUTFIELD );
	code.addIndex( baseFieldIndex );
	code.growStack( -Descriptor.dataSize( finfo.getDescriptor() ) );
	// return
	code.addOpcode( Opcode.RETURN );

	writeMethodInfo.setCodeAttribute( code.toCodeAttribute() );
	writeMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
	final CodeAttribute codeAttribute = writeMethodInfo.getCodeAttribute();
	if ( codeAttribute != null ) {
		final StackMapTable smt = MapMaker.make( classPool, writeMethodInfo );
		codeAttribute.setAttribute( smt );
	}
	classfile.addMethod( writeMethodInfo );
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:77,代码来源:FieldTransformer.java


注:本文中的javassist.bytecode.CodeAttribute.setAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。