use of javassist.CtField in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class TestJavassistTransformerExecuter method removeStamp_on_Class.
@Test
public void removeStamp_on_Class() throws CannotCompileException, NotFoundException {
// given
final String className = "test.TestClass";
final CtClass candidateClass = mock("candidateClass", CtClass.class);
// createStampField
expect(candidateClass.isInterface()).andReturn(false);
expect(candidateClass.getClassPool()).andReturn(classPool).anyTimes();
expect(candidateClass.getClassFile2()).andReturn(new ClassFile(false, className, null));
expect(candidateClass.isFrozen()).andReturn(false);
expect(candidateClass.getName()).andReturn(className).anyTimes();
final Capture<CtField> fieldCaptures = newInstance();
candidateClass.removeField(capture(fieldCaptures));
replay(candidateClass, classPool);
// when
sut.removeStamp(candidateClass);
// then
verify(candidateClass, classPool);
assertThat("generated stamp field starts with the constant prefix.", fieldCaptures.getValue().getName(), org.hamcrest.core.StringStartsWith.startsWith(JavassistTransformerExecutor.STAMP_FIELD_NAME));
assertThat("generated stamp field must have the right modifiers.", fieldCaptures.getValue().getModifiers(), is(STATIC | FINAL | PRIVATE));
assertThat("generated stamp field is a boolean.", fieldCaptures.getValue().getType(), is(booleanType));
}
use of javassist.CtField in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class TestJavassistTransformerExecuter method removeStamp_on_Interface.
@Test
public void removeStamp_on_Interface() throws CannotCompileException, NotFoundException {
// given
final String className = "test.TestClass";
final CtClass candidateClass = mock("candidateClass", CtClass.class);
// createStampField
expect(candidateClass.isInterface()).andReturn(true);
expect(candidateClass.getClassPool()).andReturn(classPool).anyTimes();
expect(candidateClass.getClassFile2()).andReturn(new ClassFile(false, className, null));
expect(candidateClass.isFrozen()).andReturn(false);
expect(candidateClass.getName()).andReturn(className).anyTimes();
final Capture<CtField> fieldCaptures = newInstance();
candidateClass.removeField(capture(fieldCaptures));
replay(candidateClass, classPool);
// when
sut.removeStamp(candidateClass);
// then
verify(candidateClass, classPool);
assertThat("generated stamp field starts with the constant prefix.", fieldCaptures.getValue().getName(), org.hamcrest.core.StringStartsWith.startsWith(JavassistTransformerExecutor.STAMP_FIELD_NAME));
assertThat("generated stamp field must have the right modifiers.", fieldCaptures.getValue().getModifiers(), is(STATIC | FINAL | PUBLIC));
assertThat("generated stamp field is a boolean.", fieldCaptures.getValue().getType(), is(booleanType));
}
use of javassist.CtField in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class TestJavassistTransformerExecuter method removeStamp_ignores_internal_NotFoundException.
@Test
public void removeStamp_ignores_internal_NotFoundException() throws CannotCompileException, NotFoundException {
// given
final String className = "test.TestClass";
final NotFoundException internalException = new NotFoundException("expected exception");
final CtClass candidateClass = mock("candidateClass", CtClass.class);
// createStampField
expect(candidateClass.isInterface()).andReturn(false);
expect(candidateClass.getClassPool()).andReturn(classPool).anyTimes();
expect(candidateClass.getClassFile2()).andReturn(new ClassFile(false, className, null));
expect(candidateClass.isFrozen()).andReturn(false);
expect(candidateClass.getName()).andReturn(className).anyTimes();
final Capture<CtField> fieldCaptures = newInstance();
candidateClass.removeField(capture(fieldCaptures));
EasyMock.expectLastCall().andThrow(internalException);
replay(candidateClass, classPool);
// when
sut.removeStamp(candidateClass);
// then
verify(candidateClass, classPool);
assertThat("generated stamp field starts with the constant prefix.", fieldCaptures.getValue().getName(), org.hamcrest.core.StringStartsWith.startsWith(JavassistTransformerExecutor.STAMP_FIELD_NAME));
assertThat("generated stamp field must have the right modifiers.", fieldCaptures.getValue().getModifiers(), is(STATIC | FINAL | PRIVATE));
assertThat("generated stamp field is a boolean.", fieldCaptures.getValue().getType(), is(booleanType));
}
use of javassist.CtField in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class JavassistTransformerExecutor method createStampField.
/**
* Creates a {@link CtField} instance associated with the passed {@code candidateClass}.
*
* @param candidateClass must not be {@code null}
*
* @return never {@code null}
*
* @throws NullPointerException if passed {@code candidateClass} is {@code null}.
* @throws CannotCompileException field could not created
*
* @see CtField
*/
private CtField createStampField(final CtClass candidateClass) throws CannotCompileException {
int stampModifiers = AccessFlag.STATIC | AccessFlag.FINAL;
if (!candidateClass.isInterface()) {
stampModifiers |= AccessFlag.PRIVATE;
} else {
stampModifiers |= AccessFlag.PUBLIC;
}
final CtField stampField = new CtField(CtClass.booleanType, createStampFieldName(), candidateClass);
stampField.setModifiers(stampModifiers);
return stampField;
}
use of javassist.CtField in project gwt-test-utils by gwt-test-utils.
the class PrefixTreePatcher method initClass.
@InitMethod
static void initClass(CtClass c) throws CannotCompileException {
// add field "private Set<?> PREFIXES_SET;"
CtClass pcType = GwtClassPool.getCtClass(Set.class);
CtField field = new CtField(pcType, PREFIXES_SET_PROPERTY, c);
field.setModifiers(Modifier.PRIVATE);
c.addField(field);
}
Aggregations