Search in sources :

Example 11 with FieldDescription

use of net.bytebuddy.description.field.FieldDescription in project hibernate-orm by hibernate.

the class PersistentAttributeTransformer method collectPersistentFields.

public static PersistentAttributeTransformer collectPersistentFields(TypeDescription managedCtClass, ByteBuddyEnhancementContext enhancementContext, TypePool classPool) {
    List<FieldDescription> persistentFieldList = new ArrayList<FieldDescription>();
    for (FieldDescription ctField : managedCtClass.getDeclaredFields()) {
        // skip static fields and skip fields added by enhancement and  outer reference in inner classes
        if (ctField.getName().startsWith("$$_hibernate_") || "this$0".equals(ctField.getName())) {
            continue;
        }
        if (!ctField.isStatic() && enhancementContext.isPersistentField(ctField)) {
            persistentFieldList.add(ctField);
        }
    }
    // HHH-10981 There is no need to do it for @MappedSuperclass
    if (!enhancementContext.isMappedSuperclassClass(managedCtClass)) {
        persistentFieldList.addAll(collectInheritPersistentFields(managedCtClass, enhancementContext));
    }
    FieldDescription[] orderedFields = enhancementContext.order(persistentFieldList.toArray(new FieldDescription[0]));
    log.debugf("Persistent fields for entity %s: %s", managedCtClass.getName(), Arrays.toString(orderedFields));
    return new PersistentAttributeTransformer(managedCtClass, enhancementContext, classPool, orderedFields);
}
Also used : ArrayList(java.util.ArrayList) FieldDescription(net.bytebuddy.description.field.FieldDescription)

Example 12 with FieldDescription

use of net.bytebuddy.description.field.FieldDescription in project hibernate-orm by hibernate.

the class PersistentAttributeTransformer method applyTo.

DynamicType.Builder<?> applyTo(DynamicType.Builder<?> builder, boolean accessor) {
    boolean compositeOwner = false;
    builder = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().method(not(nameStartsWith("$$_hibernate_")), this));
    for (FieldDescription enhancedField : enhancedFields) {
        builder = builder.defineMethod(EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + enhancedField.getName(), enhancedField.getType().asErasure(), Visibility.PUBLIC).intercept(accessor ? FieldAccessor.ofField(enhancedField.getName()).in(enhancedField.getDeclaringType().asErasure()) : fieldReader(enhancedField)).defineMethod(EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + enhancedField.getName(), TypeDescription.VOID, Visibility.PUBLIC).withParameters(enhancedField.getType().asErasure()).intercept(accessor ? FieldAccessor.ofField(enhancedField.getName()).in(enhancedField.getDeclaringType().asErasure()) : fieldWriter(enhancedField));
        if (!compositeOwner && !accessor && EnhancerImpl.isAnnotationPresent(enhancedField, Embedded.class) && enhancementContext.isCompositeClass(enhancedField.getType().asErasure()) && enhancementContext.doDirtyCheckingInline(managedCtClass)) {
            compositeOwner = true;
        }
    }
    if (compositeOwner) {
        builder = builder.implement(CompositeOwner.class);
        if (enhancementContext.isCompositeClass(managedCtClass)) {
            builder = builder.defineMethod(EnhancerConstants.TRACKER_CHANGER_NAME, void.class, Visibility.PUBLIC).withParameters(String.class).intercept(Advice.to(CodeTemplates.CompositeOwnerDirtyCheckingHandler.class).wrap(StubMethod.INSTANCE));
        }
    }
    if (enhancementContext.doExtendedEnhancement(managedCtClass)) {
        builder = applyExtended(builder);
    }
    return builder;
}
Also used : CompositeOwner(org.hibernate.engine.spi.CompositeOwner) AsmVisitorWrapper(net.bytebuddy.asm.AsmVisitorWrapper) Embedded(javax.persistence.Embedded) FieldDescription(net.bytebuddy.description.field.FieldDescription)

Example 13 with FieldDescription

use of net.bytebuddy.description.field.FieldDescription in project byte-buddy by raphw.

the class ImplementationContextDefaultTest method testDrainFieldCacheEntries.

@Test
public void testDrainFieldCacheEntries() throws Exception {
    Implementation.Context.ExtractableView implementationContext = new Implementation.Context.Default(instrumentedType, classFileVersion, auxiliaryTypeNamingStrategy, typeInitializer, auxiliaryClassFileVersion);
    FieldDescription firstField = implementationContext.cache(firstFieldValue, firstRawFieldType);
    assertThat(implementationContext.cache(firstFieldValue, firstRawFieldType), is(firstField));
    FieldDescription secondField = implementationContext.cache(secondFieldValue, secondRawFieldType);
    assertThat(implementationContext.cache(secondFieldValue, secondRawFieldType), is(secondField));
    assertThat(firstField.getName(), not(secondField.getName()));
    when(typeInitializer.expandWith(any(ByteCodeAppender.class))).thenReturn(otherTypeInitializer);
    when(otherTypeInitializer.expandWith(any(ByteCodeAppender.class))).thenReturn(thirdTypeInitializer);
    when(thirdTypeInitializer.isDefined()).thenReturn(true);
    implementationContext.drain(drain, classVisitor, annotationValueFilterFactory);
    verify(classVisitor).visitField(eq(cacheFieldModifiers), Mockito.startsWith(Implementation.Context.Default.FIELD_CACHE_PREFIX), eq(BAR), Mockito.isNull(String.class), Mockito.isNull(Object.class));
    verify(classVisitor).visitField(eq(cacheFieldModifiers), Mockito.startsWith(Implementation.Context.Default.FIELD_CACHE_PREFIX), eq(QUX), Mockito.isNull(String.class), Mockito.isNull(Object.class));
    verify(typeInitializer).expandWith(any(ByteCodeAppender.class));
    verify(otherTypeInitializer).expandWith(any(ByteCodeAppender.class));
}
Also used : ByteCodeAppender(net.bytebuddy.implementation.bytecode.ByteCodeAppender) FieldDescription(net.bytebuddy.description.field.FieldDescription) Test(org.junit.Test)

Example 14 with FieldDescription

use of net.bytebuddy.description.field.FieldDescription in project byte-buddy by raphw.

the class GenericSignatureResolutionTest method testGenericField.

@Test
public void testGenericField() throws Exception {
    DynamicType.Unloaded<?> unloaded = new ByteBuddy().redefine(GenericField.class).make();
    Class<?> type = unloaded.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    FieldDescription createdField = new FieldDescription.ForLoadedField(type.getDeclaredField(FOO));
    FieldDescription originalField = new FieldDescription.ForLoadedField(GenericField.class.getDeclaredField(FOO));
    assertThat(createdField.getType(), is(originalField.getType()));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) FieldDescription(net.bytebuddy.description.field.FieldDescription) Test(org.junit.Test)

Example 15 with FieldDescription

use of net.bytebuddy.description.field.FieldDescription in project byte-buddy by raphw.

the class AbstractTypeDescriptionGenericTest method testIntermediateRawType.

@Test
public void testIntermediateRawType() throws Exception {
    TypeDescription.Generic type = describeType(IntermediateRaw.class.getDeclaredField(FOO)).getSuperClass().getSuperClass().getSuperClass();
    FieldDescription fieldDescription = type.getDeclaredFields().filter(named(BAR)).getOnly();
    assertThat(fieldDescription.getType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(fieldDescription.getType().asErasure(), is((TypeDescription) new TypeDescription.ForLoadedType(Integer.class)));
}
Also used : FieldDescription(net.bytebuddy.description.field.FieldDescription) Test(org.junit.Test)

Aggregations

FieldDescription (net.bytebuddy.description.field.FieldDescription)15 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)3 TypeDefinition (net.bytebuddy.description.type.TypeDefinition)3 TypeDescription (net.bytebuddy.description.type.TypeDescription)2 Collection (java.util.Collection)1 Map (java.util.Map)1 Embedded (javax.persistence.Embedded)1 Transient (javax.persistence.Transient)1 ByteBuddy (net.bytebuddy.ByteBuddy)1 AsmVisitorWrapper (net.bytebuddy.asm.AsmVisitorWrapper)1 DynamicType (net.bytebuddy.dynamic.DynamicType)1 Implementation (net.bytebuddy.implementation.Implementation)1 ByteCodeAppender (net.bytebuddy.implementation.bytecode.ByteCodeAppender)1 CompositeOwnerTracker (org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker)1 DirtyTracker (org.hibernate.bytecode.enhance.internal.tracker.DirtyTracker)1 CompositeOwner (org.hibernate.engine.spi.CompositeOwner)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 ExtendedSelfDirtinessTracker (org.hibernate.engine.spi.ExtendedSelfDirtinessTracker)1 ManagedEntity (org.hibernate.engine.spi.ManagedEntity)1