Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, ...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made avai...
Upcoming SlideShare
Loading in …5
×

Why ClassforName Sucks - BJ Hargrave

264 views

Published on

OSGi DevCon 2008

Class.forName and ClassLoader.loadClass seem to be synonyms for the same basic operation: request a dynamic class load. Yet calling Class.forName does additional "checking" which is not very useful (certainly in OSGi). Class.forName does certain loader constraint checks to ensure type safety which are not necessary when doing dynamic class loads. This short talk will explore this issue and discuss a proposed "hack" to work around the issue for legacy code which can't easily be changed to avoid Class.forName.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Why ClassforName Sucks - BJ Hargrave

  1. 1. Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license Why Class.forName sucks! BJ Hargrave Senior Technical Staff Member IBM Lotus http://blog.bjhargrave.com
  2. 2. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Static Class Loading •class loading constraints [1][2] Foo f = FooFactory.createFoo() •Foo.class Class.forName("Foo") class constant pool entry • [1] http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html#78621 • [2] http://www.bracha.org/classloaders.ps
  3. 3. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Dynamic Class Loading •Class.forName •ClassLoader.loadClass
  4. 4. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Initiating class loader loadClass Defining class loader defineClass
  5. 5. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Class.forName bleeds static constraints onto dynamic loading Same internal VM method used to handle static class loads and dynamic class loads via Class.forName
  6. 6. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Class.forName Issues •Initiating class loader also caches defined class along with loader constraints •If class is cached by initiating class loader initiatingClassLoader.loadClass is never called
  7. 7. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Class.forName(String) not so bad since it will likely use bundle's class loader as initiating class loader Class.forName(String, true, callersClassLoader) Class.forName(String, boolean, ClassLoader) When ClassLoader is the thread context Class Loader is the issue!
  8. 8. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Equinox's ContextFinder •Thread Context Class Loader •Never defines classes •Delegates to bundle's class loaders based upon the call context •Wrong results returned when multiple versions of classes present
  9. 9. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Recommendation •Use ClassLoader.loadClass in your code •Or Bundle.loadClass if you know the bundle containing the class
  10. 10. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Hack?
  11. 11. Why Class.forName sucks! | Copyright © IBM Corp., 2008. All rights reserved. Source code in this presentation is made available under the EPL, v1.0, remainder of the presentation is licensed under Creative Commons Att. Nc Nd 2.5 license. Legal Notices • IBM and the IBM logo are trademarks or registered trademarks of IBM Corporation, in the United States, other countries or both. • Java and all Java-based marks, among others, are trademarks or registered trademarks of Sun Microsystems in the United States, other countries or both. • Eclipse and the Eclipse logo are trademarks of Eclipse Foundation, Inc. • Other company, product and service names may be trademarks or service marks of others. • THE INFORMATION DISCUSSED IN THIS PRESENTATION IS PROVIDED FOR INFORMATIONAL PURPOSES ONLY. WHILE EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND ACCURACY OF THE INFORMATION, IT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, AND IBM SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF THE USE OF, OR OTHERWISE RELATED TO, SUCH INFORMATION. ANY INFORMATION CONCERNING IBM'S PRODUCT PLANS OR STRATEGY IS SUBJECT TO CHANGE BY IBM WITHOUT NOTICE.

×