JPragma Blog

Pragmatic Developers, Elegant Software

Archive for February 2010

Finding unused code in Java projects

with one comment

In many projects, especially big ones, developed over several years, there is a lot unused code. Cleaning up projects and deleting such unused code is important.

Unfortunately all above methods will mark code invoked by reflection (e.g. using IoC container such as Spring) as unused. At the same time garbage code that was left behind together with its unit test won’t be detected.

I see only one way to detect such obsolete code – run the system for a while through all typical scenarios and collect statistics on loaded classes, invoked methods, etc. Of course report generated using this technique can be only considered as a hint and manual verification is required.
Proper solution should probably be done using AOP, instrumenting code either during compile or load time. There is however quick and dirty way that archives similar results.

  1. Run your app with -verbose:class option (send standard output to file in addition to console using | tee <filename>)
    $ export MAVEN_OPTS="$MAVEN_OPTS -verbose:class"
    $ mvn clean jetty:run | tee jetty.log
  2. Execute all typical scenarios of your application
  3. From generated log file create sorted list of your project classes used in executed scenarios:
    $ grep "\[Loaded.*com.jpragma" jetty.log | awk '{print $2}' | sort | uniq -u > loaded_classes.txt
  4. Create sorted list of your compiled project classes:
    $ find target/classes/ -name *.class | sed 's/target\/classes\/\///g' \
    | sed 's/.class//g' | tr -s '/' '.' \
    | sort > declared_classes.txt
  5. Compare two txt files finding all classes that declared but not used:
    $ diff loaded_classes.txt declared_classes.txt | grep ">"

Written by isaaclevin

February 14, 2010 at 7:24 pm

Posted in Java

Insert semicolons and braces in correct position

leave a comment »

One thing that always disturbed me when editing Java source files is that you need to move your hands out of keyboard’s home row, press <End> and then type semicolon or open brace. Not anymore! I recently discovered pretty cool feature in Eclipse that automatically inserts semicolons and braces in correct positions.

Enable this option via Preferences -> Java -> Editor -> Typing dialog.

Written by isaaclevin

February 14, 2010 at 7:23 pm

Posted in Uncategorized

FlexBuilder code formatter

leave a comment »

Automatic code format and correcting indentation are essential features of any IDE. Unfortunately FlexBuilder currently doesn’t have them. Recently I found very cool plugin called flexformatter that solves this issue. Start by installing flexformatter using eclipse update site. After restaring Eclipse/FlexBuilder you will find “Flex Formatting” menu under “Preferences”.

Here is some description of various configuration options and my configuration file that can be simply imported.

Written by isaaclevin

February 14, 2010 at 7:20 pm

Posted in Flex

Adjust Windows keyboard shortcuts to match Mac OS X

leave a comment »

I use MacBook as my primary computer at home and Windows in the office.

First half of every day I keep pressing Alt-C, Alt-V in order to copy/paste… Well, that’s where Mac has its “Command” key. By the end of the day I get used to Ctrl-… combination, then I come home, turn on my MacBook and the hassle continues.

Here is solution I found:

  1. Download AutoHotKey and install on Windows
  2. Put following script into “Start->All Programs->Startup”(I called it mackeys.ahk):

!c::Send ^c
!x::Send ^x
!v::Send ^v
!s::Send ^s
!a::Send ^a
!z::Send ^z
!q::Send !{F4}

From now on Alt-C sends Ctrl-C, Alt-V -> Ctrl-V, etc. Default Ctrl key combinations still can be used.

Written by isaaclevin

February 14, 2010 at 7:19 pm

Posted in General

QuickLook plugins for Mac OS X 10.5 Leopard

leave a comment »

QuickLook is a feature in Leopard, which allows to view file content without opening it, directly from Finder.

Here is the list of additional plugins I have installed on my MacBook (to install simply copy.qlgenerator package into /Library/QuickLook or ~/Library/QuickLook):

  1. BetterZipQL – view content of an archive (many archive types are supported)
  2. FolderViewer – view content of selected folder
  3. QLEnscript – colorized preview of source code files
  4. ColorXML – colorized preview of XML files

Written by isaaclevin

February 14, 2010 at 7:17 pm

Posted in General

Combined Java+Flex project with hot redeployment

leave a comment »

In small applications it makes sense to combine Java and Flex code under same project. I’m a big fan of maven and one of my favorite maven plugins is embedded jetty container. This plugin runs jetty directly from maven and supports automatic monitoring and redeployment of java classes and resources. I thought it would be nice to run Java+Flex projects in the same way and have updated SWF file instantly available in the browser immediately after Flex Builder compiles it.

You can find step-by-step instructions on such project set up on my wiki:

Written by isaaclevin

February 14, 2010 at 7:16 pm

Posted in Flex, Java

Using Groovy in JMeter

leave a comment »

Note: This is a copy from my old blog posted in 2007. Some things mentioned here have been changed since then.

Groovy is a great scripting language intimately integrated with Java. Being able to leverage any code written in Java and at the same time having all the advantages of dynamic programming language, makes it an excellent choice for tasks such as file management, build systems and testing.

JMeter is an open source load test tool. It supports many protocols (HTTP, FTP, JMS and others) out-of-the-box.

Many times I found myself in situation when I wanted to run load test against stand-alone Java components or against an application that uses proprietary binary TCP based protocol. Groovy would be an excellent choice for this task. Some work on integrating JMeter and Groovy was done by Alexandru Popescu, however the project is not available to the public yet. Then I discovered that there is an indirect support for groovy in JMeter itself. JMeter supports BSF, which is an abstraction layer on top of many scripting languages including groovy. BSF distribution that comes with JMeter actually has some glitch in groovy support, but after upgrading to the latest version of bsf.jar it was very easy to start writing JMeter scripts in groovy.

Here the steps I followed:

  1. download latest groovy binaries from
  2. copy groovy-all-1.0.jar to jmeter/lib directory
  3. download latest bsf binaries from (I was using version 2.4.0)
  4. replace bsf-2.3.0.jar in jmeter/lib with bsf.jar from the version you just downloaded
  5. start jmeter
  6. create BSF Sampler and specify “groovy” as a scripting language (see screenshot below)
  7. type your groovy script and run it

This method is far from being perfect. It actually monitors how much time it takes to execute the script itself, but in most cases you’d like to specify exactly when to start counting execution time [results.sampleStart(); results.samplePause(); results.sampleResume(); results.sampleEnd();]
I hope Alexandru will make his project available soon, or may be I’ll find some time to create more sophisticated JMeter Groovy plug-in.

Another issue is how to transfer data between several scripts. Let’s say you’d like to create 1st script that establishes TCP connection; 2nd that sends proprietary login message and gets back some security token; 3rd that sends some business message (using the same TCP connection and providing the token) and 4th that just disconnects. How to transfer Socket and Token objects between scripts? Well, you can use ThreadLocal. Following code snippet defines ThreadLocalStorage class that can be used from the scripts (isn’t it great that you can just call Java from Groovy?) to save and retrieve any data:

package com.jpragma.utils;

import java.util.HashMap;

public class ThreadLocalStorage {
  private static ThreadLocal storage = new ThreadLocal() {
    protected Object initialValue() {
      return new HashMap();
  public static Object get(String name) {
    HashMap vars = (HashMap) storage.get();
    return vars.get(name);
  public static void set(String name, Object value) {
    HashMap vars = (HashMap) storage.get();
    vars.put(name, value);

Just package it as a jar file and put into jmeter/lib directory. Now you can write your scripts like this:


import com.jpragma.utils.ThreadLocalStorage

def socket = new Socket("localhost", 18012);
def iStream = new DataInputStream(socket.getInputStream());
def oStream = new DataOutputStream(socket.getOutputStream());

ThreadLocalStorage.set('mySocket', socket)
ThreadLocalStorage.set('myInStream', iStream)
ThreadLocalStorage.set('myOutStream', oStream)

return "success"

Echo Message

import com.jpragma.utils.ThreadLocalStorage

def iStream = ThreadLocalStorage.get('myInStream')
def oStream = ThreadLocalStorage.get('myOutStream')

def str = "My String"

def msgSize = iStream.readInt()
def buf = new byte[msgSize]
def responseStr = new String(buf)

if (str == responseStr)
    return "success"
    return "failure"


import com.jpragma.utils.ThreadLocalStorage

def socket = ThreadLocalStorage.get('mySocket')
if (socket != null) {
return "success"

Written by isaaclevin

February 14, 2010 at 7:12 pm

Posted in Java