org.kuali.common.util.ReflectionUtils Maven / Gradle / Ivy
/**
* Copyright 2010-2013 The Kuali Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ecl2.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kuali.common.util;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.util.MethodInvoker;
public class ReflectionUtils extends org.springframework.util.ReflectionUtils {
@SuppressWarnings("unchecked")
public static Map describe(Object bean) {
try {
return BeanUtils.describe(bean);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
} catch (InvocationTargetException e) {
throw new IllegalStateException(e);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
}
}
public static void copyProperty(Object bean, String name, Object value) {
try {
BeanUtils.copyProperty(bean, name, value);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
} catch (InvocationTargetException e) {
throw new IllegalStateException(e);
}
}
public static Object invokeMethod(Class> targetClass, String targetMethod, Object... arguments) {
MethodInvoker invoker = new MethodInvoker();
invoker.setTargetClass(targetClass);
invoker.setTargetMethod(targetMethod);
invoker.setArguments(arguments);
return invoke(invoker);
}
public static Object invoke(MethodInvoker invoker) {
try {
invoker.prepare();
return invoker.invoke();
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
} catch (InvocationTargetException e) {
throw new IllegalStateException(e);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
public static Class> getClass(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
public static T newInstance(String className) {
@SuppressWarnings("unchecked")
Class clazz = (Class) getClass(className);
return (T) newInstance(clazz);
}
public static T newInstance(Class instanceClass) {
try {
return (T) instanceClass.newInstance();
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Unexpected error", e);
} catch (InstantiationException e) {
throw new IllegalArgumentException("Unexpected error", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy