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
33 | InvocationTargetException e) {
34 throw new IllegalStateException("Failed to instantiate exception " + ctor.getDeclaringClass(), e);
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.
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
49 public static <X extends Exception> ReflectiveExceptionMapper<X> create(final String opName,
50 final Class<X> exceptionType) throws SecurityException {
51 final Constructor<X> c;
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);
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);
65 return new ReflectiveExceptionMapper<>(opName, c);