001: package soot.toolkits.purity;
002:
003: /**
004: * This example is from the article "A Combined Pointer and Purity Analysis for
005: * Java Programs" by Alexandru Salcianu and Martin Rinard.
006: * It is supposed to demonstrate the purity analysis (-annot-purity)
007: *
008: * by Antoine Mine, 2005/02/08
009: */
010:
011: class List {
012:
013: Cell head = null;
014:
015: void add(Object e) {
016: head = new Cell(e, head);
017: }
018:
019: Iterator iterator() {
020: return new ListItr(head);
021: }
022: }
023:
024: class Cell {
025:
026: Object data;
027: Cell next;
028:
029: Cell(Object d, Cell n) {
030: data = d;
031: next = n;
032: }
033: }
034:
035: interface Iterator {
036: boolean hasNext();
037:
038: Object next();
039: }
040:
041: class ListItr implements Iterator {
042:
043: Cell cell;
044:
045: ListItr(Cell head) {
046: cell = head;
047: }
048:
049: public boolean hasNext() {
050: return cell != null;
051: }
052:
053: public Object next() {
054: Object result = cell.data;
055: cell = cell.next;
056: return result;
057: }
058: }
059:
060: class Point {
061:
062: float x, y;
063:
064: Point(float x, float y) {
065: this .x = x;
066: this .y = y;
067: }
068:
069: void flip() {
070: float t = x;
071: x = y;
072: y = t;
073: }
074:
075: void print() {
076: System.out.print("(" + x + "," + y + ")");
077: }
078: }
079:
080: public class PurityTest {
081:
082: static float sumX(List list) {
083: float s = 0;
084: Iterator it = list.iterator();
085: while (it.hasNext()) {
086: Point p = (Point) it.next();
087: s += p.x;
088: }
089: return s;
090: }
091:
092: static void flipAll(List list) {
093: Iterator it = list.iterator();
094: while (it.hasNext()) {
095: Point p = (Point) it.next();
096: p.flip();
097: }
098: }
099:
100: static void print(List list) {
101: Iterator it = list.iterator();
102: System.out.print("[");
103: while (it.hasNext()) {
104: Point p = (Point) it.next();
105: p.print();
106: if (it.hasNext())
107: System.out.print(";");
108: }
109: System.out.println("]");
110: }
111:
112: public static void main(String args[]) {
113: List list = new List();
114: list.add(new Point(1, 2));
115: list.add(new Point(2, 3));
116: list.add(new Point(3, 4));
117: print(list);
118: System.out.println("sum=" + sumX(list));
119: sumX(list);
120: print(list);
121: flipAll(list);
122: print(list);
123: }
124: }
|