Eclipse & Custom Class Loaders 

by Greg Bednarek





Listing One



try {

//Instantiate ClassLoader, passing the location (directory) of the class to

//load and the parent class loader

ClassLoader loader = new MacroClassLoader(classLocation,

                                          this.getClass().getClassLoader());



//Load the class with the custom ClassLoader;

// we can now instantiate new using the newInstance() method

Class klass = loader.loadClass(classToLoad);



//Instantiate a new instance of the loaded class - this is equivalent to the

//"new" keyword

Runnable loadedClass = (Runnable)klass.newInstance();



//Check to make sure that our custom ClassLoader succeeded

// in loading the class, and not the System class loader

if(this.getClass().getClassLoader() == loadedClass.getClass().getClassLoader())

{

  //If we get here, then the class instance loadedClass was

  //loaded by the System class loader, not by our custom class loader!!!

  printErrorMsg(classToLoad,

                new String("Class was not loaded by the proper class loader"));

  return;

}

} catch (ClassCastException e){

  printErrorMsg(classToLoad,

                new String("Class must implement java.lang.Runnable"));

  return;

} catch (ClassNotFoundException e){

  printErrorMsg(classToLoad,

                new String("Class could not be located at " + classLocation));

  return;

} catch (IllegalAccessException e){

  printErrorMsg(classToLoad,

                new String("ClassLoader threw an IllegalAccessException"))

  return;

} catch (InstantiationException e){

  printErrorMsg(classToLoad,

                new String("ClassLoader threw an InstantiationException"));

  return;

} catch (MalformedURLException e) {

  printErrorMsg(classToLoad,

                new String("ClassLoader threw a MalformedURLException"));

  return;

}





Listing Two



public class MacroClassLoader extends java.net.URLClassLoader {



  public MacroClassLoader(String classLocation, ClassLoader parent)

    throws MalformedURLException {

//Create a new URL[] array with one entry, a URL expressing the

//location of the String classLocation which is an input parameter to this

//method. We will assume that the class location is on the local filesystem,

//and will prepend the file:// (Unix) or file:/// (Windows) URL identifier as

//well as replacing any Windows-style forward slashes with URL-style

//backslashes

    super(new URL[] {

          new URL("file:" + ((classLocation.charAt(0) == '/') ? "//" : "///" )

                   + classLocation.replace('\\', '/') + "/")}, parent);

  }

}





Listing Three



public class TestRunnableProjRename implements java.lang.Runnable {

// (non-Javadoc) * @see java.lang.Runnable#run()



  public void run() {

    IWorkspace workspace = null;

    IWorkspaceRoot workspaceRoot = null;

    IProject [] projects = null;



    System.out.println("In TestRunnableProjRename...");



    workspace = ResourcesPlugin.getWorkspace();

    workspaceRoot = workspace.getRoot();

    projects = workspaceRoot.getProjects();



    //Iterate over all of the projects in the current workspace

    for(int i=0; i < projects.length; i++){



      //Closed projects should be ignored...

      if(!projects[i].isOpen())

        continue;



      //Rename all projects in workspace to $(PROJNAME)_Modified

      try {

        IProjectDescription description = projects[i].getDescription();

        System.out.println("\tAttempting to change project "

                           + description.getName());

        description.setLocation(description.getLocation());

        description.setName(description.getName().concat("_Modified"));



           //Actually perform the rename

        IResource resource = (IResource)projects[i];

        resource.move(description, true, false, null);

      } catch (Exception e) {

        e.printStackTrace();

      }

    }

  }

}







