Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.horstmann.violet.framework;
- import java.io.*;
- import java.net.*;
- /**
- * Dynamically load a class, given its location in the filesystem.
- * @author jdalbey
- * @version 0.1
- */
- public final class ClassFetcher
- {
- private static final String kPkgSeparator = ".";
- /** Dynamically load a compiled Java class, given the
- * absolute path to the .class file. For example, if the package animals
- * contains the class Wombat, the compiled class file might be located at
- * /home/joe/java/animals/Wombat.class. Given this path, this method will
- * return the Class animals.Wombat.
- * This implementation tries all variations of path name
- * and package name until it finds the one that works.
- * It assumes the .class file is in the correct location in the filesystem
- * hierarchy.
- * @param absolutePath filesystem path to the desired .class file. The path
- * may be anywhere on the filesystem, it is not limited to the
- * current classpath. Precondition: path must begin with File.separator
- * and end with ".class"
- * @return the Class type for the specified .class file,
- * or null if the Class couldn't be loaded.
- */
- public static Class<?> loadClass(String absoluteFilePath)
- {
- // remove file extension, leaving /home/joe/birds/Robin
- int end = absoluteFilePath.lastIndexOf(kPkgSeparator);
- String directory = absoluteFilePath.substring(0,end);
- // at the last separator, divide the directory name into two parts
- end = directory.lastIndexOf(File.separator);
- String lastPart = directory.substring(end+1);
- // Make the last part the class name
- String canonicalClassName = lastPart;
- // Make the front part the directory path
- directory = directory.substring(0,end);
- // Declare an array of URL's, even though we are only using one,
- // because an array is required for URLClasssLoader constructor
- URL[] urls = null;
- // The classloader we will use to try to find the target class
- ClassLoader cl = null;
- // If successful, the resulting class
- Class<?> dynamicClass = null;
- /* Iteratively work our way up the directory path, trying each segment
- * in turn as if it's the base, and the remainder is the package.
- */
- while (directory.length() > 1 && dynamicClass == null)
- {
- try
- {
- // Convert directory to a URL (array will have a single element)
- urls = new URL[]{new File(directory).toURI().toURL()};
- // Create a new class loader with the directory url
- cl = new URLClassLoader(urls);
- // Attempt to load the class
- dynamicClass = cl.loadClass(canonicalClassName);
- // If we get here, we succeeded
- }
- catch (MalformedURLException ex)
- {
- ex.printStackTrace();
- }
- // Thrown when the file doesn't exist
- catch (ClassNotFoundException ex)
- {
- ex.printStackTrace();
- }
- // Thrown if the file exists but the package the loader was using doesn't match.
- catch (NoClassDefFoundError ex)
- {
- // At the last separator, divide the directory name into two parts
- end = directory.lastIndexOf(File.separator);
- lastPart = directory.substring(end+1);
- // Add the last part to the class name
- canonicalClassName = lastPart + kPkgSeparator + canonicalClassName;
- // Make the front part the directory path
- directory = directory.substring(0,end);
- }
- } // end loop
- return dynamicClass;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement