2 * Copyright (c) 2014 Robert Varga. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util.concurrent;
10 import java.lang.reflect.Constructor;
11 import java.lang.reflect.InvocationTargetException;
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.
18 * @param <X> Exception type
20 public final class ReflectiveExceptionMapper<X extends Exception> extends ExceptionMapper<X> {
21 private final Constructor<X> ctor;
23 private ReflectiveExceptionMapper(final String opName, final Constructor<X> ctor) {
24 super(opName, ctor.getDeclaringClass());
29 protected X newWithCause(final String message, final Throwable cause) {
31 return ctor.newInstance(message, cause);
32 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
33 throw new IllegalStateException("Failed to instantiate exception " + ctor.getDeclaringClass(), e);
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.
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
48 public static <X extends Exception> ReflectiveExceptionMapper<X> create(final String opName, final Class<X> exceptionType) throws SecurityException {
49 final Constructor<X> c;
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);
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);
62 return new ReflectiveExceptionMapper<>(opName, c);