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  
                     }
 33  0
                     catch (IllegalArgumentException ex) {
 34  0
                         throw new IllegalStateException("Failure to invoke " + method + " on "
 35  
                                 + bean.getClass() + ": args=[]", ex);
 36  
                     }
 37  0
                     catch (IllegalAccessException ex) {
 38  0
                         throw new UnsupportedOperationException(ex.toString());
 39  
                     }
 40  0
                     catch (InvocationTargetException ex) {
 41  0
                         throw new UnsupportedOperationException("PostConstruct method on bean threw exception",
 42  
                                 ex.getTargetException());
 43  0
                     }
 44  
                 }
 45  0
             }
 46  
         });
 47  0
     }
 48  
 
 49  
     public static void setField(Field f, Object instance, Object value) {
 50  
         try {
 51  8
             boolean oldAccessible = f.isAccessible();
 52  8
             boolean shouldSetAccessible = !Modifier.isPublic(f.getModifiers()) && !oldAccessible;
 53  8
             if (shouldSetAccessible) {
 54  8
                 f.setAccessible(true);
 55  
             }
 56  8
             f.set(instance, value);
 57  8
             if (shouldSetAccessible) {
 58  8
                 f.setAccessible(oldAccessible);
 59  
             }
 60  
         }
 61  0
         catch (IllegalArgumentException ex) {
 62  0
             throw new UnsupportedOperationException("Cannot inject value of class '"
 63  
                     + value.getClass() + "' into " + f);
 64  
         }
 65  0
         catch (IllegalAccessException ex) {
 66  0
             ReflectionUtils.handleReflectionException(ex);
 67  8
         }
 68  8
     }
 69  
 }