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