Simple Introduction to Reflection

Reflection is a mechanism in java that allows to to get information about a class without needing to know the type of the class. The program below takes a java class name as a command line argument and shows you all of the methods and field names that are in the class.

You call the sample program like this:
java ReflectionTest java.util.Calendar

ReflectionTest takes the string you pass in on the command line and attempts to find a class by that name. If it finds one it gets an array of all the fields and displays their name and their values. It then gets an array of all the methods and displays their name and their return type.
Continue reading “Simple Introduction to Reflection”

Dealing with Slow XSLT Transformations

I have some XSLT stylesheets that I use to integrate information from Amazon with my reading list on my website. This weekend I decided to create another list using Amazon’s “People Who Bought this Book also Bought” feature. After a couple generations of downloading books that were related to books I read, I ended up with an XML file with about 1,500 books in it. I tried running the transform using Xerces and it took 13 hours.

Obviously this wasn’t going to work, so I refactored parts of the XSLT and was able to cut that time in half. 6 hours still seemed like it was taking far too long. I considered doing away with the stylesheets and doing the transforms manually using Java to write directly to a file. After doing some searches I discovered that apache.org has a good solutions for this problem that is part of the Xalan-J project. Basically it allows you to take an XSLT stylesheet and compile it into java byte code called a “translet”. You can then run the transform using Xalan and the compiled java translet. This makes things run much quicker. It appears that this is because the transform doesn’t need to deal with the XSLT file and because of other optimizations.
Continue reading “Dealing with Slow XSLT Transformations”

Running out of Memory with Java

The other day when doing some particularly large xslt conversions I kept running out of memory in the JVM. After doing a bit of research I found two command line arguments that help solve the problem. -mx allows you to set the maximum size of the memory allocation pool while -ms allows you to set the starting size.

For example:
java -ms50M -mx1000M ProgramThatNeedsLotsOfMemory

will execute ProgramThatNeedsLotsOfMemory with a starting memory allocation of 50M and let it grow up to 1GB. You can also use K to specify the memory size in kilobytes, but I’m not sure why that would be useful unless you are on a machine with very little memory.

After looking into the documentation a little more it appears that the new syntax for these commands is -Xms and -Xmx, so you’ll have to choose the one that works depending on your version of the JVM.

New Sun Java Programmer Test for 1.5

The new certification test for Java 1.5 (or Java 5 or Tiger) is going to be coming out as a beta soon. According to Kathy Sierra’s weblog the new test is going to avoid the complicated “puzzle” type problems and concentrate more on your ability to accomplish specific objectives with Java code. It sounds like they are using a new type of test question that lets you drag and drop code (kind of like magnetic poetry) to make the code work or behave in a specific way.

The test is also going to cover some of the new features in the Java language like generics and autoboxing and spend more time on collections.

Sun.com lists some details about the exam. The beta version has 138 questions and gives you up to 4 hours to complete it. You can register for the beta exam at Prometric’s website for $49.

If you want to take the test a a discounted price, this might be a very good way to do it.