Clover coverage report - Java Language Levels Test Coverage (javalanglevels-20120305-r5436)
Coverage timestamp: Sun Mar 4 2012 22:02:46 CST
file stats: LOC: 256   Methods: 33
NCLOC: 142   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Utilities.java 31.8% 36% 33.3% 34.7%
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.javalanglevels.util;
 38   
 39    import java.awt.EventQueue;
 40    import java.awt.*;
 41    import java.awt.event.*;
 42    import java.awt.datatransfer.*;
 43    import java.lang.reflect.Array;
 44   
 45    import java.io.File;
 46    import java.io.FileOutputStream;
 47    import java.io.FileInputStream;
 48    import java.io.IOException;
 49    import java.io.PrintWriter;
 50    import java.io.StringReader;
 51    import java.io.StringWriter;
 52    import java.nio.channels.FileChannel;
 53   
 54    import javax.swing.*;
 55    import java.text.DecimalFormat;
 56    import java.util.ArrayList;
 57    import java.util.List;
 58    import java.util.HashMap;
 59   
 60    import edu.rice.cs.javalanglevels.tree.ModifiersAndVisibility;
 61   
 62    public class Utilities {
 63   
 64    /** A file copy method taken from the web. */
 65  45 public static void copyFile(File sourceFile, File destFile) throws IOException {
 66  45 if (! destFile.exists()) destFile.createNewFile();
 67   
 68  45 FileChannel source = null;
 69  45 FileChannel destination = null;
 70  45 try {
 71  45 source = new FileInputStream(sourceFile).getChannel();
 72  45 destination = new FileOutputStream(destFile).getChannel();
 73  45 destination.transferFrom(source, 0, source.size());
 74    }
 75    finally {
 76  45 if (source != null) source.close();
 77  45 if (destination != null) destination.close();
 78    }
 79    }
 80   
 81   
 82    /** Runs the task synchronously if the current thread is the event thread; otherwise passes it to the
 83    * event thread to be run asynchronously after all events already on the queue have been processed.
 84    */
 85  0 public static void invokeLater(Runnable task) {
 86  0 if (EventQueue.isDispatchThread()) {
 87  0 task.run();
 88  0 return;
 89    }
 90  0 EventQueue.invokeLater(task);
 91    }
 92   
 93  0 public static void invokeAndWait(Runnable task) {
 94  0 if (EventQueue.isDispatchThread()) {
 95  0 task.run();
 96  0 return;
 97    }
 98  0 try { EventQueue.invokeAndWait(task); }
 99  0 catch(Exception e) { throw new RuntimeException(e); }
 100    }
 101   
 102  0 public static void main(String[] args) { clearEventQueue(); }
 103   
 104  0 public static void clearEventQueue() { Utilities.invokeAndWait(new Runnable() { public void run() { } }); }
 105   
 106    /** Show a modal debug message box with an OK button regardless of TEST_MODE.
 107    * @param msg string to display
 108    */
 109  0 public static void show(final String msg) {
 110  0 Utilities.invokeAndWait(new Runnable() {
 111  0 public void run() {
 112  0 new ScrollableDialog(null,"Debug Message", "Debug Message from Utilities.show():", msg, false).show();
 113    }
 114    } );
 115    }
 116   
 117    /** Shows a modal debug message box with an OK button when not in TEST_MODE.
 118    * @param msg string to display
 119    */
 120  0 public static void showDebug(String msg) { showMessageBox(msg, "Debug Message"); }
 121   
 122    /** Shows a modal message box with an OK button.
 123    * @param msg string to display
 124    */
 125  0 public static void showMessageBox(final String msg, final String title) {
 126    // Utilities.invokeAndWait(new Runnable() { public void run() { JOptionPane.showMessageDialog(null, msg); } } );
 127  0 Utilities.invokeAndWait(new Runnable() {
 128  0 public void run() { new ScrollableDialog(null, title, "Message:", msg, false).show(); }
 129    } );
 130    }
 131   
 132  0 public static void showStackTrace(final Throwable t) {
 133  0 Utilities.invokeAndWait(new Runnable() {
 134  0 public void run() { new ScrollableDialog(null, "Stack Trace", "Stack Trace:", getStackTrace(t), false).show(); }
 135    } );
 136    }
 137   
 138    /** @return a string with the current clipboard selection, or null if not available. */
 139  0 public static String getClipboardSelection(Component c) {
 140  0 Clipboard cb = c.getToolkit().getSystemClipboard();
 141  0 if (cb == null) return null;
 142  0 Transferable t = cb.getContents(null);
 143  0 if (t == null) return null;
 144  0 String s = null;
 145  0 try {
 146  0 java.io.Reader r = DataFlavor.stringFlavor.getReaderForText(t);
 147  0 int ch;
 148  0 final StringBuilder sb = new StringBuilder();
 149  0 while ((ch=r.read()) !=-1 ) { sb.append((char)ch); }
 150  0 s = sb.toString();
 151    }
 152    catch(UnsupportedFlavorException ufe) { /* ignore, return null */ }
 153    catch(java.io.IOException ioe) { /* ignore, return null */ }
 154  0 return s;
 155    }
 156   
 157    /** @return an action with a new name that delegates to another action. */
 158  0 public static AbstractAction createDelegateAction(String newName, final Action delegate) {
 159  0 return new AbstractAction(newName) {
 160  0 public void actionPerformed(ActionEvent ae) { delegate.actionPerformed(ae); }
 161    };
 162    }
 163    /** Gets the stack trace of the given Throwable as a String.
 164    * @param t the throwable object for which to get the stack trace
 165    * @return the stack trace of the given Throwable
 166    */
 167  0 public static String getStackTrace(Throwable t) {
 168  0 StringWriter sw = new StringWriter();
 169  0 PrintWriter pw = new PrintWriter(sw);
 170  0 t.printStackTrace(pw);
 171  0 return sw.toString();
 172    }
 173   
 174    /** Gets the stack trace of the current code. Does not include this method.
 175    * @return the stack trace for the current code
 176    */
 177  0 public static String getStackTrace() {
 178    // TODO: Thread.getStackTrace() which is new to Java 5.0 might be more efficient
 179  0 try { throw new Exception(); }
 180    catch (Exception e) {
 181  0 StringWriter sw = new StringWriter();
 182  0 PrintWriter pw = new PrintWriter(sw);
 183  0 StackTraceElement[] stes = e.getStackTrace();
 184  0 int skip = 1;
 185  0 for(StackTraceElement ste: stes) {
 186  0 if (skip > 0) --skip;
 187  0 else { pw.print("at "); pw.println(ste); }
 188    }
 189  0 return sw.toString();
 190    }
 191    }
 192   
 193    /** The standard java.util contains method on arrays of reference type.
 194    * @return true iff the value elt appears in a. */
 195  1732 public static boolean contains(Object[] a, Object elt) {
 196  996 for (Object o: a) { if (o.equals(elt)) return true; }
 197  736 return false;
 198    }
 199   
 200    /** @return true iff that has a visibility modifier. */
 201  176 public static boolean hasVisibilityModifier(String[] modifiers) {
 202  176 for (String s: modifiers) {
 203  37 if (s.equals("private") || s.equals("public") || s.equals("protected")) return true;
 204    }
 205  139 return false;
 206    }
 207   
 208    /** @return true iff that has "final" as a modifier. */
 209  66 public static boolean isFinal(String[] modifiers) { return contains(modifiers, "final"); }
 210   
 211    /** @return true iff that has "final" as a modifier. */
 212  107 public static boolean isStatic(String[] modifiers) { return contains(modifiers, "static"); }
 213   
 214    /** @return true iff that has "public" as a modifier. */
 215  8 public static boolean isPublic(String[] modifiers) { return contains(modifiers, "public"); }
 216   
 217    /** @return true iff that has "protected" as a modifier. */
 218  0 public static boolean isProtected(String[] modifiers) { return contains(modifiers, "protected"); }
 219   
 220    /** @return true iff that has "protected" as a modifier. */
 221  0 public static boolean isPrivate(String[] modifiers) { return contains(modifiers, "private"); }
 222   
 223    /** @return true iff that has "abstract" as a modifier. */
 224  126 public static boolean isAbstract(String[] modifiers) { return contains(modifiers, "abstract"); }
 225   
 226    // TODO: move the following to ModifiersAndVisibility
 227    /** @return true iff that has "final" as a modifier. */
 228  0 public static boolean isFinal(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "final"); }
 229   
 230    /** @return true iff that has "final" as a modifier. */
 231  0 public static boolean isStatic(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "static"); }
 232   
 233    /** @return true iff that has "public" as a modifier. */
 234  1118 public static boolean isPublic(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "public"); }
 235   
 236    /** @return true iff that has "public" as a modifier. */
 237  142 public static boolean isProtected(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "protected"); }
 238   
 239    /** @return true iff that has "public" as a modifier. */
 240  165 public static boolean isPrivate(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "private"); }
 241   
 242    /** @return true iff that has "abstract" as a modifier. */
 243  0 public static boolean isAbstract(ModifiersAndVisibility mav) { return contains(mav.getModifiers(), "abstract"); }
 244   
 245  121 public static <T> T[] catenate(T[] A, T[] B) {
 246    // T[] C = Arrays.copyOf(A, A.length + B.length); /* depends on Java 6 */
 247   
 248    /* The following block is Java 5 hackery equivalent of preceding commented out line. */
 249  121 Class<T> eltClass = (Class<T>) A.getClass().getComponentType();
 250  121 T[] C = (T[]) Array.newInstance(eltClass, A.length + B.length);
 251  121 System.arraycopy(A, 0, C, 0, A.length);
 252   
 253  121 System.arraycopy(B, 0, C, A.length, B.length);
 254  121 return C;
 255    }
 256    }