From 720a6d9d5c1900b79a91d704af8c3973f6165e0d Mon Sep 17 00:00:00 2001 From: aclement Date: Thu, 20 Oct 2005 10:32:20 +0000 Subject: Fix for ordering problem when using generic abstract aspects. Fix for crappy code (of mine) in ResolvedType.discover... - see pr112105 comment #10 --- .../generics/genericaspects/BlobContainment.aj | 3 +- .../genericaspects/ParentChildRelationship.aj | 106 ++++++++++++++------- tests/java5/generics/genericaspects/TheBigOne.java | 76 +++++++++++++++ 3 files changed, 150 insertions(+), 35 deletions(-) create mode 100644 tests/java5/generics/genericaspects/TheBigOne.java (limited to 'tests/java5') diff --git a/tests/java5/generics/genericaspects/BlobContainment.aj b/tests/java5/generics/genericaspects/BlobContainment.aj index 923e12224..9ae21df0b 100644 --- a/tests/java5/generics/genericaspects/BlobContainment.aj +++ b/tests/java5/generics/genericaspects/BlobContainment.aj @@ -1,6 +1,5 @@ public aspect BlobContainment extends ParentChildRelationship { - public static void main(String []argv) { Blob a = new Blob(); Blob b = new Blob(); @@ -17,12 +16,14 @@ public aspect BlobContainment extends ParentChildRelationship { // now query the layout +/* if (!e.getParent().equals(b)) throw new RuntimeException("why is E not parent of B? "+e.getParent()); if (!d.getParent().equals(a)) throw new RuntimeException("why is A not parent of D? "+d.getParent()); if (a.getChildren().size()!=3) throw new RuntimeException("A should have 3 children, not:"+a.getChildren().size()); +*/ } } diff --git a/tests/java5/generics/genericaspects/ParentChildRelationship.aj b/tests/java5/generics/genericaspects/ParentChildRelationship.aj index 6d9624d6d..b5fb093fb 100644 --- a/tests/java5/generics/genericaspects/ParentChildRelationship.aj +++ b/tests/java5/generics/genericaspects/ParentChildRelationship.aj @@ -1,57 +1,99 @@ import java.util.*; import org.aspectj.lang.annotation.*; + +abstract aspect ParentChildRelationship { + + interface ParentHasChildren{} + interface ChildHasParent

{} + + declare parents: Parent implements ParentHasChildren; + declare parents: Child implements ChildHasParent; + + public List ParentHasChildren.children = new ArrayList(); + public P ChildHasParent

.parent; + + public List ParentHasChildren.getChildren() { + return Collections.unmodifiableList(children); + } + + public P ChildHasParent

.getParent() { + return parent; + } + + public void ChildHasParent.setParent(R parent) { + this.parent = parent; + ParentHasChildren phc = (ParentHasChildren)parent; + if (phc.getChildren().contains(this)) + phc.addChild(this); + } + + public void ParentHasChildren.addChild(X child) { + if (((ChildHasParent)child).parent != null) { + ((ParentHasChildren)((ChildHasParent)child).parent).removeChild(child); + } else { + ((ChildHasParent)child).setParent((ParentHasChildren)this); + } + children.add(child); + } + + public void ParentHasChildren.removeChild(Y child) { + if (children.remove(child)) { + ((ChildHasParent)child).parent = null; + } + } + +} +/* abstract aspect ParentChildRelationship { - /** interface implemented by parents */ + // interface implemented by parents interface ParentHasChildren{ -// List getChildren(); -// void addChild(C child); -// void removeChild(C child); + List getChildren(); + void addChild(C child); + void removeChild(C child); } - /** interface implemented by children */ + // interface implemented by children interface ChildHasParent

{ -// P getParent(); -// void setParent(P parent); + P getParent(); + void setParent(P parent); } - /** ensure the parent type implements ParentHasChildren */ + // ensure the parent type implements ParentHasChildren declare parents: Parent implements ParentHasChildren; - /** ensure the child type implements ChildHasParent */ + // ensure the child type implements ChildHasParent declare parents: Child implements ChildHasParent; // Inter-type declarations made on the *generic* interface types to provide // default implementations. - /** list of children maintained by parent */ + // list of children maintained by parent public List ParentHasChildren.children = new ArrayList(); - /** reference to parent maintained by child */ + // reference to parent maintained by child public P ChildHasParent

.parent; - /** Default implementation of getChildren for the generic type ParentHasChildren */ + // Default implementation of getChildren for the generic + // type ParentHasChildren public List ParentHasChildren.getChildren() { return Collections.unmodifiableList(children); } - /** Default implementation of getParent for the generic type ChildHasParent */ + // Default implementation of getParent for the generic + // type ChildHasParent public P ChildHasParent

.getParent() { return parent; } - /** - * Default implementation of setParent for the generic type ChildHasParent. - * Ensures that this child is added to the children of the parent too. - */ + // Default implementation of setParent for the generic type ChildHasParent. + // Ensures that this child is added to the children of the parent too. public void ChildHasParent.setParent(R parent) { ((ParentHasChildren)parent).addChild(this); } - /** - * Default implementation of addChild, ensures that parent of child is - * also updated. - */ + // Default implementation of addChild, ensures that parent of child is + // also updated. public void ParentHasChildren.addChild(X child) { if (((ChildHasParent)child).parent != null) { ((ParentHasChildren)((ChildHasParent)child).parent).removeChild(child); @@ -60,27 +102,23 @@ abstract aspect ParentChildRelationship { ((ChildHasParent)child).parent = (ParentHasChildren)this; } - /** - * Default implementation of removeChild, ensures that parent of - * child is also updated. - */ + // Default implementation of removeChild, ensures that parent of + // child is also updated. public void ParentHasChildren.removeChild(Y child) { if (children.remove(child)) { ((ChildHasParent)child).parent = null; } } - /** - * Matches at an addChild join point for the parent type P and child type C - */ + + // Matches at an addChild join point for the parent type P and child type C @SuppressAjWarnings public pointcut addingChild(Parent p, Child c) : execution(* Parent.addChild(Child)) && this(p) && args(c); - /** - * Matches at a removeChild join point for the parent type P and child type C - */ - @SuppressAjWarnings - public pointcut removingChild(Parent p, Child c) : - execution(* Parent.removeChild(Child)) && this(p) && args(c); + // Matches at a removeChild join point for the parent type P and child type C + @SuppressAjWarnings + public pointcut removingChild(Parent p, Child c) : + execution(* Parent.removeChild(Child)) && this(p) && args(c); } +*/ diff --git a/tests/java5/generics/genericaspects/TheBigOne.java b/tests/java5/generics/genericaspects/TheBigOne.java new file mode 100644 index 000000000..42be102f1 --- /dev/null +++ b/tests/java5/generics/genericaspects/TheBigOne.java @@ -0,0 +1,76 @@ +import java.util.*; +import org.aspectj.lang.annotation.*; + +class Blob {} + +public aspect TheBigOne extends ParentChildRelationship { + + public static void main(String []argv) { + Blob a = new Blob(); + Blob b = new Blob(); + Blob c = new Blob(); + Blob d = new Blob(); + Blob e = new Blob(); + + // arrange as follows: A contains B,C,D and B contains E + + a.addChild(b); + a.addChild(c); + a.addChild(d); + b.addChild(e); + + // now query the layout + + if (!e.getParent().equals(b)) + throw new RuntimeException("why is E not parent of B? "+e.getParent()); + if (!d.getParent().equals(a)) + throw new RuntimeException("why is A not parent of D? "+d.getParent()); + if (a.getChildren().size()!=3) + throw new RuntimeException("A should have 3 children, not:"+a.getChildren().size()); + } + +} + + +abstract aspect ParentChildRelationship { + + interface ParentHasChildren{} + interface ChildHasParent

{} + + declare parents: Parent implements ParentHasChildren; + declare parents: Child implements ChildHasParent; + + public List ParentHasChildren.children = new ArrayList(); + public P ChildHasParent

.parent; + + public List ParentHasChildren.getChildren() { + return Collections.unmodifiableList(children); + } + + public P ChildHasParent

.getParent() { + return parent; + } + + public void ChildHasParent.setParent(R parent) { + this.parent = parent; + ParentHasChildren phc = (ParentHasChildren)parent; + if (phc.getChildren().contains(this)) + phc.addChild(this); + } + + public void ParentHasChildren.addChild(X child) { + if (((ChildHasParent)child).parent != null) { + ((ParentHasChildren)((ChildHasParent)child).parent).removeChild(child); + } else { + ((ChildHasParent)child).setParent((ParentHasChildren)this); + } + children.add(child); + } + + public void ParentHasChildren.removeChild(Y child) { + if (children.remove(child)) { + ((ChildHasParent)child).parent = null; + } + } + +} -- cgit v1.2.3