ExceptionWrapperHandler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.jcs3.jcache.proxy;

  20. import java.lang.reflect.Constructor;
  21. import java.lang.reflect.InvocationHandler;
  22. import java.lang.reflect.InvocationTargetException;
  23. import java.lang.reflect.Method;
  24. import java.lang.reflect.Proxy;

  25. public class ExceptionWrapperHandler<T> implements InvocationHandler
  26. {
  27.     private final T delegate;
  28.     private final Constructor<? extends RuntimeException> wrapper;

  29.     public ExceptionWrapperHandler(final T delegate, final Class<? extends RuntimeException> exceptionType)
  30.     {
  31.         this.delegate = delegate;
  32.         try
  33.         {
  34.             this.wrapper = exceptionType.getConstructor(Throwable.class);
  35.         }
  36.         catch (final NoSuchMethodException e)
  37.         {
  38.             throw new IllegalStateException(e);
  39.         }
  40.     }

  41.     @Override
  42.     public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable
  43.     {
  44.         try
  45.         {
  46.             return method.invoke(delegate, args);
  47.         }
  48.         catch (final InvocationTargetException ite)
  49.         {
  50.             final Throwable e = ite.getCause();
  51.             if (RuntimeException.class.isInstance(e))
  52.             {
  53.                 final RuntimeException re;
  54.                 try
  55.                 {
  56.                     re = wrapper.newInstance(e);
  57.                 }
  58.                 catch (final Exception e1)
  59.                 {
  60.                     throw new IllegalArgumentException(e1);
  61.                 }
  62.                 throw re;
  63.             }
  64.             throw e;
  65.         }
  66.     }

  67.     public static <T> T newProxy(final ClassLoader loader, final T delegate, final Class<? extends RuntimeException> exceptionType,
  68.             final Class<T> apis)
  69.     {
  70.         return (T) Proxy.newProxyInstance(loader, new Class<?>[] { apis }, new ExceptionWrapperHandler<>(delegate, exceptionType));
  71.     }
  72. }