001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.proxy2.provider;
019
020import java.io.Serializable;
021
022import org.apache.commons.lang3.Validate;
023import org.apache.commons.proxy2.ObjectProvider;
024import org.apache.commons.proxy2.exception.ObjectProviderException;
025
026/**
027 * Uses <code>Class.newInstance()</code> to instantiate an object.
028 * 
029 * @since 1.0
030 */
031public class BeanProvider<T> implements ObjectProvider<T>, Serializable
032{
033    /** Serialization version */
034    private static final long serialVersionUID = 1L;
035
036    //******************************************************************************************************************
037    // Fields
038    //******************************************************************************************************************
039
040    private final Class<? extends T> beanClass;
041
042    //******************************************************************************************************************
043    // Constructors
044    //******************************************************************************************************************
045
046    /**
047     * Constructs a provider which instantiates objects of the specified bean class.
048     * 
049     * @param beanClass
050     *            the bean class
051     */
052    public BeanProvider(Class<? extends T> beanClass)
053    {
054        Validate.notNull(beanClass, "Bean class cannot be null.");
055        this.beanClass = beanClass;
056    }
057
058    //******************************************************************************************************************
059    // ObjectProvider Implementation
060    //******************************************************************************************************************
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public T getObject()
067    {
068        try
069        {
070            return beanClass.newInstance();
071        }
072        catch (InstantiationException e)
073        {
074            throw new ObjectProviderException(e, "%s is not concrete.", beanClass);
075        }
076        catch (IllegalAccessException e)
077        {
078            throw new ObjectProviderException(e, "Constructor for %s is not accessible.", beanClass);
079        }
080    }
081}