3eb8c13369a18e8b96a88476c27c84f86f8c8f63
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / ReflectiveExceptionMapper.java
1 /*
2  * Copyright (c) 2014 Robert Varga.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util.concurrent;
9
10 import java.lang.reflect.Constructor;
11 import java.lang.reflect.InvocationTargetException;
12
13 /**
14  * Convenience {@link ExceptionMapper} which instantiates specified Exception using
15  * reflection. The Exception types are expected to declare an accessible constructor
16  * which takes two arguments: a String and a Throwable.
17  *
18  * @param <X> Exception type
19  */
20 public final class ReflectiveExceptionMapper<X extends Exception> extends ExceptionMapper<X> {
21     private final Constructor<X> ctor;
22
23     private ReflectiveExceptionMapper(final String opName, final Constructor<X> ctor) {
24         super(opName, ctor.getDeclaringClass());
25         this.ctor = ctor;
26     }
27
28     @Override
29     protected X newWithCause(final String message, final Throwable cause) {
30         try {
31             return ctor.newInstance(message, cause);
32         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
33             throw new IllegalStateException("Failed to instantiate exception " + ctor.getDeclaringClass(), e);
34         }
35     }
36
37     /**
38      * Create a new instance of the reflective exception mapper. This method performs basic
39      * sanity checking on the exception class. This method is potentially very costly, so
40      * users are strongly encouraged to cache the returned mapper for reuse.
41      *
42      * @param opName Operation performed
43      * @param exceptionType Exception type
44      * @return A new mapper instance
45      * @throws IllegalArgumentException when the supplied exception class does not pass sanity checks
46      * @throws SecurityException when the required constructor is not accessible
47      */
48     public static <X extends Exception> ReflectiveExceptionMapper<X> create(final String opName, final Class<X> exceptionType) throws SecurityException {
49         final Constructor<X> c;
50         try {
51             c = exceptionType.getConstructor(String.class, Throwable.class);
52         } catch (NoSuchMethodException e) {
53             throw new IllegalArgumentException("Class does not define a String, Throwable constructor", e);
54         }
55
56         try {
57             c.newInstance(opName, new Throwable());
58         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
59             throw new IllegalArgumentException("Constructor " + c.getName() + " failed to pass instantiation test", e);
60         }
61
62         return new ReflectiveExceptionMapper<>(opName, c);
63     }
64 }