本文整理汇总了Java中javassist.bytecode.Bytecode类的典型用法代码示例。如果您正苦于以下问题:Java Bytecode类的具体用法?Java Bytecode怎么用?Java Bytecode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Bytecode类属于javassist.bytecode包,在下文中一共展示了Bytecode类的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: modifyClassConstructor
import javassist.bytecode.Bytecode; //导入依赖的package包/类
private void modifyClassConstructor(ClassFile cf)
throws CannotCompileException, NotFoundException
{
if (fieldInitializers == null)
return;
Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
Javac jv = new Javac(code, this);
int stacksize = 0;
boolean doInit = false;
for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
CtField f = fi.field;
if (Modifier.isStatic(f.getModifiers())) {
doInit = true;
int s = fi.init.compileIfStatic(f.getType(), f.getName(),
code, jv);
if (stacksize < s)
stacksize = s;
}
}
if (doInit) // need an initializer for static fileds.
modifyClassConstructor(cf, code, stacksize, 0);
}
示例3: insertAuxInitializer
import javassist.bytecode.Bytecode; //导入依赖的package包/类
private static void insertAuxInitializer(CodeAttribute codeAttr,
Bytecode initializer,
int stacksize)
throws BadBytecode
{
CodeIterator it = codeAttr.iterator();
int index = it.skipSuperConstructor();
if (index < 0) {
index = it.skipThisConstructor();
if (index >= 0)
return; // this() is called.
// Neither this() or super() is called.
}
int pos = it.insertEx(initializer.get());
it.insert(initializer.getExceptionTable(), pos);
int maxstack = codeAttr.getMaxStack();
if (maxstack < stacksize)
codeAttr.setMaxStack(stacksize);
}
示例4: makeFieldInitializer
import javassist.bytecode.Bytecode; //导入依赖的package包/类
private int makeFieldInitializer(Bytecode code, CtClass[] parameters)
throws CannotCompileException, NotFoundException
{
int stacksize = 0;
Javac jv = new Javac(code, this);
try {
jv.recordParams(parameters, false);
}
catch (CompileError e) {
throw new CannotCompileException(e);
}
for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
CtField f = fi.field;
if (!Modifier.isStatic(f.getModifiers())) {
int s = fi.init.compile(f.getType(), f.getName(), code,
parameters, jv);
if (stacksize < s)
stacksize = s;
}
}
return stacksize;
}
示例5: storeStack0
import javassist.bytecode.Bytecode; //导入依赖的package包/类
private static void storeStack0(int i, int n, CtClass[] params, int regno,
Bytecode bytecode) {
if (i >= n)
return;
else {
CtClass c = params[i];
int size;
if (c instanceof CtPrimitiveType)
size = ((CtPrimitiveType)c).getDataSize();
else
size = 1;
storeStack0(i + 1, n, params, regno + size, bytecode);
bytecode.addStore(regno, c);
}
}
示例6: replace0
import javassist.bytecode.Bytecode; //导入依赖的package包/类
protected void replace0(int pos, Bytecode bytecode, int size)
throws BadBytecode {
byte[] code = bytecode.get();
edited = true;
int gap = code.length - size;
for (int i = 0; i < size; ++i)
iterator.writeByte(NOP, pos + i);
if (gap > 0)
pos = iterator.insertGapAt(pos, gap, false).position;
iterator.write(code, pos);
iterator.insert(bytecode.getExceptionTable(), pos);
maxLocals = bytecode.getMaxLocals();
maxStack = bytecode.getMaxStack();
}
示例7: 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 );
}
示例8: 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);
}
示例9: 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);
}
示例10: addTypeDependDataLoad
import javassist.bytecode.Bytecode; //导入依赖的package包/类
private static void addTypeDependDataLoad(Bytecode code, String typeName,
int i) {
if ((typeName.charAt(0) == 'L')
&& (typeName.charAt(typeName.length() - 1) == ';')
|| (typeName.charAt(0) == '[')) {
// reference type
code.addAload(i);
} else if (typeName.equals("Z") || typeName.equals("B")
|| typeName.equals("C") || typeName.equals("I")
|| typeName.equals("S")) {
// boolean, byte, char, int, short
code.addIload(i);
} else if (typeName.equals("D")) {
// double
code.addDload(i);
} else if (typeName.equals("F")) {
// float
code.addFload(i);
} else if (typeName.equals("J")) {
// long
code.addLload(i);
} else {
// bad type
throw new RuntimeException("bad type: " + typeName);
}
}
示例11: addTypeDependDataStore
import javassist.bytecode.Bytecode; //导入依赖的package包/类
private static void addTypeDependDataStore(Bytecode code, String typeName,
int i) {
if ((typeName.charAt(0) == 'L')
&& (typeName.charAt(typeName.length() - 1) == ';')
|| (typeName.charAt(0) == '[')) {
// reference type
code.addAstore(i);
} else if (typeName.equals("Z") || typeName.equals("B")
|| typeName.equals("C") || typeName.equals("I")
|| typeName.equals("S")) {
// boolean, byte, char, int, short
code.addIstore(i);
} else if (typeName.equals("D")) {
// double
code.addDstore(i);
} else if (typeName.equals("F")) {
// float
code.addFstore(i);
} else if (typeName.equals("J")) {
// long
code.addLstore(i);
} else {
// bad type
throw new RuntimeException("bad type: " + typeName);
}
}
示例12: 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);
}
}
示例13: addUnwrapper
import javassist.bytecode.Bytecode; //导入依赖的package包/类
private void addUnwrapper(Bytecode code, Class type) {
final int index = FactoryHelper.typeIndex( type );
final String wrapperType = FactoryHelper.wrapperTypes[index];
// checkcast
code.addCheckcast( wrapperType );
// invokevirtual
code.addInvokevirtual( wrapperType, FactoryHelper.unwarpMethods[index], FactoryHelper.unwrapDesc[index] );
}
示例14: 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 );
}
示例15: 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 );
}