Friday, May 02, 2014

Java Trick: Generic Array Converter

I had a problem of increasing size of the array. Of course I have to copy the array. But it is not easy with java Generics, because of the following problem:

public class ArrayUtils<t>{
 public T[] resizeArray(T[] source, int offset){
  int length = source.length+offset;
  Object[] aNew = new Object[length];
  System.arraycopy(source, 0, aNew, 0, source.length);
  return (T[])aNew;

Of course you can compile the ArrayUtils class, but you cannot use this method as shown in the following code segment:
 public static void main(String[] args){
  Integer[] ints = new Integer[2];
  Integer[] newInts =  new ArrayUtils<integer>().resizeArray(ints, 3);

Here the error:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

the problem, aNew never is possible to cast to the type T because it is created as an Object type. I found a very good answer with help of the book: Core Java: Volume I—Fundamentals, Ninth Edition. It is easy with java reflections as shown in the line# 7 to 9. getClass().getComponentType() return the type of the array elements.

import java.lang.reflect.Array;

public class ArrayUtils<t>{
 public T[] resizeArray(T[] source, int offset){
  int length = source.length+offset;
  Class arrayClass =  source.getClass();
  Class elementType =  arrayClass.getComponentType();
  Object aNew = Array.newInstance(elementType, length);
  System.arraycopy(source, 0, aNew, 0, source.length);
  return (T[])aNew;

If you need you can make the method static by changing it signature to
public <t> T[] resizeArray(T[] source, int offset){ 

Thursday, May 01, 2014

Java Trick: Double brace initialization

This is a trick I found from the Core Java™: Volume I—Fundamentals, Ninth Edition, 6.4.6. Anonymous Inner Classes.
import java.util.ArrayList;
import java.util.List;

public class TestArrayList {
 private String s1;
 private static String s2;
 public static void main(String[] args) {
  TestArrayList tl = new TestArrayList();
  tl.printList(new ArrayList(){
   {add("a"); add("b"); add(s2);}


 public void printList(/*only final variables are accessible to local inner classes*/final List sList){
  printDynamicList(new ArrayList(){{add(s1); addAll(sList);}});
 public void printDynamicList(List dList){
As shown in the above code:

  • As shown in the line# 10, you can add elements to the anonymous array: this is called Double brace initialization.
  • Again in the line# 16, in the double brace initialization, s1 is added to the array which is a property fo the outer class.
  • To add the parameter in line# 16, it should be final, this is the rule of local inner classes.
If you have more info please add to this blog.

Friday, February 28, 2014

Remote Debugging Tomcat 6 using SSH tunnel

My current IDE is Intellij Idea 12 and it is running on Windows 7 machine. However, server exist on one of the RedHat Enterprise machines. The main challenge is to debug the remote tomcat server via SSH port 22 only because that is the only allowed path. Even I cannot ping the server.

Instead of, I have to always run the "sudo service tomcatx start|stop" because tomcat is installed as a service using redhat yum. From this blog I found the details to setup the tomcat. I changed the TOMCAT_HOME/conf/tomcat6.conf file to have the following JAVA_OPTS

JAVA_OPTS="${JAVA_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,address=15005,server=y,suspend=n"

This is just a one line.

As shown in the above, the port is address that is 15005. Now I have to create the SSH connection using Putty as shown in the below

source port: 15005
Destination: localhost:15005

After add as shown in the above screenshot, you have to start the session. 

Now you have to configure the new remote debugging session in the Intellij Idea 12.0.6.

Now you are ready to debug the remote tomcat server.
However, currently I am facing a problem that is :
"each time I need to restart the server, I have to remove the above JAVA_OPTS line from the tomcat6.conf file". After I stop the server, again insert the line and start the server.