Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 361   Methods: 18
NCLOC: 197   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DrJavaRoot.java 25% 19.4% 27.8% 21.5%
coverage 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;
 38   
 39    import java.awt.EventQueue;
 40    import java.awt.Window;
 41    import java.awt.dnd.*;
 42    import java.awt.event.WindowEvent;
 43    import java.io.*;
 44    import java.lang.reflect.InvocationTargetException;
 45   
 46    import javax.swing.UIManager;
 47    import javax.swing.*;
 48   
 49    import edu.rice.cs.plt.lambda.Runnable1;
 50    import edu.rice.cs.util.FileOpenSelector;
 51    import edu.rice.cs.util.UnexpectedException;
 52    import edu.rice.cs.util.OutputStreamRedirector;
 53    import edu.rice.cs.util.swing.Utilities;
 54   
 55    import edu.rice.cs.drjava.ui.MainFrame;
 56    import edu.rice.cs.drjava.ui.DrJavaErrorWindow;
 57    import edu.rice.cs.drjava.ui.DrJavaErrorHandler;
 58    import edu.rice.cs.drjava.ui.SimpleInteractionsWindow;
 59    import edu.rice.cs.drjava.ui.SplashScreen;
 60    import edu.rice.cs.drjava.model.*;
 61    import edu.rice.cs.drjava.platform.PlatformFactory;
 62    import edu.rice.cs.drjava.config.*;
 63   
 64    import com.jgoodies.looks.plastic.PlasticLookAndFeel;
 65    import com.jgoodies.looks.plastic.PlasticTheme;
 66   
 67    import static edu.rice.cs.drjava.config.OptionConstants.*;
 68    import static edu.rice.cs.plt.debug.DebugUtil.error;
 69    import static edu.rice.cs.plt.debug.DebugUtil.debug;
 70   
 71    /** Main class for DrJava.
 72    * @version $Id: DrJavaRoot.java 5236 2010-04-27 01:43:36Z mgricken $
 73    */
 74    public class DrJavaRoot {
 75    /** Class to probe to see if the debugger is available */
 76    public static final String TEST_DEBUGGER_CLASS = "com.sun.jdi.Bootstrap";
 77   
 78    public static final String PLASTIC_THEMES_PACKAGE = "com.jgoodies.looks.plastic.theme";
 79   
 80    private static final PrintStream _consoleOut = System.out;
 81    private static final PrintStream _consoleErr = System.err;
 82   
 83    // /** This field is only used in the instance of this class in the Interpreter JVM. */
 84   
 85    private static SimpleInteractionsWindow _debugConsole = null;
 86   
 87    private static boolean anyLineNumbersSpecified = false;
 88   
 89    /** Main frame of this DrJava instance. */
 90    private static MainFrame _mainFrame = null;
 91   
 92    /* Config objects can't be public static final, since we have to delay construction until we know the
 93    * config file's location. (Might be specified on command line.) Instead, use accessor methods to
 94    * prevent others from assigning new values. */
 95   
 96  0 public static void main(final String[] args) {
 97  0 debug.log("Starting up");
 98    // Platform-specific UI setup.
 99  0 PlatformFactory.ONLY.beforeUISetup();
 100   
 101    // Utilities.show("DrJavaRoot started with args = " + Arrays.toString(args));
 102    // let DrJava class handle command line arguments
 103  0 if (!DrJava.handleCommandLineArgs(args)) {
 104  0 System.exit(0);
 105    }
 106   
 107  0 DrJava.warnIfLinuxWithCompiz();
 108  0 new SplashScreen().flash();
 109   
 110  0 final String[] filesToOpen = DrJava.getFilesToOpen();
 111  0 final int numFiles = filesToOpen.length;
 112   
 113    /* files to open held in filesToOpen[0:numFiles-1] which may be an initial segment of filesToOpen */
 114   
 115    /* In some unit test cases, creating a MainFrame in the main thread generated index out of bounds exceptions. It appear that this
 116    * creation process generates some swing events that are processed by the event thread. Hence we need to create the MainFrame in
 117    * the event thread.
 118    */
 119   
 120    /* Set the LookAndFeel for this session. If using a Plastic LAF, the theme must be set before setting the LAF. */
 121  0 try {
 122  0 String configLAFName = DrJava.getConfig().getSetting(LOOK_AND_FEEL);
 123  0 String currLAFName = UIManager.getLookAndFeel().getClass().getName();
 124  0 String failureMessage =
 125    "DrJava could not load the configured theme for the Plastic Look and Feel.\n" +
 126    "If you've manually edited your configuration file, try \n" +
 127    "removing the key \"plastic.theme\" and restarting DrJava.\n" +
 128    "In the meantime, the system default Look and Feel will be used.\n";
 129  0 String failureTitle = "Theme not found";
 130  0 if(Utilities.isPlasticLaf(configLAFName)) {
 131  0 String themeName = PLASTIC_THEMES_PACKAGE + "." + DrJava.getConfig().getSetting(PLASTIC_THEMES);
 132  0 try {
 133  0 PlasticTheme theme = (PlasticTheme) Class.forName(themeName).getConstructor(new Class<?>[]{ }).newInstance();
 134  0 PlasticLookAndFeel.setPlasticTheme(theme);
 135  0 PlasticLookAndFeel.setTabStyle(PlasticLookAndFeel.TAB_STYLE_METAL_VALUE);
 136  0 com.jgoodies.looks.Options.setPopupDropShadowEnabled(true);
 137  0 if(! configLAFName.equals(currLAFName)) UIManager.setLookAndFeel(configLAFName);
 138    } catch(NoSuchMethodException nsmex) {
 139  0 JOptionPane.showMessageDialog(null, failureMessage, failureTitle, JOptionPane.ERROR_MESSAGE);
 140    } catch(SecurityException sex) {
 141  0 JOptionPane.showMessageDialog(null, failureMessage, failureTitle, JOptionPane.ERROR_MESSAGE);
 142    } catch(InstantiationException iex) {
 143  0 JOptionPane.showMessageDialog(null, failureMessage, failureTitle, JOptionPane.ERROR_MESSAGE);
 144    } catch(IllegalAccessException iaex) {
 145  0 JOptionPane.showMessageDialog(null, failureMessage, failureTitle, JOptionPane.ERROR_MESSAGE);
 146    } catch(IllegalArgumentException iaex) {
 147  0 JOptionPane.showMessageDialog(null, failureMessage, failureTitle, JOptionPane.ERROR_MESSAGE);
 148    } catch(InvocationTargetException itex) {
 149  0 JOptionPane.showMessageDialog(null, failureMessage, failureTitle, JOptionPane.ERROR_MESSAGE);
 150    }
 151  0 } else if (! configLAFName.equals(currLAFName)) {
 152  0 UIManager.setLookAndFeel(configLAFName);
 153    }
 154   
 155    // The MainFrame *must* be constructed after the compiler setup process has
 156    // occurred; otherwise, the list of compilers in the UI will be wrong.
 157   
 158    // Utilities.showDebug("Creating MainFrame");
 159  0 _mainFrame = new MainFrame();
 160    // Utilities.showDebug("MainFrame created");
 161   
 162    // Make sure all uncaught exceptions are shown in an DrJavaErrorHandler
 163  0 DrJavaErrorWindow.setFrame(_mainFrame);
 164  0 Thread.setDefaultUncaughtExceptionHandler(DrJavaErrorHandler.INSTANCE);
 165   
 166    /* We use EventQueue.invokeLater rather than Utilities.invokeLater to ensure all files have been loaded and
 167    * added to the file view before the MainFrame is set visible. When this was not done, we occasionally encountered
 168    * a NullPointerException on start up when specifying a file (ex: java -jar drjava.jar somefile.java)
 169    */
 170  0 EventQueue.invokeLater(new Runnable(){
 171  0 public void run(){
 172  0 _mainFrame.start();
 173   
 174    // now open the files specified on the command line. must be done here
 175    // after _mainFrame.start() to address bug
 176    // [ drjava-Bugs-2831253 ] Starting DrJava with Project as Parameter
 177  0 _openCommandLineFiles(_mainFrame, filesToOpen, numFiles, true);
 178    }
 179    });
 180   
 181    // redirect stdout to DrJava's console
 182  0 System.setOut(new PrintStream(new OutputStreamRedirector() {
 183  0 public void print(String s) { _mainFrame.getModel().systemOutPrint(s); }
 184    }));
 185   
 186    // redirect stderr to DrJava's console
 187  0 System.setErr(new PrintStream(new OutputStreamRedirector() {
 188  0 public void print(String s) { _mainFrame.getModel().systemErrPrint(s); }
 189    }));
 190   
 191    // Utilities.showDebug("showDebugConsole flag = " + DrJava.getShowDebugConsole());
 192    // Show debug console if enabled
 193  0 if (DrJava.getShowDebugConsole()) showDrJavaDebugConsole(_mainFrame);
 194    }
 195    catch(Throwable t) {
 196  0 error.log(t);
 197    // Show any errors to the real System.err and in an DrJavaErrorHandler
 198  0 _consoleErr.println(t.getClass().getName() + ": " + t.getMessage());
 199  0 t.printStackTrace(_consoleErr);
 200  0 System.out.println("error thrown");
 201  0 DrJavaErrorHandler.record(t);
 202    }
 203    }
 204   
 205    /** Handle the list of files specified on the command line. Feature request #509701.
 206    * If file exists, open it in DrJava. Otherwise, ignore it.
 207    * Is there a better way to handle nonexistent files? Dialog box, maybe?
 208    */
 209  10 static void openCommandLineFiles(final MainFrame mf, final String[] filesToOpen, boolean jump) {
 210  10 openCommandLineFiles(mf, filesToOpen, filesToOpen.length, jump);
 211    }
 212   
 213    /** Handle the list of files specified on the command line. Feature request #509701. If the final element in
 214    * filesToOpen is a pathSeparator, it opens the debug console. If file exists, open it in DrJava. Otherwise, ignore
 215    * it. Is there a better way to handle nonexistent files? Dialog box, maybe?
 216    * Why the wait?
 217    */
 218  10 static void openCommandLineFiles(final MainFrame mf, final String[] filesToOpen, final int len, final boolean jump) {
 219  10 Utilities.invokeAndWait(new Runnable() { public void run() { _openCommandLineFiles(mf, filesToOpen, len, jump); }});
 220    }
 221   
 222  10 private static void _openCommandLineFiles(final MainFrame mf, String[] filesToOpen, int len, boolean jump) {
 223    // Assertion commented out because it doesn't hold at startup. See DrJava bug 2321815.
 224    /* assert EventQueue.isDispatchThread(); */
 225    // Utilities.showDebug("Files to open: " + Arrays.toString(filesToOpen));
 226  10 anyLineNumbersSpecified = false;
 227  10 for (int i = 0; i < len; i++) {
 228  21 String currFileName = filesToOpen[i];
 229   
 230    // check if the file contains a line number
 231    // the line number can be specified at the end of the file name,
 232    // separated by File.pathSeparator
 233  21 int lineNo = -1;
 234  21 int pathSepIndex = currFileName.indexOf(File.pathSeparatorChar);
 235  21 if (pathSepIndex >= 0) {
 236  0 try {
 237  0 lineNo = Integer.valueOf(currFileName.substring(pathSepIndex+1));
 238  0 anyLineNumbersSpecified = true;
 239    }
 240  0 catch(NumberFormatException nfe) { lineNo = -1; }
 241  0 currFileName = currFileName.substring(0,pathSepIndex);
 242    }
 243   
 244  21 boolean isProjectFile =
 245    currFileName.endsWith(OptionConstants.PROJECT_FILE_EXTENSION) ||
 246    currFileName.endsWith(OptionConstants.PROJECT_FILE_EXTENSION2) ||
 247    currFileName.endsWith(OptionConstants.OLD_PROJECT_FILE_EXTENSION);
 248  21 final File file = new File(currFileName).getAbsoluteFile();
 249  21 FileOpenSelector command = new FileOpenSelector() {
 250  21 public File[] getFiles() { return new File[] {file}; }
 251    };
 252  21 try {
 253  0 if (isProjectFile) mf.openProject(command);
 254  0 else if (currFileName.endsWith(OptionConstants.EXTPROCESS_FILE_EXTENSION)) MainFrame.openExtProcessFile(file);
 255    else {
 256  21 if (jump && (lineNo >= 0)) {
 257    /* if a line number has been specified, open the file using MainFrame.open and jump to lineNo using
 258    * MainFrame._jumpToLine. Note: this can only be done after MainFrame.start() has been called. */
 259  0 mf.open(command);
 260  0 mf._jumpToLine(lineNo);
 261    }
 262    else {
 263    // without line number, use the model's openFile.
 264  21 mf.getModel().openFile(command);
 265    }
 266    }
 267    }
 268   
 269    catch (FileNotFoundException ex) {
 270    // TODO: show a dialog? (file not found) (mgricken)
 271    }
 272    catch (SecurityException se) {
 273    // TODO: show a dialog? (file not found) (mgricken)
 274    }
 275    catch (AlreadyOpenException aoe) {
 276    // This explicitly does nothing to ignore duplicate files.
 277    }
 278    catch (FileMovedException aoe) {
 279    // This explicitly does nothing to ignore duplicate files.
 280    }
 281    catch (IOException ex) {
 282    // TODO: show a dialog? (file not found) (mgricken)
 283    }
 284  0 catch (Exception ex) { throw new UnexpectedException(ex); }
 285    }
 286    }
 287   
 288    /** Shows a separate interactions window with a reference to DrJava's MainFrame defined as "mainFrame".
 289    * Useful for debugging DrJava.
 290    * @param mf MainFrame to define in the new window
 291    */
 292  0 public static void showDrJavaDebugConsole(MainFrame mf) {
 293  0 if (_debugConsole == null) {
 294  0 _debugConsole = new SimpleInteractionsWindow("DrJava Debug Console") {
 295  0 protected void close() {
 296  0 dispose();
 297  0 _debugConsole = null;
 298    }
 299    };
 300    // TODO: define appropriate context
 301    // _debugConsole.defineConstant("mainFrame", mf);
 302    // _debugConsole.defineConstant("model", mf.getModel());
 303    // _debugConsole.defineConstant("config", DrJava.getConfig());
 304  0 _debugConsole.setVisible(true);
 305    }
 306  0 else _debugConsole.toFront();
 307    }
 308   
 309    /** Get the actual System.err stream.
 310    * @return System.err
 311    */
 312  0 public static PrintStream consoleErr() { return _consoleErr; }
 313   
 314    /** Get the actual System.out stream.
 315    * @return System.out
 316    */
 317  0 public static PrintStream consoleOut() { return _consoleOut; }
 318   
 319    /** User dragged something into the component. */
 320  0 public static void dragEnter(DropTargetDragEvent dropTargetDragEvent) {
 321  0 _mainFrame.dragEnter(dropTargetDragEvent);
 322    }
 323   
 324    /** User dropped something on the component. */
 325  0 public static void drop(DropTargetDropEvent dropTargetDropEvent) {
 326  0 _mainFrame.drop(dropTargetDropEvent);
 327    }
 328   
 329    /** Installs the modal window adapter if available, otherwise installs a non-modal dummy listener.
 330    * @param w window trying to get the modal window listener
 331    * @param toFrontAction action to be performed after the window has been moved to the front again
 332    * @param closeAction action to be performed when the window is closing
 333    */
 334  0 public static void installModalWindowAdapter(final Window w,
 335    final Runnable1<? super WindowEvent> toFrontAction,
 336    final Runnable1<? super WindowEvent> closeAction) {
 337  0 _mainFrame.installModalWindowAdapter(w, toFrontAction, closeAction);
 338    }
 339   
 340    /** Removes the modal window adapter.
 341    * @param w window releasing the modal window adapter */
 342  0 public static void removeModalWindowAdapter(Window w) {
 343  0 _mainFrame.removeModalWindowAdapter(w);
 344    }
 345   
 346    /** Handles an "open file" request, either from the remote control server or the operating system.
 347    * @param f file to open
 348    * @param lineNo line number to jump to, or -1 of not specified */
 349  0 public static void handleRemoteOpenFile(File f, int lineNo) {
 350  0 DrJava._log.log("DrJavaRoot.handleRemoteOpenFile, f=" + f);
 351  0 if (_mainFrame != null) {
 352  0 DrJava._log.log("\tcalling _mainFrame");
 353  0 _mainFrame.handleRemoteOpenFile(f, lineNo);
 354    }
 355    else {
 356  0 DrJava._log.log("\tadded to _filesToOpen");
 357  0 DrJava.addFileToOpen(f.getAbsolutePath());
 358    }
 359    }
 360    }
 361