
org.beanio.config.BeanUtil Maven / Gradle / Ivy
/*
* Copyright 2010-2011 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.config;
import java.beans.*;
import java.lang.reflect.*;
import java.util.*;
import org.beanio.BeanIOConfigurationException;
import org.beanio.types.*;
import org.beanio.util.*;
/**
* Utility class for instantiating configurable bean classes.
*
* @author Kevin Seim
* @since 1.0
*/
class BeanUtil {
private final static TypeHandlerFactory typeHandlerFactory;
static {
typeHandlerFactory = new TypeHandlerFactory();
// 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 className the fully qualified name of the bean class to create
* @param config the bean properties to set on the instantiated object
* @return the created bean object
*/
public static Object createBean(String className, Properties props) {
Object bean = createBean(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(String className) {
if (className == null) {
throw new BeanIOConfigurationException("Class not set");
}
Class> clazz = null;
try {
// load the class
clazz = TypeUtil.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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy