Thursday, September 30, 2010

Pitfalls in replaceAll function of String class

Lets have a look at the replaceAll function.

String s = "Yogesh C:\\test3.xml vaibhav";
s = s.replaceAll("C:\\test3.xml", "test");

You might be expecting that it would replace the string c:\\test3.xml, but it doesn't happens.
Reason :
The regex parser has backslash (\) and dollar ($) as keywords, so it treats it differently.
If your string contains these characters, you can use Matcher.quoteReplacement to skip these characters.


String s = "Yogesh C:\\test3.xml vaibhav";
s = s.replaceAll(Matcher.quoteReplacement("C:\\test3.xml"), "test");

Sunday, September 26, 2010

JAVASSIST (Java programming Assistant)

JAVASSIST (Java programming Assistant) is a utility that allows one to inspect, edit and create Java binary classes. It enables Java programs to define a new class at runtime and to modify a given class file when the JVM loads it. Its best feature is its ease of use since it allows developers to fully exploit byte code manipulation with little or no knowledge of byte code, giving them a degree of fine-grained control.

Javassist provides two levels of API: Source level and Byte code level. If the users use source level API, they can edit a class file without knowledge of the specifications of the Java byte code. The whole API is designed with only the vocabulary of the Java language. You can even specify inserted byte code in the form of source text; Javassist compiles it on the fly. On the other hand, the byte code level API allows the users to directly edit a class file as other editors.

Javassist uses the javassist.ClassPool class to track and control the classes being manipulated. Classes loaded in a class pool are represented by javassist.CtClass instances where as fields, methods and constructors are represented by javassist.CtField, javassist.CtMethod and javassist.CtConstructor instances, respectively.

An Example of Javassist

The below example depicts the usage of few of the Javassist basic APIs. All that is required is to just download ‘javassist.jar’ and add the same to the class path.

Wednesday, September 22, 2010


Some imports that you may require

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

A generic method which can print any bean class, provided all the getter methods are there.

public static void printObject(Object app) throws
IllegalArgumentException, IllegalAccessException, 
    Method[] m = app.getClass().getMethods();
    for(Method method : m)
            // if getClass then ignore this method.
                System.out.print(method.getName() + "=[ ");
                Object[] values = (Object[])method.invoke(app, null);

                for(Object v : values)
                    System.out.print(v + ", " );
                System.out.print(" ]\n");
                System.out.println(method.getName() + " = " + method.invoke(app, null));

If you would like to learn more about Reflections, you can visit this link:

Wednesday, September 8, 2010

Premature end of file when running junits


Premature end of file error when running Junits

[junitreport] [Fatal Error] :-1:-1: Premature end of file.
[junitreport] The file /tmp/repository/Yogesh/task2652_story1984/modulesmgr/junit_report/out/xml/TEST-com.coresecurity.ctrlsvc.modules.ModulesImportAndBasicCRUDTest.xml is not a valid XML document. It is possibly corrupted.

1) TESTS-testsuites.xml was coming out to be empty.
2) The resulting xml file was getting too large in size.

Hibernate logging was enabled which was leading to large size of XML.
We disabled logging in config.xml as given below
Earlier it was true, we made it false

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<qualifier value ="DataSource" />
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.coresecurity.datamanager"/>
<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="">update</prop>
<prop key="hibernate.transaction">true</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>