|
Practical Eclipse (Continued)
These methods use a key to encrypt and decrypt data. They are very similar and in fact have one block of code in common: the code that creates the key.
There is a refactor called Extract Method, which takes a block of code and puts it in its own method. Do this by highlighting the block of code and then selecting Refactor > Extract Method (Alt-Shift-M). In the Extract Method dialog (see Figure 3) enter a name for the method, and make sure that the parameter name is what you want. Select OK and you should get the new method:
private SecretKey createKey(
String algorithm) throws
NoSuchAlgorithmException,
InvalidKeySpecException
{
KeySpec ks = new PBEKeySpec(
passphrase.toCharArray());
SecretKeyFactory skf =
SecretKeyFactory.
getInstance(algorithm);
SecretKey sk = skf.
generateSecret(ks);
return sk;
}
You can now replace the method code in all other places it is used with a call to your new method. The two prior methods—encryptKey() and decrytptKey()—are in different classes that extend the same base class, so ideally createKey() should be a method in the base class. Enter another refactor: Pull Up. You use this refactor to move methods up the class hierarchy. If we do this with createKey(), it will also change the protection level from private to protected to allow the method to be called in the derived class. The final code looks like:
protected SecretKey createKey(
String algorithm) throws
NoSuchAlgorithmException,
InvalidKeySpecException
{
KeySpec ks = new PBEKeySpec(
passphrase.toCharArray());
SecretKeyFactory skf =
SecretKeyFactory.
getInstance(algorithm);
SecretKey sk = skf.
generateSecret(ks);
return sk;
}
After making the changes, rerun the unit tests to ensure that nothing is broken.
JUnit with Ant
I mentioned that you must have unit tests if you are going to refactor to prove that your changes have not broken the code. Eclipse has very good support for JUnit; however, I tend to use Ant to build and test my code, and I use the JUnit test within Ant. If you load an Ant build file into Eclipse and run a JUnit Ant task, you are likely to see:
BUILD FAILED:
D:\home\kevinj\Java\Security\
tests.xml:94: Could not create
task or type of type: junit.
This message means that Ant could not find the task or a class this task relies on. Ant is trying to load the JUnit JAR files. When using Ant outside of the IDE you have to place junit.jar in the %ANT_HOME%/lib directory. That task doesn't work when using Eclipse because it uses its own version of Ant. Instead junit.jar must be placed in Eclipse's Ant directory, that is, in %ECLIPSE_HOME%/plugins/org.apache.ant_1.6.2/lib (obviously the version of Ant may vary with the version of Eclipse).
Eclipse is a world-class IDE with many features. We have only scratched the surface here, and over the coming months we'll explore more—and in greater depth—features that Eclipse offers.
As seems to be the way of first columns, I'd like to request reader feedback. I have some topics I'd like to write about, and colleagues of mine have come up with other ideas. Now I'd like to know what you would like. E-mail me directly or through the magazine.
About the Author
Kevin Jones is a developer who researches and teaches Java programming and explores HTTP and XML. He lives in the U.K. and works for Developmentor, a training company based in the United States and Europe that specializes in technical training on Java and Microsoft platforms. Contact Kevin at .
Back to top
|