I am attempting to create a System Time Independent timer. In order to do this I have created an extension of the Timer object which overwrites the schedule methods and uses them as adapters to the schedule methods of the new TimerObject. The new Timer schedules Tasks which are of a local duplicate of the base TimerTask class to allow for access to the package private fields of TimerTask. This new TimerTask class includes a field for a base TimerTask object and behaves as a wrapper, the run method of the containing TimerTask calls the run method of the contained TimerTask to maintain the expected behavior. The problem I have encountered is with rescheduling, cancellation and repeated events. The new Timer is unable to access the nextExecutionTime and status fields of the inner TimerTask and so is unable to detect when the inner TimerTask has been cancelled and so events are executed regardless of whether or not they have been cancelled or experienced other status changes.
This is relevant because this class is meant to be swapped in for testing purposes and should not require modifications to the class it is testing, the new Timer is passed in in place of a standard timer and appears to behave as such from the application perspective however I am unable to achieve this without access to the fields of the contained TimerTask with which the class being tested interacts.
The present state of the code below is attempting only to make single executions work with all repeating schedule throwing a runtime error although the original code from each is present but commented out. As an attempted workaround for the one-time execution case The run method of the containing TimerTask calls the cancel method of the contained task which should return true if the contained task was scheduled and had not beed previously cancelled however the inner tasks state remains VIRGIN (read 0) and so this method does not work either.
Can anyone suggest a way of fixing this problem either by gaining access or through another design?
The code is in the following gitHub repo:
I have renamed the files and removed packages, in the working code they are both done according to convention.
Aucun commentaire:
Enregistrer un commentaire