Generic Interfaces Arabian Magic
[flash bang!] Say words like generics, enums, or interfaces, and you've immediately transported to the realm of conjured whirlwind sandstorms and curved-sword-weilding assasins. Well, I guess we're going there, because I think this is pretty cool. Plus, undocumented, or at least un-reiterated to myself, and later I'll look at it and think it's just pretty complicated. Prepare for angle brackets in the amount of parens in lisp.
What's REALLY going on in that HashSet.contains() method
We all know that contains() is a handy way to check for the presence of an item in a HashSet. The java docs state: "Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e))." So, you just need to override equals() the way you want, and everything's hunky-dory, right? Wrong!
The power of Groovy closures
In the Java world, we don’t get closures until Java 7. Groovy already has them. Here's what I’ve learned about these important programming structures, and why they're powerful.
Caching Ajax Requests in IE
Have you ever had a problem with ajax requests caching in Internet Explorer. I have, and recently I came across a nicer solution than I have traditionally used. In the past, I have used a unique parameter on the url to keep IE from caching previous requests to the same url, like so:
Django on Jython Intro
Here is a "new and shiney" presentation for running Django on Jython. It's based on my experiences with the DjangoJython tutorial.
As is discussed in the slide deck, and hence the subtitle of the presentation, much could be helped by more careful reading of documentation. Well, moments ago, I decided that I hadn't checked out django-jython 1.1. Well.... turns out many more problems could have been helped by using this later version. Sweet!
Install Java 5 on Karmic Koala
In Ubuntu 9.10, they removed Java 5 from the update repositories for Karmic because Java 5 was end of lifed in Oct. 09. This has made it more cumbersome for the Java 5 user on Karmic. Luckily, cumbersome is still pretty easy.
Parameterized States in Webflow
My first experience with Spring Webflow is on a project that uses Webflow 1.0.5. So far, I'm impressed. Apparently, Webflow 2 offers even more incredible awesomeness, but thusfar I would put my webflow experience over that of my previous couple years in Seam. (I was just waiting for a chance to abandon that library anyway.)
Change Maven JDK
Sometimes you want Maven to compile your code with a different JDK than is the one assigned to your JAVA_HOME. For instance, I have code in a project that I'm working on that only compiles on Java 1.5. My JAVA_HOME, however, is 1.6. So, how do we specify the JDK for Maven?
InstallCert for Java Security Certificate
Sometimes you need to install a security certificate for authentication to work for certain services -- services that are accessed by your java application that requirement a secure connection. For instance, needing to authenticate against an LDAP server from one of our apps, we had to run a little InstallCert.java on all JDKs used to run the app.
Environment-based Dependency Injection
In developing an email notification system recently, we became interested in code acting differently depending on what environment we were in. A potentially good solution for this is environment-based dependency injection. This means that different Spring beans, for instance, will be used depending on the environment, ie dev, test, etc. This is useful for something like emailing, because we may not want real emails hitting the mail server in dev or test environments, but we do in prod. With some Spring constructs, it's pretty easy.
Variable Declaration Performance
Often when coding, we use a single local variable multiple times, overwriting the value many times. It is considered good practice to move the variable out of the looping overwrite and into the smallest scope of code that is run once. But, this makes the code a little bit less concise. So, how useful is it, anyway? I wanted to run a few little tests and see if there's really a noticable difference in performance.
EasyMock Cause-Effect Exception Mapping
EasyMock is a great tool for separating external dependencies from unit tests. There is a learning curve to learning the mock method of testing, and unfortunately, EasyMock does not give very good prompts when you do something wrong. The exception messages are actually quite cryptic. This article is meant to be a crude mapping of exception output and the behavior that might have caused it. At least, this is a log of many of my experiences with EasyMock and how I usually get into the messes I do. It is quite possible that the same exception output could be had via different behavior. It's also important to note that I'm not trying to show how to create meaningful tests here (I don't even show full tests half the time), only help figure out how mysterious EasyMock exceptions were thrown. These experiences were documented on EasyMock 2.2 and 2.4.
Java Swing and Compiz
On Ubuntu 9.04, Jaunty, Compiz comes running out of the box on the mid-level preset for desktop effects. These effects and generally beautiful and pleasing, but a couple of the apps that I use are Java-based and have some compatibility issues with Compiz. At least for my apps, I've found a way around this issue.
Expose Fields via Java Reflection
For unit testing purposes, I often want to set field values in objects so that I can setup for the test conditions. One of most annoying things about testing is the urge to change code design for just the sake of testing -- especially if it's in a way that is considered less safe, like exposing elements or lessening accessibility. (This is not to say that trying to test code can reveal certain code smells and prompt refactoring). I, myself, have a number of setter methods with this comment prepended: "// for test only comments". Stinkers! Well, sometimes enough becomes enough. So, here's a way to set any field on an object w/o exposing it. This is done via reflection.
ConcurrentModificationException Within One Thread
I at first thought it odd that a ConcurrentModificationException could be thrown w/in the context of a single Thread. But, what do you know, it can! And I seem to be getting better at writing code that does!
Arrays.asList returns immutable List
I was having problems doing operations on a java.util.List object returned by Arrays.asList() method call. The specific exception was a java.lang.UnsupportedOperationException. I soon found out why this was a problem.
Java Reflection for methods with primitive params
First-class objects are the norm in the code that I usually write and edit, but every now and then I run across a method with a primitive parameter. I use EasyMock a lot in testing, and need to find these methods by reflection, this is how it's done...
Facelets Template Includes and Params
Facelets is a great template framework that sits on top of JSF. They have some fun template includes and parameters that you can pass into the included templates. Here is an example with one gotcha pertaining to the parameter name.
Create Custom Facelets Component
Facelets is a great addition to jsf. It is very extensible, and it makes creating custom components even easier. It's fantastic. This is almost TOO easy. If you have a pile of view markup and/or client-side logic, then this is a good option. And by good, I mean awesome.
Use Java Reflection to Get Field w/ Accessor
Java is about objects. Reflection is about knowing things about those objects generally without have specific fields and methods in hand. I must get to one of those field values via its accessor, allowing me to keep the fields private and abstracted away... of course, until I start unit testing. Here's one method...