View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of Genjava-Core nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package com.generationjava.lang;
33  
34  import java.lang.reflect.InvocationTargetException;
35  import java.lang.reflect.Method;
36  
37  import com.generationjava.collections.CollectionsW;
38  
39  /***
40   * A set of static utilities for use with Classes.
41   *
42   * @author bayard@generationjava.com
43   * @date   2001-05-19
44   */
45  final public class ClassW {
46  
47      /***
48       * Create an object from the classname. Must have an empty constructor.
49       *
50       * @param classname String name of the class
51       *
52       * @return Object instance of the class or null
53       */
54      static public Object createObject(String classname) {
55          Class tmpClass = null;
56  
57          tmpClass = getClass(classname);
58  
59          if(tmpClass == null) {
60  //            System.err.println("No class for: "+classname);
61              return null;
62          }
63  
64  
65          return createObject(tmpClass);
66      }
67  
68      /***
69       * Create an object from a class. 
70       *
71       * @param clss Class object to instantiate
72       *
73       * @return Object instance of the class or null
74       */
75      static public Object createObject(Class clss) {
76          if(clss == null) {
77  //            System.err.println("Null class passed in. ");
78              return null;
79          }
80  
81          try {
82              return clss.newInstance();
83          } catch (IllegalAccessException  iae) {
84  //            System.err.println("Cant instantiate " + clss.getName() + " because " +
85  //                   iae.getMessage());
86          } catch (InstantiationException  ie) {
87  //            System.err.println("Cant instantiate " + clss.getName() + " because " +
88  //                   ie.getMessage());
89          }
90  
91          return null;
92      }
93  
94      /***
95       * Is this Class in the CLASSPATH
96       *
97       * @param classname String of the class
98       *
99       * @return boolean exists or not.
100      */
101     static public boolean classExists(String classname) {
102         Class tmpClass = null;
103 
104         /* try and load class */
105         try {
106 //            tmpClass = Class.forName(classname);
107             tmpClass = Thread.currentThread().getContextClassLoader().loadClass(classname);
108         } catch (ClassNotFoundException cnfe) {
109             return false;
110         } catch (IllegalArgumentException iae) {
111             return false;
112         }
113      
114         return true;   
115     }
116 
117     /***
118      * Get the Class object for a classname.
119      *
120      * @param classname String of the class
121      *
122      * @return Class instance for the class.
123      */
124     static public Class getClass(String classname) {
125         Class tmpClass = null;
126 
127         /* try an load class */
128         try {
129 //            tmpClass = Class.forName(classname);
130             tmpClass = Thread.currentThread().getContextClassLoader().loadClass(classname);
131         } catch (ClassNotFoundException cnfe) {
132 //            System.err.println("Can not resolve classname " + classname);
133         } catch (IllegalArgumentException iae) {
134 //            System.err.println("Can nott resolve " + tmpClass.getName() + " because " + iae.getMessage());
135         }
136      
137         return tmpClass;   
138     }
139 
140     static public void callMain(String[] args) {
141         callMain(args[0], CollectionsW.getSubArray(args, 1) );
142     }
143     static public void callMain(String classname, String[] args) {
144         callStatic(classname, "main", new Class[] { String[].class }, new Object[] { args } );
145     }
146     static public void callMain(Class clss, String[] args) {
147         callStatic(clss, "main", new Class[] { String[].class }, new Object[] { args } );
148     }
149 // figure out types from object args
150    // static public void callStatic(String classname, String method, Object[] args) {
151    // }
152     /***
153      * Runs a static method on a class.
154      *
155      * @param classname     String name of class to invoke on.
156      * @param methodName    String name of method to call.
157      * @param args          Object[] arguments to method.
158      */
159     static public Object callStatic(String classname, String methodName, 
160                                     Class[] types, Object[] args) 
161     {
162         return callStatic(getClass(classname), methodName, types, args);
163     }
164 
165     static public Object callStatic(Class clss, String methodName, 
166                                   Class[] types, Object[] args) 
167     {
168         try {
169             Method method = clss.getMethod(methodName, types );
170             return method.invoke( null, args );
171         } catch(NoSuchMethodException nsme) {
172         } catch(IllegalAccessException iae) {
173         } catch(InvocationTargetException ite) {
174         }
175         return null;
176     }
177 
178 }