1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.lang.reflect.Constructor;
18  
19  /**
20   * <a href="InstanceFactory.java.html"><b><i>View Source</i></b></a>
21   *
22   * @author Brian Wing Shun Chan
23   */
24  public class InstanceFactory {
25  
26      public static Object newInstance(ClassLoader classLoader, String className)
27          throws Exception {
28  
29          return newInstance(
30              classLoader, className, (Class<?>[])null, (Object[])null);
31      }
32  
33      public static Object newInstance(
34              ClassLoader classLoader, String className, Class<?> parameterType,
35              Object argument)
36          throws Exception {
37  
38          return newInstance(
39              classLoader, className, new Class<?>[] {parameterType},
40              new Object[] {argument});
41      }
42  
43      public static Object newInstance(
44              ClassLoader classLoader, String className,
45              Class<?>[] parameterTypes, Object[] arguments)
46          throws Exception {
47  
48          if (classLoader == null) {
49              Thread currentThread = Thread.currentThread();
50  
51              classLoader = currentThread.getContextClassLoader();
52          }
53  
54          Class<?> classObject = classLoader.loadClass(className);
55  
56          if ((parameterTypes != null) && (arguments != null) &&
57              (parameterTypes.length > 0) && (arguments.length > 0) &&
58              (parameterTypes.length == arguments.length)) {
59  
60              Constructor<?> constructor = classObject.getConstructor(
61                  parameterTypes);
62  
63              return constructor.newInstance(arguments);
64          }
65          else {
66              return classObject.newInstance();
67          }
68      }
69  
70      public static Object newInstance(String className) throws Exception {
71          return newInstance(null, className);
72      }
73  
74      public static Object newInstance(
75              String className, Class<?> parameterType, Object argument)
76          throws Exception {
77  
78          return newInstance(
79              null, className, new Class<?>[] {parameterType},
80              new Object[] {argument});
81      }
82  
83      public static Object newInstance(
84              String className, Class<?>[] parameterTypes, Object[] arguments)
85          throws Exception {
86  
87          return newInstance(null, className, parameterTypes, arguments);
88      }
89  
90  }