1 16 package org.apache.cocoon.util; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 23 30 public class ClassUtils { 31 32 39 public static Object newInstance(String className) throws Exception { 40 return ClassUtils.loadClass(className).newInstance(); 41 } 42 43 52 public static Class loadClass(String className) throws ClassNotFoundException { 53 return ClassUtils.getClassLoader().loadClass(className); 54 } 55 56 65 public static URL getResource(String resource) throws MalformedURLException { 66 return ClassUtils.getClassLoader().getResource(resource); 67 } 68 69 77 public static ClassLoader getClassLoader() { 78 return Thread.currentThread().getContextClassLoader(); 79 } 80 81 87 public static boolean implementsInterface(String className, String iface) throws Exception { 88 Class class1 = ClassUtils.loadClass (className); 89 Class class2 = ClassUtils.loadClass (iface); 90 return ClassUtils.implementsInterface(class1, class2); 91 } 92 93 99 public static boolean implementsInterface(Class class1, Class iface) { 100 return iface.isAssignableFrom (class1); 101 } 102 103 114 public static long lastModified(Class aClass) 115 throws IOException , IllegalArgumentException { 116 URL url = aClass.getProtectionDomain().getCodeSource().getLocation(); 117 118 if (!url.getProtocol().equals("file")) { 119 throw new IllegalArgumentException ("Class was not loaded from a file url"); 120 } 121 122 File directory = new File (url.getFile()); 123 if (!directory.isDirectory()) { 124 throw new IllegalArgumentException ("Class was not loaded from a directory"); 125 } 126 127 String className = aClass.getName(); 128 String basename = className.substring(className.lastIndexOf(".") + 1); 129 130 File file = new File (directory, basename + ".class"); 131 132 return file.lastModified(); 133 } 134 135 143 public static String which(Class aClass) { 144 String path = null; 145 try { 146 path = aClass.getProtectionDomain().getCodeSource().getLocation().toString(); 147 } catch (Throwable t){ 148 } 149 return path; 150 } 151 152 } 153 | Popular Tags |