Archive for February, 2008

Feb 28 2008

Jakarta BCEL

Published by under IT,Java

BCEL (Byte Code Engineering Library) gives a possibility to analyze, create and modify Java class files. It gives very nice API for manipulating inside bytecode. Here is an example of using BCEL. It adds instructions relevant to System.out.println(“About to call: METHOD_NAME METHOD_SIGNATURE”) before invocation of any method in the class.

 
import java.io.IOException;
import java.util.Iterator;
 
import org.apache.bcel.Repository;
import org.apache.bcel.classfile.ClassFormatException;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.*;
 
/**
 * Instruments byte-code by adding: <code>
 * System.out.println("About to call: METHOD_NAME METHOD_SIGNATURE");
 * </code>
 * before method invocation.
 *
 * @author Radoslaw Urbas
 * @since Oct 27, 2007
 *
 */
public class Transform
{
 
    private ClassGen classGen = null;
 
    /**
     *
     * @param className name of class to instrument
     * @throws ClassNotFoundException
     * @throws ClassFormatException
     * @throws IOException
     */
    public Transform(String className) throws ClassNotFoundException,
            ClassFormatException, IOException
    {
        this.classGen = new ClassGen(Repository.lookupClass(className));
    }
 
    /**
     * Instruments the class by displaying method information before invocation.
     *
     * @return instrumented ClassGen object
     */
    private ClassGen instrument()
    {
        ConstantPoolGen pgen = classGen.getConstantPool();
        String cname = classGen.getClassName();
        for (Method method : classGen.getMethods())
        {
            MethodGen methgen = instrumentMethod(pgen, cname, method);
            classGen.replaceMethod(method, methgen.getMethod());
        }
        return classGen;
    }
 
    /**
     * Instruments method by displaying information before method invocation.
     *
     * @param constantPoolGen constant pool for class containing method to
     *            instrument
     * @param className name of the class containing method to instrument
     * @param method method to instrument
     * @return instrumented MethodGen object
     */
    private MethodGen instrumentMethod(ConstantPoolGen constantPoolGen,
            String className, Method method)
    {
        InstructionFactory instructionFactory = new InstructionFactory(classGen);
        MethodGen methgen = new MethodGen(method, className, constantPoolGen);
        InstructionList originalInstructionList = methgen.getInstructionList();
        Iterator instructionIterator = originalInstructionList.iterator();
        while (instructionIterator.hasNext())
        {
            InstructionHandle ih = (InstructionHandle) instructionIterator
                    .next();
            if (ih.getInstruction() instanceof InvokeInstruction)
            {
                originalInstructionList.insert(ih, instructionFactory
                        .createPrintln(getMessage((InvokeInstruction) ih
                                .getInstruction(), constantPoolGen)));
            }
        }
        methgen.setMaxStack();
        return methgen;
    }
 
    /**
     *
     * @param invokeInstruction object representing invoke instruction
     * @param constantPoolGen
     * @return message based on method name and signature
     */
    private String getMessage(InvokeInstruction invokeInstruction,
            ConstantPoolGen constantPoolGen)
    {
        String text = "About to call: "
                + invokeInstruction.getMethodName(constantPoolGen)
                + invokeInstruction.getSignature(constantPoolGen);
        return text;
    }
 
    /**
     *
     * @param args requires one parameter: class name. Class should be available
     *            in directory ./classes
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public static void main(String[] args) throws ClassNotFoundException,
            IOException
    {
        String className = args[0];
        Transform transform = new Transform(className);
        ClassGen classGen = transform.instrument();
        classGen.getJavaClass().dump(className + ".class");
    }
}

Homepage: http://jakarta.apache.org/bcel/

4 responses so far

Feb 21 2008

PicLens Add-on

Published by under IT

PicLens is a Firefox Add-on for displaying images in more “interactive” way. It has sort 3D Wall of images and allows to view them with smooth mouse movements.

It works with multiple websites, e.g. Picasa, Flickr, Facebook, MySpace and image search on Google, Yahoo, Live, AOL.

Check it out https://addons.mozilla.org/en-US/firefox/addon/5579

No responses yet

Feb 12 2008

AppFuse – first impression

Published by under Java

Couple days ago I was looking for some starter kit for Java web applications based on Spring framework. After short investigation I have decide to try some of Maven2 archetypes. If you just type: mvn archetype:create you get list of standard archetypes. Take a look on positions 1-9 (AppFuse starter-kits).

For further investigation I chose appfuse-modular-struts which is a solution based on Hibernate, Spring and Struts2.

To kickoff application kickoffs there are just few simple steps:

  • Launch database. I was trying it with Derby and PostgreSQL.
  • Configure database settings in pom.xml in main directory by adding line true in ‘profiles’ section after ‘id’ of DB of your choice and setting proper user/pass and database name if applicable.
  • Kickoff Maven build and run application:
    mvn
    cd web
    mvn jetty:run-war
  • Navigate to http://localhost:8080 in you browser.

You will see simple application that allows to:

  • Log in (admin/admin).
  • List the users.
  • Edit user data.

Everything looks great. That is the moment when you are starting to look for sources of this application. To get the source you need to invoke another Maven target
mvn appfuse:full-source.

Now you can rebuild the application and do the changes you want.
There is also nice feature for adding standard code. You can generate all required class and configurations for domain objects. To do this add new domain object in your.application.model package in core module and annotate it properly. After that:
cd core
mvn appfuse:gen -Dentity=YourNewObject
cd ..
cd web
mvn appfuse:gen -Dentity=YourNewObject

All the classes, configurations and webpages should be ready for performing CRUD operations for this POJO!

Everything looks great, but there is one concern. Don’t use Java 1.6. Use Java 1.5. It took me several hours to figure out that this is the problem while tests fails (just HTTP 500 respond, no exceptions, no logs). Haven’t investigated yet what’s it the core of the problem with using it on Java 6.

AppFuse homepage: http://appfuse.org/display/APF/Home

No responses yet

Feb 07 2008

20 questions

Published by under AI

I have just found out an interesting webpage: http://www.20q.net/

It’s sort of an expert system, which is reasoning based on human’s answer given for specific questions. Questions are quite generic. While the system is self-learning, the more people play the game, the more accurate results are.

Based on the authors’ information the accuracy is about 80%. Check it out and have fun!

No responses yet

Feb 05 2008

Code quality ;-)

Published by under IT

No responses yet