|
Practical Eclipse (Continued)
Code style is one of the great debates among programmers. For example, where do you place your braces? Do you indent case from switch? Where does the while keyword go? And of course we all know that we are correct, that our style is the only one that matters. If you have a house style, or if you have personal preferences, then Eclipse can help enforce that style throughout your code.
Select Windows > Preferences, and select Java > Code Style > Formatter. In the right-hand pane you can select New to create a new set of preferences, give it a name, and then change the preferences to suit yourself. The most contentious issues are going to be the location of braces, but you also get control over line wrapping, white space, comments, and so on. You can also export and import profiles, which are extremely useful for teamwork.
Once you've selected your preferences you can open a Java file and choose Source > Format (Ctrl-Shift-F). You can also reformat multiple files at once. In the package explorer select the group of files you want to reformat, right-click, and then select Source > Format.
Refactoring
One of the most powerful Eclipse features is its refactoring support. This support requires a full article to discuss in depth, but it's worth noting here because it is so important. In the introduction to his informative book on the subject, Martin Fowler said "Refactoring is the process of changing a software system in such a way that it does not alter external behavior of the code yet improves on its internal structure." When refactoring you really also must have unit tests in place (see the next tip); otherwise, how can you know that the external behavior of the code has not changed?
Eclipse offers several refactorings, and we'll take a look at just a couple using them as an example to understand the process. Take these two methods:
byte[] decryptKey(
byte[] encryptedKeyBytes)
throws
NoSuchAlgorithmException,
InvalidKeySpecException,
NoSuchPaddingException,
InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException,
InvalidAlgorithmParameter
Exception
{
String alg =
"PBEWithMD5AndDES";
PBEParameterSpec spe =
createPBEParameterSpec();
KeySpec ks = new PBEKeySpec(
passphrase.toCharArray());
SecretKeyFactory skf =
SecretKeyFactory.
getInstance(alg);
SecretKey sk = skf.
generateSecret(ks);
Cipher cipher = Cipher.
getInstance(alg);
cipher.init(
Cipher.DECRYPT_MODE, sk,
spe);
return cipher.doFinal(
encryptedKeyBytes);
}
and
byte[] encryptKey() throws
NoSuchAlgorithmException,
InvalidKeySpecException,
NoSuchPaddingException,
InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException,
InvalidAlgorithmParameter
Exception
{
String alg =
"PBEWithMD5AndDES";
PBEParameterSpec spe =
createPBEParameterSpec();
KeySpec ks = new PBEKeySpec(
passphrase.toCharArray());
SecretKeyFactory skf =
SecretKeyFactory.
getInstance(alg);
SecretKey sk = skf.
generateSecret(ks);
Cipher encrypt = Cipher.
getInstance(alg);
encrypt.init(
Cipher.ENCRYPT_MODE, sk,
spe);
return encrypt.doFinal(
secret.getEncoded());
}
Back to top
|