Fix eclipse/checkstyle warnings
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / ReflectiveExceptionMapper.java
1 /*
2  * Copyright (c) 2014 Robert Varga. and others.  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
33                 | InvocationTargetException e) {
34             throw new IllegalStateException("Failed to instantiate exception " + ctor.getDeclaringClass(), e);
35         }
36     }
37
38     /**
39      * Create a new instance of the reflective exception mapper. This method performs basic
40      * sanity checking on the exception class. This method is potentially very costly, so
41      * users are strongly encouraged to cache the returned mapper for reuse.
42      *
43      * @param opName Operation performed
44      * @param exceptionType Exception type
45      * @return A new mapper instance
46      * @throws IllegalArgumentException when the supplied exception class does not pass sanity checks
47      * @throws SecurityException when the required constructor is not accessible
48      */
49     public static <X extends Exception> ReflectiveExceptionMapper<X> create(final String opName,
50             final Class<X> exceptionType) throws SecurityException {
51         final Constructor<X> c;
52         try {
53             c = exceptionType.getConstructor(String.class, Throwable.class);
54         } catch (NoSuchMethodException e) {
55             throw new IllegalArgumentException("Class does not define a String, Throwable constructor", e);
56         }
57
58         try {
59             c.newInstance(opName, new Throwable());
60         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
61                 | InvocationTargetException e) {
62             throw new IllegalArgumentException("Constructor " + c.getName() + " failed to pass instantiation test", e);
63         }
64
65         return new ReflectiveExceptionMapper<>(opName, c);
66     }
67 }