org.beanio.internal.util.BeanUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of beanio Show documentation
Show all versions of beanio Show documentation
A Java un/marshalling library for CSV, XML, delimited and fixed length stream formats.
/*
* Copyright 2010-2012 Kevin Seim
*
* Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
*
* 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.beanio.internal.util;
import java.beans.*;
import java.lang.reflect.Method;
import java.util.*;
import org.beanio.BeanIOConfigurationException;
import org.beanio.types.*;
/**
* Utility class for instantiating configurable bean classes.
*
* @author Kevin Seim
* @since 1.0
*/
public class BeanUtil {
private static final boolean NULL_ESCAPING_ENABLED = "true".equalsIgnoreCase(
Settings.getInstance().getProperty(Settings.NULL_ESCAPING_ENABLED));
private final static TypeHandlerFactory typeHandlerFactory;
static {
typeHandlerFactory = new TypeHandlerFactory(BeanUtil.class.getClassLoader());
// string arrays allowed for setting 'comments' on a csv/delimited/fixedlength reader
typeHandlerFactory.registerHandlerFor(String[].class, new StringArrayTypeHandler());
// override string and character type handlers is property escaping is enabled
if ("true".equals(Settings.getInstance().getProperty(Settings.PROPERTY_ESCAPING_ENABLED))) {
typeHandlerFactory.registerHandlerFor(String.class, new EscapedStringTypeHandler());
typeHandlerFactory.registerHandlerFor(Character.class, new EscapedCharacterTypeHandler());
}
}
private BeanUtil() { }
/**
* Instantiates a bean class.
* @param classLoader the {@link ClassLoader} to use to resolve className
* @param className the fully qualified name of the bean class to create
* @param props the bean properties to set on the instantiated object
* @return the created bean object
*/
public static Object createBean(ClassLoader classLoader, String className, Properties props) {
Object bean = createBean(classLoader, className);
configure(bean, props);
return bean;
}
/**
* Instantiates a bean class using its class name.
* @param className the fully qualified name of the class to instantiate
* @return the created bean object
*/
public static Object createBean(ClassLoader classLoader, String className) {
if (className == null) {
throw new BeanIOConfigurationException("Class not set");
}
// use our own class loader for BeanIO classes
if (className.startsWith("org.beanio.")) {
classLoader = BeanUtil.class.getClassLoader();
}
Class> clazz = null;
try {
// load the class
clazz = classLoader.loadClass(className);
}
catch (ClassNotFoundException e) {
throw new BeanIOConfigurationException(
"Class not found '" + className + "'", e);
}
try {
// instantiate an instance of the class
return clazz.newInstance();
}
catch (Exception e) {
throw new BeanIOConfigurationException(
"Cound not instantiate class '" + clazz + "'", e);
}
}
/**
* Sets properties on a bean object using default type handlers.
* @param bean the object to set the properties on
* @param props the bean properties to set on the object
*/
public static void configure(Object bean, Properties props) {
// if no properties, we're done...
if (props == null || props.isEmpty()) {
return;
}
Class> clazz = bean.getClass();
BeanInfo info;
try {
info = Introspector.getBeanInfo(clazz);
}
catch (IntrospectionException e) {
throw new BeanIOConfigurationException(e);
}
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
for (Map.Entry