SlideShare a Scribd company logo
Future Endeavors in Automated
Refactoring of Legacy Java Software
to Enumerated Types*
Raffi Khatchadourian, Jason Sawin, and Atanas Rountev
PRESTO: Program Analyses and Software Tools Research
Group, Ohio State University
* Work supported in part by NSF
Motivation
2
Motivation
• Software changes over
time:
2
Motivation
• Software changes over
time:
• Requirements evolve
2
Motivation
• Software changes over
time:
• Requirements evolve
• Different platforms
(e.g., mobile devices)
2
Motivation
• Software changes over
time:
• Requirements evolve
• Different platforms
(e.g., mobile devices)
• New framework
versions (e.g., XML vs.
annotation-based)
2
Motivation
Motivation
Changing and/or maintaining large, complex software
systems can be non-trivial:
Motivation
Changing and/or maintaining large, complex software
systems can be non-trivial:
Tedious:May require changing many lines of
code.
Motivation
Changing and/or maintaining large, complex software
systems can be non-trivial:
Tedious:May require changing many lines of
code.
Adding a
parameter to a
method
Motivation
Changing and/or maintaining large, complex software
systems can be non-trivial:
Tedious:May require changing many lines of
code.
Error-prone: Changes may be implemented
incorrectly.
Motivation
Changing and/or maintaining large, complex software
systems can be non-trivial:
Tedious:May require changing many lines of
code.
Error-prone: Changes may be implemented
incorrectly.
Removing a
method parameter may
alter overloading to
overriding
Motivation
Changing and/or maintaining large, complex software
systems can be non-trivial:
Tedious:May require changing many lines of
code.
Error-prone: Changes may be implemented
incorrectly.
Omission-
prone:
May opportunities to produce better
code.
Motivation
Changing and/or maintaining large, complex software
systems can be non-trivial:
Tedious:May require changing many lines of
code.
Error-prone: Changes may be implemented
incorrectly.
Omission-
prone:
May opportunities to produce better
code.
HashTable vs.
HashMap
Motivation
Changing and/or maintaining large, complex software
systems can be non-trivial:
Tedious:May require changing many lines of
code.
Error-prone: Changes may be implemented
incorrectly.
Omission-
prone:
May opportunities to produce better
code.
Solution?
Solution?
• Approaches made to
provide mechanical
assistance in evolution
tasks.
Solution?
• Approaches made to
provide
assistance in evolution
tasks.
• Typically in the form of
plug-ins to IDEs.
Solution?
• Approaches made to
provide
assistance in evolution
tasks.
• Typically in the form of
plug-ins to IDEs.
• Ease the burden of
software maintenance
and evolution.
Solution?
• Approaches made to
provide
assistance in evolution
tasks.
• Typically in the form of
plug-ins to IDEs.
• Ease the burden of
software maintenance
and evolution.
Restrict
workspace to only
displays elements
relevant to the
task
Solution?
• Approaches made to
provide
assistance in evolution
tasks.
• Typically in the form of
plug-ins to IDEs.
• Ease the burden of
software maintenance
and evolution.
Restrict
workspace to only
displays elements
relevant to the
task
Restructure
code while preserving
semantics (i.e.,
refactoring)
Introduction
Introduction
•Java 5 introduced a rich set of new features such as
generics, metadata annotations, boxing/unboxing,
and type-safe enumerations.
Introduction
•Java 5 introduced a rich set of new features such as
generics, metadata annotations, boxing/unboxing,
and
•Highlight an automated semantics-preserving
approach for migrating legacy Java code to take
advantage of the new language enumerated type
constructs.
Introduction
•Java 5 introduced a rich set of new features such as
generics, metadata annotations, boxing/unboxing,
and
•Highlight an automated semantics-preserving
approach for migrating legacy Java code to take
advantage of the new language enumerated type
constructs.
•Present experimental results from research prototype
as an Eclipse IDE plug-in.
Introduction
•Java 5 introduced a rich set of new features such as
generics, metadata annotations, boxing/unboxing,
and
•Highlight an automated semantics-preserving
approach for migrating legacy Java code to take
advantage of the new language enumerated type
constructs.
•Present experimental results from research prototype
as an Eclipse IDE plug-in.
•In progress to be included with the standard
distribution of Eclipse.
Introduction
•Java 5 introduced a rich set of new features such as
generics, metadata annotations, boxing/unboxing,
and
•Highlight an automated semantics-preserving
approach for migrating legacy Java code to take
advantage of the new language enumerated type
constructs.
•Present experimental results from research prototype
as an Eclipse IDE plug-in.
•In progress to be included with the standard
distribution of Eclipse.
•Discuss directions for future work.
Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Motivating Example
Weak Enum
Pattern
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Motivating Example
Type Safety
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Manual
Enumeration
Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Namespacing
Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Brittle
Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Language Enum
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Type Safety
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Singletons in
Natural Order
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Prefixed
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Supports
Separate
Compilation
Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
Automobile
Action Enum
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
Named-
Constant
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
Promising...
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
Definitely not
enumerizable
Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W  {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Enumerization Approach
function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W  {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Subset of static
final fields
Enumerization Approach
function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W  {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Abstract Syntax Tree (AST)
Enumerization Approach
function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W  {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
Output:
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Enumerization Approach
• Partitioning of program entities:
• Fields
• Method declarations (return types)
• Local variables (including formal parameters)
function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W  {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
Output:
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Enumerization Approach
• Partitioning of program entities:
• Fields
• Method declarations (return types)
• Local variables (including formal parameters)
• Each partition contains elements that:
• Transitively type-dependent upon one another
• Are safe for enum type transformation
function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W  {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
Output:
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Enumerization Approach
Future Endeavors in Automated Refactoring of Legacy Java Software to Enumerated Types
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
1 class TrafficSignal {
2 public enum Color {RED,
3 YELLOW,
4 GREEN};
5 /* Current color of the traffic signal, initially red by default */
6 private Color color = Color.RED;
7 /* Accessor for the light’s current color */
8 public Color getColor() {return this.color;
9
10 class Automobile {
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
1 class TrafficSignal {
2 public enum Color {RED,
3 YELLOW,
4 GREEN};
5 /* Current color of the traffic signal, initially red by default */
6 private Color color = Color.RED;
7 /* Accessor for the light’s current color */
8 public Color getColor() {return this.color;
9
10 class Automobile {
color
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
1 class TrafficSignal {
2 public enum Color {RED,
3 YELLOW,
4 GREEN};
5 /* Current color of the traffic signal, initially red by default */
6 private Color color = Color.RED;
7 /* Accessor for the light’s current color */
8 public Color getColor() {return this.color;}}
9
0 class Automobile {
1 private enum Action {IDLE,
2 INCREASE_SPEED,
color
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
1 class TrafficSignal {
2 public enum Color {RED,
3 YELLOW,
4 GREEN};
5 /* Current color of the traffic signal, initially red by default */
6 private Color color = Color.RED;
7 /* Accessor for the light’s current color */
8 public Color getColor() {return this.color;}}
9
0 class Automobile {
1 private enum Action {IDLE,
2 INCREASE_SPEED,
color
getColor
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
color
getColor
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by defaul
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by defa
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
28 return INCREASE_SPEED;
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
color
getColor
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by defaul
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by defa
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
28 return INCREASE_SPEED;
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
color
getColor
13 private static final int INCREASE_SPEED =
14 private static final int DECREASE_SPEED =
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by d
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
28 return INCREASE_SPEED;
29 else return STOP;
30 case TrafficSignal.GREEN: // no change
31 return this.currentAction;
32 default: throw new IllegalArgumentExce
33 ("Invalid traffic color");}} // required
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by defaul
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
color
getColor
13 private static final int INCREASE_SPEED =
14 private static final int DECREASE_SPEED =
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by d
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
28 return INCREASE_SPEED;
29 else return STOP;
30 case TrafficSignal.GREEN: // no change
31 return this.currentAction;
32 default: throw new IllegalArgumentExce
33 ("Invalid traffic color");}} // required
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by defaul
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
color
getColor
2 public static final int RED = 0;
3 public static final int RED_YELLOW = 1;
4 public static final int YELLOW = 2;
5 public static final int GREEN = 3;
6 / Current color of the traffic signal, initially red by default /
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1
14 private static final int DECREASE_SPEED = 2
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by de
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
color
getColor
2 public static final int RED = 0;
3 public static final int RED_YELLOW = 1;
4 public static final int YELLOW = 2;
5 public static final int GREEN = 3;
6 / Current color of the traffic signal, initially red by default /
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1
14 private static final int DECREASE_SPEED = 2
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by de
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
currentAction
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
color
getColor
currentAction
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by default /
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
RED;
urrent color /
() {return this.color;}}
l int IDLE = 0;
l int INCREASE_SPEED = 1;
l int DECREASE_SPEED = 2;
l int STOP = 3;
l int MAX_SPEED = 140;
e is currently performing, idle by default /
Action = IDLE;
automobile, initially 5 mph. /
Speed = 5;
rafficSignal signal) {
Color()) {
nal.RED: return STOP;
nal.YELLOW:
o stop or go
ldGo())
6 private C
7 /* Accessor f
8 public Co
9
10 class Autom
11 private e
12 INCREAS
13 DECREAS
14 STOP};
15 private s
16 /* The action
17 private A
18 /* The curren
19 private i
20
21 private A
22 switch(
23 case
24 case
25 // de
26 if
27 r
public int getColor() {return this.color;}}
lass Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
unning example: a hypothetical drive-by-wire applica
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
l signal) {
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
22 switch(signal.getColor(
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOPIDLE
color
getColor
currentAction
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by default /
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
RED;
urrent color /
() {return this.color;}}
l int IDLE = 0;
l int INCREASE_SPEED = 1;
l int DECREASE_SPEED = 2;
l int STOP = 3;
l int MAX_SPEED = 140;
e is currently performing, idle by default /
Action = IDLE;
automobile, initially 5 mph. /
Speed = 5;
rafficSignal signal) {
Color()) {
nal.RED: return STOP;
nal.YELLOW:
o stop or go
ldGo())
6 private C
7 /* Accessor f
8 public Co
9
10 class Autom
11 private e
12 INCREAS
13 DECREAS
14 STOP};
15 private s
16 /* The action
17 private A
18 /* The curren
19 private i
20
21 private A
22 switch(
23 case
24 case
25 // de
26 if
27 r
public int getColor() {return this.color;}}
lass Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
unning example: a hypothetical drive-by-wire applica
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
l signal) {
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
22 switch(signal.getColor(
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOP
IDLE
color
getColor
currentAction
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by default /
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
RED;
urrent color /
() {return this.color;}}
l int IDLE = 0;
l int INCREASE_SPEED = 1;
l int DECREASE_SPEED = 2;
l int STOP = 3;
l int MAX_SPEED = 140;
e is currently performing, idle by default /
Action = IDLE;
automobile, initially 5 mph. /
Speed = 5;
rafficSignal signal) {
Color()) {
nal.RED: return STOP;
nal.YELLOW:
o stop or go
ldGo())
6 private C
7 /* Accessor f
8 public Co
9
10 class Autom
11 private e
12 INCREAS
13 DECREAS
14 STOP};
15 private s
16 /* The action
17 private A
18 /* The curren
19 private i
20
21 private A
22 switch(
23 case
24 case
25 // de
26 if
27 r
public int getColor() {return this.color;}}
lass Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
unning example: a hypothetical drive-by-wire applica
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
l signal) {
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
22 switch(signal.getColor(
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
react
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOP
IDLE
color
getColor
currentAction
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by default /
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
RED;
urrent color /
() {return this.color;}}
l int IDLE = 0;
l int INCREASE_SPEED = 1;
l int DECREASE_SPEED = 2;
l int STOP = 3;
l int MAX_SPEED = 140;
e is currently performing, idle by default /
Action = IDLE;
automobile, initially 5 mph. /
Speed = 5;
rafficSignal signal) {
Color()) {
nal.RED: return STOP;
nal.YELLOW:
o stop or go
ldGo())
6 private C
7 /* Accessor f
8 public Co
9
10 class Autom
11 private e
12 INCREAS
13 DECREAS
14 STOP};
15 private s
16 /* The action
17 private A
18 /* The curren
19 private i
20
21 private A
22 switch(
23 case
24 case
25 // de
26 if
27 r
public int getColor() {return this.color;}}
lass Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
unning example: a hypothetical drive-by-wire applica
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
l signal) {
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
22 switch(signal.getColor(
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
react
DECREASE_SPEED
MAX_SPEED
GREEN
YELLOW
RED
INCREASE_SPEED
STOP
IDLE
color
getColor
currentAction
7 private int color = RED;
8 / Accessor for the light’s current color /
9 public int getColor() {return this.color;}}
10
11 class Automobile {
12 private static final int IDLE = 0;
13 private static final int INCREASE_SPEED = 1;
14 private static final int DECREASE_SPEED = 2;
15 private static final int STOP = 3;
16 private static final int MAX_SPEED = 140;
17 / The action this automobile is currently performing, idle by default /
18 private int currentAction = IDLE;
19 / The current speed of the automobile, initially 5 mph. /
20 private int currentSpeed = 5;
21
22 private int react(TrafficSignal signal) {
23 switch(signal.getColor()) {
24 case TrafficSignal.RED: return STOP;
25 case TrafficSignal.YELLOW:
26 // decide whether to stop or go
27 if (this.shouldGo())
RED;
urrent color /
() {return this.color;}}
l int IDLE = 0;
l int INCREASE_SPEED = 1;
l int DECREASE_SPEED = 2;
l int STOP = 3;
l int MAX_SPEED = 140;
e is currently performing, idle by default /
Action = IDLE;
automobile, initially 5 mph. /
Speed = 5;
rafficSignal signal) {
Color()) {
nal.RED: return STOP;
nal.YELLOW:
o stop or go
ldGo())
6 private C
7 /* Accessor f
8 public Co
9
10 class Autom
11 private e
12 INCREAS
13 DECREAS
14 STOP};
15 private s
16 /* The action
17 private A
18 /* The curren
19 private i
20
21 private A
22 switch(
23 case
24 case
25 // de
26 if
27 r
public int getColor() {return this.color;}}
lass Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
unning example: a hypothetical drive-by-wire applica
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
or enumerated types.
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
l signal) {
turn STOP;
// no change
n;
ArgumentException
);}} // required
;
ignal);
Action &&
EED ||
X_SPEED))
ction);}
action) {...}}
22 switch(signal.getColor(
23 case TrafficSignal.RE
24 case TrafficSignal.YE
25 // decide whether to stop or
26 if (this.shouldGo()
27 return Action.INC
28 else return Action.
29 case TrafficSignal.GR
30 return this.current
31 default: throw new Il
32 ("Invalid traffic c
33
34 public void drive() {
35 TrafficSignal aSignal =
36 Action reaction = this.
37 if (reaction != this.cu
38 (reaction != Action
39 this.currentSpeed
40 this.performActio
41
42 private void performActio
(b) Improvements afte
Identifiers
function EA(α,ID)
1: return EA(α, Parent(ID))
Parenthesized expressions
function EA(α,(ID))
1: return EA(α, Parent(ID))
Cast expressions
function EA(α, (TYPE)EXP)
1: return false
Field access expressions
function EA(α, EXP.ID)
1: return EA(α, Parent(EXP))
Assignment expressions
function EA(α,EXP1 = EXP2)
1: return ED(EXP1) ∧ ED(EXP2)
Subtract assignment expressions
function EA(α,EXP1 -= EXP2)
1: return false
Divide assignment expressions
function EA(α,EXP1 /= EXP2)
1: return false
Infix addition expressions
function EA(α,EXP1 + EXP2)
1: return false
Infix multiplication expressions
function EA(α,EXP1 * EXP2)
1: return false
Prefix unary minus expressions
function EA(α,-EXP)
1: return false
Postfix increment expressions
function EA(α,EXP++)
1: return false
Equality expressions
function EA(α,EXP1 == EXP2)
1: return ED(EXP1) ∧ ED(EXP2)
Inequality expressions
function EA(α,EXP1 != EXP2)
1: return ED(EXP1) ∧ ED(EXP2)
Switch statements
function EA(α, switch(EXP))
1: let se = ED(EXP)
2: let ce = true
3: for all case EXPc ∈ cases(switch(EXP)) do
4: ce ← ce ∧ ED(EXPc)
5: end for
6: return se ∧ ce
Switch case statements
function EA(α,case EXP)
1: return EA(α, switchStmt(case EXP))
Conditional expressions
function EA(α, EXP1 ? EXP2 : EXP3)
1: if contains(EXP2, α) ∨ contains(EXP3, α) then
2: return EA(α, Parent(EXP1 ? EXP2 : EXP3))
3: else
4: return true
5: end if
Array access/creation expressions
function EA(α, EXP1[EXP2])
1: if contains(EXP2, α) then
2: return false
3: else
4: return EA(α, Parent(EXP1))
5: end if
Array initialization expressions
function EA(α, {EXP1, . . . , EXPn})
1: let ie = true
2: for EXPi, 1 ≤ i ≤ n do
3: ie ← ie ∧ ED(EXPi)
4: end for
5: return ie
Return statements
function EA(α,return EXP)
1: return true
Method declaration statements
function EA(α,ID(P1, . . . ,Pn))
1: let re = true
2: for all return EXPr ∈ returnStmts(ID(P1, . . . , Pn)) do
3: re ← re ∧ ED(EXPr)
4: end for
5: return re
Formal parameters
function EA(α,Pi)
1: let ae = true
2: /*check the ith argument of each invocation of the declaring
method*/
3: let ˆα = MethodDecl(Pi)
4: for all ˆαctxt ∈ Invocations(ˆα, P) do
5: ae ← ae ∧ ED(Arg(ˆαctxt, i))
6: end for
7: return ae
Method invocation expressions
function EA(α,ID(EXP1, . . . , EXPn))
1: for EXPi, 1 ≤ i ≤ n do
2: if contains(EXPi, α) then
3: return true
4: end if
5: end for
6: return EA(α, Parent(ID(EXP1, . . . , EXPn)))
General statements
function XA(α,SMT)
1: let se = true
2: for EXP ∈ Children(SMT) do
3: se ← se ∨ED(EXP)
4: end for
5: return se
Figure 7. Enumerizable ascender.
10
Identifiers
function EA(α,ID)
1: return EA(α, Parent(ID))
Parenthesized expressions
function EA(α,(ID))
1: return EA(α, Parent(ID))
Cast expressions
function EA(α, (TYPE)EXP)
1: return false
Field access expressions
function EA(α, EXP.ID)
1: return EA(α, Parent(EXP))
Assignment expressions
function EA(α,EXP1 = EXP2)
1: return ED(EXP1) ∧ ED(EXP2)
Subtract assignment expressions
function EA(α,EXP1 -= EXP2)
1: return false
Divide assignment expressions
function EA(α,EXP1 /= EXP2)
1: return false
Infix addition expressions
function EA(α,EXP1 + EXP2)
1: return false
Infix multiplication expressions
function EA(α,EXP1 * EXP2)
1: return false
Prefix unary minus expressions
function EA(α,-EXP)
1: return false
Postfix increment expressions
function EA(α,EXP++)
1: return false
Equality expressions
function EA(α,EXP1 == EXP2)
1: return ED(EXP1) ∧ ED(EXP2)
Inequality expressions
function EA(α,EXP1 != EXP2)
1: return ED(EXP1) ∧ ED(EXP2)
Switch statements
function EA(α, switch(EXP))
1: let se = ED(EXP)
2: let ce = true
3: for all case EXPc ∈ cases(switch(EXP)) do
4: ce ← ce ∧ ED(EXPc)
5: end for
6: return se ∧ ce
Switch case statements
function EA(α,case EXP)
1: return EA(α, switchStmt(case EXP))
Conditional expressions
function EA(α, EXP1 ? EXP2 : EXP3)
1: if contains(EXP2, α) ∨ contains(EXP3, α) then
2: return EA(α, Parent(EXP1 ? EXP2 : EXP3))
3: else
4: return true
5: end if
Array access/creation expressions
function EA(α, EXP1[EXP2])
1: if contains(EXP2, α) then
2: return false
3: else
4: return EA(α, Parent(EXP1))
5: end if
Array initialization expressions
function EA(α, {EXP1, . . . , EXPn})
1: let ie = true
2: for EXPi, 1 ≤ i ≤ n do
3: ie ← ie ∧ ED(EXPi)
4: end for
5: return ie
Return statements
function EA(α,return EXP)
1: return true
Method declaration statements
function EA(α,ID(P1, . . . ,Pn))
1: let re = true
2: for all return EXPr ∈ returnStmts(ID(P1, . . . , Pn)) do
3: re ← re ∧ ED(EXPr)
4: end for
5: return re
Formal parameters
function EA(α,Pi)
1: let ae = true
2: /*check the ith argument of each invocation of the declaring
method*/
3: let ˆα = MethodDecl(Pi)
4: for all ˆαctxt ∈ Invocations(ˆα, P) do
5: ae ← ae ∧ ED(Arg(ˆαctxt, i))
6: end for
7: return ae
Method invocation expressions
function EA(α,ID(EXP1, . . . , EXPn))
1: for EXPi, 1 ≤ i ≤ n do
2: if contains(EXPi, α) then
3: return true
4: end if
5: end for
6: return EA(α, Parent(ID(EXP1, . . . , EXPn)))
General statements
function XA(α,SMT)
1: let se = true
2: for EXP ∈ Children(SMT) do
3: se ← se ∨ED(EXP)
4: end for
5: return se
Figure 7. Enumerizable ascender.
10
Identifiers
function XA(α,ID)
1: return XA(α, Parent(ID))
Parenthesized expressions
function XA(α,(ID))
1: return XA(α, Parent(ID))
Cast expressions
function XA(α, (TYPE)EXP)
1: return ∅
Field access expressions
function XA(α, EXP.ID)
1: return XA(α, Parent(EXP))
Assignment expressions
function XA(α,EXP1 = EXP2)
1: return XD(EXP1) ∪ XD(EXP2)
Subtract assignment expressions
function XA(α,EXP1 -= EXP2)
1: return ∅
Divide assignment expressions
function XA(α,EXP1 /= EXP2)
1: return ∅
Infix addition expressions
function XA(α,EXP1 + EXP2)
1: return ∅
Infix multiplication expressions
function XA(α,EXP1 * EXP2)
1: return ∅
Prefix unary minus expressions
function XA(α,-EXP)
1: return ∅
Postfix increment expressions
function XA(α,EXP++)
1: return ∅
Equality expressions
function XA(α,EXP1 == EXP2)
1: return XD(EXP2) ∪ XD(EXP1)
Inequality expressions
function XA(α,EXP1 != EXP2)
1: return XD(EXP1) ∪ XD(EXP2)
Switch statements
function XA(α, switch(EXP))
1: R ← XD(EXP)
2: for all case EXPc ∈ cases(switch(EXP)) do
3: R ← XD(EXPc)
4: end for
5: return R
Switch case statements
function XA(α,case EXP)
1: return XA(α, switchStmt(case EXP))
Conditional expressions
function XA(α, EXP1 ? EXP2 : EXP3)
1: if contains(EXP2, α) ∨ contains(EXP3, α) then
2: return XA(α, Parent(EXP1 ? EXP2 : EXP3))
3: else
4: return ∅
5: end if
Array access/creation expressions
function XA(α, EXP1[EXP2])
1: if contains(EXP2, α) then
2: return ∅
3: else
4: return XA(α, Parent(EXP1))
5: end if
Array initialization expressions
function XA(α, {EXP1, . . . , EXPn})
1: R ← ∅
2: for EXPi, 1 ≤ i ≤ n do
3: R ← R ∪ XD(EXPi)
4: end for
5: return R
Return statements
function XA(α,return EXP)
1: return XD(MethodDecl(return EXP))
Method declaration statements
function XA(α,ID(P1, . . . ,Pn))
1: R ← ∅
2: for all return EXPr ∈ returnStmts(ID(P1, . . . , Pn)) do
3: R ← R ∪ XD(EXPr)
4: end for
5: return R
Formal parameters
function XA(α,Pi)
1: R ← ∅
2: /*extract the ith argument of each invocation of the declaring
method*/
3: let ˆα = MethodDecl(Pi)
4: for all ˆαctxt ∈ Invocations(ˆα, P) do
5: R ← R ∪ XD(Arg(ˆαctxt, i))
6: end for
7: return R
Method invocation expressions
function XA(α,ID(EXP1, . . . , EXPn))
1: R ← ∅
2: for EXPi, 1 ≤ i ≤ n do
3: if contains(EXPi, α) then
4: R ← R ∪ XD(EXP1)
5: end if
6: end for
7: if R ̸= ∅ then
8: return R
9: else
10: return XA(α, Parent(ID(EXP1, . . . , EXPn)))
11: end if
General statements
function XA(α,SMT)
1: R ← ∅
2: for EXP ∈ Children(SMT) do
3: R ← R ∪ XD(EXP)
4: end for
5: return R
Figure 9. Extraction ascender.
12
Identifiers
function XA(α,ID)
1: return XA(α, Parent(ID))
Parenthesized expressions
function XA(α,(ID))
1: return XA(α, Parent(ID))
Cast expressions
function XA(α, (TYPE)EXP)
1: return ∅
Field access expressions
function XA(α, EXP.ID)
1: return XA(α, Parent(EXP))
Assignment expressions
function XA(α,EXP1 = EXP2)
1: return XD(EXP1) ∪ XD(EXP2)
Subtract assignment expressions
function XA(α,EXP1 -= EXP2)
1: return ∅
Divide assignment expressions
function XA(α,EXP1 /= EXP2)
1: return ∅
Infix addition expressions
function XA(α,EXP1 + EXP2)
1: return ∅
Infix multiplication expressions
function XA(α,EXP1 * EXP2)
1: return ∅
Prefix unary minus expressions
function XA(α,-EXP)
1: return ∅
Postfix increment expressions
function XA(α,EXP++)
1: return ∅
Equality expressions
function XA(α,EXP1 == EXP2)
1: return XD(EXP2) ∪ XD(EXP1)
Inequality expressions
function XA(α,EXP1 != EXP2)
1: return XD(EXP1) ∪ XD(EXP2)
Switch statements
function XA(α, switch(EXP))
1: R ← XD(EXP)
2: for all case EXPc ∈ cases(switch(EXP)) do
3: R ← XD(EXPc)
4: end for
5: return R
Switch case statements
function XA(α,case EXP)
1: return XA(α, switchStmt(case EXP))
Conditional expressions
function XA(α, EXP1 ? EXP2 : EXP3)
1: if contains(EXP2, α) ∨ contains(EXP3, α) then
2: return XA(α, Parent(EXP1 ? EXP2 : EXP3))
3: else
4: return ∅
5: end if
Array access/creation expressions
function XA(α, EXP1[EXP2])
1: if contains(EXP2, α) then
2: return ∅
3: else
4: return XA(α, Parent(EXP1))
5: end if
Array initialization expressions
function XA(α, {EXP1, . . . , EXPn})
1: R ← ∅
2: for EXPi, 1 ≤ i ≤ n do
3: R ← R ∪ XD(EXPi)
4: end for
5: return R
Return statements
function XA(α,return EXP)
1: return XD(MethodDecl(return EXP))
Method declaration statements
function XA(α,ID(P1, . . . ,Pn))
1: R ← ∅
2: for all return EXPr ∈ returnStmts(ID(P1, . . . , Pn)) do
3: R ← R ∪ XD(EXPr)
4: end for
5: return R
Formal parameters
function XA(α,Pi)
1: R ← ∅
2: /*extract the ith argument of each invocation of the declaring
method*/
3: let ˆα = MethodDecl(Pi)
4: for all ˆαctxt ∈ Invocations(ˆα, P) do
5: R ← R ∪ XD(Arg(ˆαctxt, i))
6: end for
7: return R
Method invocation expressions
function XA(α,ID(EXP1, . . . , EXPn))
1: R ← ∅
2: for EXPi, 1 ≤ i ≤ n do
3: if contains(EXPi, α) then
4: R ← R ∪ XD(EXP1)
5: end if
6: end for
7: if R ̸= ∅ then
8: return R
9: else
10: return XA(α, Parent(ID(EXP1, . . . , EXPn)))
11: end if
General statements
function XA(α,SMT)
1: R ← ∅
2: for EXP ∈ Children(SMT) do
3: R ← R ∪ XD(EXP)
4: end for
5: return R
Figure 9. Extraction ascender.
12
Algorithmic Distinction: Enums vs. Constants
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
if (reaction != this.currentAction &&
(reaction != INCREASE_SPEED ||
this.currentSpeed <= MAX_SPEED))
this.performAction(reaction);}
private void performAction(int action) {...}}
(a) Using integer constants for enumerated types.
Algorithmic Distinction: Enums vs. Constants
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
if (reaction != this.currentAction &&
(reaction != INCREASE_SPEED ||
this.currentSpeed <= MAX_SPEED))
this.performAction(reaction);}
private void performAction(int action) {...}}
(a) Using integer constants for enumerated types.
Algorithmic Distinction: Enums vs. Constants
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
if (reaction != this.currentAction &&
(reaction != INCREASE_SPEED ||
this.currentSpeed <= MAX_SPEED))
this.performAction(reaction);}
private void performAction(int action) {...}}
(a) Using integer constants for enumerated types.
Algorithmic Distinction: Enums vs. Constants
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
if (reaction != this.currentAction &&
(reaction != INCREASE_SPEED ||
this.currentSpeed <= MAX_SPEED))
this.performAction(reaction);}
private void performAction(int action) {...}}
(a) Using integer constants for enumerated types.
Algorithmic Distinction: Enums vs. Constants
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
if (reaction != this.currentAction &&
(reaction != INCREASE_SPEED ||
this.currentSpeed <= MAX_SPEED))
this.performAction(reaction);}
private void performAction(int action) {...}}
(a) Using integer constants for enumerated types.
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
Algorithmic Distinction: Enums vs. Constants
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
if (reaction != this.currentAction &&
(reaction != INCREASE_SPEED ||
this.currentSpeed <= MAX_SPEED))
this.performAction(reaction);}
private void performAction(int action) {...}}
(a) Using integer constants for enumerated types.
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
Algorithmic Distinction: Enums vs. Constants
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
if (reaction != this.currentAction &&
(reaction != INCREASE_SPEED ||
this.currentSpeed <= MAX_SPEED))
this.performAction(reaction);}
private void performAction(int action) {...}}
(a) Using integer constants for enumerated types.
?
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
Algorithmic Distinction: Enums vs. Constants
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
if (reaction != this.currentAction &&
(reaction != INCREASE_SPEED ||
this.currentSpeed <= MAX_SPEED))
this.performAction(reaction);}
private void performAction(int action) {...}}
(a) Using integer constants for enumerated types.
?
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
Algorithmic Distinction: Enums vs. Constants
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
if (reaction != this.currentAction &&
(reaction != INCREASE_SPEED ||
this.currentSpeed <= MAX_SPEED))
this.performAction(reaction);}
private void performAction(int action) {...}}
(a) Using integer constants for enumerated types.
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
Top-level Enumerization Algorithm
µ(P) {m | m is a method in P}
υ(P) {l | l is a variable in P}
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to static
identify all references to candidate elds and transitively
Top-level Enumerization Algorithm
µ(P) {m | m is a method in P}
υ(P) {l | l is a variable in P}
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to static
identify all references to candidate elds and transitively
Subset of static
final fields
Top-level Enumerization Algorithm
µ(P) {m | m is a method in P}
υ(P) {l | l is a variable in P}
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to static
identify all references to candidate elds and transitively
Original Program
Top-level Enumerization Algorithm
µ(P) {m | m is a method in P}
υ(P) {l | l is a variable in P}
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to static
identify all references to candidate elds and transitively
Infers which
candidate fields are being
used as enums
Top-level Enumerization Algorithm
µ(P) {m | m is a method in P}
υ(P) {l | l is a variable in P}
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to static
identify all references to candidate elds and transitively
Groups
them into minimal sets
Top-level Enumerization Algorithm
µ(P) {m | m is a method in P}
υ(P) {l | l is a variable in P}
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to static
identify all references to candidate elds and transitively
Top-level Enumerization Algorithm
µ(P) {m | m is a method in P}
υ(P) {l | l is a variable in P}
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to static
identify all references to candidate elds and transitively
Top-level Enumerization Algorithm
µ(P) {m | m is a method in P}
υ(P) {l | l is a variable in P}
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to static
identify all references to candidate elds and transitively
Top-level Enumerization Algorithm
µ(P) {m | m is a method in P}
υ(P) {l | l is a variable in P}
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to static
identify all references to candidate elds and transitively
Natural
ordering
Empirical Evaluation
Benchmark KLOC Classes Sites Time (s)
ArtOfIllusion 75 378 111 207
Azureus 272 1894 635 1269
java5 180 1586 572 760
JavaCup 6 41 3 19
jdepend 3 28 1 1
JFlex 10 46 27 75
JFreeChart 71 420 43 128
jGap 6 137 5 7
jgraph 14 91 6 11
JHotDraw 29 496 24 14
junit 8 271 3 1
jwps 20 155 102 60
sablecc 29 237 10 9
tomcat6 153 1164 400 346
verbos 5 41 15 3
VietPad 11 84 22 8
Violet 7 73 20 9
Totals 899 7142 1999 2227
Empirical Evaluation
Benchmark KLOC Classes Sites Time (s)
ArtOfIllusion 75 378 111 207
Azureus 272 1894 635 1269
java5 180 1586 572 760
JavaCup 6 41 3 19
jdepend 3 28 1 1
JFlex 10 46 27 75
JFreeChart 71 420 43 128
jGap 6 137 5 7
jgraph 14 91 6 11
JHotDraw 29 496 24 14
junit 8 271 3 1
jwps 20 155 102 60
sablecc 29 237 10 9
tomcat6 153 1164 400 346
verbos 5 41 15 3
VietPad 11 84 22 8
Violet 7 73 20 9
Totals 899 7142 1999 2227
2.4 / KLOC
Empirical Evaluation
Benchmark KLOC Classes Sites Time (s)
ArtOfIllusion 75 378 111 207
Azureus 272 1894 635 1269
java5 180 1586 572 760
JavaCup 6 41 3 19
jdepend 3 28 1 1
JFlex 10 46 27 75
JFreeChart 71 420 43 128
jGap 6 137 5 7
jgraph 14 91 6 11
JHotDraw 29 496 24 14
junit 8 271 3 1
jwps 20 155 102 60
sablecc 29 237 10 9
tomcat6 153 1164 400 346
verbos 5 41 15 3
VietPad 11 84 22 8
Violet 7 73 20 9
Totals 899 7142 1999 2227
384 sites /
Benchmark
Filtered Contexts
Filtered Contexts
•Certain fields cannot be refactored.
Filtered Contexts
•Certain fields cannot be refactored.
•Program semantics preservation depends on the
actual values of these fields.
Filtered Contexts
•Certain fields cannot be refactored.
•Program semantics preservation depends on the
actual values of these fields.
•For example, named-constants (used in
mathematical calculations).
Filtered Contexts
•Certain fields cannot be refactored.
•Program semantics preservation depends on the
actual values of these fields.
•For example,
mathematical calculations).
•Transitive dependency of an element outside
available source code.
Filtered Contexts
•Certain fields cannot be refactored.
•Program semantics preservation depends on the
actual values of these fields.
•For example,
mathematical calculations).
•Transitive dependency of an element outside
available source code.
•Language constraints prohibit enumerization.
Filtered Contexts
•Certain fields cannot be refactored.
•Program semantics preservation depends on the
actual values of these fields.
•For example,
mathematical calculations).
•Transitive dependency of an element outside
available source code.
•Language constraints prohibit enumerization.
•Remaining fields denoted as candidates.
Filtered Contexts
•Certain fields cannot be refactored.
•Program semantics preservation depends on the
actual values of these fields.
•For example,
mathematical calculations).
•Transitive dependency of an element outside
available source code.
•Language constraints prohibit enumerization.
•Remaining fields denoted as candidates.
arr[RED]
Filtered Contexts
•Certain fields cannot be refactored.
•Program semantics preservation depends on the
actual values of these fields.
•For example,
mathematical calculations).
•Transitive dependency of an element outside
available source code.
•Language constraints prohibit enumerization.
•Remaining fields denoted as candidates.
Enum Identification Effectiveness
Enum Identification Effectiveness
cands enum
Enum Identification Effectiveness
cands enum
Refactored
87%
Result Summary
Result Summary
•37% of all primitive constant fields identified as
candidates.
Result Summary
•37% of all primitive constant fields identified as
candidates.
•87% of candidate fields found safe for refactoring.
Result Summary
•37% of all primitive constant fields identified as
candidates.
•87% of candidate fields found
•Remaining 13% required more sophisticated analysis:
Result Summary
•37% of all primitive constant fields identified as
candidates.
•87% of candidate fields found
•Remaining 13% required more sophisticated analysis:
•Explicit primitive conversion analysis (casts).
Result Summary
•37% of all primitive constant fields identified as
candidates.
•87% of candidate fields found
•Remaining 13% required more sophisticated analysis:
•Explicit primitive conversion analysis (casts).
•Native array copying method usage.
Conclusions
Conclusions
•Semantics preserving, declarative type inferencing
algorithm migrating legacy Java code to the enum
language construct introduced in Java 1.5.
Conclusions
•Semantics preserving, declarative type inferencing
algorithm migrating legacy Java code to the
language construct introduced in Java 1.5.
•Implemented algorithm as a plug-in for the Eclipse
IDE and evaluated it on 17 open source applications.
Conclusions
•Semantics preserving, declarative type inferencing
algorithm migrating legacy Java code to the
language construct introduced in Java 1.5.
•Implemented algorithm as a plug-in for the Eclipse
IDE and evaluated it on 17 open source applications.
•Analysis cost is practical.
Conclusions
•Semantics preserving, declarative type inferencing
algorithm migrating legacy Java code to the
language construct introduced in Java 1.5.
•Implemented algorithm as a plug-in for the Eclipse
IDE and evaluated it on 17 open source applications.
•Analysis cost is practical.
•Weak enum pattern is commonly used in legacy
Java software.
Conclusions
•Semantics preserving, declarative type inferencing
algorithm migrating legacy Java code to the
language construct introduced in Java 1.5.
•Implemented algorithm as a plug-in for the Eclipse
IDE and evaluated it on 17 open source applications.
•Analysis cost is practical.
•Weak enum pattern
Java software.
•Proposal successfully refactors a large number of
static final fields into enumerated types.
Future work
Future work
• Better mimic human results:
Future work
• Better mimic human results:
• Algorithms for grouping constants.
Future work
• Better mimic human results:
• Algorithms for grouping constants.
• Visualization techniques to facilitate grouping.
Future work
• Better mimic human results:
• Algorithms for grouping constants.
• Visualization techniques to facilitate grouping.
• Refactor additional legacy patterns.
Future work
• Better mimic human results:
• Algorithms for grouping constants.
• Visualization techniques to facilitate grouping.
• Refactor additional legacy patterns.
• In-depth empirical evaluation.
Comparing Human Produceable Results
Comparing Human Produceable Results
•Preliminary approach considers only transitive
closure of type-dependent entities in current program
state.
Comparing Human Produceable Results
•Preliminary approach considers only transitive
closure of type-dependent entities in
state.
•May not accurately mimic human produceable
results.
Comparing Human Produceable Results
Comparing Human Produceable Results
Enhanced Enum Constant Grouping
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Structural commonality using Concern Graphs
[Robillard and Murphy, ICSE ’02]:
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Structural commonality using Concern Graphs
[Robillard and Murphy, ICSE ’02]:
•DECREASE_SPEED, INCREASE_SPEED, and STOP
are all declared in the same class!
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Structural commonality using Concern Graphs
[Robillard and Murphy, ICSE ’02]:
•DECREASE_SPEED
are all declared in the same class!
•If not in the same class, do they have other things in
common?
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Structural commonality using Concern Graphs
[Robillard and Murphy, ICSE ’02]:
•DECREASE_SPEED
are all declared in the same class!
•If not in the same class, do they have other things in
common?
•For example, are they all annotated with the same
type of annotation?
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Lexical proximity of original constant declarations
[Gravley and Lakhotia, ‘96].
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Lexical proximity of original constant declarations
[Gravley and Lakhotia, ‘96].
•Distance between original primitive values.
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Lexical proximity of original constant declarations
[Gravley and Lakhotia, ‘96].
•Distance between original primitive
•Edit-distance of constant names.
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Lexical proximity of original constant declarations
[Gravley and Lakhotia, ‘96].
•Distance between original primitive
•Edit-distance of constant names.
•Relationships in version control systems (CVS/SVN)
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Lexical proximity of original constant declarations
[Gravley and Lakhotia, ‘96].
•Distance between original primitive
•Edit-distance of constant names.
•Relationships in version control systems (CVS/SVN)
•Ontology-based approach.
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Lexical proximity of original constant declarations
[Gravley and Lakhotia, ‘96].
•Distance between original primitive
•Edit-distance of constant names.
•Relationships in version control systems (CVS/SVN)
•Ontology-based approach.
•Common field annotations and structural
commonality in general.
Heuristically anticipate future type-dependence:
Enhanced Enum Constant Grouping
•Lexical proximity of original constant declarations
[Gravley and Lakhotia, ‘96].
•Distance between original primitive
•Edit-distance of constant names.
•Relationships in version control systems (CVS/SVN)
•Ontology-based approach.
•Common field annotations and
commonality
•Javadoc analysis/NLP.
Heuristically anticipate future type-dependence:
Visualization
•3-D images representing constants?
•Shapes/colors combinations could help depict how
much constants have in common.
•Can common usages be presented to the develop
effectively?
How can we present discovered constant commonality to
the developer?
Additional Compensation Patterns
27
Additional Compensation Patterns
•Patterns involving reference types, e.g., String.
27
Additional Compensation Patterns
•Patterns involving reference types, e.g.,
•Commonly occur in legacy Java code.
27
Additional Compensation Patterns
•Patterns involving reference types, e.g.,
•Commonly occur in legacy Java code.
•Use may degrade performance.
27
Additional Compensation Patterns
•Patterns involving reference types, e.g.,
•Commonly occur in legacy Java code.
•Use may degrade performance.
•Refactoring must preserve reference semantics.
27
Additional Compensation Patterns
•Patterns involving reference types, e.g.,
•Commonly occur in legacy Java code.
•Use may degrade performance.
•Refactoring must preserve
•Develop additional inference rules specifically for
reference types.
27
Additional Compensation Patterns
•Patterns involving reference types, e.g.,
•Commonly occur in legacy Java code.
•Use may degrade performance.
•Refactoring must preserve
•Develop additional inference rules specifically for
reference types.
•Use type constraints [Palsberg ’94] as in [Balaban,
Tip, Fuhrer ’05].
27
Additional Compensation Patterns
•Patterns involving reference types, e.g.,
•Commonly occur in legacy Java code.
•Use may degrade performance.
•Refactoring must preserve
•Develop additional inference rules specifically for
reference types.
•Use type constraints [Palsberg ’94] as in [Balaban,
Tip, Fuhrer ’05].
27
Additional Compensation Patterns
•Patterns involving reference types, e.g.,
•Commonly occur in legacy Java code.
•Use may degrade performance.
•Refactoring must preserve
•Develop additional inference rules specifically for
reference types.
•Use type constraints [Palsberg ’94] as in [Balaban,
Tip, Fuhrer ’05].
27
Mechanized
class migration,
e.g., HashTable
to HashMap
Additional weak enum Operations
28
Additional weak enum Operations
•Migrate constants (transitively) involving bit-wise
operations:
28
Additional weak enum Operations
•Migrate constants (transitively) involving bit-wise
operations:
•Prevalent during experimental study.
28
Additional weak enum Operations
•Migrate constants (transitively) involving bit-wise
operations:
•Prevalent during experimental study.
•Recognize true bit-wise vs. enum manipulation.
28
Additional weak enum Operations
•Migrate constants (transitively) involving bit-wise
operations:
•Prevalent during experimental study.
•Recognize
•Convert operations to method calls.
28
Additional weak enum Operations
•Migrate constants (transitively) involving bit-wise
operations:
•Prevalent during experimental study.
•Recognize
•Convert operations to method calls.
28
Additional weak enum Operations
•Migrate constants (transitively) involving bit-wise
operations:
•Prevalent during experimental study.
•Recognize
•Convert operations to method calls.
28
Color
orange =
RED.combine(YEL
LOW)
Thorough Usefulness Evaluation
Thorough Usefulness Evaluation
•Preliminary study involved constants passing
“preconditions” and not filtered.
Thorough Usefulness Evaluation
•Preliminary study involved constants passing
“preconditions” and not filtered.
•Ideally, developer feedback would enable a more
accurate study:
Thorough Usefulness Evaluation
•Preliminary study involved constants passing
“preconditions” and not filtered.
•Ideally, developer feedback would enable a more
accurate study:
•Were constants intended to be enums?
Thorough Usefulness Evaluation
•Preliminary study involved constants passing
“preconditions” and not filtered.
•Ideally, developer feedback would enable a more
accurate study:
•Were constants intended to be enums?
•Were constants grouped as desired?
Thorough Usefulness Evaluation
•Preliminary study involved constants passing
“preconditions” and not filtered.
•Ideally, developer feedback would enable a more
accurate study:
•Were constants intended to be enums?
•Were constants grouped as desired?
•Contact original subject developers.
Thorough Usefulness Evaluation
•Preliminary study involved constants passing
“preconditions” and not filtered.
•Ideally, developer feedback would enable a more
accurate study:
•Were constants intended to be enums?
•Were constants grouped as desired?
•Contact original subject developers.
•Automated feedback system built into plug-in.
Download
Download
•Tool publicly available via google code at http://
code.google.com/p/constants-to-enum-eclipse-
plugin/
Download
•Tool publicly available via google code at
code.google.com/p/constants-to-enum-eclipse-
plugin/
•Integrated into Eclipse refactoring framework.
Download
•Tool publicly available via google code at
code.google.com/p/constants-to-enum-eclipse-
plugin/
•Integrated into Eclipse refactoring framework.
•Includes GUI with preview pane.
Download
•Tool publicly available via google code at
code.google.com/p/constants-to-enum-eclipse-
plugin/
•Integrated into Eclipse refactoring framework.
•Includes GUI with preview pane.
•All transformation automated.
Download
•Tool publicly available via google code at
code.google.com/p/constants-to-enum-eclipse-
plugin/
•Integrated into Eclipse refactoring framework.
•Includes GUI with preview pane.
•All transformation automated.
•Integration into standard distribution of Eclipse IDE
in progress.
Download
•Tool publicly available via google code at
code.google.com/p/constants-to-enum-eclipse-
plugin/
•Integrated into Eclipse refactoring framework.
•Includes GUI with preview pane.
•All transformation automated.
•Integration into standard distribution of Eclipse IDE
in progress.
Proposed as
GSoC project in
2009, 2010
Download
•Tool publicly available via google code at
code.google.com/p/constants-to-enum-eclipse-
plugin/
•Integrated into Eclipse refactoring framework.
•Includes GUI with preview pane.
•All transformation automated.
•Integration into standard distribution of Eclipse IDE
in progress.
•GUI for manual constant grouping.
Download
•Tool publicly available via google code at
code.google.com/p/constants-to-enum-eclipse-
plugin/
•Integrated into Eclipse refactoring framework.
•Includes GUI with preview pane.
•All transformation automated.
•Integration into standard distribution of Eclipse IDE
in progress.
•GUI for manual constant grouping.
•Full test suite.
Download
•Tool publicly available via google code at
code.google.com/p/constants-to-enum-eclipse-
plugin/
•Integrated into Eclipse refactoring framework.
•Includes GUI with preview pane.
•All transformation automated.
•Integration into standard distribution of Eclipse IDE
in progress.
•GUI for manual constant grouping.
•Full test suite.
•Refactoring history (undo) and scripting support.
Where to find more information
Where to find more information
•Raffi Khatchadourian, Jason Sawin, and Atanas Rountev.
Automated refactoring of legacy Java software to enumerated
types. International Conference on Software Maintenance
(ICSM ’07), pages 224–233, Paris, France, October 2007.
IEEE. See http://tinyurl.com/ics0m7enum.
Where to find more information
•Raffi Khatchadourian, Jason Sawin, and Atanas Rountev.
Automated refactoring of legacy Java software to enumerated
types. International Conference on Software Maintenance
(ICSM ’07), pages 224–233, Paris, France, October 2007.
IEEE. See http://tinyurl.com/ics0m7enum.
•Raffi Khatchadourian, Jason Sawin, and Atanas Rountev.
Automated refactoring of legacy Java software to enumerated
types. Technical Report OSU-CISRC-4/07-TR26, Ohio State
University, April 2007. See http://tinyurl.com/khatchad-tr07.
Where to find more information
•Raffi Khatchadourian, Jason Sawin, and Atanas Rountev.
Automated refactoring of legacy Java software to enumerated
types. International Conference on Software Maintenance
(ICSM ’07), pages 224–233, Paris, France, October 2007.
IEEE. See http://tinyurl.com/ics0m7enum.
•Raffi Khatchadourian, Jason Sawin, and Atanas Rountev.
Automated refactoring of legacy Java software to enumerated
types. Technical Report OSU-CISRC-4/07-TR26, Ohio State
University, April 2007. See http://tinyurl.com/khatchad-tr07.
•Raffi Khatchadourian and Benjamin Muskalla. Enumeration
refactoring: A tool for automatically converting java constants
to enumerated types. Tool demonstration to appear in
International Conference on Automated Software
Engineering (ASE ’10), Antwerp, Belgium, September 2010.
IEEE/ACM.
Questions?
Questions?
And short demo if
time (and demo
god)permits!
Questions?
And short demo if
time (and demo
god)permits!
Is there a shrine in
Tokyo for successful tool
demonstrations?

More Related Content

PDF
Automated Refactoring of Legacy Java Software to Enumerated Types
PDF
Evaluation and analysis of ALGOL, PASCAL and ADA
PPTX
PPSX
Compiler designs presentation final
PPTX
9 subprograms
PPTX
Structure of the compiler
PPTX
9. control statement
PDF
Ooabap notes with_programs
Automated Refactoring of Legacy Java Software to Enumerated Types
Evaluation and analysis of ALGOL, PASCAL and ADA
Compiler designs presentation final
9 subprograms
Structure of the compiler
9. control statement
Ooabap notes with_programs

What's hot (20)

PDF
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
PPTX
C++ ppt
PDF
Compiler unit 1
PPT
Csci360 08-subprograms
KEY
Unit 1 cd
PPTX
OOP Poster Presentation
PPT
Unit 5 cspc
PDF
9 subprograms
PPT
Analysis of the source program
DOCX
Compiler design important questions
PDF
New c sharp3_features_(linq)_part_iv
PDF
Cs6660 compiler design
PPTX
PPTX
What is keyword in c programming
PDF
08 subprograms
PPTX
10 implementing subprograms
DOCX
Vhdl introduction
PDF
Different phases of a compiler
PPTX
Compiler Design
PPTX
Procedural programming
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
C++ ppt
Compiler unit 1
Csci360 08-subprograms
Unit 1 cd
OOP Poster Presentation
Unit 5 cspc
9 subprograms
Analysis of the source program
Compiler design important questions
New c sharp3_features_(linq)_part_iv
Cs6660 compiler design
What is keyword in c programming
08 subprograms
10 implementing subprograms
Vhdl introduction
Different phases of a compiler
Compiler Design
Procedural programming
Ad

Similar to Future Endeavors in Automated Refactoring of Legacy Java Software to Enumerated Types (20)

KEY
Enumeration Refactoring: A Tool for Automatically Converting Java Constants t...
PPT
java programming - applets
ODP
Aspect-oriented programming in Perl
PPTX
Coding standards for java
PDF
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
DOCX
Traffic lights-system-2(Microprocessor &Assembly Language)
PPTX
Core java
PPTX
lecture 6
PPTX
Introduction to computer science
PDF
Wien15 java8
PDF
Building Windows 8 Apps with Windows Azure
PPTX
Cleaner Code - CodeStock 2019 Edition
PPTX
Android RenderScript
ODP
Bring the fun back to java
PDF
Advanced CA Endevor® Software Change Manager Processor Coding Techniques: Pra...
PPT
Mod05 application migration
PPT
What is new in J2SE 5
PDF
Compiler Design- Machine Independent Optimizations
PDF
Commenting in Agile Development
PPT
Pluggin creation
Enumeration Refactoring: A Tool for Automatically Converting Java Constants t...
java programming - applets
Aspect-oriented programming in Perl
Coding standards for java
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
Traffic lights-system-2(Microprocessor &Assembly Language)
Core java
lecture 6
Introduction to computer science
Wien15 java8
Building Windows 8 Apps with Windows Azure
Cleaner Code - CodeStock 2019 Edition
Android RenderScript
Bring the fun back to java
Advanced CA Endevor® Software Change Manager Processor Coding Techniques: Pra...
Mod05 application migration
What is new in J2SE 5
Compiler Design- Machine Independent Optimizations
Commenting in Agile Development
Pluggin creation
Ad

More from Raffi Khatchadourian (20)

PDF
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
PDF
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
PDF
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
PDF
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
PDF
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
PDF
A Tool for Rejuvenating Feature Logging Levels via Git Histories and Degree o...
PDF
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
PPTX
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
PDF
An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...
PDF
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
PDF
An Empirical Study on the Use and Misuse of Java 8 Streams
PDF
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
PDF
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
PDF
A Brief Introduction to Type Constraints
PDF
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
PDF
A Tool for Optimizing Java 8 Stream Software via Automated Refactoring
PDF
Porting the NetBeans Java 8 Enhanced For Loop Lambda Expression Refactoring t...
PDF
Towards Safe Refactoring for Intelligent Parallelization of Java 8 Streams
PDF
Proactive Empirical Assessment of New Language Feature Adoption via Automated...
PDF
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
A Tool for Rejuvenating Feature Logging Levels via Git Histories and Degree o...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
An Empirical Study on the Use and Misuse of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
A Brief Introduction to Type Constraints
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
A Tool for Optimizing Java 8 Stream Software via Automated Refactoring
Porting the NetBeans Java 8 Enhanced For Loop Lambda Expression Refactoring t...
Towards Safe Refactoring for Intelligent Parallelization of Java 8 Streams
Proactive Empirical Assessment of New Language Feature Adoption via Automated...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...

Future Endeavors in Automated Refactoring of Legacy Java Software to Enumerated Types

  • 1. Future Endeavors in Automated Refactoring of Legacy Java Software to Enumerated Types* Raffi Khatchadourian, Jason Sawin, and Atanas Rountev PRESTO: Program Analyses and Software Tools Research Group, Ohio State University * Work supported in part by NSF
  • 4. Motivation • Software changes over time: • Requirements evolve 2
  • 5. Motivation • Software changes over time: • Requirements evolve • Different platforms (e.g., mobile devices) 2
  • 6. Motivation • Software changes over time: • Requirements evolve • Different platforms (e.g., mobile devices) • New framework versions (e.g., XML vs. annotation-based) 2
  • 8. Motivation Changing and/or maintaining large, complex software systems can be non-trivial:
  • 9. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code.
  • 10. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Adding a parameter to a method
  • 11. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly.
  • 12. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly. Removing a method parameter may alter overloading to overriding
  • 13. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly. Omission- prone: May opportunities to produce better code.
  • 14. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly. Omission- prone: May opportunities to produce better code. HashTable vs. HashMap
  • 15. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly. Omission- prone: May opportunities to produce better code.
  • 17. Solution? • Approaches made to provide mechanical assistance in evolution tasks.
  • 18. Solution? • Approaches made to provide assistance in evolution tasks. • Typically in the form of plug-ins to IDEs.
  • 19. Solution? • Approaches made to provide assistance in evolution tasks. • Typically in the form of plug-ins to IDEs. • Ease the burden of software maintenance and evolution.
  • 20. Solution? • Approaches made to provide assistance in evolution tasks. • Typically in the form of plug-ins to IDEs. • Ease the burden of software maintenance and evolution. Restrict workspace to only displays elements relevant to the task
  • 21. Solution? • Approaches made to provide assistance in evolution tasks. • Typically in the form of plug-ins to IDEs. • Ease the burden of software maintenance and evolution. Restrict workspace to only displays elements relevant to the task Restructure code while preserving semantics (i.e., refactoring)
  • 23. Introduction •Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and type-safe enumerations.
  • 24. Introduction •Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and •Highlight an automated semantics-preserving approach for migrating legacy Java code to take advantage of the new language enumerated type constructs.
  • 25. Introduction •Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and •Highlight an automated semantics-preserving approach for migrating legacy Java code to take advantage of the new language enumerated type constructs. •Present experimental results from research prototype as an Eclipse IDE plug-in.
  • 26. Introduction •Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and •Highlight an automated semantics-preserving approach for migrating legacy Java code to take advantage of the new language enumerated type constructs. •Present experimental results from research prototype as an Eclipse IDE plug-in. •In progress to be included with the standard distribution of Eclipse.
  • 27. Introduction •Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and •Highlight an automated semantics-preserving approach for migrating legacy Java code to take advantage of the new language enumerated type constructs. •Present experimental results from research prototype as an Eclipse IDE plug-in. •In progress to be included with the standard distribution of Eclipse. •Discuss directions for future work.
  • 28. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3;
  • 29. Motivating Example Weak Enum Pattern class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3;
  • 30. Motivating Example Type Safety class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3;
  • 31. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Manual Enumeration
  • 32. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Namespacing
  • 33. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Brittle
  • 34. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3;
  • 35. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3;
  • 36. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3;
  • 37. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP};
  • 38. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP};
  • 39. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3;
  • 40. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Language Enum
  • 41. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Type Safety
  • 42. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Singletons in Natural Order
  • 43. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Prefixed
  • 44. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Supports Separate Compilation
  • 45. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the traffic signal, initially red by default */ private Color color = Color.RED; /* Accessor for the light’s current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the traffic signal, initially red by default / private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3;
  • 46. Traffic Signal Client / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic
  • 47. Traffic Signal Client / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic
  • 48. Traffic Signal Client / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic
  • 49. Traffic Signal Client / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic
  • 50. Traffic Signal Client private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go Automobile Action Enum
  • 51. Traffic Signal Client private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go Named- Constant
  • 52. Traffic Signal Client private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go Promising...
  • 53. Traffic Signal Client private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go Definitely not enumerizable
  • 54. Traffic Signal Client / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic
  • 55. Traffic Signal Client / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic
  • 56. Traffic Signal Client / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic
  • 57. / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); Traffic Signal Client private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal);
  • 58. Traffic Signal Client private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal);
  • 59. Traffic Signal Client private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal);
  • 60. /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); Traffic Signal Client private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal);
  • 61. /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); Traffic Signal Client private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal); /* The action this automobile is currently performing, idle by default */ private Action currentAction = Action.IDLE; /* The current speed of the automobile, initially 5 mph. */ private int currentSpeed = 5; private Action react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return Action.STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return Action.INCREASE_SPEED; else return Action.STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; Action reaction = this.react(aSignal);
  • 62. function Enumerizable(C) 1: W ← C /* seed the worklist with the input constants */ 2: N ← ∅ /* the non-enumerizable set list, initially empty */ 3: for all c ∈ C do 4: MakeSet(c) /* init the union-find data structure */ 5: end for 6: while W ̸= ∅ do 7: /* remove an element from the worklist */ 8: α ← e | e ∈ W 9: W ← W {α} 10: for all αctxt ∈ Contexts(α, P) do 11: if ¬isEnumerizableContext(α, αctxt ) then α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to statica identify all references to candidate elds and transitively d pendent program entities. This assumption could be inva dated through the use of reection and custom class loade Enumerization Approach
  • 63. function Enumerizable(C) 1: W ← C /* seed the worklist with the input constants */ 2: N ← ∅ /* the non-enumerizable set list, initially empty */ 3: for all c ∈ C do 4: MakeSet(c) /* init the union-find data structure */ 5: end for 6: while W ̸= ∅ do 7: /* remove an element from the worklist */ 8: α ← e | e ∈ W 9: W ← W {α} 10: for all αctxt ∈ Contexts(α, P) do 11: if ¬isEnumerizableContext(α, αctxt ) then α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to statica identify all references to candidate elds and transitively d pendent program entities. This assumption could be inva dated through the use of reection and custom class loade Subset of static final fields Enumerization Approach
  • 64. function Enumerizable(C) 1: W ← C /* seed the worklist with the input constants */ 2: N ← ∅ /* the non-enumerizable set list, initially empty */ 3: for all c ∈ C do 4: MakeSet(c) /* init the union-find data structure */ 5: end for 6: while W ̸= ∅ do 7: /* remove an element from the worklist */ 8: α ← e | e ∈ W 9: W ← W {α} 10: for all αctxt ∈ Contexts(α, P) do 11: if ¬isEnumerizableContext(α, αctxt ) then α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to statica identify all references to candidate elds and transitively d pendent program entities. This assumption could be inva dated through the use of reection and custom class loade Abstract Syntax Tree (AST) Enumerization Approach
  • 65. function Enumerizable(C) 1: W ← C /* seed the worklist with the input constants */ 2: N ← ∅ /* the non-enumerizable set list, initially empty */ 3: for all c ∈ C do 4: MakeSet(c) /* init the union-find data structure */ 5: end for 6: while W ̸= ∅ do 7: /* remove an element from the worklist */ 8: α ← e | e ∈ W 9: W ← W {α} 10: for all αctxt ∈ Contexts(α, P) do 11: if ¬isEnumerizableContext(α, αctxt ) then Output: α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to statica identify all references to candidate elds and transitively d pendent program entities. This assumption could be inva dated through the use of reection and custom class loade Enumerization Approach
  • 66. • Partitioning of program entities: • Fields • Method declarations (return types) • Local variables (including formal parameters) function Enumerizable(C) 1: W ← C /* seed the worklist with the input constants */ 2: N ← ∅ /* the non-enumerizable set list, initially empty */ 3: for all c ∈ C do 4: MakeSet(c) /* init the union-find data structure */ 5: end for 6: while W ̸= ∅ do 7: /* remove an element from the worklist */ 8: α ← e | e ∈ W 9: W ← W {α} 10: for all αctxt ∈ Contexts(α, P) do 11: if ¬isEnumerizableContext(α, αctxt ) then Output: α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to statica identify all references to candidate elds and transitively d pendent program entities. This assumption could be inva dated through the use of reection and custom class loade Enumerization Approach
  • 67. • Partitioning of program entities: • Fields • Method declarations (return types) • Local variables (including formal parameters) • Each partition contains elements that: • Transitively type-dependent upon one another • Are safe for enum type transformation function Enumerizable(C) 1: W ← C /* seed the worklist with the input constants */ 2: N ← ∅ /* the non-enumerizable set list, initially empty */ 3: for all c ∈ C do 4: MakeSet(c) /* init the union-find data structure */ 5: end for 6: while W ̸= ∅ do 7: /* remove an element from the worklist */ 8: α ← e | e ∈ W 9: W ← W {α} 10: for all αctxt ∈ Contexts(α, P) do 11: if ¬isEnumerizableContext(α, αctxt ) then Output: α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to statica identify all references to candidate elds and transitively d pendent program entities. This assumption could be inva dated through the use of reection and custom class loade Enumerization Approach
  • 70. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE 1 class TrafficSignal { 2 public enum Color {RED, 3 YELLOW, 4 GREEN}; 5 /* Current color of the traffic signal, initially red by default */ 6 private Color color = Color.RED; 7 /* Accessor for the light’s current color */ 8 public Color getColor() {return this.color; 9 10 class Automobile {
  • 71. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE 1 class TrafficSignal { 2 public enum Color {RED, 3 YELLOW, 4 GREEN}; 5 /* Current color of the traffic signal, initially red by default */ 6 private Color color = Color.RED; 7 /* Accessor for the light’s current color */ 8 public Color getColor() {return this.color; 9 10 class Automobile { color
  • 72. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE 1 class TrafficSignal { 2 public enum Color {RED, 3 YELLOW, 4 GREEN}; 5 /* Current color of the traffic signal, initially red by default */ 6 private Color color = Color.RED; 7 /* Accessor for the light’s current color */ 8 public Color getColor() {return this.color;}} 9 0 class Automobile { 1 private enum Action {IDLE, 2 INCREASE_SPEED, color
  • 73. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE 1 class TrafficSignal { 2 public enum Color {RED, 3 YELLOW, 4 GREEN}; 5 /* Current color of the traffic signal, initially red by default */ 6 private Color color = Color.RED; 7 /* Accessor for the light’s current color */ 8 public Color getColor() {return this.color;}} 9 0 class Automobile { 1 private enum Action {IDLE, 2 INCREASE_SPEED, color getColor
  • 74. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE color getColor 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by defaul 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by defa 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) 28 return INCREASE_SPEED;
  • 75. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE color getColor 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by defaul 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by defa 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) 28 return INCREASE_SPEED;
  • 76. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE color getColor 13 private static final int INCREASE_SPEED = 14 private static final int DECREASE_SPEED = 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by d 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) 28 return INCREASE_SPEED; 29 else return STOP; 30 case TrafficSignal.GREEN: // no change 31 return this.currentAction; 32 default: throw new IllegalArgumentExce 33 ("Invalid traffic color");}} // required 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by defaul 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo())
  • 77. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE color getColor 13 private static final int INCREASE_SPEED = 14 private static final int DECREASE_SPEED = 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by d 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) 28 return INCREASE_SPEED; 29 else return STOP; 30 case TrafficSignal.GREEN: // no change 31 return this.currentAction; 32 default: throw new IllegalArgumentExce 33 ("Invalid traffic color");}} // required 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by defaul 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo())
  • 78. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE color getColor 2 public static final int RED = 0; 3 public static final int RED_YELLOW = 1; 4 public static final int YELLOW = 2; 5 public static final int GREEN = 3; 6 / Current color of the traffic signal, initially red by default / 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1 14 private static final int DECREASE_SPEED = 2 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by de 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21
  • 79. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE color getColor 2 public static final int RED = 0; 3 public static final int RED_YELLOW = 1; 4 public static final int YELLOW = 2; 5 public static final int GREEN = 3; 6 / Current color of the traffic signal, initially red by default / 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1 14 private static final int DECREASE_SPEED = 2 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by de 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 currentAction
  • 80. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE color getColor currentAction 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by default / 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) RED; urrent color / () {return this.color;}} l int IDLE = 0; l int INCREASE_SPEED = 1; l int DECREASE_SPEED = 2; l int STOP = 3; l int MAX_SPEED = 140; e is currently performing, idle by default / Action = IDLE; automobile, initially 5 mph. / Speed = 5; rafficSignal signal) { Color()) { nal.RED: return STOP; nal.YELLOW: o stop or go ldGo()) 6 private C 7 /* Accessor f 8 public Co 9 10 class Autom 11 private e 12 INCREAS 13 DECREAS 14 STOP}; 15 private s 16 /* The action 17 private A 18 /* The curren 19 private i 20 21 private A 22 switch( 23 case 24 case 25 // de 26 if 27 r public int getColor() {return this.color;}} lass Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte unning example: a hypothetical drive-by-wire applica turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; l signal) { turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} 22 switch(signal.getColor( 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte
  • 81. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOPIDLE color getColor currentAction 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by default / 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) RED; urrent color / () {return this.color;}} l int IDLE = 0; l int INCREASE_SPEED = 1; l int DECREASE_SPEED = 2; l int STOP = 3; l int MAX_SPEED = 140; e is currently performing, idle by default / Action = IDLE; automobile, initially 5 mph. / Speed = 5; rafficSignal signal) { Color()) { nal.RED: return STOP; nal.YELLOW: o stop or go ldGo()) 6 private C 7 /* Accessor f 8 public Co 9 10 class Autom 11 private e 12 INCREAS 13 DECREAS 14 STOP}; 15 private s 16 /* The action 17 private A 18 /* The curren 19 private i 20 21 private A 22 switch( 23 case 24 case 25 // de 26 if 27 r public int getColor() {return this.color;}} lass Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte unning example: a hypothetical drive-by-wire applica turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; l signal) { turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} 22 switch(signal.getColor( 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte
  • 82. DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOP IDLE color getColor currentAction 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by default / 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) RED; urrent color / () {return this.color;}} l int IDLE = 0; l int INCREASE_SPEED = 1; l int DECREASE_SPEED = 2; l int STOP = 3; l int MAX_SPEED = 140; e is currently performing, idle by default / Action = IDLE; automobile, initially 5 mph. / Speed = 5; rafficSignal signal) { Color()) { nal.RED: return STOP; nal.YELLOW: o stop or go ldGo()) 6 private C 7 /* Accessor f 8 public Co 9 10 class Autom 11 private e 12 INCREAS 13 DECREAS 14 STOP}; 15 private s 16 /* The action 17 private A 18 /* The curren 19 private i 20 21 private A 22 switch( 23 case 24 case 25 // de 26 if 27 r public int getColor() {return this.color;}} lass Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte unning example: a hypothetical drive-by-wire applica turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; l signal) { turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} 22 switch(signal.getColor( 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte
  • 83. react DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOP IDLE color getColor currentAction 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by default / 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) RED; urrent color / () {return this.color;}} l int IDLE = 0; l int INCREASE_SPEED = 1; l int DECREASE_SPEED = 2; l int STOP = 3; l int MAX_SPEED = 140; e is currently performing, idle by default / Action = IDLE; automobile, initially 5 mph. / Speed = 5; rafficSignal signal) { Color()) { nal.RED: return STOP; nal.YELLOW: o stop or go ldGo()) 6 private C 7 /* Accessor f 8 public Co 9 10 class Autom 11 private e 12 INCREAS 13 DECREAS 14 STOP}; 15 private s 16 /* The action 17 private A 18 /* The curren 19 private i 20 21 private A 22 switch( 23 case 24 case 25 // de 26 if 27 r public int getColor() {return this.color;}} lass Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte unning example: a hypothetical drive-by-wire applica turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; l signal) { turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} 22 switch(signal.getColor( 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte
  • 84. react DECREASE_SPEED MAX_SPEED GREEN YELLOW RED INCREASE_SPEED STOP IDLE color getColor currentAction 7 private int color = RED; 8 / Accessor for the light’s current color / 9 public int getColor() {return this.color;}} 10 11 class Automobile { 12 private static final int IDLE = 0; 13 private static final int INCREASE_SPEED = 1; 14 private static final int DECREASE_SPEED = 2; 15 private static final int STOP = 3; 16 private static final int MAX_SPEED = 140; 17 / The action this automobile is currently performing, idle by default / 18 private int currentAction = IDLE; 19 / The current speed of the automobile, initially 5 mph. / 20 private int currentSpeed = 5; 21 22 private int react(TrafficSignal signal) { 23 switch(signal.getColor()) { 24 case TrafficSignal.RED: return STOP; 25 case TrafficSignal.YELLOW: 26 // decide whether to stop or go 27 if (this.shouldGo()) RED; urrent color / () {return this.color;}} l int IDLE = 0; l int INCREASE_SPEED = 1; l int DECREASE_SPEED = 2; l int STOP = 3; l int MAX_SPEED = 140; e is currently performing, idle by default / Action = IDLE; automobile, initially 5 mph. / Speed = 5; rafficSignal signal) { Color()) { nal.RED: return STOP; nal.YELLOW: o stop or go ldGo()) 6 private C 7 /* Accessor f 8 public Co 9 10 class Autom 11 private e 12 INCREAS 13 DECREAS 14 STOP}; 15 private s 16 /* The action 17 private A 18 /* The curren 19 private i 20 21 private A 22 switch( 23 case 24 case 25 // de 26 if 27 r public int getColor() {return this.color;}} lass Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte unning example: a hypothetical drive-by-wire applica turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} or enumerated types. 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; l signal) { turn STOP; // no change n; ArgumentException );}} // required ; ignal); Action && EED || X_SPEED)) ction);} action) {...}} 22 switch(signal.getColor( 23 case TrafficSignal.RE 24 case TrafficSignal.YE 25 // decide whether to stop or 26 if (this.shouldGo() 27 return Action.INC 28 else return Action. 29 case TrafficSignal.GR 30 return this.current 31 default: throw new Il 32 ("Invalid traffic c 33 34 public void drive() { 35 TrafficSignal aSignal = 36 Action reaction = this. 37 if (reaction != this.cu 38 (reaction != Action 39 this.currentSpeed 40 this.performActio 41 42 private void performActio (b) Improvements afte
  • 85. Identifiers function EA(α,ID) 1: return EA(α, Parent(ID)) Parenthesized expressions function EA(α,(ID)) 1: return EA(α, Parent(ID)) Cast expressions function EA(α, (TYPE)EXP) 1: return false Field access expressions function EA(α, EXP.ID) 1: return EA(α, Parent(EXP)) Assignment expressions function EA(α,EXP1 = EXP2) 1: return ED(EXP1) ∧ ED(EXP2) Subtract assignment expressions function EA(α,EXP1 -= EXP2) 1: return false Divide assignment expressions function EA(α,EXP1 /= EXP2) 1: return false Infix addition expressions function EA(α,EXP1 + EXP2) 1: return false Infix multiplication expressions function EA(α,EXP1 * EXP2) 1: return false Prefix unary minus expressions function EA(α,-EXP) 1: return false Postfix increment expressions function EA(α,EXP++) 1: return false Equality expressions function EA(α,EXP1 == EXP2) 1: return ED(EXP1) ∧ ED(EXP2) Inequality expressions function EA(α,EXP1 != EXP2) 1: return ED(EXP1) ∧ ED(EXP2) Switch statements function EA(α, switch(EXP)) 1: let se = ED(EXP) 2: let ce = true 3: for all case EXPc ∈ cases(switch(EXP)) do 4: ce ← ce ∧ ED(EXPc) 5: end for 6: return se ∧ ce Switch case statements function EA(α,case EXP) 1: return EA(α, switchStmt(case EXP)) Conditional expressions function EA(α, EXP1 ? EXP2 : EXP3) 1: if contains(EXP2, α) ∨ contains(EXP3, α) then 2: return EA(α, Parent(EXP1 ? EXP2 : EXP3)) 3: else 4: return true 5: end if Array access/creation expressions function EA(α, EXP1[EXP2]) 1: if contains(EXP2, α) then 2: return false 3: else 4: return EA(α, Parent(EXP1)) 5: end if Array initialization expressions function EA(α, {EXP1, . . . , EXPn}) 1: let ie = true 2: for EXPi, 1 ≤ i ≤ n do 3: ie ← ie ∧ ED(EXPi) 4: end for 5: return ie Return statements function EA(α,return EXP) 1: return true Method declaration statements function EA(α,ID(P1, . . . ,Pn)) 1: let re = true 2: for all return EXPr ∈ returnStmts(ID(P1, . . . , Pn)) do 3: re ← re ∧ ED(EXPr) 4: end for 5: return re Formal parameters function EA(α,Pi) 1: let ae = true 2: /*check the ith argument of each invocation of the declaring method*/ 3: let ˆα = MethodDecl(Pi) 4: for all ˆαctxt ∈ Invocations(ˆα, P) do 5: ae ← ae ∧ ED(Arg(ˆαctxt, i)) 6: end for 7: return ae Method invocation expressions function EA(α,ID(EXP1, . . . , EXPn)) 1: for EXPi, 1 ≤ i ≤ n do 2: if contains(EXPi, α) then 3: return true 4: end if 5: end for 6: return EA(α, Parent(ID(EXP1, . . . , EXPn))) General statements function XA(α,SMT) 1: let se = true 2: for EXP ∈ Children(SMT) do 3: se ← se ∨ED(EXP) 4: end for 5: return se Figure 7. Enumerizable ascender. 10 Identifiers function EA(α,ID) 1: return EA(α, Parent(ID)) Parenthesized expressions function EA(α,(ID)) 1: return EA(α, Parent(ID)) Cast expressions function EA(α, (TYPE)EXP) 1: return false Field access expressions function EA(α, EXP.ID) 1: return EA(α, Parent(EXP)) Assignment expressions function EA(α,EXP1 = EXP2) 1: return ED(EXP1) ∧ ED(EXP2) Subtract assignment expressions function EA(α,EXP1 -= EXP2) 1: return false Divide assignment expressions function EA(α,EXP1 /= EXP2) 1: return false Infix addition expressions function EA(α,EXP1 + EXP2) 1: return false Infix multiplication expressions function EA(α,EXP1 * EXP2) 1: return false Prefix unary minus expressions function EA(α,-EXP) 1: return false Postfix increment expressions function EA(α,EXP++) 1: return false Equality expressions function EA(α,EXP1 == EXP2) 1: return ED(EXP1) ∧ ED(EXP2) Inequality expressions function EA(α,EXP1 != EXP2) 1: return ED(EXP1) ∧ ED(EXP2) Switch statements function EA(α, switch(EXP)) 1: let se = ED(EXP) 2: let ce = true 3: for all case EXPc ∈ cases(switch(EXP)) do 4: ce ← ce ∧ ED(EXPc) 5: end for 6: return se ∧ ce Switch case statements function EA(α,case EXP) 1: return EA(α, switchStmt(case EXP)) Conditional expressions function EA(α, EXP1 ? EXP2 : EXP3) 1: if contains(EXP2, α) ∨ contains(EXP3, α) then 2: return EA(α, Parent(EXP1 ? EXP2 : EXP3)) 3: else 4: return true 5: end if Array access/creation expressions function EA(α, EXP1[EXP2]) 1: if contains(EXP2, α) then 2: return false 3: else 4: return EA(α, Parent(EXP1)) 5: end if Array initialization expressions function EA(α, {EXP1, . . . , EXPn}) 1: let ie = true 2: for EXPi, 1 ≤ i ≤ n do 3: ie ← ie ∧ ED(EXPi) 4: end for 5: return ie Return statements function EA(α,return EXP) 1: return true Method declaration statements function EA(α,ID(P1, . . . ,Pn)) 1: let re = true 2: for all return EXPr ∈ returnStmts(ID(P1, . . . , Pn)) do 3: re ← re ∧ ED(EXPr) 4: end for 5: return re Formal parameters function EA(α,Pi) 1: let ae = true 2: /*check the ith argument of each invocation of the declaring method*/ 3: let ˆα = MethodDecl(Pi) 4: for all ˆαctxt ∈ Invocations(ˆα, P) do 5: ae ← ae ∧ ED(Arg(ˆαctxt, i)) 6: end for 7: return ae Method invocation expressions function EA(α,ID(EXP1, . . . , EXPn)) 1: for EXPi, 1 ≤ i ≤ n do 2: if contains(EXPi, α) then 3: return true 4: end if 5: end for 6: return EA(α, Parent(ID(EXP1, . . . , EXPn))) General statements function XA(α,SMT) 1: let se = true 2: for EXP ∈ Children(SMT) do 3: se ← se ∨ED(EXP) 4: end for 5: return se Figure 7. Enumerizable ascender. 10 Identifiers function XA(α,ID) 1: return XA(α, Parent(ID)) Parenthesized expressions function XA(α,(ID)) 1: return XA(α, Parent(ID)) Cast expressions function XA(α, (TYPE)EXP) 1: return ∅ Field access expressions function XA(α, EXP.ID) 1: return XA(α, Parent(EXP)) Assignment expressions function XA(α,EXP1 = EXP2) 1: return XD(EXP1) ∪ XD(EXP2) Subtract assignment expressions function XA(α,EXP1 -= EXP2) 1: return ∅ Divide assignment expressions function XA(α,EXP1 /= EXP2) 1: return ∅ Infix addition expressions function XA(α,EXP1 + EXP2) 1: return ∅ Infix multiplication expressions function XA(α,EXP1 * EXP2) 1: return ∅ Prefix unary minus expressions function XA(α,-EXP) 1: return ∅ Postfix increment expressions function XA(α,EXP++) 1: return ∅ Equality expressions function XA(α,EXP1 == EXP2) 1: return XD(EXP2) ∪ XD(EXP1) Inequality expressions function XA(α,EXP1 != EXP2) 1: return XD(EXP1) ∪ XD(EXP2) Switch statements function XA(α, switch(EXP)) 1: R ← XD(EXP) 2: for all case EXPc ∈ cases(switch(EXP)) do 3: R ← XD(EXPc) 4: end for 5: return R Switch case statements function XA(α,case EXP) 1: return XA(α, switchStmt(case EXP)) Conditional expressions function XA(α, EXP1 ? EXP2 : EXP3) 1: if contains(EXP2, α) ∨ contains(EXP3, α) then 2: return XA(α, Parent(EXP1 ? EXP2 : EXP3)) 3: else 4: return ∅ 5: end if Array access/creation expressions function XA(α, EXP1[EXP2]) 1: if contains(EXP2, α) then 2: return ∅ 3: else 4: return XA(α, Parent(EXP1)) 5: end if Array initialization expressions function XA(α, {EXP1, . . . , EXPn}) 1: R ← ∅ 2: for EXPi, 1 ≤ i ≤ n do 3: R ← R ∪ XD(EXPi) 4: end for 5: return R Return statements function XA(α,return EXP) 1: return XD(MethodDecl(return EXP)) Method declaration statements function XA(α,ID(P1, . . . ,Pn)) 1: R ← ∅ 2: for all return EXPr ∈ returnStmts(ID(P1, . . . , Pn)) do 3: R ← R ∪ XD(EXPr) 4: end for 5: return R Formal parameters function XA(α,Pi) 1: R ← ∅ 2: /*extract the ith argument of each invocation of the declaring method*/ 3: let ˆα = MethodDecl(Pi) 4: for all ˆαctxt ∈ Invocations(ˆα, P) do 5: R ← R ∪ XD(Arg(ˆαctxt, i)) 6: end for 7: return R Method invocation expressions function XA(α,ID(EXP1, . . . , EXPn)) 1: R ← ∅ 2: for EXPi, 1 ≤ i ≤ n do 3: if contains(EXPi, α) then 4: R ← R ∪ XD(EXP1) 5: end if 6: end for 7: if R ̸= ∅ then 8: return R 9: else 10: return XA(α, Parent(ID(EXP1, . . . , EXPn))) 11: end if General statements function XA(α,SMT) 1: R ← ∅ 2: for EXP ∈ Children(SMT) do 3: R ← R ∪ XD(EXP) 4: end for 5: return R Figure 9. Extraction ascender. 12 Identifiers function XA(α,ID) 1: return XA(α, Parent(ID)) Parenthesized expressions function XA(α,(ID)) 1: return XA(α, Parent(ID)) Cast expressions function XA(α, (TYPE)EXP) 1: return ∅ Field access expressions function XA(α, EXP.ID) 1: return XA(α, Parent(EXP)) Assignment expressions function XA(α,EXP1 = EXP2) 1: return XD(EXP1) ∪ XD(EXP2) Subtract assignment expressions function XA(α,EXP1 -= EXP2) 1: return ∅ Divide assignment expressions function XA(α,EXP1 /= EXP2) 1: return ∅ Infix addition expressions function XA(α,EXP1 + EXP2) 1: return ∅ Infix multiplication expressions function XA(α,EXP1 * EXP2) 1: return ∅ Prefix unary minus expressions function XA(α,-EXP) 1: return ∅ Postfix increment expressions function XA(α,EXP++) 1: return ∅ Equality expressions function XA(α,EXP1 == EXP2) 1: return XD(EXP2) ∪ XD(EXP1) Inequality expressions function XA(α,EXP1 != EXP2) 1: return XD(EXP1) ∪ XD(EXP2) Switch statements function XA(α, switch(EXP)) 1: R ← XD(EXP) 2: for all case EXPc ∈ cases(switch(EXP)) do 3: R ← XD(EXPc) 4: end for 5: return R Switch case statements function XA(α,case EXP) 1: return XA(α, switchStmt(case EXP)) Conditional expressions function XA(α, EXP1 ? EXP2 : EXP3) 1: if contains(EXP2, α) ∨ contains(EXP3, α) then 2: return XA(α, Parent(EXP1 ? EXP2 : EXP3)) 3: else 4: return ∅ 5: end if Array access/creation expressions function XA(α, EXP1[EXP2]) 1: if contains(EXP2, α) then 2: return ∅ 3: else 4: return XA(α, Parent(EXP1)) 5: end if Array initialization expressions function XA(α, {EXP1, . . . , EXPn}) 1: R ← ∅ 2: for EXPi, 1 ≤ i ≤ n do 3: R ← R ∪ XD(EXPi) 4: end for 5: return R Return statements function XA(α,return EXP) 1: return XD(MethodDecl(return EXP)) Method declaration statements function XA(α,ID(P1, . . . ,Pn)) 1: R ← ∅ 2: for all return EXPr ∈ returnStmts(ID(P1, . . . , Pn)) do 3: R ← R ∪ XD(EXPr) 4: end for 5: return R Formal parameters function XA(α,Pi) 1: R ← ∅ 2: /*extract the ith argument of each invocation of the declaring method*/ 3: let ˆα = MethodDecl(Pi) 4: for all ˆαctxt ∈ Invocations(ˆα, P) do 5: R ← R ∪ XD(Arg(ˆαctxt, i)) 6: end for 7: return R Method invocation expressions function XA(α,ID(EXP1, . . . , EXPn)) 1: R ← ∅ 2: for EXPi, 1 ≤ i ≤ n do 3: if contains(EXPi, α) then 4: R ← R ∪ XD(EXP1) 5: end if 6: end for 7: if R ̸= ∅ then 8: return R 9: else 10: return XA(α, Parent(ID(EXP1, . . . , EXPn))) 11: end if General statements function XA(α,SMT) 1: R ← ∅ 2: for EXP ∈ Children(SMT) do 3: R ← R ∪ XD(EXP) 4: end for 5: return R Figure 9. Extraction ascender. 12
  • 86. Algorithmic Distinction: Enums vs. Constants private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); if (reaction != this.currentAction && (reaction != INCREASE_SPEED || this.currentSpeed <= MAX_SPEED)) this.performAction(reaction);} private void performAction(int action) {...}} (a) Using integer constants for enumerated types.
  • 87. Algorithmic Distinction: Enums vs. Constants private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); if (reaction != this.currentAction && (reaction != INCREASE_SPEED || this.currentSpeed <= MAX_SPEED)) this.performAction(reaction);} private void performAction(int action) {...}} (a) Using integer constants for enumerated types.
  • 88. Algorithmic Distinction: Enums vs. Constants private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); if (reaction != this.currentAction && (reaction != INCREASE_SPEED || this.currentSpeed <= MAX_SPEED)) this.performAction(reaction);} private void performAction(int action) {...}} (a) Using integer constants for enumerated types.
  • 89. Algorithmic Distinction: Enums vs. Constants private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); if (reaction != this.currentAction && (reaction != INCREASE_SPEED || this.currentSpeed <= MAX_SPEED)) this.performAction(reaction);} private void performAction(int action) {...}} (a) Using integer constants for enumerated types.
  • 90. Algorithmic Distinction: Enums vs. Constants private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); if (reaction != this.currentAction && (reaction != INCREASE_SPEED || this.currentSpeed <= MAX_SPEED)) this.performAction(reaction);} private void performAction(int action) {...}} (a) Using integer constants for enumerated types. class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change
  • 91. Algorithmic Distinction: Enums vs. Constants private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); if (reaction != this.currentAction && (reaction != INCREASE_SPEED || this.currentSpeed <= MAX_SPEED)) this.performAction(reaction);} private void performAction(int action) {...}} (a) Using integer constants for enumerated types. class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo())
  • 92. Algorithmic Distinction: Enums vs. Constants private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); if (reaction != this.currentAction && (reaction != INCREASE_SPEED || this.currentSpeed <= MAX_SPEED)) this.performAction(reaction);} private void performAction(int action) {...}} (a) Using integer constants for enumerated types. ? class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo())
  • 93. Algorithmic Distinction: Enums vs. Constants private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); if (reaction != this.currentAction && (reaction != INCREASE_SPEED || this.currentSpeed <= MAX_SPEED)) this.performAction(reaction);} private void performAction(int action) {...}} (a) Using integer constants for enumerated types. ? class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction;
  • 94. Algorithmic Distinction: Enums vs. Constants private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed <= MAX_SPEED)) rmAction(reaction);} rmAction(int action) {...}} eger constants for enumerated types. 38 (reacti 39 this.c 40 this. 41 42 private void (b) Im Figure 1. Running example: a hypothetical drive-by-w ch machinery. Third, the weak enum ogrammer to manually enumerate the ts, which increases the likelihood of different enum constants may be unin- he same internal value. Finally, the rittle [?]: since the values are com- at compile time they are inlined into new constants are added in between example, in which by language enum and Automobile. ations of these ne through compile-tim between the named ated values. It is als is an Action, whic case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); if (reaction != this.currentAction && (reaction != INCREASE_SPEED || this.currentSpeed <= MAX_SPEED)) this.performAction(reaction);} private void performAction(int action) {...}} (a) Using integer constants for enumerated types. class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; private int color = RED; / Accessor for the light’s current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction;
  • 95. Top-level Enumerization Algorithm µ(P) {m | m is a method in P} υ(P) {l | l is a variable in P} α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to static identify all references to candidate elds and transitively
  • 96. Top-level Enumerization Algorithm µ(P) {m | m is a method in P} υ(P) {l | l is a variable in P} α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to static identify all references to candidate elds and transitively Subset of static final fields
  • 97. Top-level Enumerization Algorithm µ(P) {m | m is a method in P} υ(P) {l | l is a variable in P} α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to static identify all references to candidate elds and transitively Original Program
  • 98. Top-level Enumerization Algorithm µ(P) {m | m is a method in P} υ(P) {l | l is a variable in P} α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to static identify all references to candidate elds and transitively Infers which candidate fields are being used as enums
  • 99. Top-level Enumerization Algorithm µ(P) {m | m is a method in P} υ(P) {l | l is a variable in P} α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to static identify all references to candidate elds and transitively Groups them into minimal sets
  • 100. Top-level Enumerization Algorithm µ(P) {m | m is a method in P} υ(P) {l | l is a variable in P} α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to static identify all references to candidate elds and transitively
  • 101. Top-level Enumerization Algorithm µ(P) {m | m is a method in P} υ(P) {l | l is a variable in P} α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to static identify all references to candidate elds and transitively
  • 102. Top-level Enumerization Algorithm µ(P) {m | m is a method in P} υ(P) {l | l is a variable in P} α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to static identify all references to candidate elds and transitively
  • 103. Top-level Enumerization Algorithm µ(P) {m | m is a method in P} υ(P) {l | l is a variable in P} α variable, eld, method αctxt context in which α may occur Figure 2. Formalism notation. procedure Enumerize(F, P) 1: R ← Enumerizable(F) 2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R) 3: for all T ∈ R do 4: Transform(T) 5: end for Figure 3. Top-level enumerization algorithm. refactoring. We also assume that we are able to static identify all references to candidate elds and transitively Natural ordering
  • 104. Empirical Evaluation Benchmark KLOC Classes Sites Time (s) ArtOfIllusion 75 378 111 207 Azureus 272 1894 635 1269 java5 180 1586 572 760 JavaCup 6 41 3 19 jdepend 3 28 1 1 JFlex 10 46 27 75 JFreeChart 71 420 43 128 jGap 6 137 5 7 jgraph 14 91 6 11 JHotDraw 29 496 24 14 junit 8 271 3 1 jwps 20 155 102 60 sablecc 29 237 10 9 tomcat6 153 1164 400 346 verbos 5 41 15 3 VietPad 11 84 22 8 Violet 7 73 20 9 Totals 899 7142 1999 2227
  • 105. Empirical Evaluation Benchmark KLOC Classes Sites Time (s) ArtOfIllusion 75 378 111 207 Azureus 272 1894 635 1269 java5 180 1586 572 760 JavaCup 6 41 3 19 jdepend 3 28 1 1 JFlex 10 46 27 75 JFreeChart 71 420 43 128 jGap 6 137 5 7 jgraph 14 91 6 11 JHotDraw 29 496 24 14 junit 8 271 3 1 jwps 20 155 102 60 sablecc 29 237 10 9 tomcat6 153 1164 400 346 verbos 5 41 15 3 VietPad 11 84 22 8 Violet 7 73 20 9 Totals 899 7142 1999 2227 2.4 / KLOC
  • 106. Empirical Evaluation Benchmark KLOC Classes Sites Time (s) ArtOfIllusion 75 378 111 207 Azureus 272 1894 635 1269 java5 180 1586 572 760 JavaCup 6 41 3 19 jdepend 3 28 1 1 JFlex 10 46 27 75 JFreeChart 71 420 43 128 jGap 6 137 5 7 jgraph 14 91 6 11 JHotDraw 29 496 24 14 junit 8 271 3 1 jwps 20 155 102 60 sablecc 29 237 10 9 tomcat6 153 1164 400 346 verbos 5 41 15 3 VietPad 11 84 22 8 Violet 7 73 20 9 Totals 899 7142 1999 2227 384 sites / Benchmark
  • 108. Filtered Contexts •Certain fields cannot be refactored.
  • 109. Filtered Contexts •Certain fields cannot be refactored. •Program semantics preservation depends on the actual values of these fields.
  • 110. Filtered Contexts •Certain fields cannot be refactored. •Program semantics preservation depends on the actual values of these fields. •For example, named-constants (used in mathematical calculations).
  • 111. Filtered Contexts •Certain fields cannot be refactored. •Program semantics preservation depends on the actual values of these fields. •For example, mathematical calculations). •Transitive dependency of an element outside available source code.
  • 112. Filtered Contexts •Certain fields cannot be refactored. •Program semantics preservation depends on the actual values of these fields. •For example, mathematical calculations). •Transitive dependency of an element outside available source code. •Language constraints prohibit enumerization.
  • 113. Filtered Contexts •Certain fields cannot be refactored. •Program semantics preservation depends on the actual values of these fields. •For example, mathematical calculations). •Transitive dependency of an element outside available source code. •Language constraints prohibit enumerization. •Remaining fields denoted as candidates.
  • 114. Filtered Contexts •Certain fields cannot be refactored. •Program semantics preservation depends on the actual values of these fields. •For example, mathematical calculations). •Transitive dependency of an element outside available source code. •Language constraints prohibit enumerization. •Remaining fields denoted as candidates. arr[RED]
  • 115. Filtered Contexts •Certain fields cannot be refactored. •Program semantics preservation depends on the actual values of these fields. •For example, mathematical calculations). •Transitive dependency of an element outside available source code. •Language constraints prohibit enumerization. •Remaining fields denoted as candidates.
  • 120. Result Summary •37% of all primitive constant fields identified as candidates.
  • 121. Result Summary •37% of all primitive constant fields identified as candidates. •87% of candidate fields found safe for refactoring.
  • 122. Result Summary •37% of all primitive constant fields identified as candidates. •87% of candidate fields found •Remaining 13% required more sophisticated analysis:
  • 123. Result Summary •37% of all primitive constant fields identified as candidates. •87% of candidate fields found •Remaining 13% required more sophisticated analysis: •Explicit primitive conversion analysis (casts).
  • 124. Result Summary •37% of all primitive constant fields identified as candidates. •87% of candidate fields found •Remaining 13% required more sophisticated analysis: •Explicit primitive conversion analysis (casts). •Native array copying method usage.
  • 126. Conclusions •Semantics preserving, declarative type inferencing algorithm migrating legacy Java code to the enum language construct introduced in Java 1.5.
  • 127. Conclusions •Semantics preserving, declarative type inferencing algorithm migrating legacy Java code to the language construct introduced in Java 1.5. •Implemented algorithm as a plug-in for the Eclipse IDE and evaluated it on 17 open source applications.
  • 128. Conclusions •Semantics preserving, declarative type inferencing algorithm migrating legacy Java code to the language construct introduced in Java 1.5. •Implemented algorithm as a plug-in for the Eclipse IDE and evaluated it on 17 open source applications. •Analysis cost is practical.
  • 129. Conclusions •Semantics preserving, declarative type inferencing algorithm migrating legacy Java code to the language construct introduced in Java 1.5. •Implemented algorithm as a plug-in for the Eclipse IDE and evaluated it on 17 open source applications. •Analysis cost is practical. •Weak enum pattern is commonly used in legacy Java software.
  • 130. Conclusions •Semantics preserving, declarative type inferencing algorithm migrating legacy Java code to the language construct introduced in Java 1.5. •Implemented algorithm as a plug-in for the Eclipse IDE and evaluated it on 17 open source applications. •Analysis cost is practical. •Weak enum pattern Java software. •Proposal successfully refactors a large number of static final fields into enumerated types.
  • 132. Future work • Better mimic human results:
  • 133. Future work • Better mimic human results: • Algorithms for grouping constants.
  • 134. Future work • Better mimic human results: • Algorithms for grouping constants. • Visualization techniques to facilitate grouping.
  • 135. Future work • Better mimic human results: • Algorithms for grouping constants. • Visualization techniques to facilitate grouping. • Refactor additional legacy patterns.
  • 136. Future work • Better mimic human results: • Algorithms for grouping constants. • Visualization techniques to facilitate grouping. • Refactor additional legacy patterns. • In-depth empirical evaluation.
  • 138. Comparing Human Produceable Results •Preliminary approach considers only transitive closure of type-dependent entities in current program state.
  • 139. Comparing Human Produceable Results •Preliminary approach considers only transitive closure of type-dependent entities in state. •May not accurately mimic human produceable results.
  • 142. Enhanced Enum Constant Grouping Heuristically anticipate future type-dependence:
  • 143. Enhanced Enum Constant Grouping •Structural commonality using Concern Graphs [Robillard and Murphy, ICSE ’02]: Heuristically anticipate future type-dependence:
  • 144. Enhanced Enum Constant Grouping •Structural commonality using Concern Graphs [Robillard and Murphy, ICSE ’02]: •DECREASE_SPEED, INCREASE_SPEED, and STOP are all declared in the same class! Heuristically anticipate future type-dependence:
  • 145. Enhanced Enum Constant Grouping •Structural commonality using Concern Graphs [Robillard and Murphy, ICSE ’02]: •DECREASE_SPEED are all declared in the same class! •If not in the same class, do they have other things in common? Heuristically anticipate future type-dependence:
  • 146. Enhanced Enum Constant Grouping •Structural commonality using Concern Graphs [Robillard and Murphy, ICSE ’02]: •DECREASE_SPEED are all declared in the same class! •If not in the same class, do they have other things in common? •For example, are they all annotated with the same type of annotation? Heuristically anticipate future type-dependence:
  • 147. Enhanced Enum Constant Grouping Heuristically anticipate future type-dependence:
  • 148. Enhanced Enum Constant Grouping •Lexical proximity of original constant declarations [Gravley and Lakhotia, ‘96]. Heuristically anticipate future type-dependence:
  • 149. Enhanced Enum Constant Grouping •Lexical proximity of original constant declarations [Gravley and Lakhotia, ‘96]. •Distance between original primitive values. Heuristically anticipate future type-dependence:
  • 150. Enhanced Enum Constant Grouping •Lexical proximity of original constant declarations [Gravley and Lakhotia, ‘96]. •Distance between original primitive •Edit-distance of constant names. Heuristically anticipate future type-dependence:
  • 151. Enhanced Enum Constant Grouping •Lexical proximity of original constant declarations [Gravley and Lakhotia, ‘96]. •Distance between original primitive •Edit-distance of constant names. •Relationships in version control systems (CVS/SVN) Heuristically anticipate future type-dependence:
  • 152. Enhanced Enum Constant Grouping •Lexical proximity of original constant declarations [Gravley and Lakhotia, ‘96]. •Distance between original primitive •Edit-distance of constant names. •Relationships in version control systems (CVS/SVN) •Ontology-based approach. Heuristically anticipate future type-dependence:
  • 153. Enhanced Enum Constant Grouping •Lexical proximity of original constant declarations [Gravley and Lakhotia, ‘96]. •Distance between original primitive •Edit-distance of constant names. •Relationships in version control systems (CVS/SVN) •Ontology-based approach. •Common field annotations and structural commonality in general. Heuristically anticipate future type-dependence:
  • 154. Enhanced Enum Constant Grouping •Lexical proximity of original constant declarations [Gravley and Lakhotia, ‘96]. •Distance between original primitive •Edit-distance of constant names. •Relationships in version control systems (CVS/SVN) •Ontology-based approach. •Common field annotations and commonality •Javadoc analysis/NLP. Heuristically anticipate future type-dependence:
  • 155. Visualization •3-D images representing constants? •Shapes/colors combinations could help depict how much constants have in common. •Can common usages be presented to the develop effectively? How can we present discovered constant commonality to the developer?
  • 157. Additional Compensation Patterns •Patterns involving reference types, e.g., String. 27
  • 158. Additional Compensation Patterns •Patterns involving reference types, e.g., •Commonly occur in legacy Java code. 27
  • 159. Additional Compensation Patterns •Patterns involving reference types, e.g., •Commonly occur in legacy Java code. •Use may degrade performance. 27
  • 160. Additional Compensation Patterns •Patterns involving reference types, e.g., •Commonly occur in legacy Java code. •Use may degrade performance. •Refactoring must preserve reference semantics. 27
  • 161. Additional Compensation Patterns •Patterns involving reference types, e.g., •Commonly occur in legacy Java code. •Use may degrade performance. •Refactoring must preserve •Develop additional inference rules specifically for reference types. 27
  • 162. Additional Compensation Patterns •Patterns involving reference types, e.g., •Commonly occur in legacy Java code. •Use may degrade performance. •Refactoring must preserve •Develop additional inference rules specifically for reference types. •Use type constraints [Palsberg ’94] as in [Balaban, Tip, Fuhrer ’05]. 27
  • 163. Additional Compensation Patterns •Patterns involving reference types, e.g., •Commonly occur in legacy Java code. •Use may degrade performance. •Refactoring must preserve •Develop additional inference rules specifically for reference types. •Use type constraints [Palsberg ’94] as in [Balaban, Tip, Fuhrer ’05]. 27
  • 164. Additional Compensation Patterns •Patterns involving reference types, e.g., •Commonly occur in legacy Java code. •Use may degrade performance. •Refactoring must preserve •Develop additional inference rules specifically for reference types. •Use type constraints [Palsberg ’94] as in [Balaban, Tip, Fuhrer ’05]. 27 Mechanized class migration, e.g., HashTable to HashMap
  • 165. Additional weak enum Operations 28
  • 166. Additional weak enum Operations •Migrate constants (transitively) involving bit-wise operations: 28
  • 167. Additional weak enum Operations •Migrate constants (transitively) involving bit-wise operations: •Prevalent during experimental study. 28
  • 168. Additional weak enum Operations •Migrate constants (transitively) involving bit-wise operations: •Prevalent during experimental study. •Recognize true bit-wise vs. enum manipulation. 28
  • 169. Additional weak enum Operations •Migrate constants (transitively) involving bit-wise operations: •Prevalent during experimental study. •Recognize •Convert operations to method calls. 28
  • 170. Additional weak enum Operations •Migrate constants (transitively) involving bit-wise operations: •Prevalent during experimental study. •Recognize •Convert operations to method calls. 28
  • 171. Additional weak enum Operations •Migrate constants (transitively) involving bit-wise operations: •Prevalent during experimental study. •Recognize •Convert operations to method calls. 28 Color orange = RED.combine(YEL LOW)
  • 173. Thorough Usefulness Evaluation •Preliminary study involved constants passing “preconditions” and not filtered.
  • 174. Thorough Usefulness Evaluation •Preliminary study involved constants passing “preconditions” and not filtered. •Ideally, developer feedback would enable a more accurate study:
  • 175. Thorough Usefulness Evaluation •Preliminary study involved constants passing “preconditions” and not filtered. •Ideally, developer feedback would enable a more accurate study: •Were constants intended to be enums?
  • 176. Thorough Usefulness Evaluation •Preliminary study involved constants passing “preconditions” and not filtered. •Ideally, developer feedback would enable a more accurate study: •Were constants intended to be enums? •Were constants grouped as desired?
  • 177. Thorough Usefulness Evaluation •Preliminary study involved constants passing “preconditions” and not filtered. •Ideally, developer feedback would enable a more accurate study: •Were constants intended to be enums? •Were constants grouped as desired? •Contact original subject developers.
  • 178. Thorough Usefulness Evaluation •Preliminary study involved constants passing “preconditions” and not filtered. •Ideally, developer feedback would enable a more accurate study: •Were constants intended to be enums? •Were constants grouped as desired? •Contact original subject developers. •Automated feedback system built into plug-in.
  • 180. Download •Tool publicly available via google code at http:// code.google.com/p/constants-to-enum-eclipse- plugin/
  • 181. Download •Tool publicly available via google code at code.google.com/p/constants-to-enum-eclipse- plugin/ •Integrated into Eclipse refactoring framework.
  • 182. Download •Tool publicly available via google code at code.google.com/p/constants-to-enum-eclipse- plugin/ •Integrated into Eclipse refactoring framework. •Includes GUI with preview pane.
  • 183. Download •Tool publicly available via google code at code.google.com/p/constants-to-enum-eclipse- plugin/ •Integrated into Eclipse refactoring framework. •Includes GUI with preview pane. •All transformation automated.
  • 184. Download •Tool publicly available via google code at code.google.com/p/constants-to-enum-eclipse- plugin/ •Integrated into Eclipse refactoring framework. •Includes GUI with preview pane. •All transformation automated. •Integration into standard distribution of Eclipse IDE in progress.
  • 185. Download •Tool publicly available via google code at code.google.com/p/constants-to-enum-eclipse- plugin/ •Integrated into Eclipse refactoring framework. •Includes GUI with preview pane. •All transformation automated. •Integration into standard distribution of Eclipse IDE in progress. Proposed as GSoC project in 2009, 2010
  • 186. Download •Tool publicly available via google code at code.google.com/p/constants-to-enum-eclipse- plugin/ •Integrated into Eclipse refactoring framework. •Includes GUI with preview pane. •All transformation automated. •Integration into standard distribution of Eclipse IDE in progress. •GUI for manual constant grouping.
  • 187. Download •Tool publicly available via google code at code.google.com/p/constants-to-enum-eclipse- plugin/ •Integrated into Eclipse refactoring framework. •Includes GUI with preview pane. •All transformation automated. •Integration into standard distribution of Eclipse IDE in progress. •GUI for manual constant grouping. •Full test suite.
  • 188. Download •Tool publicly available via google code at code.google.com/p/constants-to-enum-eclipse- plugin/ •Integrated into Eclipse refactoring framework. •Includes GUI with preview pane. •All transformation automated. •Integration into standard distribution of Eclipse IDE in progress. •GUI for manual constant grouping. •Full test suite. •Refactoring history (undo) and scripting support.
  • 189. Where to find more information
  • 190. Where to find more information •Raffi Khatchadourian, Jason Sawin, and Atanas Rountev. Automated refactoring of legacy Java software to enumerated types. International Conference on Software Maintenance (ICSM ’07), pages 224–233, Paris, France, October 2007. IEEE. See http://tinyurl.com/ics0m7enum.
  • 191. Where to find more information •Raffi Khatchadourian, Jason Sawin, and Atanas Rountev. Automated refactoring of legacy Java software to enumerated types. International Conference on Software Maintenance (ICSM ’07), pages 224–233, Paris, France, October 2007. IEEE. See http://tinyurl.com/ics0m7enum. •Raffi Khatchadourian, Jason Sawin, and Atanas Rountev. Automated refactoring of legacy Java software to enumerated types. Technical Report OSU-CISRC-4/07-TR26, Ohio State University, April 2007. See http://tinyurl.com/khatchad-tr07.
  • 192. Where to find more information •Raffi Khatchadourian, Jason Sawin, and Atanas Rountev. Automated refactoring of legacy Java software to enumerated types. International Conference on Software Maintenance (ICSM ’07), pages 224–233, Paris, France, October 2007. IEEE. See http://tinyurl.com/ics0m7enum. •Raffi Khatchadourian, Jason Sawin, and Atanas Rountev. Automated refactoring of legacy Java software to enumerated types. Technical Report OSU-CISRC-4/07-TR26, Ohio State University, April 2007. See http://tinyurl.com/khatchad-tr07. •Raffi Khatchadourian and Benjamin Muskalla. Enumeration refactoring: A tool for automatically converting java constants to enumerated types. Tool demonstration to appear in International Conference on Automated Software Engineering (ASE ’10), Antwerp, Belgium, September 2010. IEEE/ACM.
  • 194. Questions? And short demo if time (and demo god)permits!
  • 195. Questions? And short demo if time (and demo god)permits! Is there a shrine in Tokyo for successful tool demonstrations?

Editor's Notes

  • #2: Today I will be discussing an automated approach to refactoring legacy Java software to utilize the new Java enumerated type language construct. This is joint work Jason Sawin and Dr. Atanas Rountev at the Ohio State University and is supported in part by the NSF.
  • #4: Changing a method to accommodate a new parameter requires changing the method declaration for each method in the type hierarchy, examining other methods in the hierarchy to assure they are not being overridden, and altering each method invocation site scattered throughout the source code. HashMap iterator is fail-safe while Hashtable enumerator isn't. If you change the map while iterating, you'll know.
  • #6: With the emergence of Java’s 1.5 (Tiger) release came a rich set of new features including but not limited to generics, annotations, primitive boxing and unboxing, and the concentration of this work, type-safe enumerations. Our focus today will be to highlight an automated, semantics-preserving approach based on declarative type inferencing for the migration of legacy Java code (in particular but not limited to 1.4) to take advantage of these new, highly desirable language enumeration constructs. I will then proceed to discuss some experimental results from a research prototype of the tool on 17 large Java applications. Oh, and by the way, the tool is currently in progress to be integrated with the standard distribution of Eclipse.
  • #7: An enumerated (enum) type is a data type whose legal values consist of a fixed, closely related set of items known at compile time. They are typically used for comparisons in order to “parameterize” the behavior of the system. Values are commonly distinct from one another and possibly ordered in some preconceived way. And since it was not included up until the release of Java 5, developers, over the years, were forced to use certain “compensation patterns” to represent enum types in Java. Although several of these compensation patterns exist, here is an instance of a particular pattern, one that has been labeled by the literature as the “standard” way to represent enumerated types in Java 1.4 and below. This pattern has been commonly referred to as the “int enum pattern,” however, since our tool does not discriminate against constants belonging to most primitive types, we will refer to it as the “weak enum pattern.” The term “weak” is used here to denote the pattern’s inherent lack of several key features as I will explain shortly. Taking a look at our example, here we have a class representing a traffic light signal. The members of the class are as follows. There are three static final constant fields defined, RED, YELLOW, and GREEN, symbolizing the different colors of a traffic signal. Also notice that the constants are ordered akin to the order in which the signals typically appear. We also have a private instance variable called “color” representing the current color the traffic signal is displaying. And lastly, we have an accessor method called “getColor” which simply returns the current value of the preceding instance variable. Almost immediately apparent is the significant lack of type safety inherent to this pattern. For instance, the field color may receive values from any legal integer outside the set of 0, 1, and 2. Furthermore, operations that are legal for integers may accidentally be applied to colors, such as addition, which may not make a whole lot of sense and possibly produce values outside the intended set of original values. Another disadvantage of this pattern is that the constants are manually enumerated and its easy to see that such a task is error prone. In particular, a developer may accidentally assign the same value to two different constants unintentionally thereby mistakenly giving the same semantics to two different constants, an error that would not manifest itself until run time. Yet another drawback is the pattern’s lack of namespacing. That is, by looking at this constant alone, it is unclear as to what RED is referring to. Is it the color of a traffic signal or is it the color of a matador's cape? Last but not least, the pattern produces constants are brittle, that is, separate compilation is not supported. Since the value of the constant is in-lined into clients at compile time, changing their internal value not only requires recompilation of this class, but also recompilation of all clients. Such a situation would arise during software evolution when perhaps a new constant was to be added in between existing constants. Now let’s take a look at how we can manually transform this particular instance of the weak enum pattern to instead utilize the new language enumeration constructed provided in Java 5 ...
  • #8: First, let’s remove the static final integer constants and replace them with an equivalent language enumerated type called “Color.” Next, since we changed the type of the constants and that there is a type dependency with the private instance variable “color,” we must change the declared type of this member from integer to the “Color” enumerated type we declared above. Of course, since the color field has changed type, its associated assessor method must also be declared to return the new type of this field.
  • #9: Let’s now examine some of the key features that our new language enumerated type offers over the weak enum pattern instance we saw previously. Notice that our color constants are now associated with their own type, specifically, TrafficSignal.Color. As a consequence, we have improved the type safety of the code. Also notice that we have forgone the need to manually enumerate the constants with internal primitive values. Language enumerated types in Java 5 instead utilize the well known “Singleton” pattern where each constant refers to a single memory location in the entire system. Further notice that although the primitive values have disappeared, we have preserved the ordering we had previously bestowed on the constants in that they are now in a so-called “natural” order, that is, they are ordered by the way they are listed in the new type declaration. Moreover, language enumerated constructs must be properly prefixed by their enclosing type, thereby improving namespace. For instance, we know that red refers to a Traffic Signal color. And last but not least, since language enumerated constants are reference types, their values, since they are determined at run time, are not in-lined into clients thereby supporting separate compilation.
  • #10: Now that we have altered our Traffic Signal class to take advantage of the new language enumerated types offered in Java 1.5, let’s consider clients of the traffic signal class. What sorts of changes should be propagated to these clients? Here is a simple example where client code must be examined. Here we have an “Automobile” class that is a client of “Traffic Signals.” It has a method named “react” that takes a reference to a traffic signal as input and returns the appropriate action this automobile should take dependent upon the color of the signal. So, let’s take a look at the contexts in which the signal is used. Here’s a call to the signal’s color access method that appears in a switch statement. One nice, new feature of the enum construct is that it is compatible with switch statements. That is, previously, only integral types were allowed inside the switch expression, however, in Java 1.5, expressions evaluating to an enum type are also allowed, thus allowing for a more general switch statement. Of course, we must also ensure that each case expression evaluates to one of three colors we defined earlier before we can do any transformation. From a refactoring point of view, one stipulation is that the case statements inherit the namespace of the switch expression, so we must remove the prefix from each case statement. As I mentioned earlier, the react method returns an automobile action that should be taken by this automobile dependent upon the color of the traffic single. Interestingly, the set of “automobile actions” constitute another, separate enum type participating in the weak enum pattern. Moving further up the automobile class, we see that automobile actions are similarly declared. Also notice that there’s another constant named “MAX_SPEED” which has an almost identical declaration as the automobile actions above it, however, the connotation of this constant is very different from that of automobile actions. MAX_SPEED is what is known as a “named constant,” its a symbolic name for a particular threshold value. Another example of a named constant is java.lang.PI. You can imagine that such a value would appear in various mathematical operations which would not be directly compatible with enum types. As for MAX_SPEED In this context, is being used to denote the maximum acceleration of the automobile. As we will see, we don’t want to refactor these sorts of constants, and that distinguishing between these kinds of entities presents itself as one of the key challenges of an automated approach. Just as we did for the traffic signal class, let’s examine the code for type dependent entities of our automobile actions. Here we see a instance variable named “currentAction” which is intended to represent the current action the automobile is performing. We see that it receives its value from one of the constants so we’ll mark this entity as “promising” in that it potentially could take on an enum type. We’ll have to further examine type dependent entities of “currentAction” in order to say for sure. Here’s an example of an instance variable with a very similar declaration as the entity above, however, we definitely do not want to refactor this field as it is meant to represent the current acceleration of the automobile. Thus, its “internal value” has a very precise meaning and we do not want to remove from the code. Now that we have identified another promising enum type, we more to our second category of contexts in which these entities appear, specifically return statements. Due to this context, notice that signatures should change. Furthermore, the type of the field current action should change since it is a possible return value.
  • #11: Let’s now examine the changes required to transform automobile actions into language enumeration types. Since we are returning “Actions” we must now alter the return type of the method signature. Doing so requires further investigation as we will see shortly. The other changes are simpler in that we only need to add a prefix to each constant. Notice that the field “currentAction” does not change since its declared type has already been altered as well as the appropriate method signature. That’s one thing that’s nice about this refactoring, the original source is type correct once the declarations of the entities that have been considered “safe” for enumerization has changed. I’ll discuss what it means for a program entity to be considered safe next.
  • #13: We use a union-find data structure to track entity dependencies and to form the partitions.
  • #14: Adaptation of a classic declarative type inferencing algorithm inductively defined in the Java grammar. Here is a snippet of the formalization of our algorithm.
  • #16: Groups them into minimal sets s.t. each set must share the same enum type. Natural ordering by original primitive values in order to preserve comparability semantics.
  • #17: Column uses shows the total number of declaration sites that must be modified to accommodate the enumerization.
  • #18: See technical report for a complete list of filtered contexts.
  • #19: 13% needs more sophisticated primitive value analysis.
  • #20: Native array copying requires more sophisticated tracking of entities.