Java tip: Getting most out of exceptions

28. December, 2010

Exceptions should have two purposes: 1. Clean up after an error and 2. help you solve the issue. Sadly, many Java developers often forget about #2.

So you end up with an exception thrown in SignatureFileVerifier (no source). Or even in a native method. The error message is:

Invalid signature file digest for Manifest main attributes

Right. Which tells us exactly … nothing. The stack trace isn’t better:

java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
	at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:221)
	at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:176)
	at java.util.jar.JarVerifier.processEntry(JarVerifier.java:233)
	at java.util.jar.JarVerifier.update(JarVerifier.java:188)
	at java.util.jar.JarFile.initializeVerifier(JarFile.java:325)
	at java.util.jar.JarFile.getInputStream(JarFile.java:390)
	at sun.misc.URLClassPath$JarLoader$1.getInputStream(URLClassPath.java:620)
	at sun.misc.Resource.cachedInputStream(Resource.java:59)
	at sun.misc.Resource.getByteBuffer(Resource.java:84)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:249)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
	at org.eclipse.jface.action.LegacyActionTools.initLocalizedModifiers(LegacyActionTools.java:699)
	at org.eclipse.jface.action.LegacyActionTools.findLocalizedModifier(LegacyActionTools.java:356)
	at org.eclipse.jface.action.LegacyActionTools.convertLocalizedAccelerator(LegacyActionTools.java:167)
	at org.eclipse.jface.action.Action.setText(Action.java:665)
	at de.pdark.epen.editor.actions.ForwardAction.(ForwardAction.java:29)
	at de.pdark.epen.editor.actions.ForwardActionTest.testCreate(ForwardActionTest.java:21)

So LegacyActionTools needs a class. Which one? Since I don’t have the source, how can I set a breakpoint?

Simple: Set the breakpoint in the constructor of the exception! Even native code has to pass through here, eventually.


Cutting a Large Sheet of Paper in Half

22. April, 2009

Here is a simple trick how to cut a large piece of paper or cardboard into two perfectly symmetrical halves:

Cutting large piece of cardboard in half

Cutting large piece of cardboard in half

  1. Fold the paper. Make sure that the open side matches perfectly. Put some weights on them to keep them in place.
  2. Now sharpen the fold. Use a book or the handles of your scissors. It doesn’t have to be razor sharp, though.
  3. Cut the fold away with a knife or scissors.

It’s simple to make a straight cut when you only have to keep 1-2mm distance to the edge of the fold. Also, even if you’re completely inept, the two pieces will have the exact same width at the point where you started. If you need to glue them together, they will match perfectly.


Verifying Results in Tests

5. March, 2009

So you finally got yourself writing a test against a database. In the test, you have to verify that a row in the table is correct, so you write:

assertEquals (
    "2008-09-16-13.50.18.000000;;;;1;2008-08-07;2008-08-07;JUNIT;2008-09-16;t0001;001;;Doe;Jane;;Street;2;Doe Jane;;;;;X;2575;John;;;;US;E;;01;;;;125;01425;0;Shop;;;DOE;JANE;JOHN;032;1;;0001010301;;;;",
    dumpRow(key));

and it sucks. Yeah, junit will notify you when something in that row is wrong and if you have a cool IDE, you can even compare the fields … but it still sucks. If one of the fields in the middle change, you have to scroll and eyeball-diff, wasting your time. The solution is pretty simple:

assertEquals (
    "2008-09-16-13.50.18.000000\n"
    + "\n"
    + "\n"
    + "\n"
    + "1\n"
    + "2008-08-07\n"
    + "2008-08-07\n"
    + "JUNIT\n"
    + "2008-09-16\n"
...
    dumpRow(key).replaceAll(";", "\n");

Instead of dumping the data in a single long string, split it into lines so you can compare fields side by side and without scrolling sideways.


Java Tricks: Commenting Out Code

25. February, 2009

How do you comment out code? Using /*...*/? Using your IDE to put // before each line? There is a third method:

    if (0 == 1) {
        ... code ...
    }

Pros:

  • Nests
  • You need only change a single character to “flip” the comment: Replace the 0 with 1.
  • You won’t get warnings because of suddenly unused imports or local variables.
  • The Java compiler will remove this block of code in the optimization step, so no runtime penality.

Cons:

  • Can only be used inside of methods.

If you need a fast way to repeatedly comment in/out a piece of code elsewhere, use this syntax:

    //* Fast flip comment starts here
        ... code ...
    /* and ends here */

Inside of a /*...*/ comment, the sequence “/*” has no special meaning. To comment out the block of code, remove the first “/” in “//*”:

    /* Fast flip comment starts here
        ... code ...
    /* and ends here */

Java Tricks: Fastest Way to Collecting Objects in a String

11. July, 2008

The fastest way to collect a list of objects in a String in Java:

StringBuilder buffer = new StringBuilder ();
String delim = "";
for (Object o: list)
{
    buffer.append (delim);
    delim = ", "; // Avoid if(); assignment is very fast!
    buffer.append (o);
}
buffer.toString ();