Skip to content

Improve org.springframework.util.StopWatch [SPR-13509] #18087

@spring-projects-issues

Description

@spring-projects-issues

Thomas Escolan opened SPR-13509 and commented

  • The currentTaskName field is private (final) without a getter
    The running task's name cannot be retrieved unless stop is called.
    WORKAROUND
StopWatch sw = new StopWatch();
try {
     Field f = StopWatch.class.getDeclaredField("currentTaskName");
     f.setAccessible(true);  // I'm a hacker!
     String taskname = f.get(sw);
} catch (NoSuchFieldException | IllegalAccessException e) {
     throw new IllegalStateException(e);
}
  • The start method runs init actions AFTER storing timestamp
    ILLUSTRATION
public void start(String taskName) throws IllegalStateException {
     if (this.running) {
          throw new IllegalStateException("Can't start StopWatch: it's already running");
     }
     this.startTimeMillis = System.currentTimeMillis();  // to be done last...
     this.running = true;
     this.currentTaskName = taskName;
     // ... there ; in order not to take inits above into account
}

Affects: 4.1.7

Referenced from: commits a6a6aed

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions