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);
}
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;
}
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));
}
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()));
}
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)));
}
Aggregations