import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @interface Colored { String color(); } public class InitBinding { public static void main(String[]argv) { new A(); new B(); new C(); X.verifyRun(); } } class A { @Colored(color="orange") public A() {} } class B { @Colored(color="yellow") public B() {} } class C { @Colored(color="brown") public C() {} } aspect X { // Expected color order static String exp[] = new String[]{"orange","yellow","brown"}; static int i = 0; // Count of advice executions before(Colored c): preinitialization(new(..)) && !within(X) && @annotation(c) { System.err.println(thisJoinPoint+" color="+c.color()); if (!c.color().equals(exp[i])) throw new RuntimeException("not "+exp[i]+"? "+c.color()); i++; } public static void verifyRun() { if (X.i != exp.length) throw new RuntimeException("Expected "+exp.length+" advice runs but did "+X.i); } } //aspect X { // int i = 0; // // before(Colored c): preinitialization(new(..)) && !within(X) && @annotation(c) { // i++; // System.err.println(thisJoinPoint+" color="+c.color()); // if (i==1 && !c.color().equals("orange")) throw new RuntimeException("First time through should be red, but is "+c.color()); // if (i==2 && !c.color().equals("yellow")) throw new RuntimeException("Second time through should be green, but is "+c.color()); // if (i==3 && !c.color().equals("brown")) throw new RuntimeException("Third time through should be blue, but is "+c.color()); // System.err.println(c.color()); // } //} // ns-logging-1.2 A seamless aspect-oriented extension to the Java programming language: https://github.com/eclipse-aspectj/aspectjwww-data
summaryrefslogtreecommitdiffstats
path: root/tests/incremental/model/introduction/changes/Point.30.java
blob: 609a0488c7c30b58c5114c070db1cc8a24657362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
  Copyright (c) Xerox Corporation 1998-2002.  All rights reserved.

  Use and copying of this software and preparation of derivative works based
  upon this software are permitted.  Any distribution of this software or
  derivative works must comply with all applicable United States export control
  laws.

  This software is made available AS IS, and Xerox Corporation makes no warranty
  about the software, its performance or its conformity to any specification.
*/

package introduction;

public class Point {

   protected double x = 0;
   protected double y = 0;
   protected double theta = 0;
   protected double rho = 0;

   protected boolean polar = true;
   protected boolean rectangular = true;

   public double getX(){
      makeRectangular();
      return x;
   }

   public double getY(){
      makeRectangular();
      return y;
   }

   public double getTheta(){
      makePolar();
      return theta;
   }

   public double getRho(){
      makePolar();
      return rho;
   }

   public void setRectangular(double newX, double newY){
      x = newX;
      y = newY;
      rectangular = true;
      polar = false;
   }

   public void setPolar(double newTheta, double newRho){
      theta = newTheta;
      rho = newRho;
      rectangular = false;
      polar = true;
   }

   public void rotate(double angle){
      setPolar(theta + angle, rho);
   }

   public void offset(double deltaX, double deltaY){
      setRectangular(x + deltaX, y + deltaY);
   }

   protected void makePolar(){
      if (!polar){
	 theta = Math.atan2(y,x);
	 rho = y / Math.sin(theta);
	 polar = true;
      }
   }

   protected void makeRectangular(){
      if (!rectangular) {
	 x = rho * Math.sin(theta);
	 y = rho * Math.cos(theta);
	 rectangular = true;
      }
   }

   public String toString(){
      return "(" + getX() + ", " + getY() + ")[" 
	 + getTheta() + " : " + getRho() + "]";
   }

   public static void main(String[] args){
      Point p1 = new Point();
      System.out.println("p1 =" + p1);
      p1.setRectangular(5,2);
      System.out.println("p1 =" + p1);
      p1.setPolar( Math.PI / 4.0 , 1.0);
      System.out.println("p1 =" + p1);
      p1.setPolar( 0.3805 , 5.385);
      System.out.println("p1 =" + p1);
   }
}