本文整理汇总了Java中javassist.bytecode.Bytecode.addOpcode方法的典型用法代码示例。如果您正苦于以下问题:Java Bytecode.addOpcode方法的具体用法?Java Bytecode.addOpcode怎么用?Java Bytecode.addOpcode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javassist.bytecode.Bytecode
的用法示例。
在下文中一共展示了Bytecode.addOpcode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addDefaultConstructor
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
/**
* Declares a constructor that takes no parameter.
*
* @param classfile The class descriptor
*
* @throws CannotCompileException Indicates trouble with the underlying Javassist calls
*/
private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
final ConstPool constPool = classfile.getConstPool();
final String constructorSignature = "()V";
final MethodInfo constructorMethodInfo = new MethodInfo( constPool, MethodInfo.nameInit, constructorSignature );
final Bytecode code = new Bytecode( constPool, 0, 1 );
// aload_0
code.addAload( 0 );
// invokespecial
code.addInvokespecial( BulkAccessor.class.getName(), MethodInfo.nameInit, constructorSignature );
// return
code.addOpcode( Opcode.RETURN );
constructorMethodInfo.setCodeAttribute( code.toCodeAttribute() );
constructorMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
classfile.addMethod( constructorMethodInfo );
}
示例2: addDefaultConstructor
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
/**
* Declares a constructor that takes no parameter.
*
* @param classfile
* @throws CannotCompileException
*/
private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
ConstPool cp = classfile.getConstPool();
String cons_desc = "()V";
MethodInfo mi = new MethodInfo( cp, MethodInfo.nameInit, cons_desc );
Bytecode code = new Bytecode( cp, 0, 1 );
// aload_0
code.addAload( 0 );
// invokespecial
code.addInvokespecial( BulkAccessor.class.getName(), MethodInfo.nameInit, cons_desc );
// return
code.addOpcode( Opcode.RETURN );
mi.setCodeAttribute( code.toCodeAttribute() );
mi.setAccessFlags( AccessFlag.PUBLIC );
classfile.addMethod( mi );
}
示例3: addGetFieldHandlerMethod
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
private void addGetFieldHandlerMethod(ClassFile classfile)
throws CannotCompileException {
ConstPool cp = classfile.getConstPool();
int this_class_index = cp.getThisClassInfo();
MethodInfo minfo = new MethodInfo(cp, GETFIELDHANDLER_METHOD_NAME,
GETFIELDHANDLER_METHOD_DESCRIPTOR);
/* local variable | this | */
Bytecode code = new Bytecode(cp, 2, 1);
// aload_0 // load this
code.addAload(0);
// getfield // get field "$JAVASSIST_CALLBACK" defined already
code.addOpcode(Opcode.GETFIELD);
int field_index = cp.addFieldrefInfo(this_class_index,
HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR);
code.addIndex(field_index);
// areturn // return the value of the field
code.addOpcode(Opcode.ARETURN);
minfo.setCodeAttribute(code.toCodeAttribute());
minfo.setAccessFlags(AccessFlag.PUBLIC);
classfile.addMethod(minfo);
}
示例4: addSetFieldHandlerMethod
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
private void addSetFieldHandlerMethod(ClassFile classfile)
throws CannotCompileException {
ConstPool cp = classfile.getConstPool();
int this_class_index = cp.getThisClassInfo();
MethodInfo minfo = new MethodInfo(cp, SETFIELDHANDLER_METHOD_NAME,
SETFIELDHANDLER_METHOD_DESCRIPTOR);
/* local variables | this | callback | */
Bytecode code = new Bytecode(cp, 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);
int field_index = cp.addFieldrefInfo(this_class_index,
HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR);
code.addIndex(field_index);
// return
code.addOpcode(Opcode.RETURN);
minfo.setCodeAttribute(code.toCodeAttribute());
minfo.setAccessFlags(AccessFlag.PUBLIC);
classfile.addMethod(minfo);
}
示例5: addTypeDependDataReturn
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
private static void addTypeDependDataReturn(Bytecode code, String typeName) {
if ((typeName.charAt(0) == 'L')
&& (typeName.charAt(typeName.length() - 1) == ';')
|| (typeName.charAt(0) == '[')) {
// reference type
code.addOpcode(Opcode.ARETURN);
} else if (typeName.equals("Z") || typeName.equals("B")
|| typeName.equals("C") || typeName.equals("I")
|| typeName.equals("S")) {
// boolean, byte, char, int, short
code.addOpcode(Opcode.IRETURN);
} else if (typeName.equals("D")) {
// double
code.addOpcode(Opcode.DRETURN);
} else if (typeName.equals("F")) {
// float
code.addOpcode(Opcode.FRETURN);
} else if (typeName.equals("J")) {
// long
code.addOpcode(Opcode.LRETURN);
} else {
// bad type
throw new RuntimeException("bad type: " + typeName);
}
}
示例6: addGetFieldHandlerMethod
import javassist.bytecode.Bytecode; //导入方法依赖的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 );
}
示例7: addSetFieldHandlerMethod
import javassist.bytecode.Bytecode; //导入方法依赖的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 );
}
示例8: addTypeDependDataReturn
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
private static void addTypeDependDataReturn(Bytecode code, String typeName) {
if ( typeName.charAt( 0 ) == 'L'
&& typeName.charAt( typeName.length() - 1 ) == ';'
|| typeName.charAt( 0 ) == '[') {
// reference type
code.addOpcode( Opcode.ARETURN );
}
else if ( typeName.equals( "Z" )
|| typeName.equals( "B" )
|| typeName.equals( "C" )
|| typeName.equals( "I" )
|| typeName.equals( "S" ) ) {
// boolean, byte, char, int, short
code.addOpcode( Opcode.IRETURN );
}
else if ( typeName.equals( "D" ) ) {
// double
code.addOpcode( Opcode.DRETURN );
}
else if ( typeName.equals( "F" ) ) {
// float
code.addOpcode( Opcode.FRETURN );
}
else if ( typeName.equals( "J" ) ) {
// long
code.addOpcode( Opcode.LRETURN );
}
else {
// bad type
throw new RuntimeException( "bad type: " + typeName );
}
}
示例9: storeStack
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
static final void storeStack(CtClass[] params, boolean isStaticCall,
int regno, Bytecode bytecode) {
storeStack0(0, params.length, params, regno + 1, bytecode);
if (isStaticCall)
bytecode.addOpcode(ACONST_NULL);
bytecode.addAstore(regno);
}
示例10: makeDefaultBody
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
private static void makeDefaultBody(Bytecode b, CtClass type) {
int op;
int value;
if (type instanceof CtPrimitiveType) {
CtPrimitiveType pt = (CtPrimitiveType)type;
op = pt.getReturnOp();
if (op == Opcode.DRETURN)
value = Opcode.DCONST_0;
else if (op == Opcode.FRETURN)
value = Opcode.FCONST_0;
else if (op == Opcode.LRETURN)
value = Opcode.LCONST_0;
else if (op == Opcode.RETURN)
value = Opcode.NOP;
else
value = Opcode.ICONST_0;
}
else {
op = Opcode.ARETURN;
value = Opcode.ACONST_NULL;
}
if (value != Opcode.NOP)
b.addOpcode(value);
b.addOpcode(op);
}
示例11: addGetter
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
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 );
}
示例12: delegateNewArray
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
/**
* Replaces creation of the array with the creation of the <em>KoratArray</em>
* field. Just invokes its constructor with the <code>int</code> parameter
* that represents the length of the array.
*
* @param arrayType -
* type of array in terms of JVM
* @param cit -
* iterator through method's body code
* @param idx -
* current position in the <code>cit</code>
* @throws BadBytecode
*/
private void delegateNewArray(int arrayType, CodeIterator cit, int idx) {
// check how this new array is going to be consumed
if (operandStack.peek().consumedBy != ConsumedByKind.PUTFIELD)
return;
String koratClassName = koratArrayClassNames[arrayType];
int locals = cit.get().getMaxLocals();
int localVar1 = locals + 1;
try {
Bytecode codes = new Bytecode(constPool);
codes.addIstore(localVar1);
codes.addNew(koratClassName);
codes.addOpcode(Opcode.DUP);
codes.addIload(localVar1);
String descr = Descriptor.ofMethod(CtClass.voidType,
new CtClass[] { CtClass.intType });
codes.addInvokespecial(koratClassName, "<init>", descr);
int diff = codes.getSize() - 2;
// -2 because original instruction
// (NEWARRAY) is 2 bytes long.
cit.insertGap(idx, diff);
cit.write(codes.get(), idx);
cit.get().setMaxLocals(localVar1 + 1);
} catch (BadBytecode e) {
throw new RuntimeException(e);
}
}
示例13: addGetter
import javassist.bytecode.Bytecode; //导入方法依赖的package包/类
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 );
}
示例14: addReadMethod
import javassist.bytecode.Bytecode; //导入方法依赖的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 );
}
示例15: addWriteMethod
import javassist.bytecode.Bytecode; //导入方法依赖的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 );
}