001package org.apache.commons.beanutils2;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.apache.commons.beanutils2.Assertions.checkNotNull;
023
024/**
025 * Entry point into the API. Provides methods for:
026 * <p><ul>
027 *  <li>Accessing properties of a bean
028 *  <li>Accessing classes
029 *  <li>Loading classes by name
030 * </ul><p>
031 * All methods will throw {@code NullPointerException} if null is passed in.
032 */
033public final class BeanUtils
034{
035
036    // POJO/JavaBean stuff
037
038    /**
039     * Creates a {@link BeanAcessor} for the given bean, that allows access to the bean's properties and methods.
040     *
041     * @param bean the bean to be accessed. Must not be {@code null}!
042     * @param <B> the type of the bean.
043     * @return a BeanAccessor for the given bean.
044     */
045    public static <B> BeanAccessor<B> on( B bean )
046    {
047        bean = checkNotNull( bean, "No bean specified" );
048        return new DefaultBeanAccessor<B>( bean );
049    }
050
051    // introspection stuff
052
053    /**
054     * Creates a {@link ClassAccessor} for the given type that allows for creating instances of the type and calling
055     * static methods.
056     *
057     * @param beanType the type to be accessed. Must not be {@code null}!
058     * @param <B> the type modeled by the given class object.
059     * @return a ClassAccessor for the given type.
060     */
061    public static <B> ClassAccessor<B> on( Class<B> beanType )
062    {
063        beanType = checkNotNull( beanType, "No bean class specified" );
064        return new DefaultClassAccessor<B>( beanType );
065    }
066
067    /**
068     * Creates a {@link ClassLoaderBuilder} that allows to load the type given by {@code beanTypeName}.
069     *
070     * @param beanTypeName the name of the type to load. Must not be {@code null}!
071     * @return the ClassLoaderBuilder for the given type name.
072     */
073    public static ClassLoaderBuilder onClassName( String beanTypeName )
074    {
075        beanTypeName = checkNotNull( beanTypeName, "No bean class name specified" );
076        return new DefaultClassLoaderBuilder( beanTypeName );
077    }
078
079    /**
080     * Hidden constructor, this class cannot be instantiated directly
081     */
082    private BeanUtils()
083    {
084        // do nothing
085    }
086
087}