com.feilong.commons.core.lang.reflect.ConstructorUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.commons.core.lang.reflect.ConstructorUtil.java

Source

/*
 * Copyright (C) 2008 feilong (venusdrogon@163.com)
 *
 * 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 com.feilong.commons.core.lang.reflect;

import java.lang.reflect.Constructor;

import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.commons.core.lang.ClassUtil;
import com.feilong.commons.core.log.Slf4jUtil;
import com.feilong.commons.core.util.Validator;

/**
 * ????????,focused on constructors.
 * 
 * <h3>?:</h3>
 * 
 * <blockquote>
 * <p>
 * <ol>
 * <li>{@link #newInstance(Class, Object...)} </li>
 * <li>{@link #newInstance(String, Object...)} </li>
 * <li>{@link #newInstance(Class, Object[], Class[])} </li>
 * </ol>
 * 
 * ?,? {@link ConstructorUtils#invokeConstructor(Class, Object[], Class[])} , 
 * {@link ConstructorUtils#getMatchingAccessibleConstructor(Class, Class...)} ??,? {@link ReflectException} 
 * 
 * <p>
 * ,? int-->Integer ??,?/? ????
 * </p>
 * </p>
 * </blockquote>
 * 
 * <h3>??</h3>
 * 
 * <blockquote>
 * <p>
 * ? ,? ,???,
 * 
 * <ol>
 * <li>{@link ConstructorUtils#invokeExactConstructor(Class, Object...)} </li>
 * <li>{@link ConstructorUtils#invokeExactConstructor(Class, Object[], Class[])} </li>
 * </ol>
 * 
 * ?,?
 * <ol>
 * <li>{@link ConstructorUtils#getAccessibleConstructor(Constructor)}</li>
 * <li>{@link ConstructorUtils#getAccessibleConstructor(Class, Class...)}</li>
 * <li>{@link ConstructorUtils#getMatchingAccessibleConstructor(Class, Class...)}</li>
 * </ol>
 * 
 * 
 * </p>
 * </blockquote>
 * 
 * @author <a href="mailto:venusdrogon@163.com">feilong</a>
 * @version 1.0.7 2014715 ?1:08:15
 * @see org.apache.commons.lang3.reflect.ConstructorUtils
 * @since 1.0.7
 */
public final class ConstructorUtil {

    /** The Constant log. */
    private static final Logger log = LoggerFactory.getLogger(ConstructorUtil.class);

    /** Don't let anyone instantiate this class. */
    private ConstructorUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    // [start] newInstance

    /**
     * ,Returns a new instance of the specified class choosing the right constructor from the list of parameter types.<br>
     * :
     * 
     * <pre>
     * {@code
     * User user = ConstructorUtil.newInstance("com.feilong.test.User") user 
     * 
     * ? 
     * User user1 = ConstructorUtil.newInstance("com.feilong.test.User", 100L);  id 100
     * }
     * </pre>
     *
     * @param <T>
     *            t
     * @param className
     *            ??, com.feilong.test.User
     * @param parameterValues
     *            ?
     * @return ,??T 
     * @throws ReflectException
     *             the reflect exception
     * @throws NullPointerException
     *             if isNullOrEmpty(className)
     * @see ClassUtil#loadClass(String)
     * @see #newInstance(Class, Object...)
     */
    @SuppressWarnings("unchecked")
    public static <T> T newInstance(String className, Object... parameterValues)
            throws ReflectException, NullPointerException {
        if (Validator.isNullOrEmpty(className)) {
            throw new NullPointerException("className can't be null/empty!");
        }

        // ?
        Class<?> klass = null;
        try {
            klass = ClassUtil.loadClass(className);
        } catch (ClassNotFoundException e) {
            log.error(e.getClass().getName(), e);
            throw new ReflectException(e);
        }
        return (T) newInstance(klass, parameterValues);
    }

    /**
     * ,Returns a new instance of the specified class choosing the right constructor from the list of parameter types.
     *
     * @param <T>
     *            the generic type
     * @param klass
     *            
     * @param parameterValues
     *            ?, 100L
     * @return the t
     * @throws ReflectException
     *             the reflect exception
     * @see com.feilong.commons.core.lang.ClassUtil#toClass(Object...)
     * @see java.lang.Class#getConstructor(Class...)
     * @see java.lang.reflect.Constructor#newInstance(Object...)
     * @see org.apache.commons.lang3.reflect.ConstructorUtils#invokeConstructor(Class, Object...)
     */
    public static <T> T newInstance(Class<T> klass, Object... parameterValues) throws ReflectException {
        Class<?>[] parameterTypes = ClassUtil.toClass(parameterValues);
        return newInstance(klass, parameterValues, parameterTypes);
    }

    /**
     * Returns a new instance of the specified class choosing the right constructor from the list of parameter types.
     *
     * @param <T>
     *            the generic type
     * @param klass
     *            the cls
     * @param args
     *            the args
     * @param parameterTypes
     *            the parameter types
     * @return the t
     * @throws ReflectException
     *             the reflect exception
     * @see org.apache.commons.lang3.reflect.ConstructorUtils#invokeConstructor(Class, Object[], Class[])
     */
    public static <T> T newInstance(Class<T> klass, Object[] args, Class<?>[] parameterTypes)
            throws ReflectException {
        try {
            return org.apache.commons.lang3.reflect.ConstructorUtils.invokeConstructor(klass, args, parameterTypes);
        } catch (Exception e) {
            String message = Slf4jUtil.formatMessage(
                    "invokeConstructor Exception,input params info: class:[{}].args:[{}],parameterTypes:[{}]",
                    klass, args, parameterTypes);
            throw new ReflectException(message, e);
        }
    }
}