Coverage Report - org.apache.camel.spring.util.ReflectionUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectionUtils
33% 
75% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.spring.util;
 18  
 
 19  
 import java.lang.annotation.Annotation;
 20  
 import java.lang.reflect.Field;
 21  
 import java.lang.reflect.InvocationTargetException;
 22  
 import java.lang.reflect.Method;
 23  
 import java.lang.reflect.Modifier;
 24  
 
 25  0
 public class ReflectionUtils extends org.springframework.util.ReflectionUtils {
 26  
     public static <T extends Annotation> void callLifecycleMethod(final Object bean, final Class<T> annotation) {
 27  0
         ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
 28  0
             public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
 29  0
                 if (method.getAnnotation(annotation) != null) {
 30  
                     try {
 31  0
                         method.invoke(bean, (Object[])null);
 32  0
                     } catch (IllegalArgumentException ex) {
 33  0
                         throw new IllegalStateException("Failure to invoke " + method + " on " + bean.getClass() + ": args=[]", ex);
 34  0
                     } catch (IllegalAccessException ex) {
 35  0
                         throw new UnsupportedOperationException(ex.toString());
 36  0
                     } catch (InvocationTargetException ex) {
 37  0
                         throw new UnsupportedOperationException("PostConstruct method on bean threw exception", ex.getTargetException());
 38  0
                     }
 39  
                 }
 40  0
             }
 41  
         });
 42  0
     }
 43  
 
 44  
     public static void setField(Field f, Object instance, Object value) {
 45  
         try {
 46  8
             boolean oldAccessible = f.isAccessible();
 47  8
             boolean shouldSetAccessible = !Modifier.isPublic(f.getModifiers()) && !oldAccessible;
 48  8
             if (shouldSetAccessible) {
 49  8
                 f.setAccessible(true);
 50  
             }
 51  8
             f.set(instance, value);
 52  8
             if (shouldSetAccessible) {
 53  8
                 f.setAccessible(oldAccessible);
 54  
             }
 55  0
         } catch (IllegalArgumentException ex) {
 56  0
             throw new UnsupportedOperationException("Cannot inject value of class '" + value.getClass() + "' into " + f);
 57  0
         } catch (IllegalAccessException ex) {
 58  0
             ReflectionUtils.handleReflectionException(ex);
 59  8
         }
 60  8
     }
 61  
 }