...
This noncompliant example overrides the methods after()
and compareTo()
of the class java.util.Calendar
. The Calendar.after()
method returns a boolean
value depending on whether the Calendar represents a time after the time represented by the specified Object
parameter. The programmer wishes to extend this functionality and return true
even when the two objects are equal. If it is the first day of week it must return true
, else run a regular comparison. Note that compareTo()
is also overridden in this example, to provide a "comparisons by day" option to clients. For example, comparing today's day with the first day of week (which differs from country to country) to check whether it is a weekday.
...
Code Block | ||
---|---|---|
| ||
class CalendarSubclass extends Calendar { @Override public boolean after(Object when) { if(when instanceof Calendar && super.compareTo((Calendar)when) == 0) // correctly calls CalendarSubclassCalendar.compareTo() return true; return super.after(when); // incorrectly calls CalendarSubclass.compareTo() due to polymorphism } @Override public int compareTo(Calendar anotherCalendar) { // This method is erroneously invoked by Calendar.after() return compareTo(anotherCalendar.getFirstDayOfWeek(),anotherCalendar); } private int compareTo(int firstDayOfWeek, Calendar c) { int thisTime = c.get(Calendar.DAY_OF_WEEK); return (thisTime > firstDayOfWeek) ? 1 : (thisTime == firstDayOfWeek) ? 0 : -1; } public static void main(String[] args) { CalendarSubclass cscs1 = new CalendarSubclass(); cs.set(Calendar.DAY_OF_WEEK, 3); // set the day to the third day (Tuesday in US)CalendarSubclass cs2 = new CalendarSubclass(); cs1.setTime(new Date()); System.out.println(cscs1.after(cscs2)); // returnsprints truefalse } /* Implementation of other abstract methods */ } // The implementation of java.util.Calendar.after() method is shown below public boolean after(Object when) { return when instanceof Calendar && compareTo((Calendar)when) > 0; // forwards to the subclass's implementation erroneously } |
...
Code Block | ||
---|---|---|
| ||
// The CalendarImplementation object is a concrete implementation of the abstract Calendar class // Class ForwardingCalendar public class ForwardingCalendar { private final CalendarImplementation c; public ForwardingCalendar(CalendarImplementation c) { this.c = c; } public boolean after(Object when) { return c.after(when); } public int compareTo(Calendar anotherCalendar) { // ForwardingCalendar's compareTo() will not be called now return c.compareTo(anotherCalendar); } } //Class CompositeCalendar class CompositeCalendar extends ForwardingCalendar { public CompositeCalendar(CalendarImplementation ci) { super(ci); } @Override public boolean after(Object when) { if(when instanceof Calendar && super.compareTo((Calendar)when) == 0) // this will call the overridden version i.e. CompositeClass.compareTo(); return true if it is the first day of week return true; return super.after(when); // does not compare with first day of week anymore; uses default comparison with epoch } @Override public int compareTo(Calendar anotherCalendar) { // CompositeCalendarcompareTo() will not be called now return compareTo(anotherCalendar.getFirstDayOfWeek(),anotherCalendar); } private int compareTo(int firstDayOfWeek, Calendar c) { int thisTime = c.get(Calendar.DAY_OF_WEEK); return (thisTime > firstDayOfWeek) ? 1 : (thisTime == firstDayOfWeek) ? 0 : -1; } public static void main(String[] args) { CalendarImplementation cici1 = new CalendarImplementation(); ci.set(Calendar.DAY_OF_WEEK, 3); // set the day to the third day (Tuesday in US)CalendarImplementation ci2 = new CalendarImplementation(); CompositeCalendar c = new CompositeCalendar(cici1); ci1.setTime(new Date()); System.out.println(c.after(cici2)); // returnsprints true false } } |
This technique allows a superclass to evolve without causing much distress for to its extending classes.
Risk Assessment
...