本文整理汇总了Java中javassist.bytecode.Opcode类的典型用法代码示例。如果您正苦于以下问题:Java Opcode类的具体用法?Java Opcode怎么用?Java Opcode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Opcode类属于javassist.bytecode包,在下文中一共展示了Opcode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addDefaultConstructor
import javassist.bytecode.Opcode; //导入依赖的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: transformInvokevirtualsIntoGetfields
import javassist.bytecode.Opcode; //导入依赖的package包/类
private int transformInvokevirtualsIntoGetfields(ClassFile classfile, CodeIterator iter, int pos) {
final ConstPool constPool = classfile.getConstPool();
final int c = iter.byteAt( pos );
if ( c != Opcode.GETFIELD ) {
return pos;
}
final int index = iter.u16bitAt( pos + 1 );
final String fieldName = constPool.getFieldrefName( index );
final String className = constPool.getFieldrefClassName( index );
if ( !filter.handleReadAccess( className, fieldName ) ) {
return pos;
}
final String fieldReaderMethodDescriptor = "()" + constPool.getFieldrefType( index );
final int fieldReaderMethodIndex = constPool.addMethodrefInfo(
constPool.getThisClassInfo(),
EACH_READ_METHOD_PREFIX + fieldName,
fieldReaderMethodDescriptor
);
iter.writeByte( Opcode.INVOKEVIRTUAL, pos );
iter.write16bit( fieldReaderMethodIndex, pos + 1 );
return pos;
}
示例3: transformInvokevirtualsIntoPutfields
import javassist.bytecode.Opcode; //导入依赖的package包/类
private int transformInvokevirtualsIntoPutfields(ClassFile classfile, CodeIterator iter, int pos) {
final ConstPool constPool = classfile.getConstPool();
final int c = iter.byteAt( pos );
if ( c != Opcode.PUTFIELD ) {
return pos;
}
final int index = iter.u16bitAt( pos + 1 );
final String fieldName = constPool.getFieldrefName( index );
final String className = constPool.getFieldrefClassName( index );
if ( !filter.handleWriteAccess( className, fieldName ) ) {
return pos;
}
final String fieldWriterMethodDescriptor = "(" + constPool.getFieldrefType( index ) + ")V";
final int fieldWriterMethodIndex = constPool.addMethodrefInfo(
constPool.getThisClassInfo(),
EACH_WRITE_METHOD_PREFIX + fieldName,
fieldWriterMethodDescriptor
);
iter.writeByte( Opcode.INVOKEVIRTUAL, pos );
iter.write16bit( fieldWriterMethodIndex, pos + 1 );
return pos;
}
示例4: getNext
import javassist.bytecode.Opcode; //导入依赖的package包/类
private CheckCast getNext() throws BadBytecode {
int prevPos = 0;
while (m_iter.hasNext()) {
int pos = m_iter.next();
int opcode = m_iter.byteAt(pos);
switch (opcode) {
case Opcode.CHECKCAST:
// get the type of this op code (next two bytes are a classinfo index)
MethodEntry prevMethodEntry = getMethodEntry(prevPos);
if (prevMethodEntry != null) {
return new CheckCast(m_constants.getClassInfo(m_iter.s16bitAt(pos + 1)), prevMethodEntry);
}
break;
}
prevPos = pos;
}
return null;
}
示例5: addDefaultConstructor
import javassist.bytecode.Opcode; //导入依赖的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 );
}
示例6: addGetFieldHandlerMethod
import javassist.bytecode.Opcode; //导入依赖的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);
}
示例7: addSetFieldHandlerMethod
import javassist.bytecode.Opcode; //导入依赖的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);
}
示例8: transformInvokevirtualsIntoGetfields
import javassist.bytecode.Opcode; //导入依赖的package包/类
private int transformInvokevirtualsIntoGetfields(ClassFile classfile,
CodeIterator iter, int pos) {
ConstPool cp = classfile.getConstPool();
int c = iter.byteAt(pos);
if (c != Opcode.GETFIELD) {
return pos;
}
int index = iter.u16bitAt(pos + 1);
String fieldName = cp.getFieldrefName(index);
String className = cp.getFieldrefClassName(index);
if ((!classfile.getName().equals(className))
|| (!readableFields.containsKey(fieldName))) {
return pos;
}
String desc = "()" + (String) readableFields.get(fieldName);
int read_method_index = cp.addMethodrefInfo(cp.getThisClassInfo(),
EACH_READ_METHOD_PREFIX + fieldName, desc);
iter.writeByte(Opcode.INVOKEVIRTUAL, pos);
iter.write16bit(read_method_index, pos + 1);
return pos;
}
示例9: transformInvokevirtualsIntoPutfields
import javassist.bytecode.Opcode; //导入依赖的package包/类
private int transformInvokevirtualsIntoPutfields(ClassFile classfile,
CodeIterator iter, int pos) {
ConstPool cp = classfile.getConstPool();
int c = iter.byteAt(pos);
if (c != Opcode.PUTFIELD) {
return pos;
}
int index = iter.u16bitAt(pos + 1);
String fieldName = cp.getFieldrefName(index);
String className = cp.getFieldrefClassName(index);
if ((!classfile.getName().equals(className))
|| (!writableFields.containsKey(fieldName))) {
return pos;
}
String desc = "(" + (String) writableFields.get(fieldName) + ")V";
int write_method_index = cp.addMethodrefInfo(cp.getThisClassInfo(),
EACH_WRITE_METHOD_PREFIX + fieldName, desc);
iter.writeByte(Opcode.INVOKEVIRTUAL, pos);
iter.write16bit(write_method_index, pos + 1);
return pos;
}
示例10: addTypeDependDataReturn
import javassist.bytecode.Opcode; //导入依赖的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);
}
}
示例11: getNext
import javassist.bytecode.Opcode; //导入依赖的package包/类
private CheckCast getNext() throws BadBytecode
{
int prevPos = 0;
while(m_iter.hasNext())
{
int pos = m_iter.next();
int opcode = m_iter.byteAt(pos);
switch(opcode)
{
case Opcode.CHECKCAST:
// get the type of this op code (next two bytes are a
// classinfo index)
MethodEntry prevMethodEntry = getMethodEntry(prevPos);
if(prevMethodEntry != null)
return new CheckCast(m_constants.getClassInfo(m_iter
.s16bitAt(pos + 1)), prevMethodEntry);
break;
}
prevPos = pos;
}
return null;
}
示例12: edit
import javassist.bytecode.Opcode; //导入依赖的package包/类
/**
* Edits a method call.
*
* @param mc the method call
* @throws CannotCompileException in case that compile errors occur
*/
public void edit(MethodCall mc) throws CannotCompileException {
if (mc.getMethodName().equals("byteAt")) {
try {
String opcode = Opcode.class.getName();
CtMethod method = mc.getMethod();
CtClass declaring = method.getDeclaringClass();
if (declaring.getName().equals(
CodeIterator.class.getName())) {
mc.replace("$_ = $proceed($$); "
+ "if (disableInstanceof && $_=="
+ opcode + ".INSTANCEOF)"
+ "{return false;}"
+ "if (disableCast && $_=="
+ opcode + ".CHECKCAST)"
+ "{return false;}");
}
} catch (NotFoundException e) {
throw new CannotCompileException(e);
}
}
}
示例13: addGetFieldHandlerMethod
import javassist.bytecode.Opcode; //导入依赖的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 );
}
示例14: addSetFieldHandlerMethod
import javassist.bytecode.Opcode; //导入依赖的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 );
}
示例15: addTypeDependDataReturn
import javassist.bytecode.Opcode; //导入依赖的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 );
}
}