001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.lang.reflect.Constructor;
018    
019    /**
020     * @author Brian Wing Shun Chan
021     */
022    public class InstanceFactory {
023    
024            public static Object newInstance(ClassLoader classLoader, String className)
025                    throws Exception {
026    
027                    return newInstance(
028                            classLoader, className, (Class<?>[])null, (Object[])null);
029            }
030    
031            public static Object newInstance(
032                            ClassLoader classLoader, String className, Class<?> parameterType,
033                            Object argument)
034                    throws Exception {
035    
036                    return newInstance(
037                            classLoader, className, new Class<?>[] {parameterType},
038                            new Object[] {argument});
039            }
040    
041            public static Object newInstance(
042                            ClassLoader classLoader, String className,
043                            Class<?>[] parameterTypes, Object[] arguments)
044                    throws Exception {
045    
046                    if (classLoader == null) {
047                            Thread currentThread = Thread.currentThread();
048    
049                            classLoader = currentThread.getContextClassLoader();
050                    }
051    
052                    Class<?> clazz = classLoader.loadClass(className);
053    
054                    if ((parameterTypes != null) && (arguments != null) &&
055                            (parameterTypes.length > 0) && (arguments.length > 0) &&
056                            (parameterTypes.length == arguments.length)) {
057    
058                            Constructor<?> constructor = clazz.getConstructor(parameterTypes);
059    
060                            return constructor.newInstance(arguments);
061                    }
062                    else {
063                            return clazz.newInstance();
064                    }
065            }
066    
067            public static Object newInstance(String className) throws Exception {
068                    return newInstance(null, className);
069            }
070    
071            public static Object newInstance(
072                            String className, Class<?> parameterType, Object argument)
073                    throws Exception {
074    
075                    return newInstance(
076                            null, className, new Class<?>[] {parameterType},
077                            new Object[] {argument});
078            }
079    
080            public static Object newInstance(
081                            String className, Class<?>[] parameterTypes, Object[] arguments)
082                    throws Exception {
083    
084                    return newInstance(null, className, parameterTypes, arguments);
085            }
086    
087    }