Wade Chandler has been a software engineer and consultant for 14 years. He has worked with many languages and technologies such C, C++, Java, C#, Perl, PHP, HTML, and JavaScript along with various server side specific technologies and various frameworks. Along with contributing to various open source projects, he is a member of the NetBeans Dream Team and former NetBeans Governance Board member. Wade has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Java Module Systems, the EDT, and ClassLoader Issues

03.05.2012
| 5769 views |
  • submit to reddit

As mentioned in Java Module Systems SwingWorker, Runnable, Thread and ClassLoader Issues, where invokeLater and invokeAndWait are discussed, the EDT is a separate thread with its own context classloader, and this is most likely the system classloader. It is important to understand this.

If you create a user interface in a modular system, such as the NetBeans Platform, upon a user action logic will run on the EDT, and if in that logic you access classes by name, create new instances of them, or perform casts and those classes could possibly be in more than one module, you will run into classloader collision issues. I will restate here that this also affects EventQueue.invokeLater and invokeAndWait.

Along with collision issues, you need to also understand that certain class access may be blocked depending on the classloader being used once you set the thread context classloader. Imagine you have some classes your current UI components classloader can see; they are part of its dependencies. Those dependencies classloaders may not have access to each other. If you use a classloader which does not have access to some classes to set the thread context classloader, then access will be blocked to those classes, and your logic will not work.

Suppose there is a button and it has an action listener attached to it. It could be written as:

public class MyActionListener implements 
ActionListener {
  public void actionPerformed(ActionEvent evt){
      ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
      try {
          //here we are actually taking the current instances classloader
          //which could be slightly better than
          //MyActionListener.class.getClassLoader()
          Thread.currentThread()
          .setContextClassLoader(getClass().getClassLoader());
          //create instances, cast, load classes by name
      }finally{
          Thread.currentThread().setContextClassLoader(oldCl);
      }
  }
}

The above could make an assumption it is the only possible some.specificpackage.MyActionListener to be within any module in the system at one time where it to use MyActionListener.class.getClassLoader(), and if that were that not to be the case, you would need to get creative in the manner in which you get a classloader into this object.

The code does slightly guard against that since it uses getClass().getClassLoader() versus the static MyActionListener.class.getClassLoader() which would unequivocally infer it is expected to be the only one in the system per the way the class and classloader are being accessed. This means the instances classloader will be used, and if the instance was created using a specific classloader, and it is correct, then the code will simply work. If the classloader is not correct, then the logic which created the instance will need to be fixed.

This is another subtle way in which classloader issues can creep into the domain of concurrency and Java modular systems. You can use this information to ensure you have protected your modules logic from being broken by other modules at runtime which you probably will not have tested against.

Remember, if you use 3rd party libraries in a module, or you are writing one, it is highly probable and definitely possible others will too, and you may use the same ones. Whether the library is the same version or not, unless it is accessed in the context of a shared module classloader, i.e., the library is itself in a module versus a simple JAR dependency and all other modules use it this way, it can cause collision and access issues.

References
Published at DZone with permission of its author, Wade Chandler. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)