Search in sources :

Example 1 with TypeDefinition

use of net.bytebuddy.description.type.TypeDefinition in project byte-buddy by raphw.

the class DuplicationOtherTest method testDoubleToZeroFlip.

@Test(expected = IllegalArgumentException.class)
public void testDoubleToZeroFlip() throws Exception {
    TypeDefinition typeDefinition = mock(TypeDefinition.class);
    when(typeDefinition.getStackSize()).thenReturn(StackSize.ZERO);
    Duplication.DOUBLE.flipOver(typeDefinition);
}
Also used : TypeDefinition(net.bytebuddy.description.type.TypeDefinition) Test(org.junit.Test)

Example 2 with TypeDefinition

use of net.bytebuddy.description.type.TypeDefinition in project byte-buddy by raphw.

the class TransformerForFieldTest method testSimpleTransformation.

@Test
public void testSimpleTransformation() throws Exception {
    when(tokenTransformer.transform(instrumentedType, fieldToken)).thenReturn(fieldToken);
    FieldDescription transformed = new Transformer.ForField(tokenTransformer).transform(instrumentedType, fieldDescription);
    assertThat(transformed.getDeclaringType(), is((TypeDefinition) declaringType));
    assertThat(transformed.getInternalName(), is(FOO));
    assertThat(transformed.getModifiers(), is(MODIFIERS));
    assertThat(transformed.getDeclaredAnnotations(), is(Collections.singletonList(fieldAnnotation)));
    assertThat(transformed.getType(), is(fieldType));
    assertThat(transformed.asDefined(), is(definedField));
}
Also used : FieldDescription(net.bytebuddy.description.field.FieldDescription) TypeDefinition(net.bytebuddy.description.type.TypeDefinition) Test(org.junit.Test)

Example 3 with TypeDefinition

use of net.bytebuddy.description.type.TypeDefinition in project byte-buddy by raphw.

the class TransformerForMethodTest method testSimpleTransformation.

@Test
public void testSimpleTransformation() throws Exception {
    when(tokenTransformer.transform(instrumentedType, methodToken)).thenReturn(methodToken);
    MethodDescription transformed = new Transformer.ForMethod(tokenTransformer).transform(instrumentedType, methodDescription);
    assertThat(transformed.getDeclaringType(), is((TypeDefinition) declaringType));
    assertThat(transformed.getInternalName(), is(FOO));
    assertThat(transformed.getModifiers(), is(MODIFIERS));
    assertThat(transformed.getReturnType(), is(returnType));
    assertThat(transformed.getTypeVariables().size(), is(1));
    assertThat(transformed.getTypeVariables().getOnly().getSymbol(), is(QUX));
    assertThat(transformed.getExceptionTypes().size(), is(1));
    assertThat(transformed.getExceptionTypes().getOnly(), is(exceptionType));
    assertThat(transformed.getDeclaredAnnotations(), is(Collections.singletonList(methodAnnotation)));
    assertThat(transformed.getParameters().size(), is(1));
    assertThat(transformed.getParameters().getOnly().getType(), is(parameterType));
    assertThat(transformed.getParameters().getOnly().getName(), is(BAR));
    assertThat(transformed.getParameters().getOnly().getModifiers(), is(MODIFIERS * 2));
    assertThat(transformed.getParameters().getOnly().getDeclaredAnnotations().size(), is(1));
    assertThat(transformed.getParameters().getOnly().getDeclaredAnnotations().getOnly(), is(parameterAnnotation));
    assertThat(transformed.getParameters().getOnly().asDefined(), is(definedParameter));
    assertThat(transformed.getParameters().getOnly().getDeclaredAnnotations(), is(Collections.singletonList(parameterAnnotation)));
    assertThat(transformed.getParameters().getOnly().getDeclaringMethod(), is(transformed));
    assertThat(transformed.asDefined(), is(definedMethod));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeDefinition(net.bytebuddy.description.type.TypeDefinition) Test(org.junit.Test)

Example 4 with TypeDefinition

use of net.bytebuddy.description.type.TypeDefinition in project hibernate-orm by hibernate.

the class PersistentAttributeTransformer method collectInheritPersistentFields.

private static Collection<FieldDescription> collectInheritPersistentFields(TypeDefinition managedCtClass, ByteBuddyEnhancementContext enhancementContext) {
    if (managedCtClass == null || managedCtClass.represents(Object.class)) {
        return Collections.emptyList();
    }
    TypeDefinition managedCtSuperclass = managedCtClass.getSuperClass();
    if (!enhancementContext.isMappedSuperclassClass(managedCtSuperclass.asErasure())) {
        return collectInheritPersistentFields(managedCtSuperclass, enhancementContext);
    }
    log.debugf("Found @MappedSuperclass %s to collectPersistenceFields", managedCtSuperclass);
    List<FieldDescription> persistentFieldList = new ArrayList<FieldDescription>();
    for (FieldDescription ctField : managedCtSuperclass.getDeclaredFields()) {
        if (ctField.getName().startsWith("$$_hibernate_") || "this$0".equals(ctField.getName())) {
            continue;
        }
        if (!ctField.isStatic() && enhancementContext.isPersistentField(ctField)) {
            persistentFieldList.add(ctField);
        }
    }
    persistentFieldList.addAll(collectInheritPersistentFields(managedCtSuperclass, enhancementContext));
    return persistentFieldList;
}
Also used : ArrayList(java.util.ArrayList) FieldDescription(net.bytebuddy.description.field.FieldDescription) TypeDefinition(net.bytebuddy.description.type.TypeDefinition)

Example 5 with TypeDefinition

use of net.bytebuddy.description.type.TypeDefinition in project hibernate-orm by hibernate.

the class EnhancerImpl method collectInheritCollectionFields.

private Collection<FieldDescription> collectInheritCollectionFields(TypeDefinition managedCtClass) {
    TypeDefinition managedCtSuperclass = managedCtClass.getSuperClass();
    if (managedCtSuperclass == null || managedCtSuperclass.represents(Object.class)) {
        return Collections.emptyList();
    }
    if (!enhancementContext.isMappedSuperclassClass(managedCtSuperclass.asErasure())) {
        return collectInheritCollectionFields(managedCtSuperclass.asErasure());
    }
    List<FieldDescription> collectionList = new ArrayList<FieldDescription>();
    for (FieldDescription ctField : managedCtSuperclass.getDeclaredFields()) {
        if (!Modifier.isStatic(ctField.getModifiers()) && enhancementContext.isPersistentField(ctField)) {
            if (ctField.getType().asErasure().isAssignableTo(Collection.class) || ctField.getType().asErasure().isAssignableTo(Map.class)) {
                collectionList.add(ctField);
            }
        }
    }
    collectionList.addAll(collectInheritCollectionFields(managedCtSuperclass));
    return collectionList;
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) Map(java.util.Map) FieldDescription(net.bytebuddy.description.field.FieldDescription) TypeDefinition(net.bytebuddy.description.type.TypeDefinition)

Aggregations

TypeDefinition (net.bytebuddy.description.type.TypeDefinition)6 Test (org.junit.Test)4 FieldDescription (net.bytebuddy.description.field.FieldDescription)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)1 Map (java.util.Map)1 MethodDescription (net.bytebuddy.description.method.MethodDescription)1