diff options
Diffstat (limited to 'docs/teaching/exercises/answers')
20 files changed, 559 insertions, 0 deletions
diff --git a/docs/teaching/exercises/answers/Answer1a.java b/docs/teaching/exercises/answers/Answer1a.java new file mode 100644 index 000000000..94d4522c4 --- /dev/null +++ b/docs/teaching/exercises/answers/Answer1a.java @@ -0,0 +1,22 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.*; + +aspect Answer1a { + declare error + : get(java.io.PrintStream System.out) + && within(figures..*) + : "illegal access to System.out"; +} diff --git a/docs/teaching/exercises/answers/Answer1b.java b/docs/teaching/exercises/answers/Answer1b.java new file mode 100644 index 000000000..ef634a974 --- /dev/null +++ b/docs/teaching/exercises/answers/Answer1b.java @@ -0,0 +1,21 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +aspect Answer1b { + declare warning + : set(private * *) + && !withincode(* set*(..)) + && within(figures.*) + : "bad field set"; +} diff --git a/docs/teaching/exercises/answers/Answer1c.java b/docs/teaching/exercises/answers/Answer1c.java new file mode 100644 index 000000000..ff31b6378 --- /dev/null +++ b/docs/teaching/exercises/answers/Answer1c.java @@ -0,0 +1,21 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +aspect Answer1c { + declare error + : set(private * *) + && !(withincode(* set*(..)) || withincode(new(..))) + && within(figures.*) + : "bad field set"; +} diff --git a/docs/teaching/exercises/answers/Answer1d.java b/docs/teaching/exercises/answers/Answer1d.java new file mode 100644 index 000000000..9fb32390b --- /dev/null +++ b/docs/teaching/exercises/answers/Answer1d.java @@ -0,0 +1,23 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +aspect Answer1c { + declare error + : set(private * *) + && !(withincode(* set*(..)) + || withincode(new(..)) + || withincode(void figures.Point.move(int, int))) + && within(figures.*) + : "bad field set"; +} diff --git a/docs/teaching/exercises/answers/Answer2a.java b/docs/teaching/exercises/answers/Answer2a.java new file mode 100644 index 000000000..b874f06eb --- /dev/null +++ b/docs/teaching/exercises/answers/Answer2a.java @@ -0,0 +1,26 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.Point; +import figures.FigureElement; + +public aspect Answer2a { + before(int newValue): set(int Point.*) && args(newValue) { + if (newValue < FigureElement.MIN_VALUE) { + throw new IllegalArgumentException("too small"); + } else if (newValue > FigureElement.MAX_VALUE) { + throw new IllegalArgumentException("too large"); + } + } +} diff --git a/docs/teaching/exercises/answers/Answer2b.java b/docs/teaching/exercises/answers/Answer2b.java new file mode 100644 index 000000000..fc45ad265 --- /dev/null +++ b/docs/teaching/exercises/answers/Answer2b.java @@ -0,0 +1,26 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.Group; +import figures.FigureElement; + +public aspect Answer2b { + before(FigureElement newValue): + call(void Group.add(FigureElement)) + && args(newValue) { + if (newValue == null) { + throw new IllegalArgumentException("null not allowed"); + } + } +} diff --git a/docs/teaching/exercises/answers/Answer2c.java b/docs/teaching/exercises/answers/Answer2c.java new file mode 100644 index 000000000..6dbd7fa90 --- /dev/null +++ b/docs/teaching/exercises/answers/Answer2c.java @@ -0,0 +1,31 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.Group; +import figures.FigureElement; + +public aspect Answer2c { + before(FigureElement newValue, Group g): + call(void Group.add(FigureElement)) + && args(newValue) + && target(g) { + if (newValue == null) { + throw new IllegalArgumentException("null not allowed"); + } + if (newValue == g) { + throw new IllegalArgumentException("self not allowed"); + } + + } +} diff --git a/docs/teaching/exercises/answers/Answer2d.java b/docs/teaching/exercises/answers/Answer2d.java new file mode 100644 index 000000000..75937ca86 --- /dev/null +++ b/docs/teaching/exercises/answers/Answer2d.java @@ -0,0 +1,24 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.SlothfulPoint; + +aspect Answer2d { + after(int newValue, SlothfulPoint p) returning: + call(void setX(int)) && args(newValue) && target(p) { + if (newValue != p.getX()) { + throw new RuntimeException("setter didn't set"); + } + } +} diff --git a/docs/teaching/exercises/answers/Answer2e.java b/docs/teaching/exercises/answers/Answer2e.java new file mode 100644 index 000000000..a0500bc6b --- /dev/null +++ b/docs/teaching/exercises/answers/Answer2e.java @@ -0,0 +1,24 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.*; + +public aspect Answer2e { + pointcut checkpoint(Box box): + call(void move(int, int)) && target(box); + + after(Box box) returning: checkpoint(box) { + box.checkBoxness(); + } +} diff --git a/docs/teaching/exercises/answers/Answer2f.java b/docs/teaching/exercises/answers/Answer2f.java new file mode 100644 index 000000000..aad64f4e2 --- /dev/null +++ b/docs/teaching/exercises/answers/Answer2f.java @@ -0,0 +1,24 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.*; + +public aspect Answer2f { + pointcut checkpoint(Box box): + target(box) && call(public * *(..)) && !within(Answer*); + + after(Box box) returning: checkpoint(box) { + box.checkBoxness(); + } +} diff --git a/docs/teaching/exercises/answers/Answer2g.java b/docs/teaching/exercises/answers/Answer2g.java new file mode 100644 index 000000000..9bc9a521c --- /dev/null +++ b/docs/teaching/exercises/answers/Answer2g.java @@ -0,0 +1,27 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.*; + +aspect Answer2g { + int around(int val): (set(int Point._x) || set(int Point._y)) + && args(val) { + return proceed(trim(val)); + } + + private int trim(int val) { + return Math.max(Math.min(val, FigureElement.MAX_VALUE), + FigureElement.MIN_VALUE); + } +} diff --git a/docs/teaching/exercises/answers/Answer2h.java b/docs/teaching/exercises/answers/Answer2h.java new file mode 100644 index 000000000..3dcc47bef --- /dev/null +++ b/docs/teaching/exercises/answers/Answer2h.java @@ -0,0 +1,31 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.*; + +import java.awt.Rectangle; + +aspect Answer2h { + void around(FigureElement fe, int dx, int dy): + target(fe) && call(void move(int, int)) && args(dx, dy) { + Rectangle preBounds = new Rectangle(fe.getBounds()); + proceed(fe, dx, dy); + + preBounds.translate(dx, dy); + + if (!preBounds.equals(fe.getBounds())) { + throw new IllegalStateException("bounds don't match move"); + } + } +} diff --git a/docs/teaching/exercises/answers/Answer3a.java b/docs/teaching/exercises/answers/Answer3a.java new file mode 100644 index 000000000..7d9c91f0e --- /dev/null +++ b/docs/teaching/exercises/answers/Answer3a.java @@ -0,0 +1,23 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import support.Log; + +import figures.Point; + +aspect Answer3a { + before(): execution(void Point.move(int, int)) { + Log.log("moving"); + } +} diff --git a/docs/teaching/exercises/answers/Answer3b.java b/docs/teaching/exercises/answers/Answer3b.java new file mode 100644 index 000000000..b978f1a5e --- /dev/null +++ b/docs/teaching/exercises/answers/Answer3b.java @@ -0,0 +1,26 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import support.Log; + +import figures.Point; +import figures.Group; +import figures.FigureElement; + +aspect Answer3b { + before(): + execution(void Group.add(FigureElement)) && args(Point) { + Log.log("adding Point"); + } +} diff --git a/docs/teaching/exercises/answers/Answer3c.java b/docs/teaching/exercises/answers/Answer3c.java new file mode 100644 index 000000000..68797b9e0 --- /dev/null +++ b/docs/teaching/exercises/answers/Answer3c.java @@ -0,0 +1,34 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import support.Log; + +import figures.Point; +import figures.Group; +import figures.FigureElement; + +aspect Answer3c { + private Group Point.enclosingGroup = null; + + before(Point p, Group g): + execution(void add(FigureElement)) && args(p) && target(g) { + p.enclosingGroup = g; + } + + before(Point p): + call(void move(int, int)) && target(p) { + Log.log("moving as a part of " + p.enclosingGroup); + } + +} diff --git a/docs/teaching/exercises/answers/Answer4a.java b/docs/teaching/exercises/answers/Answer4a.java new file mode 100644 index 000000000..2f3956aea --- /dev/null +++ b/docs/teaching/exercises/answers/Answer4a.java @@ -0,0 +1,28 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.FigureElement; +import figures.Group; +import java.awt.Rectangle; + +aspect Answer4a { + private Rectangle wholeCanvas = + new Rectangle(FigureElement.MIN_VALUE, FigureElement.MIN_VALUE, + FigureElement.MAX_VALUE - FigureElement.MIN_VALUE, + FigureElement.MAX_VALUE - FigureElement.MIN_VALUE); + + Rectangle around(): execution(Rectangle Group.getBounds()) { + return wholeCanvas; + } +} diff --git a/docs/teaching/exercises/answers/Answer4b.java b/docs/teaching/exercises/answers/Answer4b.java new file mode 100644 index 000000000..d0292b14f --- /dev/null +++ b/docs/teaching/exercises/answers/Answer4b.java @@ -0,0 +1,29 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.FigureElement; +import figures.Group; +import java.awt.Rectangle; + +aspect Answer4b { + private Rectangle Group.cache = null; + + Rectangle around(Group g): + execution(Rectangle Group.getBounds()) && this(g) { + if (g.cache == null) { + g.cache = proceed(g); + } + return g.cache; + } +} diff --git a/docs/teaching/exercises/answers/Answer4c.java b/docs/teaching/exercises/answers/Answer4c.java new file mode 100644 index 000000000..54107cf8f --- /dev/null +++ b/docs/teaching/exercises/answers/Answer4c.java @@ -0,0 +1,33 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.FigureElement; +import figures.Group; +import java.awt.Rectangle; + +aspect Answer4c { + private Rectangle Group.cache = null; + + Rectangle around(Group g): + execution(Rectangle Group.getBounds()) && this(g) { + if (g.cache == null) { + g.cache = proceed(g); + } + return g.cache; + } + + before(Group g): call(void move(int, int)) && target(g) { + g.cache = null; + } +} diff --git a/docs/teaching/exercises/answers/Answer4d.java b/docs/teaching/exercises/answers/Answer4d.java new file mode 100644 index 000000000..10d90f6dd --- /dev/null +++ b/docs/teaching/exercises/answers/Answer4d.java @@ -0,0 +1,42 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.FigureElement; +import figures.Group; +import figures.Point; +import java.awt.Rectangle; + +aspect Answer4d { + private Rectangle Group.cache = null; + private Group Point.enclosingGroup = null; + + before(Point p, Group g): + execution(void add(FigureElement)) && args(p) && target(g) { + p.enclosingGroup = g; + } + + Rectangle around(Group g): + execution(Rectangle Group.getBounds()) && this(g) { + if (g.cache == null) { + g.cache = proceed(g); + } + return g.cache; + } + + before(Point p): set(* Point.*) && target(p) { + if (p.enclosingGroup != null) { + p.enclosingGroup.cache = null; + } + } +} diff --git a/docs/teaching/exercises/answers/Answer4e.java b/docs/teaching/exercises/answers/Answer4e.java new file mode 100644 index 000000000..20f98909a --- /dev/null +++ b/docs/teaching/exercises/answers/Answer4e.java @@ -0,0 +1,44 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * PARC initial implementation + * ******************************************************************/ + +package answers; + +import figures.FigureElement; +import figures.Group; +import figures.Point; +import java.awt.Rectangle; + +aspect Answer4e { + private Rectangle Group.cache = null; + private Group FigureElement.enclosingGroup = null; + + before(FigureElement p, Group g): + execution(void add(FigureElement)) && args(p) && target(g) { + p.enclosingGroup = g; + } + + Rectangle around(Group g): + execution(Rectangle Group.getBounds()) && this(g) { + if (g.cache == null) { + g.cache = proceed(g); + } + return g.cache; + } + + before(Point p): set(* Point.*) && target(p) { + FigureElement fe = p; + while (fe.enclosingGroup != null) { + fe.enclosingGroup.cache = null; + fe = fe.enclosingGroup; + } + } +} |