Using Pugwash modules with Java
Java provides methods in the Class class which allow us to
load a class from the classpath given it's name and create a new
instance of the class object. Example code to do this is given below.
try {
Class provider = Class.forName(name);
// check this class is a SpamServiceProvider
Method m = provider.getMethod("getDescription",null);
// this creates an instance of an object whose methods can be called
SpamServiceProvider ssp = (SpamServiceProvider)provider.newInstance();
// i.e
System.out.println("loaded "+ssp.getName());
} catch (NoSuchMethodException ex) {
// not a SpamServiceProvider as it didn't have a method called getDescription
return;
}
|