KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > validation > util > ObjectFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.validation.util;
21
22 import java.lang.reflect.Constructor JavaDoc;
23
24 /**
25  * This class is a generic Factory that employes Java reflection to
26  * create Objects.
27  * @author Rajeshwar Patil
28  * @version %I%, %G%
29  */

30
31 public class ObjectFactory {
32     /* A class implementation comment can go here. */
33
34     /** Create an instance of the class with the specified name by calling the
35       * no-argument constructor.
36      */

37     public static Object JavaDoc newInstance(String JavaDoc className){
38
39         Utils utils = new Utils();
40         return utils.createObject(className);
41     }
42
43
44     /** Create an instance of the class with the specified name by calling the
45       * a constructor that takes an String.
46      */

47     public static Object JavaDoc newInstance(String JavaDoc className, String JavaDoc argument){
48         Class JavaDoc classObject = null;
49         Utils utils = new Utils();
50
51         Class JavaDoc[] argumentTypes = new Class JavaDoc[] {String JavaDoc.class};
52         Constructor JavaDoc constructor =
53             utils.getConstructor(className, argumentTypes);
54
55         Object JavaDoc[] argumentValues = new Object JavaDoc[] {argument};
56
57         return utils.createObject(constructor, argumentValues);
58     }
59
60
61     /** Create an instance of the class with the specified name by calling the
62       * a constructor that takes an String.
63      */

64     public static Object JavaDoc newInstance(String JavaDoc className, Object JavaDoc argument){
65         Class JavaDoc classObject = null;
66         Utils utils = new Utils();
67
68         Class JavaDoc[] argumentTypes = new Class JavaDoc[] {Object JavaDoc.class};
69         Constructor JavaDoc constructor =
70             utils.getConstructor(className, argumentTypes);
71
72         Object JavaDoc[] argumentValues = new Object JavaDoc[] {argument};
73
74         return utils.createObject(constructor, argumentValues);
75     }
76 }
77
Popular Tags