KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > ClassUtils


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.util;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 /**
24  * A collection of class management utility methods.
25  *
26  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
27  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
28  * @version CVS $Id: ClassUtils.java 56874 2004-11-07 23:09:11Z antonio $
29  */

30 public class ClassUtils {
31
32     /**
33      * Create a new instance given a class name
34      *
35      * @param className A class name
36      * @return A new instance
37      * @exception Exception If an instantiation error occurs
38      */

39     public static Object JavaDoc newInstance(String JavaDoc className) throws Exception JavaDoc {
40         return ClassUtils.loadClass(className).newInstance();
41     }
42
43     /**
44      * Load a class given its name.
45      * BL: We wan't to use a known ClassLoader--hopefully the hierarchy
46      * is set correctly.
47      *
48      * @param className A class name
49      * @return The class pointed to by <code>className</code>
50      * @exception ClassNotFoundException If a loading error occurs
51      */

52     public static Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
53         return ClassUtils.getClassLoader().loadClass(className);
54     }
55
56     /**
57      * Return a resource URL.
58      * BL: if this is command line operation, the classloading issues
59      * are more sane. During servlet execution, we explicitly set
60      * the ClassLoader.
61      *
62      * @return The context classloader.
63      * @exception MalformedURLException If a loading error occurs
64      */

65     public static URL JavaDoc getResource(String JavaDoc resource) throws MalformedURLException JavaDoc {
66         return ClassUtils.getClassLoader().getResource(resource);
67     }
68
69     /**
70      * Return the context classloader.
71      * BL: if this is command line operation, the classloading issues
72      * are more sane. During servlet execution, we explicitly set
73      * the ClassLoader.
74      *
75      * @return The context classloader.
76      */

77     public static ClassLoader JavaDoc getClassLoader() {
78         return Thread.currentThread().getContextClassLoader();
79     }
80
81     /**
82      * Tests if a class implements a given interface
83      *
84      * @return true if class implements given interface.
85      * @deprecated Will be removed 2 versions after 2.1.5
86      */

87     public static boolean implementsInterface(String JavaDoc className, String JavaDoc iface) throws Exception JavaDoc {
88         Class JavaDoc class1 = ClassUtils.loadClass (className);
89         Class JavaDoc class2 = ClassUtils.loadClass (iface);
90         return ClassUtils.implementsInterface(class1, class2);
91     }
92
93     /**
94      * Tests if a class implements a given interface
95      *
96      * @return true if class implements given interface.
97      * @deprecated Will be removed 2 versions after 2.1.5
98      */

99     public static boolean implementsInterface(Class JavaDoc class1, Class JavaDoc iface) {
100         return iface.isAssignableFrom (class1);
101     }
102
103     /**
104      * Determine the last modification date for this
105      * class file or its enclosing library
106      *
107      * @param aClass A class whose last modification date is queried
108      * @return The time the given class was last modified
109      * @exception IOException IOError
110      * @exception IllegalArgumentException The class was not loaded from a file
111      * or directory
112      * @deprecated Will be removed 2 versions after 2.1.5
113      */

114     public static long lastModified(Class JavaDoc aClass)
115         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
116         URL JavaDoc url = aClass.getProtectionDomain().getCodeSource().getLocation();
117
118         if (!url.getProtocol().equals("file")) {
119             throw new IllegalArgumentException JavaDoc("Class was not loaded from a file url");
120         }
121
122         File JavaDoc directory = new File JavaDoc(url.getFile());
123         if (!directory.isDirectory()) {
124             throw new IllegalArgumentException JavaDoc("Class was not loaded from a directory");
125         }
126
127         String JavaDoc className = aClass.getName();
128         String JavaDoc basename = className.substring(className.lastIndexOf(".") + 1);
129
130         File JavaDoc file = new File JavaDoc(directory, basename + ".class");
131
132         return file.lastModified();
133     }
134
135     /**
136      * Gets the absolute pathname of the class file
137      * containing the specified class name, as prescribed
138      * by the current classpath.
139      *
140      * @param aClass Name of the class.
141      * @deprecated Will be removed 2 versions after 2.1.5
142      */

143      public static String JavaDoc which(Class JavaDoc aClass) {
144         String JavaDoc path = null;
145         try {
146             path = aClass.getProtectionDomain().getCodeSource().getLocation().toString();
147         } catch (Throwable JavaDoc t){
148         }
149         return path;
150     }
151
152 }
153
Popular Tags