Skip to content

Instantly share code, notes, and snippets.

@sebersole
Forked from barreiro/Experiment1.java
Last active November 13, 2015 16:36
Show Gist options
  • Save sebersole/842618c6c19765f679fa to your computer and use it in GitHub Desktop.
Save sebersole/842618c6c19765f679fa to your computer and use it in GitHub Desktop.
Discussion regarding design changes in bytecode enhancement
public class BytecodeInterceptorImpl implements BytecodeInterceptor {
private final Object interceptedEntity;
// "delegates" for the various interception capabilities.
private LazyAttributeInterceptionHandler lazyAttributeHandler;
private DirtinessTrackingInterceptionHandler dirtinessHandler;
private AssociationManagementInterceptionHandler associationHandler;
...
public BytecodeInterceptorImpl(Object interceptedEntity) {
this.interceptedEntity = interceptedEntity;
}
@Override
public Object interceptReadObject(String attributeName, Object currentValue) {
return lazyAttributeHandler.readObject( interceptedEntity, attributeName, currentValue );
}
@Override
public Object interceptWriteObject(String attributeName, Object currentValue, Object incomingValue) {
associationHandler.disassociateCurrent( interceptedEntity, attributeName, currentValue );
dirtinessHandler.interceptWrite( interceptedEntity, attributeName, currentValue, incomingValue );
lazyAttributeHandler.writeObject( interceptedEntity, attributeName );
associationHandler.associateIncoming( interceptedEntity, attributeName, incomingValue );
}
...
}
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
// - Here are my edits as well...
// NOTE : For now I dispense with the idea of "attribute indexes", lets come back to this later.
// For now I just focus on the idea of a singular interceptor hook.
// One change is to always have an interceptor. We'll initialize it as instance state.
// Then Hibernate internals need just manage its associated Session, etc.
private final BytecodeInterceptor $$_hibernateInterceptor = new BytecodeInterceptorImpl( this );
public List $$_hibernate_read_children() {
this.children = $$_hibernateInterceptor.interceptReadObject( "children", children );
return this.children;
}
public void $$_hibernate_write_children(List var1) {
this.children = (List) $$_hibernateInterceptor.interceptWriteObject( "children", children, var1 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment