Warning: Declaration of PageLinesBanners::section_template($clone_id) should be compatible with PageLinesSection::section_template() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/banners/section.php on line 205

Warning: Declaration of PageLinesFeatures::section_template($clone_id) should be compatible with PageLinesSection::section_template() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/features/section.php on line 254

Warning: Declaration of PageLinesFeatures::section_head($clone_id) should be compatible with PageLinesSection::section_head() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/features/section.php on line 61

Warning: Declaration of PageLinesHighlight::section_template($clone_id) should be compatible with PageLinesSection::section_template() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/highlight/section.php on line 94

Warning: Declaration of PageLinesCarousel::section_template($clone_id) should be compatible with PageLinesSection::section_template() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/carousel/section.php on line 168

Warning: Declaration of PLheroUnit::section_template($clone_id) should be compatible with PageLinesSection::section_template() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/hero/section.php on line 140

Warning: Declaration of PLNavBar::section_template($clone_id, $location = '') should be compatible with PageLinesSection::section_template() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/navbar/section.php on line 199

Warning: Declaration of PLMasthead::section_template($clone_id) should be compatible with PageLinesSection::section_template() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/masthead/section.php on line 178

Warning: Declaration of PageLinesQuickSlider::section_template($clone_id) should be compatible with PageLinesSection::section_template() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/quickslider/section.php on line 59

Warning: Declaration of PageLinesQuickSlider::section_head($clone_id) should be compatible with PageLinesSection::section_head() in /home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/quickslider/section.php on line 29
c0nan.net | Loading classes on the fly

Lets try and put this in perspective. for some or other reason, you or your has this dire need to hot swop classes in your Java EE solution, maybe because of some trivial reason, or maybe because you are a freaking genius and have this magic trick up your sleeve, either way you have to get it done.

Lets start with a simple example:

First, we get the foundation in place.
N.B The jar file needs to be included in the classpath

MyPlugandPlayEngin.java

public interface MyPnPInterface{
   // This interface only makes life easier when defining the scope and functionality of the dynamic class
   String doStuff(Properties properties);
}

public class MyPlugandPlayEngin{

   public static main(String[] args){

      //If you want you can use a custom classloader here, else just use the default, supplied by the VM

      URLClassLoader classLoader = null;
      if (this.driverFilePaths != null){
         classLoader = new URLClassLoader();
      }

      MyPnPInterface pnp = null;

      try {
         if (classLoader != null){
            // Class.forName will return a Class object, so you can then just return instance and cast to the interface
            pnp = (MyPnPInterface ) Class.forName(args[0], true, classLoader).newInstance();
            DriverManager.registerDriver(driver);
            classLoader.loadClass(this.className);
         }else{
            // Default classloader
            Class clazz = Class.forName(args[0]);
            pnp = (MyPnPInterface ) clazz.newInstance();
         }

         Properties properties = new Properties();
         properties.setProperty("property1", args[1]);

         pnp.doStuff(properties);

      }catch Exception e(){
         e.printStacktrace;
      }
   }
}

MyPnPClass.java

public class MyPnPClass implements MyPnPInterface{
   String doStuff(Properties props);
}

Now when you are working with many times you will need to dynamically generate
an instance of the generic type, there is a more simpler way to get this instance.

public class myClass<A as MyPnPInterface>{

   private Class<a> getAClass(){ 
   //When you are working with generics, those generics as part of the class as an array of arguments Class
      clazz = ((Class<a>)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
      return clazz; 
   } 

   private A getA(){ 
      return getAClass.newInstance(); 
   } 
}

How AWESOME is that? Now you can just instantiate the class with the generic


public MyClass<MyPnPInterface> myClazz = new MyClass<MyPnPClass>();

Share →

Leave a Reply

Your email address will not be published. Required fields are marked *