Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.commons.configuration2.beanutils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.FluentPropertyBeanIntrospector;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.beanutils.WrapDynaBean;
import org.apache.commons.beanutils.WrapDynaClass;
import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
import org.apache.commons.lang3.ClassUtils;
/**
*
* A helper class for creating bean instances that are defined in configuration files.
*
*
* This class provides utility methods related to bean creation operations. These methods simplify such operations
* because a client need not deal with all involved interfaces. Usually, if a bean declaration has already been
* obtained, a single method call is necessary to create a new bean instance.
*
*
* This class also supports the registration of custom bean factories. Implementations of the {@link BeanFactory}
* interface can be registered under a symbolic name using the {@code registerBeanFactory()} method. In the
* configuration file the name of the bean factory can be specified in the bean declaration. Then this factory will be
* used to create the bean.
*
*
* In order to create beans using {@code BeanHelper}, create and instance of this class and initialize it accordingly -
* a default {@link BeanFactory} can be passed to the constructor, and additional bean factories can be registered (see
* above). Then this instance can be used to create beans from {@link BeanDeclaration} objects. {@code BeanHelper} is
* thread-safe. So an instance can be passed around in an application and shared between multiple components.
*
*
* @since 1.3
*/
public final class BeanHelper {
/**
* An implementation of the {@code BeanCreationContext} interface used by {@code BeanHelper} to communicate with a
* {@code BeanFactory}. This class contains all information required for the creation of a bean. The methods for
* creating and initializing bean instances are implemented by calling back to the provided {@code BeanHelper} instance
* (which is the instance that created this object).
*/
private static final class BeanCreationContextImpl implements BeanCreationContext {
/** The association BeanHelper instance. */
private final BeanHelper beanHelper;
/** The class of the bean to be created. */
private final Class> beanClass;
/** The underlying bean declaration. */
private final BeanDeclaration data;
/** The parameter for the bean factory. */
private final Object param;
private BeanCreationContextImpl(final BeanHelper helper, final Class> beanClass, final BeanDeclaration data, final Object param) {
beanHelper = helper;
this.beanClass = beanClass;
this.param = param;
this.data = data;
}
@Override
public Object createBean(final BeanDeclaration data) {
return beanHelper.createBean(data);
}
@Override
public Class> getBeanClass() {
return beanClass;
}
@Override
public BeanDeclaration getBeanDeclaration() {
return data;
}
@Override
public Object getParameter() {
return param;
}
@Override
public void initBean(final Object bean, final BeanDeclaration data) {
beanHelper.initBean(bean, data);
}
}
/**
* A default instance of {@code BeanHelper} which can be shared between arbitrary components. If no special
* configuration is needed, this instance can be used throughout an application. Otherwise, new instances can be created
* with their own configuration.
*/
public static final BeanHelper INSTANCE = new BeanHelper();
/**
* A special instance of {@code BeanUtilsBean} which is used for all property set and copy operations. This instance was
* initialized with {@code BeanIntrospector} objects which support fluent interfaces. This is required for handling
* builder parameter objects correctly.
*/
private static final BeanUtilsBean BEAN_UTILS_BEAN = initBeanUtilsBean();
/**
* Copies matching properties from the source bean to the destination bean using a specially configured
* {@code PropertyUtilsBean} instance. This method ensures that enhanced introspection is enabled when doing the copy
* operation.
*
* @param dest the destination bean
* @param orig the source bean
* @throws NoSuchMethodException exception thrown by {@code PropertyUtilsBean}
* @throws InvocationTargetException exception thrown by {@code PropertyUtilsBean}
* @throws IllegalAccessException exception thrown by {@code PropertyUtilsBean}
* @since 2.0
*/
public static void copyProperties(final Object dest, final Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
BEAN_UTILS_BEAN.getPropertyUtils().copyProperties(dest, orig);
}
/**
* Creates a concrete collection instance to populate a property of type collection. This method tries to guess an
* appropriate collection type. Mostly the type of the property will be one of the collection interfaces rather than a
* concrete class; so we have to create a concrete equivalent.
*
* @param propName the name of the collection property
* @param propertyClass the type of the property
* @return the newly created collection
*/
private static Collection