Search in sources :

Example 71 with CtField

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));
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) CtField(javassist.CtField) Test(org.junit.Test)

Example 72 with CtField

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));
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) CtField(javassist.CtField) Test(org.junit.Test)

Example 73 with CtField

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));
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) CtField(javassist.CtField) NotFoundException(javassist.NotFoundException) Test(org.junit.Test)

Example 74 with CtField

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;
}
Also used : CtField(javassist.CtField)

Example 75 with CtField

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);
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) InitMethod(com.googlecode.gwt.test.patchers.InitMethod)

Aggregations

CtField (javassist.CtField)76 CtClass (javassist.CtClass)47 CtMethod (javassist.CtMethod)27 CannotCompileException (javassist.CannotCompileException)24 NotFoundException (javassist.NotFoundException)22 ClassPool (javassist.ClassPool)20 CtConstructor (javassist.CtConstructor)15 Test (org.junit.Test)12 ClassFile (javassist.bytecode.ClassFile)9 IOException (java.io.IOException)7 Method (java.lang.reflect.Method)7 ArrayList (java.util.ArrayList)6 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 ConstPool (javassist.bytecode.ConstPool)4 SMethod (org.bimserver.shared.meta.SMethod)4 SParameter (org.bimserver.shared.meta.SParameter)4 InsertableMethod (com.github.stephanenicolas.afterburner.inserts.InsertableMethod)3 SimpleInsertableMethod (com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3