001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }