Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
14   69   7   3.5
2   37   0.5   4
4     1.75  
1    
 
  ProtectingTimerTask       Line # 30 14 0% 7 1 95% 0.95
 
No Tests
 
1    /*
2    * Created on Mar 30, 2008
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2008-2010 the original author or authors.
15    */
16    package org.fest.swing.monitor;
17   
18    import static java.util.logging.Level.WARNING;
19    import static org.fest.reflect.core.Reflection.field;
20   
21    import java.util.TimerTask;
22    import java.util.logging.Logger;
23   
24    /**
25    * Prevents misbehaving <code>{@link TimerTask}</code>s from canceling the timer thread by throwing exceptions and/or
26    * errors.
27    *
28    * @author Alex Ruiz
29    */
 
30    class ProtectingTimerTask extends TimerTask {
31   
32    private static final int CANCELED = 3;
33   
34    private static Logger logger = Logger.getLogger(ProtectingTimerTask.class.getName());
35   
36    private final TimerTask task;
37   
 
38  1553 toggle ProtectingTimerTask(TimerTask task) {
39  1553 this.task = task;
40    }
41   
42    /** {@inheritDoc} */
 
43  137 toggle public void run() {
44  137 if (isCanceled()) {
45  1 cancel();
46  1 return;
47    }
48  136 try {
49  136 task.run();
50    } catch (Throwable thrown) {
51  1 handleException(thrown);
52    }
53    }
54   
 
55  137 toggle private boolean isCanceled() {
56  137 boolean isCancelled = false;
57  137 try {
58  137 int state = field("state").ofType(int.class).in(task).get();
59  137 isCancelled = state == CANCELED;
60    } catch (RuntimeException e) {
61  0 handleException(e);
62    }
63  137 return isCancelled;
64    }
65   
 
66  1 toggle private void handleException(Throwable thrown) {
67  1 logger.log(WARNING, "Exception thrown by a TimerTask", thrown);
68    }
69    }