giacomo 00/10/08 14:10:51
Modified: src/org/apache/cocoon/util Tag: xml-cocoon2 ClassUtils.java
Log:
extended with new method to check for implemented interface of a class
Revision Changes Path
No revision
No revision
1.1.2.5 +72 -68
xml-cocoon/src/org/apache/cocoon/util/Attic/ClassUtils.java
Index: ClassUtils.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/util/Attic/ClassUtils.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- ClassUtils.java 2000/09/06 23:22:26 1.1.2.4
+++ ClassUtils.java 2000/10/08 21:10:51 1.1.2.5
@@ -18,82 +18,86 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Ricardo Rocha</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version CVS $Revision: 1.1.2.4 $ $Date: 2000/09/06 23:22:26 $
+ * @version CVS $Revision: 1.1.2.5 $ $Date: 2000/10/08 21:10:51 $
*/
public class ClassUtils {
- /**
- * Create a new instance given a class name
- *
- * @param className A class name
- * @return A new instance
- * @exception Exception If an instantiation error occurs
- */
- public static Object newInstance(String className) throws Exception {
- return loadClass(className).newInstance();
- }
-
- /**
- * Load a class given its name
- *
- * @param className A class name
- * @return The class pointed to by <code>className</code>
- * @exception Exception If a loading error occurs
- */
- public static Class loadClass(String className) throws Exception {
- // return getClassLoader().loadClass(className);
- return Class.forName(className);
- }
-
- /**
- * Return the context classloader
- *
- * @return The context classloader.
- * @exception Exception If a loading error occurs
- */
- public static ClassLoader getClassLoader() {
- // return Thread.currentThread().getContextClassLoader();
- return ClassUtils.class.getClassLoader();
- }
-
- /**
- * Determine the last modification date for this
- * class file or its enclosing library
- *
- * @param aClass A class whose last modification date is queried
- * @return The time the given class was last modified
- * @exception IOException IOError
- * @exception IllegalArgumentException The class was not loaded from a file
- * or directory
- */
- public static long lastModified(Class aClass)
- throws IOException, IllegalArgumentException
- {
- URL url =
- aClass.
- getProtectionDomain().
- getCodeSource().
- getLocation();
+ /**
+ * Create a new instance given a class name
+ *
+ * @param className A class name
+ * @return A new instance
+ * @exception Exception If an instantiation error occurs
+ */
+ public static Object newInstance(String className) throws Exception {
+ return loadClass(className).newInstance();
+ }
+
+ /**
+ * Load a class given its name
+ *
+ * @param className A class name
+ * @return The class pointed to by <code>className</code>
+ * @exception Exception If a loading error occurs
+ */
+ public static Class loadClass(String className) throws Exception {
+ // return getClassLoader().loadClass(className);
+ return Class.forName(className);
+ }
- if (!url.getProtocol().equals("file")) {
- throw new IllegalArgumentException("Class was not loaded from a file
url");
+ /**
+ * Return the context classloader
+ *
+ * @return The context classloader.
+ * @exception Exception If a loading error occurs
+ */
+ public static ClassLoader getClassLoader() {
+ // return Thread.currentThread().getContextClassLoader();
+ return ClassUtils.class.getClassLoader();
}
- File directory = new File(url.getFile());
- if (!directory.isDirectory()) {
- throw new IllegalArgumentException("Class was not loaded from a
directory");
+ /**
+ * Tests if a class implements a given interface
+ *
+ * @return true if class implements given interface.
+ */
+ public static boolean implementsInterface(String className, String
iface) throws Exception {
+ Class class1 = loadClass (className);
+ Class class2 = loadClass (iface);
+ if (class2.isAssignableFrom (class1)) {
+ return true;
+ }
+ return false;
}
- String className = aClass.getName();
- String basename = className.substring(className.lastIndexOf(".") + 1);
+ /**
+ * Determine the last modification date for this
+ * class file or its enclosing library
+ *
+ * @param aClass A class whose last modification date is queried
+ * @return The time the given class was last modified
+ * @exception IOException IOError
+ * @exception IllegalArgumentException The class was not loaded from a
file
+ * or directory
+ */
+ public static long lastModified(Class aClass)
+ throws IOException, IllegalArgumentException {
+ URL url = aClass.getProtectionDomain().getCodeSource().getLocation();
+
+ if (!url.getProtocol().equals("file")) {
+ throw new IllegalArgumentException("Class was not loaded from a
file url");
+ }
+
+ File directory = new File(url.getFile());
+ if (!directory.isDirectory()) {
+ throw new IllegalArgumentException("Class was not loaded from a
directory");
+ }
- File file = new File(
- directory.getCanonicalPath() +
- File.separator +
- basename +
- ".class"
- );
+ String className = aClass.getName();
+ String basename = className.substring(className.lastIndexOf(".") +
1);
- return file.lastModified();
- }
+ File file = new File(directory.getCanonicalPath() + File.separator +
basename + ".class");
+
+ return file.lastModified();
+ }
}