use of net.bytebuddy.description.field.FieldDescription in project byte-buddy by raphw.
the class TransformerForFieldTest method testRetainsInstrumentedType.
@Test
public void testRetainsInstrumentedType() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Bar.class);
FieldDescription fieldDescription = typeDescription.getSuperClass().getDeclaredFields().filter(named(BAR)).getOnly();
FieldDescription transformed = Transformer.ForField.withModifiers().transform(typeDescription, fieldDescription);
assertThat(transformed, is(fieldDescription));
assertThat(transformed.getModifiers(), is(fieldDescription.getModifiers()));
assertThat(transformed.getType().asErasure(), is(typeDescription));
assertThat(transformed.getType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
assertThat(transformed.getType().getTypeArguments().size(), is(1));
assertThat(transformed.getType().getTypeArguments().getOnly(), is(typeDescription.getSuperClass().getDeclaredFields().filter(named(FOO)).getOnly().getType()));
}
use of net.bytebuddy.description.field.FieldDescription 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));
}
use of net.bytebuddy.description.field.FieldDescription in project hibernate-orm by hibernate.
the class EnhancerImpl method doEnhance.
private DynamicType.Builder<?> doEnhance(DynamicType.Builder<?> builder, TypeDescription managedCtClass) {
// can't effectively enhance interfaces
if (managedCtClass.isInterface()) {
log.debugf("Skipping enhancement of [%s]: it's an interface!", managedCtClass.getName());
return null;
}
// skip already enhanced classes
if (alreadyEnhanced(managedCtClass)) {
log.debugf("Skipping enhancement of [%s]: already enhanced", managedCtClass.getName());
return null;
}
PersistentAttributeTransformer transformer = PersistentAttributeTransformer.collectPersistentFields(managedCtClass, enhancementContext, classPool);
if (enhancementContext.isEntityClass(managedCtClass)) {
log.infof("Enhancing [%s] as Entity", managedCtClass.getName());
builder = builder.implement(ManagedEntity.class).defineMethod(EnhancerConstants.ENTITY_INSTANCE_GETTER_NAME, Object.class, Visibility.PUBLIC).intercept(FixedValue.self());
builder = addFieldWithGetterAndSetter(builder, EntityEntry.class, EnhancerConstants.ENTITY_ENTRY_FIELD_NAME, EnhancerConstants.ENTITY_ENTRY_GETTER_NAME, EnhancerConstants.ENTITY_ENTRY_SETTER_NAME);
builder = addFieldWithGetterAndSetter(builder, ManagedEntity.class, EnhancerConstants.PREVIOUS_FIELD_NAME, EnhancerConstants.PREVIOUS_GETTER_NAME, EnhancerConstants.PREVIOUS_SETTER_NAME);
builder = addFieldWithGetterAndSetter(builder, ManagedEntity.class, EnhancerConstants.NEXT_FIELD_NAME, EnhancerConstants.NEXT_GETTER_NAME, EnhancerConstants.NEXT_SETTER_NAME);
builder = addInterceptorHandling(builder, managedCtClass);
if (enhancementContext.doDirtyCheckingInline(managedCtClass)) {
builder = builder.implement(ExtendedSelfDirtinessTracker.class).defineField(EnhancerConstants.TRACKER_FIELD_NAME, DirtyTracker.class, FieldManifestation.TRANSIENT, Visibility.PRIVATE).annotateField(AnnotationDescription.Builder.ofType(Transient.class).build()).defineField(EnhancerConstants.TRACKER_COLLECTION_NAME, CollectionTracker.class, FieldManifestation.TRANSIENT, Visibility.PRIVATE).annotateField(AnnotationDescription.Builder.ofType(Transient.class).build()).defineMethod(EnhancerConstants.TRACKER_CHANGER_NAME, void.class, Visibility.PUBLIC).withParameters(String.class).intercept(Advice.to(CodeTemplates.TrackChange.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_GET_NAME, String[].class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.GetDirtyAttributes.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_HAS_CHANGED_NAME, boolean.class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.AreCollectionFieldsDirty.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_CLEAR_NAME, void.class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.ClearDirtyAttributes.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_SUSPEND_NAME, void.class, Visibility.PUBLIC).withParameters(boolean.class).intercept(Advice.to(CodeTemplates.SuspendDirtyTracking.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_COLLECTION_GET_NAME, CollectionTracker.class, Visibility.PUBLIC).intercept(FieldAccessor.ofField(EnhancerConstants.TRACKER_COLLECTION_NAME));
Implementation isDirty = StubMethod.INSTANCE, getDirtyNames = StubMethod.INSTANCE, clearDirtyNames = StubMethod.INSTANCE;
for (FieldDescription collectionField : collectCollectionFields(managedCtClass)) {
if (!enhancementContext.isMappedCollection(collectionField)) {
if (collectionField.getType().asErasure().isAssignableTo(Map.class)) {
isDirty = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.MapAreCollectionFieldsDirty.class).wrap(isDirty);
getDirtyNames = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.MapGetCollectionFieldDirtyNames.class).wrap(getDirtyNames);
clearDirtyNames = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.MapGetCollectionClearDirtyNames.class).wrap(clearDirtyNames);
} else {
isDirty = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.CollectionAreCollectionFieldsDirty.class).wrap(isDirty);
getDirtyNames = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.CollectionGetCollectionFieldDirtyNames.class).wrap(getDirtyNames);
clearDirtyNames = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.CollectionGetCollectionClearDirtyNames.class).wrap(clearDirtyNames);
}
}
}
if (enhancementContext.hasLazyLoadableAttributes(managedCtClass)) {
clearDirtyNames = Advice.to(CodeTemplates.InitializeLazyAttributeLoadingInterceptor.class).wrap(clearDirtyNames);
}
builder = builder.defineMethod(EnhancerConstants.TRACKER_COLLECTION_CHANGED_NAME, boolean.class, Visibility.PUBLIC).intercept(isDirty).defineMethod(EnhancerConstants.TRACKER_COLLECTION_CHANGED_FIELD_NAME, void.class, Visibility.PUBLIC).withParameters(DirtyTracker.class).intercept(getDirtyNames).defineMethod(EnhancerConstants.TRACKER_COLLECTION_CLEAR_NAME, void.class, Visibility.PUBLIC).intercept(Advice.withCustomMapping().to(CodeTemplates.ClearDirtyCollectionNames.class).wrap(StubMethod.INSTANCE)).defineMethod(ExtendedSelfDirtinessTracker.REMOVE_DIRTY_FIELDS_NAME, void.class, Visibility.PUBLIC).withParameters(LazyAttributeLoadingInterceptor.class).intercept(clearDirtyNames);
}
return transformer.applyTo(builder, false);
} else if (enhancementContext.isCompositeClass(managedCtClass)) {
log.infof("Enhancing [%s] as Composite", managedCtClass.getName());
builder = builder.implement(ManagedComposite.class);
builder = addInterceptorHandling(builder, managedCtClass);
if (enhancementContext.doDirtyCheckingInline(managedCtClass)) {
builder = builder.implement(CompositeTracker.class).defineField(EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME, CompositeOwnerTracker.class, FieldManifestation.TRANSIENT, Visibility.PRIVATE).annotateField(AnnotationDescription.Builder.ofType(Transient.class).build()).defineMethod(EnhancerConstants.TRACKER_COMPOSITE_SET_OWNER, void.class, Visibility.PUBLIC).withParameters(String.class, CompositeOwner.class).intercept(Advice.to(CodeTemplates.SetOwner.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_COMPOSITE_CLEAR_OWNER, void.class, Visibility.PUBLIC).withParameters(String.class).intercept(Advice.to(CodeTemplates.ClearOwner.class).wrap(StubMethod.INSTANCE));
}
return transformer.applyTo(builder, false);
} else if (enhancementContext.isMappedSuperclassClass(managedCtClass)) {
log.infof("Enhancing [%s] as MappedSuperclass", managedCtClass.getName());
builder = builder.implement(ManagedMappedSuperclass.class);
return transformer.applyTo(builder, true);
} else if (enhancementContext.doExtendedEnhancement(managedCtClass)) {
log.infof("Extended enhancement of [%s]", managedCtClass.getName());
return transformer.applyExtended(builder);
} else {
log.debugf("Skipping enhancement of [%s]: not entity or composite", managedCtClass.getName());
return null;
}
}
use of net.bytebuddy.description.field.FieldDescription 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;
}
use of net.bytebuddy.description.field.FieldDescription 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;
}
Aggregations