本文整理汇总了Java中javassist.bytecode.CodeIterator类的典型用法代码示例。如果您正苦于以下问题:Java CodeIterator类的具体用法?Java CodeIterator怎么用?Java CodeIterator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CodeIterator类属于javassist.bytecode包,在下文中一共展示了CodeIterator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: transformInvokevirtualsIntoPutAndGetfields
import javassist.bytecode.CodeIterator; //导入依赖的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 );
}
}
示例2: transformInvokevirtualsIntoGetfields
import javassist.bytecode.CodeIterator; //导入依赖的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.CodeIterator; //导入依赖的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: scan
import javassist.bytecode.CodeIterator; //导入依赖的package包/类
public Subroutine[] scan(MethodInfo method) throws BadBytecode {
CodeAttribute code = method.getCodeAttribute();
CodeIterator iter = code.iterator();
subroutines = new Subroutine[code.getCodeLength()];
subTable.clear();
done.clear();
scan(0, iter, null);
ExceptionTable exceptions = code.getExceptionTable();
for (int i = 0; i < exceptions.size(); i++) {
int handler = exceptions.handlerPc(i);
// If an exception is thrown in subroutine, the handler
// is part of the same subroutine.
scan(handler, iter, subroutines[exceptions.startPc(i)]);
}
return subroutines;
}
示例5: evalNewObjectArray
import javassist.bytecode.CodeIterator; //导入依赖的package包/类
private void evalNewObjectArray(int pos, CodeIterator iter, Frame frame) throws BadBytecode {
// Convert to x[] format
Type type = resolveClassInfo(constPool.getClassInfo(iter.u16bitAt(pos + 1)));
String name = type.getCtClass().getName();
int opcode = iter.byteAt(pos);
int dimensions;
if (opcode == MULTIANEWARRAY) {
dimensions = iter.byteAt(pos + 3);
} else {
name = name + "[]";
dimensions = 1;
}
while (dimensions-- > 0) {
verifyAssignable(Type.INTEGER, simplePop(frame));
}
simplePush(getType(name), frame);
}
示例6: insertAuxInitializer
import javassist.bytecode.CodeIterator; //导入依赖的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);
}
示例7: runEditor
import javassist.bytecode.CodeIterator; //导入依赖的package包/类
protected void runEditor(ExprEditor ed, CodeIterator oldIterator)
throws CannotCompileException
{
CodeAttribute codeAttr = oldIterator.get();
int orgLocals = codeAttr.getMaxLocals();
int orgStack = codeAttr.getMaxStack();
int newLocals = locals();
codeAttr.setMaxStack(stack());
codeAttr.setMaxLocals(newLocals);
ExprEditor.LoopContext context
= new ExprEditor.LoopContext(newLocals);
int size = oldIterator.getCodeLength();
int endPos = oldIterator.lookAhead();
oldIterator.move(currentPos);
if (ed.doit(thisClass, thisMethod, context, oldIterator, endPos))
edited = true;
oldIterator.move(endPos + oldIterator.getCodeLength() - size);
codeAttr.setMaxLocals(orgLocals);
codeAttr.setMaxStack(orgStack);
maxLocals = context.maxLocals;
maxStack += context.maxStack;
}
示例8: getBehaviourBytecode
import javassist.bytecode.CodeIterator; //导入依赖的package包/类
/**
* This is basically the InstructionPrinter.getMethodBytecode but with a
* CtBehaviour parameter instead of a CtMethod
*
* @param behavior
* @return
*/
public static String getBehaviourBytecode(CtBehavior behavior) {
MethodInfo info = behavior.getMethodInfo2();
CodeAttribute code = info.getCodeAttribute();
if (code == null) {
return "";
}
ConstPool pool = info.getConstPool();
StringBuilder sb = new StringBuilder(1024);
CodeIterator iterator = code.iterator();
while (iterator.hasNext()) {
int pos;
try {
pos = iterator.next();
} catch (BadBytecode e) {
throw new JVoidIntrumentationException("BadBytecoode", e);
}
sb.append(pos + ": " + InstructionPrinter.instructionString(iterator, pos, pool) + "\n");
}
return sb.toString();
}
示例9: transformInvokevirtualsIntoGetfields
import javassist.bytecode.CodeIterator; //导入依赖的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;
}
示例10: transformInvokevirtualsIntoPutfields
import javassist.bytecode.CodeIterator; //导入依赖的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;
}
示例11: edit
import javassist.bytecode.CodeIterator; //导入依赖的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);
}
}
}
示例12: initialize
import javassist.bytecode.CodeIterator; //导入依赖的package包/类
public void initialize(ConstPool cp, CtClass clazz, MethodInfo minfo) throws CannotCompileException {
/*
* This transformer must be isolated from other transformers, since some
* of them affect the local variable and stack maximums without updating
* the code attribute to reflect the changes. This screws up the
* data-flow analyzer, since it relies on consistent code state. Even
* if the attribute values were updated correctly, we would have to
* detect it, and redo analysis, which is not cheap. Instead, we are
* better off doing all changes in initialize() before everyone else has
* a chance to muck things up.
*/
CodeIterator iterator = minfo.getCodeAttribute().iterator();
while (iterator.hasNext()) {
try {
int pos = iterator.next();
int c = iterator.byteAt(pos);
if (c == AALOAD)
initFrames(clazz, minfo);
if (c == AALOAD || c == BALOAD || c == CALOAD || c == DALOAD
|| c == FALOAD || c == IALOAD || c == LALOAD
|| c == SALOAD) {
pos = replace(cp, iterator, pos, c, getLoadReplacementSignature(c));
} else if (c == AASTORE || c == BASTORE || c == CASTORE
|| c == DASTORE || c == FASTORE || c == IASTORE
|| c == LASTORE || c == SASTORE) {
pos = replace(cp, iterator, pos, c, getStoreReplacementSignature(c));
}
} catch (Exception e) {
throw new CannotCompileException(e);
}
}
}
示例13: replace
import javassist.bytecode.CodeIterator; //导入依赖的package包/类
private int replace(ConstPool cp, CodeIterator iterator, int pos,
int opcode, String signature) throws BadBytecode {
String castType = null;
String methodName = getMethodName(opcode);
if (methodName != null) {
// See if the object must be cast
if (opcode == AALOAD) {
castType = getTopType(iterator.lookAhead());
// Do not replace an AALOAD instruction that we do not have a type for
// This happens when the state is guaranteed to be null (Type.UNINIT)
// So we don't really care about this case.
if (castType == null)
return pos;
if ("java/lang/Object".equals(castType))
castType = null;
}
// The gap may include extra padding
// Write a nop in case the padding pushes the instruction forward
iterator.writeByte(NOP, pos);
CodeIterator.Gap gap
= iterator.insertGapAt(pos, castType != null ? 5 : 2, false);
pos = gap.position;
int mi = cp.addClassInfo(methodClassname);
int methodref = cp.addMethodrefInfo(mi, methodName, signature);
iterator.writeByte(INVOKESTATIC, pos);
iterator.write16bit(methodref, pos + 1);
if (castType != null) {
int index = cp.addClassInfo(castType);
iterator.writeByte(CHECKCAST, pos + 3);
iterator.write16bit(index, pos + 4);
}
pos = updatePos(pos, gap.length);
}
return pos;
}
示例14: scanLookupSwitch
import javassist.bytecode.CodeIterator; //导入依赖的package包/类
private void scanLookupSwitch(int pos, CodeIterator iter, Subroutine sub) throws BadBytecode {
int index = (pos & ~3) + 4;
// default
scan(pos + iter.s32bitAt(index), iter, sub);
int npairs = iter.s32bitAt(index += 4);
int end = npairs * 8 + (index += 4);
// skip "match"
for (index += 4; index < end; index += 8) {
int target = iter.s32bitAt(index) + pos;
scan(target, iter, sub);
}
}
示例15: scanTableSwitch
import javassist.bytecode.CodeIterator; //导入依赖的package包/类
private void scanTableSwitch(int pos, CodeIterator iter, Subroutine sub) throws BadBytecode {
// Skip 4 byte alignment padding
int index = (pos & ~3) + 4;
// default
scan(pos + iter.s32bitAt(index), iter, sub);
int low = iter.s32bitAt(index += 4);
int high = iter.s32bitAt(index += 4);
int end = (high - low + 1) * 4 + (index += 4);
// Offset table
for (; index < end; index += 4) {
int target = iter.s32bitAt(index) + pos;
scan(target, iter, sub);
}
}