Skip to content

Instantly share code, notes, and snippets.

@barreiro
Last active November 13, 2015 16:08
Show Gist options
  • Save barreiro/bff8a7f9540b080a3fa3 to your computer and use it in GitHub Desktop.
Save barreiro/bff8a7f9540b080a3fa3 to your computer and use it in GitHub Desktop.
Discussion regarding design changes in bytecode enhancement
public class Parent {
Long id;
@OneToMany(
mappedBy = "parent",
cascade = {CascadeType.ALL},
fetch = FetchType.LAZY
)
List<Child> children;
public List<Child> getChildren() {
return this.$$_hibernate_read_children();
}
public void setChildren(List<Child> children) {
this.$$_hibernate_write_children(children);
}
// This is what exists today
public List $$_hibernate_read_children() {
if(this.$$_hibernate_getInterceptor() != null) {
this.children = (List)this.$$_hibernate_getInterceptor().readObject(this, "children", this.children);
}
return this.children;
}
public void $$_hibernate_write_children(List var1) {
if(this.children != null && Hibernate.isPropertyInitialized(this.children, "parent")) {
Object[] var2 = this.children.toArray();
for(int var3 = 0; var3 < var2.length; ++var3) {
Child var4 = (Child)var2[var3];
if(var1 == null || !var1.contains(var4)) {
var4.$$_hibernate_write_parent((Parent)null);
}
}
}
if(!EqualsHelper.areEqual(this.children, var1)) {
this.$$_hibernate_trackChange("children");
}
List var10 = var1;
if(this.$$_hibernate_getInterceptor() != null) {
var10 = (List)this.$$_hibernate_getInterceptor().writeObject(this, "children", this.children, var1);
}
this.children = var10;
Object var6 = null;
if(var1 != null && Hibernate.isPropertyInitialized(var1, "parent")) {
Object[] var7 = var1.toArray();
for(int var8 = 0; var8 < var7.length; ++var8) {
Child var9 = (Child)var7[var8];
if(Hibernate.isPropertyInitialized(var9, "parent") && var9.$$_hibernate_read_parent() != this) {
var9.$$_hibernate_write_parent(this);
}
}
}
}
// THIS IS FOR DISCUSSION
public enum $$_hibernate_enhanced_attribute_enumer implements EnhancedAttributeEnumeration {
children
}
// I'm considering adding a few static methods to deal with the interception --- let's just say they go on Enhancer class for now
public List $$_hibernate_read_children() {
// thos code fragment is only injected if there is lazy loading properties
if( this.$$_hibernate_getInterceptor() != null ) {
this.children = (List) this.$$_hibernate_getInterceptor().readObject( this, EnhancedAttribute.children, this.children );
}
return this.children;
}
public void $$_hibernate_write_children(List var1) {
Enhancer.disassociateOneToMany( EnhancedAttribute.children, this.children, "parent"); // iterates over this.children and call $$_hibernate_write_parent(null)
Enhancer.dirtyCheck( EnhancedAttribute.children, this.children, var1 ); // The logic here is simple, but still ... for completeness
if( this.$$_hibernate_getInterceptor() != null ) {
this.children = (List) this.$$_hibernate_getInterceptor().writeObject( this, EnhancedAttribute.children, this.children );
}
Enhancer.associateOneToMany( EnhancedAttribute.children, this.children, this, "parent"); // iterates over this.children and call $$_hibernate_write_parent(this)
}
// to handle bi-direction assotiation we would need something like this
// because only we know the actual types on the assotiation
public static void $$_hibernate_target_association(EnhancedAttribute attribute, Object target, Object value) {
switch ( ($$_hibernate_enhanced_attribute) attribute ) {
case children:
( (Child) target ).$$_hibernate_write_parent( (Parent) value );
}
}
}
// this enum would be the mapping contract to be passed around -- it's our vision of the attribute order and we may be able to explore further optimization with that
interface EnhancedAttributeEnumeration {
String name();
int ordinal();
}
@sebersole
Copy link

Oh, I guess we cannot do in-line comments :)

One random comment... I very very very much dislike the term "enum" in EnhancedAttributeEnumeration. Another fundamental point in defining something as an enum is the idea that the possibility set is finite and known a-priori. Neither of those 2 hold true here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment