View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.exception;
14  
15  import net.sf.oval.ConstraintViolation;
16  import net.sf.oval.context.ConstructorParameterContext;
17  import net.sf.oval.context.FieldContext;
18  import net.sf.oval.context.MethodEntryContext;
19  import net.sf.oval.context.MethodParameterContext;
20  import net.sf.oval.context.MethodReturnValueContext;
21  import net.sf.oval.context.OValContext;
22  import net.sf.oval.internal.Log;
23  
24  /**
25   * Translates OVal specific exceptions to standard exceptions part of the JRE:
26   * <ul>
27   * <li><code>ConstraintsViolatedException</code> for constructor/method parameter translated to <code>IllegalArgumentException</code>
28   * <li><code>ConstraintsViolatedException</code> for class field translated to <code>IllegalStateException</code>
29   * <li><code>ConstraintsViolatedException</code> for method return values translated to <code>IllegalStateException</code>
30   * <li>Other exceptions based on <code>OValException</code> translated to <code>RuntimeException</code>
31   * </ul>
32   * @author Sebastian Thomschke
33   */
34  public class ExceptionTranslatorJDKExceptionsImpl implements ExceptionTranslator
35  {
36  	private static final Log LOG = Log.getLog(ExceptionTranslatorJDKExceptionsImpl.class);
37  
38  	/**
39  	 * {@inheritDoc}
40  	 */
41  	public RuntimeException translateException(final OValException ex)
42  	{
43  		// translate ConstraintsViolatedException based on the validation context
44  		if (ex instanceof ConstraintsViolatedException)
45  		{
46  			final ConstraintsViolatedException cex = (ConstraintsViolatedException) ex;
47  			final ConstraintViolation cv = cex.getConstraintViolations()[0];
48  			final OValContext ctx = cv.getContext();
49  
50  			// translate exceptions for preconditions to IllegalArgumentExceptions
51  			if (ctx instanceof MethodParameterContext || ctx instanceof ConstructorParameterContext
52  					|| ctx instanceof MethodEntryContext)
53  			{
54  				final IllegalArgumentException iaex = new IllegalArgumentException(cv.getMessage(), ex.getCause());
55  				iaex.setStackTrace(ex.getStackTrace());
56  				LOG.debug("Translated Exception {1} to {2}", ex, iaex);
57  				return iaex;
58  			}
59  
60  			// translate invariant exceptions to IllegalStateExceptions
61  			if (ctx instanceof FieldContext || ctx instanceof MethodReturnValueContext)
62  			{
63  				final IllegalStateException ise = new IllegalStateException(cv.getMessage(), ex.getCause());
64  				ise.setStackTrace(ex.getStackTrace());
65  				LOG.debug("Translated Exception {1} to {2}", ex, ise);
66  				return ise;
67  			}
68  		}
69  
70  		// translate all other messages to runtime exceptions
71  		{
72  			final RuntimeException rex = new RuntimeException(ex.getMessage(), ex.getCause());
73  			rex.setStackTrace(ex.getStackTrace());
74  			LOG.debug("Translated Exception {1} to {2}", ex, rex);
75  			return rex;
76  		}
77  	}
78  }