Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 57: Applet parameterization via class reflection

Making applets configurable from HTML docs can be a chore -- unless you know how to do it the easy way

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 3 of 3

Therefore we need a list of all public fields that are declared in the applet. The class reflection mechanism provides this kind of information. This mechanism is located in the java.lang.reflect package, that's why we import it.

We obtain a reference to the applet's metaclass by calling applet.getClass(). Then we obtain the array of metafields by calling metaclass.getFields(). Then, for each metafield in the array, we check that it does have a corresponding PARAM tag in the HTML document. If it does, we check whether it represents a final field by calling:

   Modifier.isFinal(fields[i].getModifiers()) 


Method getModifiers() returns a mask of "modifiers" (like static, final, volatile, and so on) that are part of the field's declaration.

Once we are sure that the metafield represents a public non-final field, we convert the PARAM string to the appropriate field type.

Setting private fields

If you wish to automatically initialize your private fields, you must write a similar initializeApplet method inside the applet itself, because the Field.setXXX methods called in class Util can't have access to the applet's private fields. You must also replace getFields (returns public fields only) by getDeclaredFields (returns all fields).

Conclusion

This tip has shown you how the class reflection mechanism can be used to ease the programmer's task of developing configurable applets. Using a method that does all the parameter fetching for you, it may be awhile before you have to type getParameter again.

About the author

Yvon received a degree in mathematics and computer science from McGill University in Montreal. He has seven years of programming experience, ranging from business server applications to GUI programming. Two years ago he became a Java addict (as he puts it!) He received his Sun Certified Java Programmer title last January, and more recently, he became a Sun Certified Java Developer. He is working as a consultant for MTLI-NSK Technologies in Paris. His current project is a human resource management system written entirely in Java and based on an ObjectStore OODBMS. Life is interesting in Europe, but as a Canadian he misses hockey very much!
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • For information on class reflection, see Chuck McManis's JavaWorld article, "Take an in-depth look at the Java Reflection API" http://www.javaworld.com/javaworld/jw-09-1997/jw-09-indepth.html