001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util;
017    
018    import java.lang.reflect.InvocationTargetException;
019    import java.util.Map;
020    
021    import org.apache.commons.beanutils.BeanUtils;
022    import org.springframework.util.MethodInvoker;
023    
024    public class ReflectionUtils extends org.springframework.util.ReflectionUtils {
025    
026            @SuppressWarnings("unchecked")
027            public static Map<String, Object> describe(Object bean) {
028                    try {
029                            return BeanUtils.describe(bean);
030                    } catch (IllegalAccessException e) {
031                            throw new IllegalStateException(e);
032                    } catch (InvocationTargetException e) {
033                            throw new IllegalStateException(e);
034                    } catch (NoSuchMethodException e) {
035                            throw new IllegalStateException(e);
036                    }
037            }
038    
039            public static void copyProperty(Object bean, String name, Object value) {
040                    try {
041                            BeanUtils.copyProperty(bean, name, value);
042                    } catch (IllegalAccessException e) {
043                            throw new IllegalStateException(e);
044                    } catch (InvocationTargetException e) {
045                            throw new IllegalStateException(e);
046                    }
047            }
048    
049            public static Object invokeMethod(Class<?> targetClass, String targetMethod, Object... arguments) {
050                    MethodInvoker invoker = new MethodInvoker();
051                    invoker.setTargetClass(targetClass);
052                    invoker.setTargetMethod(targetMethod);
053                    invoker.setArguments(arguments);
054                    return invoke(invoker);
055            }
056    
057            public static Object invoke(MethodInvoker invoker) {
058                    try {
059                            invoker.prepare();
060                            return invoker.invoke();
061                    } catch (ClassNotFoundException e) {
062                            throw new IllegalStateException(e);
063                    } catch (NoSuchMethodException e) {
064                            throw new IllegalStateException(e);
065                    } catch (InvocationTargetException e) {
066                            throw new IllegalStateException(e);
067                    } catch (IllegalAccessException e) {
068                            throw new IllegalStateException(e);
069                    }
070            }
071    
072            public static Class<?> getClass(String className) {
073                    try {
074                            return Class.forName(className);
075                    } catch (ClassNotFoundException e) {
076                            throw new IllegalArgumentException(e);
077                    }
078            }
079    
080            public static <T> T newInstance(String className) {
081                    @SuppressWarnings("unchecked")
082                    Class<T> clazz = (Class<T>) getClass(className);
083                    return (T) newInstance(clazz);
084            }
085    
086            public static <T> T newInstance(Class<T> instanceClass) {
087                    try {
088                            return (T) instanceClass.newInstance();
089                    } catch (IllegalAccessException e) {
090                            throw new IllegalArgumentException("Unexpected error", e);
091                    } catch (InstantiationException e) {
092                            throw new IllegalArgumentException("Unexpected error", e);
093                    }
094            }
095    
096    }