javassist.bytecode.BadBytecode Java Examples

The following examples show how to use javassist.bytecode.BadBytecode. 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: PersistentAttributesHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static String inferFieldTypeName(CtField field) {
	try {
		if ( field.getFieldInfo2().getAttribute( SignatureAttribute.tag ) == null ) {
			return field.getType().getName();
		}
		return inferGenericTypeName(
				field.getType(),
				SignatureAttribute.toTypeSignature( field.getGenericSignature() )
		);
	}
	catch (BadBytecode ignore) {
		return null;
	}
	catch (NotFoundException e) {
		return null;
	}
}
 
Example #2
Source File: PersistentAttributesHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static String inferMethodTypeName(CtMethod method) {
	try {
		if ( method.getMethodInfo2().getAttribute( SignatureAttribute.tag ) == null ) {
			return method.getReturnType().getName();
		}
		return inferGenericTypeName(
				method.getReturnType(),
				SignatureAttribute.toMethodSignature( method.getGenericSignature() ).getReturnType()
		);
	}
	catch (BadBytecode ignore) {
		return null;
	}
	catch (NotFoundException e) {
		return null;
	}
}
 
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 givenTableOfInstructions_whenAddNewInstruction_thenShouldConstructProperSequence() throws NotFoundException, BadBytecode, CannotCompileException, IllegalAccessException, InstantiationException {
    // given
    ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.ThreeDimensionalPoint").getClassFile();

    // when
    FieldInfo f = new FieldInfo(cf.getConstPool(), "id", "I");
    f.setAccessFlags(AccessFlag.PUBLIC);
    cf.addField(f);

    ClassPool classPool = ClassPool.getDefault();
    Field[] fields = classPool.makeClass(cf).toClass().getFields();
    List<String> fieldsList = Stream.of(fields).map(Field::getName).collect(Collectors.toList());
    assertTrue(fieldsList.contains("id"));

}
 
Example #5
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 #6
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);
			}
		}
	}
}