/** * Copyright (c) 2007, Aleksander Adamowski * All rights reserved. * * BSD License * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @author Aleksander Adamowski aleksander.adamowski@gmail.com */ import java.io.File; import java.io.IOException; import java.net.JarURLConnection; import java.net.URL; import java.util.Enumeration; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; /** * This class can be used to preload all the classes in the same package. * * * @author Aleksander Adamowski aleksander.adamowski@gmail.com * */ public class ClassPreloader { /** * Preload all the classes in the same package as the ClassPreloader class. */ public static void preloadCurrentPackageClasses() { String packageName = ClassPreloader.class.getPackage().getName(); URL url = ClassPreloader.class.getClassLoader() .getResource(packageName); if (url == null) { System.err.println("Cannot read the URL of package " + packageName); return; } if (url.getProtocol().equals("jar")) { try { JarURLConnection jarConnection = (JarURLConnection) (url .openConnection()); ZipFile jarZipFile = jarConnection.getJarFile(); Enumeration entriesEnum = jarZipFile .entries(); while (entriesEnum.hasMoreElements()) { String zipEntry = entriesEnum.nextElement().toString(); Matcher m = Pattern.compile("^.*/([A-Za-z0-9]+)\\.class") .matcher(zipEntry); if (m.matches()) { String className = packageName + "." + m.group(1); try { Object obj = Class.forName(className) .getClassLoader().loadClass(className); System.out.println("preloading " + obj); obj = null; } catch (ClassNotFoundException cnfe) { System.err.println(cnfe.getMessage()); cnfe.printStackTrace(System.err); } } /* * else { System.out.println("doesn't match: " + m); } */ } } catch (IOException ioex) { System.err.println(ioex.getMessage()); ioex.printStackTrace(System.err); } } else { File directory = new File(url.getFile()); if (directory.exists()) { if (directory.isDirectory()) { String[] files = directory.list(); for (String fname : files) { if (fname.endsWith(".class")) { String className = packageName + "." + fname.substring(0, fname.length() - 6); try { Object obj = Class.forName(className) .getClassLoader().loadClass(className); System.out.println("preloading " + obj); obj = null; } catch (Exception ex) { System.err.println(ex.getMessage()); ex.printStackTrace(System.err); } } } } } else { System.err.println("Doesn't exist: " + directory); return; } } } }