Coverage Report - edu.rice.cs.drjava.model.repl.newjvm.InterpretResult
 
Classes in this File Line Coverage Branch Coverage Complexity
InterpretResult
0%
0/10
N/A
1.083
InterpretResult$BooleanValueResult
0%
0/2
N/A
1.083
InterpretResult$BusyResult
0%
0/3
N/A
1.083
InterpretResult$CharValueResult
0%
0/2
N/A
1.083
InterpretResult$ExceptionResult
0%
0/12
0%
0/4
1.083
InterpretResult$NoValueResult
0%
0/3
N/A
1.083
InterpretResult$NumberValueResult
0%
0/2
N/A
1.083
InterpretResult$ObjectValueResult
0%
0/5
N/A
1.083
InterpretResult$StringValueResult
0%
0/2
N/A
1.083
InterpretResult$UnexpectedExceptionResult
0%
0/2
N/A
1.083
InterpretResult$Visitor
N/A
N/A
1.083
 
 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.repl.newjvm;
 38  
 
 39  
 import java.io.StringWriter;
 40  
 import java.io.PrintWriter;
 41  
 import java.io.Serializable;
 42  
 import edu.rice.cs.dynamicjava.interpreter.InterpreterException;
 43  
 import edu.rice.cs.dynamicjava.interpreter.EvaluatorException;
 44  
 
 45  
 /**
 46  
  * Super class for any type of result that can occur from a call to interpret.
 47  
  * 
 48  
  * @version $Id: InterpretResult.java 5181 2010-02-24 16:53:27Z mgricken $
 49  
  */
 50  0
 public abstract class InterpretResult implements Serializable {  
 51  
   public abstract <T> T apply(Visitor<T> v);
 52  
 
 53  
   public static interface Visitor<T> {
 54  
     public T forNoValue();
 55  
     public T forStringValue(String val);
 56  
     public T forCharValue(Character val);
 57  
     public T forNumberValue(Number val);
 58  
     public T forBooleanValue(Boolean val);
 59  
     public T forObjectValue(String valString, String objTypeStr);
 60  
     public T forException(String message);
 61  
     public T forEvalException(String message, StackTraceElement[] stackTrace);
 62  
     public T forUnexpectedException(Throwable t);
 63  
     public T forBusy();
 64  
   }
 65  
   
 66  0
   public static InterpretResult busy() { return BusyResult.INSTANCE; }
 67  
   
 68  
   // This and later classes are declared explicitly rather than anonymously as a
 69  
   // serialization best practice.
 70  0
   private static class BusyResult extends InterpretResult {
 71  0
     public static final BusyResult INSTANCE = new BusyResult();
 72  0
     public <T> T apply(Visitor<T> v) { return v.forBusy(); }
 73  
   }
 74  
   
 75  
   
 76  0
   public static InterpretResult exception(InterpreterException e) { return new ExceptionResult(e); }
 77  
   
 78  
   private static class ExceptionResult extends InterpretResult {
 79  
     private final String _msg;
 80  
     private final StackTraceElement[] _stackTrace;
 81  0
     public ExceptionResult(InterpreterException e) {
 82  0
       if (e instanceof EvaluatorException) {
 83  
         // for EvaluatorException, we want to keep the stack trace
 84  0
         _msg = e.getMessage();
 85  0
         _stackTrace = e.getCause().getStackTrace();
 86  
       }
 87  
       else {
 88  
         // for other InterpreterExceptions, we need to convert to a string here
 89  0
         StringWriter msg = new StringWriter();
 90  0
         e.printUserMessage(new PrintWriter(msg));
 91  0
         _msg = msg.toString().trim();
 92  0
         _stackTrace = null;
 93  
       }
 94  0
     }
 95  
     public <T> T apply(Visitor<T> v) {
 96  0
       if (_stackTrace != null) 
 97  0
         return v.forEvalException(_msg, _stackTrace);
 98  
       else
 99  0
         return v.forException(_msg);
 100  
     }
 101  
   }
 102  
   
 103  
 
 104  
   public static InterpretResult unexpectedException(Throwable t) {
 105  0
     return new UnexpectedExceptionResult(t);
 106  
   }
 107  
   
 108  
   private static class UnexpectedExceptionResult extends InterpretResult {
 109  
     private final Throwable _t;
 110  0
     public UnexpectedExceptionResult(Throwable t) { _t = t; }
 111  0
     public <T> T apply(Visitor<T> v) { return v.forUnexpectedException(_t); }
 112  
   }
 113  
   
 114  
   
 115  0
   public static InterpretResult noValue() { return NoValueResult.INSTANCE; }
 116  
   
 117  0
   private static class NoValueResult extends InterpretResult {
 118  0
     public static final NoValueResult INSTANCE = new NoValueResult();
 119  0
     public <T> T apply(Visitor<T> v) { return v.forNoValue(); }
 120  
   }
 121  
   
 122  
   
 123  0
   public static InterpretResult stringValue(String s) { return new StringValueResult(s); }
 124  
   
 125  
   private static class StringValueResult extends InterpretResult {
 126  
     private final String _val;
 127  0
     public StringValueResult(String val) { _val = val; }
 128  0
     public <T> T apply(Visitor<T> v) { return v.forStringValue(_val); }
 129  
   }
 130  
   
 131  
   
 132  0
   public static InterpretResult charValue(Character c) { return new CharValueResult(c); }
 133  
   
 134  
   private static class CharValueResult extends InterpretResult {
 135  
     private final Character _val;
 136  0
     public CharValueResult(Character val) { _val = val; }
 137  0
     public <T> T apply(Visitor<T> v) { return v.forCharValue(_val); }
 138  
   }
 139  
   
 140  
 
 141  0
   public static InterpretResult numberValue(Number n) { return new NumberValueResult(n); }
 142  
   
 143  
   private static class NumberValueResult extends InterpretResult {
 144  
     private final Number _val;
 145  0
     public NumberValueResult(Number val) { _val = val; }
 146  0
     public <T> T apply(Visitor<T> v) { return v.forNumberValue(_val); }
 147  
   }
 148  
   
 149  
 
 150  0
   public static InterpretResult booleanValue(Boolean b) { return new BooleanValueResult(b); }
 151  
   
 152  
   private static class BooleanValueResult extends InterpretResult {
 153  
     private final Boolean _val;
 154  0
     public BooleanValueResult(Boolean val) { _val = val; }
 155  0
     public <T> T apply(Visitor<T> v) { return v.forBooleanValue(_val); }
 156  
   }
 157  
   
 158  
 
 159  0
   public static InterpretResult objectValue(String objS, String objTS) { return new ObjectValueResult(objS, objTS); }
 160  
 
 161  
   private static class ObjectValueResult extends InterpretResult {
 162  
     private final String _objString;
 163  
     private final String _objTypeStr;
 164  0
     public ObjectValueResult(String objString, String objTypeStr) {
 165  0
         _objString = objString;
 166  0
         _objTypeStr = objTypeStr;
 167  0
     }
 168  0
     public <T> T apply(Visitor<T> v) { return v.forObjectValue(_objString, _objTypeStr); }
 169  
   } 
 170  
   
 171  
 }