javassist.bytecode.CodeIterator Java Examples

The following examples show how to use javassist.bytecode.CodeIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
}
 
Example #2
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
}
 
Example #3
Source File: JavasisstUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenJavaClass_whenLoadAtByJavassist_thenTraversWholeClass() throws NotFoundException, CannotCompileException, BadBytecode {
    // given
    ClassPool cp = ClassPool.getDefault();
    ClassFile cf = cp.get("com.baeldung.javasisst.Point").getClassFile();
    MethodInfo minfo = cf.getMethod("move");
    CodeAttribute ca = minfo.getCodeAttribute();
    CodeIterator ci = ca.iterator();

    // when
    List<String> operations = new LinkedList<>();
    while (ci.hasNext()) {
        int index = ci.next();
        int op = ci.byteAt(index);
        operations.add(Mnemonic.OPCODE[op]);
    }

    // then
    assertEquals(operations, Arrays.asList("aload_0", "iload_1", "putfield", "aload_0", "iload_2", "putfield", "return"));

}
 
Example #4
Source File: JavasisstUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor() throws NotFoundException, CannotCompileException, BadBytecode {
    // given
    ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.Point").getClassFile();
    Bytecode code = new Bytecode(cf.getConstPool());
    code.addAload(0);
    code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V");
    code.addReturn(null);

    // when
    MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V");
    minfo.setCodeAttribute(code.toCodeAttribute());
    cf.addMethod(minfo);

    // then
    CodeIterator ci = code.toCodeAttribute().iterator();
    List<String> operations = new LinkedList<>();
    while (ci.hasNext()) {
        int index = ci.next();
        int op = ci.byteAt(index);
        operations.add(Mnemonic.OPCODE[op]);
    }

    assertEquals(operations, Arrays.asList("aload_0", "invokespecial", "return"));

}
 
Example #5
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void transformInvokevirtualsIntoPutAndGetfields(ClassFile classfile)
		throws CannotCompileException {
	List methods = classfile.getMethods();
	for (Iterator method_iter = methods.iterator(); method_iter.hasNext();) {
		MethodInfo minfo = (MethodInfo) method_iter.next();
		String methodName = minfo.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;
		}
		CodeAttribute codeAttr = minfo.getCodeAttribute();
		if (codeAttr == null) {
			return;
		}
		CodeIterator iter = codeAttr.iterator();
		while (iter.hasNext()) {
			try {
				int pos = iter.next();
				pos = transformInvokevirtualsIntoGetfields(classfile, iter,
				                                           pos);
				pos = transformInvokevirtualsIntoPutfields(classfile, iter,
				                                           pos);

			} catch (BadBytecode e) {
				throw new CannotCompileException(e);
			}
		}
	}
}
 
Example #6
Source File: JavassistPerfTest.java    From annotation-tools with MIT License 5 votes vote down vote up
byte[] counterAdaptClass(final InputStream is, final String name)
        throws Exception
{
    CtClass cc = pool.makeClass(is);
    if (!cc.isInterface()) {
        cc.addField(new CtField(CtClass.intType, "_counter", cc));
    }
    CtMethod[] ms = cc.getDeclaredMethods();
    for (int j = 0; j < ms.length; ++j) {
        CtMethod m = ms[j];
        int modifiers = m.getModifiers();
        if (!Modifier.isStatic(modifiers)
                && !Modifier.isAbstract(modifiers)
                && !Modifier.isNative(modifiers))
        {
            if (!m.isEmpty()) {
                MethodInfo info = m.getMethodInfo();
                Bytecode bc = new Bytecode(info.getConstPool(), 1, 0);
                bc.addAload(0);
                bc.addAload(0);
                bc.addGetfield(cc, "_counter", "I");
                bc.add(Opcode.ICONST_1);
                bc.add(Opcode.IADD);
                bc.addPutfield(cc, "_counter", "I");
                CodeIterator iter = info.getCodeAttribute().iterator();
                iter.begin();
                iter.insert(bc.get());
            }
        }
    }
    return cc.toBytecode();
}
 
Example #7
Source File: JavassistPerfTest.java    From annotation-tools with MIT License 5 votes vote down vote up
byte[] counterAdaptClass(final InputStream is, final String name)
        throws Exception
{
    CtClass cc = pool.makeClass(is);
    if (!cc.isInterface()) {
        cc.addField(new CtField(CtClass.intType, "_counter", cc));
    }
    CtMethod[] ms = cc.getDeclaredMethods();
    for (int j = 0; j < ms.length; ++j) {
        CtMethod m = ms[j];
        int modifiers = m.getModifiers();
        if (!Modifier.isStatic(modifiers)
                && !Modifier.isAbstract(modifiers)
                && !Modifier.isNative(modifiers))
        {
            if (!m.isEmpty()) {
                MethodInfo info = m.getMethodInfo();
                Bytecode bc = new Bytecode(info.getConstPool(), 1, 0);
                bc.addAload(0);
                bc.addAload(0);
                bc.addGetfield(cc, "_counter", "I");
                bc.add(Opcode.ICONST_1);
                bc.add(Opcode.IADD);
                bc.addPutfield(cc, "_counter", "I");
                CodeIterator iter = info.getCodeAttribute().iterator();
                iter.begin();
                iter.insert(bc.get());
            }
        }
    }
    return cc.toBytecode();
}