Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 210   Methods: 14
NCLOC: 112   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultLightWeightParsingControl.java 0% 0% 0% 0%
coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.model;
 38   
 39    import java.util.List;
 40    import java.util.LinkedList;
 41    import java.util.HashMap;
 42    import javax.swing.text.*;
 43   
 44    import edu.rice.cs.drjava.DrJava;
 45    import edu.rice.cs.drjava.config.OptionConstants;
 46    import edu.rice.cs.drjava.model.definitions.*;
 47    import edu.rice.cs.drjava.ui.DrJavaErrorHandler;
 48    import edu.rice.cs.util.Log;
 49    import edu.rice.cs.util.swing.Utilities;
 50   
 51    /** Default light-weight parsing control. This class is declared final because it cannot be robustly subclassed because
 52    * the constructor starts a thread.
 53    * @version $Id: DefaultLightWeightParsingControl.java 5236 2010-04-27 01:43:36Z mgricken $
 54    */
 55    public final class DefaultLightWeightParsingControl implements LightWeightParsingControl {
 56    /** The model. */
 57    private AbstractGlobalModel _model;
 58   
 59    /** The time at which updates may be performed. */
 60    private long _beginUpdates;
 61   
 62    /** The time at which the last delay operation was performed. */
 63    private long _lastDelay = System.currentTimeMillis();
 64   
 65    /** Last updates for the documents. */
 66    private HashMap<OpenDefinitionsDocument, Long> _lastUpdates = new HashMap<OpenDefinitionsDocument, Long>();
 67   
 68    /** Enclosing class names for the documents. */
 69    private HashMap<OpenDefinitionsDocument, String> _enclosingClassNames = new HashMap<OpenDefinitionsDocument, String>();
 70   
 71    /** Flag to stop automatic updates. */
 72    private volatile boolean _running = false;
 73   
 74    /** Monitor to restart automatic updates. */
 75    private Object _restart = new Object();
 76   
 77    /** List of listeners. */
 78    private LinkedList<LightWeightParsingListener> _listeners = new LinkedList<LightWeightParsingListener>();
 79   
 80    /** Log file. */
 81    private static final Log _log = new Log("LightWeightParsing", false);
 82   
 83    /** Thread group for the updater. */
 84    private ThreadGroup _updaterThreadGroup = new ThreadGroup("Light-weight parsing updater thread group") {
 85  0 public void uncaughtException(Thread t, Throwable e) {
 86  0 _log.log("Uncaught exception in updater; disabled for rest of session", e);
 87  0 DrJavaErrorHandler.record(e);
 88    }
 89    };
 90   
 91    /** Thread to perform automatic updates. */
 92    private Thread _updater = new Thread(_updaterThreadGroup, new Runnable() {
 93  0 public void run() {
 94  0 while(true) { // this is ok, it's a daemon thread and will die when all other threads have died
 95  0 while (!_running) {
 96  0 _log.log("Waiting...");
 97  0 try { synchronized(_restart) { if (! _running) _restart.wait(); } }
 98    catch(InterruptedException e) { }
 99    }
 100  0 long current = System.currentTimeMillis();
 101  0 long delta = (_beginUpdates-current);
 102    // _log.logTime("Begin updates at " + _beginUpdates + " (delta=" + delta + ")");
 103  0 if (current>=_beginUpdates) {
 104  0 OpenDefinitionsDocument doc = _model.getActiveDocument();
 105  0 Long last = _lastUpdates.get(doc);
 106  0 if ((last == null) || (last < _lastDelay)) {
 107  0 update(doc);
 108    // _log.logTime("Update done.");
 109    }
 110    else {
 111    // _log.logTime("Not updating, last update was at " + last);
 112    }
 113  0 delta = DrJava.getConfig().getSetting(OptionConstants.DIALOG_LIGHTWEIGHT_PARSING_DELAY).intValue();
 114    }
 115    // _log.logTime("Not updating, sleeping for " + delta);
 116  0 try {
 117  0 Thread.sleep(delta);
 118    }
 119    catch (InterruptedException e) { /* ignore, just wake up earlier and retry */ }
 120    }
 121    }
 122    });
 123   
 124    /** Create the default light-weight parsing control. Starts a new thread.
 125    * @param model the model */
 126  0 public DefaultLightWeightParsingControl(AbstractGlobalModel model) {
 127  0 _model = model;
 128  0 _updater.setDaemon(true);
 129  0 _updater.start();
 130    }
 131   
 132    /** Perform light-weight parsing. */
 133  0 public synchronized void update(final OpenDefinitionsDocument doc) {
 134  0 _log.log("Update for " + doc);
 135  0 try {
 136  0 _lastUpdates.put(doc, System.currentTimeMillis());
 137  0 final String old = _enclosingClassNames.get(doc);
 138  0 final String updated = doc.getEnclosingClassName(doc.getCurrentLocation(), true);
 139  0 if ((old == null) || (!old.equals(updated))) {
 140  0 _enclosingClassNames.put(doc, updated);
 141  0 Utilities.invokeLater(new Runnable() {
 142  0 public void run() {
 143  0 List<LightWeightParsingListener> listeners = getListeners();
 144  0 for (LightWeightParsingListener l: listeners) { l.enclosingClassNameUpdated(doc, old, updated); }
 145    }
 146    });
 147    }
 148    }
 149    catch(BadLocationException e) { /* ignore */ }
 150    catch(ClassNameNotFoundException e) { /* ignore */ }
 151    }
 152   
 153    /** Start or stop automatic updates.
 154    * @param b {@code true} to start or {@code false} to stop automatic updates
 155    */
 156  0 public void setAutomaticUpdates(boolean b) {
 157  0 _log.log("setAutomaticUpdates(" + b + ")");
 158  0 _running = b;
 159  0 if (b) {
 160  0 delay();
 161  0 synchronized(_restart) {
 162  0 _restart.notify();
 163    }
 164    }
 165    }
 166   
 167    /** Delay the next update. */
 168  0 public void delay() {
 169  0 _lastDelay = System.currentTimeMillis();
 170  0 _beginUpdates = _lastDelay + (DrJava.getConfig().getSetting(OptionConstants.DIALOG_LIGHTWEIGHT_PARSING_DELAY).intValue());
 171    }
 172   
 173    /** Reset light-weight parsing. Forget everything. */
 174  0 public synchronized void reset() {
 175  0 for(final OpenDefinitionsDocument doc: _enclosingClassNames.keySet()) {
 176  0 final String old = _enclosingClassNames.get(doc);
 177  0 Utilities.invokeLater(new Runnable() {
 178  0 public void run() {
 179  0 List<LightWeightParsingListener> listeners = getListeners();
 180  0 for (LightWeightParsingListener l: listeners) { l.enclosingClassNameUpdated(doc, old, null); }
 181    }
 182    });
 183    }
 184  0 _enclosingClassNames.clear();
 185  0 _lastUpdates.clear();
 186    }
 187   
 188    /** Return the last enclosing class name for the specified document, "" if not inside a class, or
 189    * null if unknown.
 190    * WARNING: In long source files and when contained in anonymous inner classes, this function might take a LONG time.
 191    * @param doc the document for which we want the information
 192    * @return the enclosing class name
 193    */
 194  0 public synchronized String getEnclosingClassName(OpenDefinitionsDocument doc) { return _enclosingClassNames.get(doc); }
 195   
 196    /** Add the listener to this controller.
 197    * @param l listener to add */
 198  0 public synchronized void addListener(LightWeightParsingListener l) { _listeners.add(l); }
 199   
 200    /** Remove the listener from this controller. */
 201  0 public synchronized void removeListener(LightWeightParsingListener l) { _listeners.remove(l); }
 202   
 203    /** Remove all listeners from this controller. */
 204  0 public synchronized void removeAllListeners() { _listeners.clear(); }
 205   
 206    /** @return a copy of the list of listeners. */
 207  0 public synchronized List<LightWeightParsingListener> getListeners() {
 208  0 return new LinkedList<LightWeightParsingListener>(_listeners);
 209    }
 210    }