Wednesday, December 8, 2010

Determine if Class or Interface??

How to determine if a Class object represents a Class or Interface??

You can determine by calling isInterface method in Class-
...
Class cls = java.lang.String.class; 
// returns true as String is a class and not an Interface
boolean isClass = !cls.isInterface();
cls = java.lang.Cloneable.class; 
isClass = !cls.isInterface(); // false as Cloneable is an interface, note negation sign
...

So at runtime if you want to know about the entity you are working with, the Class or Interface, you can use the above method.

Tuesday, December 7, 2010

Break Oop's concepts - Java Reflection API

As you all know that we can not access private members of a class, but using Java Reflection API you can. Yes you can break Oops concept by accessing private variable, by calling private methods and also you can create an instance of class which is having private constructor-

Access private members of a class

/**
Class having only one private member
*/

package com.examples;
class test
{
private int youCanAccessMe;
}


Now to access "youCanAccessMe" variable -

package com.examples;
import java.lang.reflect.Field;
public class ReflectExample
{
public static void main(String args[])
{
try
{
Class cls = Class.forName("com.examples.test");
Object obj = cls.newInstance();
Field[] fields = cls.getDeclaredFields();
for( int i = 0 ; i <>fields[i].setAccessible(true);
System.out.println("Field Name-->"+fields[i].getName()+"\t"
+"Field Type-->"+ fields[i].getType().getName()+"\t"
+"Field Value-->"+ fields[i].get(obj));

}
}
catch(Exception e)
{
System.out.println("Exception occured" + e);
}
}
}

You can get the Class object and using Class object you can also create an object of class.
Class cls = Class.forName("com.examples.test");
Object obj = cls.newInstance();
To access private member variable, you have to call setAccessible method to set accessibility level to true.
fields[i].getName() - To get name of variable
fields[i].getType().getName() - To get the type of variable
fields[i].get(obj) - To get its values
Thus Reflection is a powerful approach to analyze the class at runtime.

New post - which is irrelevant in this blog

Hi, i am publishing this post here, which i liked and is not supposed to post in this blog, but i can't resist posting it. One of my friend(Uday Shankar) has written a very touchy poem(Only people who are in love or had break up can understand).

"beautiful memories".

We had our first glance,
that was the start of our romance.
Then we had a small talk
while we had a pleasant walk.

We had some fortunate encounters
and we became regular chatters.
We always enjoyed each others company
so we started meeting few times ... then many.

Love was in the air,
each other was all, we care.
One day those feelings were expressed.
From that moment life get blessed.

We had our first fight,
but we can't live without others sight.
so we make up our mind,
to never leave each other behind.

Don't know what had gone wrong
I'm feeling dreadful for so long
The life has parted our ways
But those were my most beautiful days.

Monday, December 6, 2010

Java 5 feature - Covariant return types

Now with Java 5 you can override a function and can make their return type different* which was not allowed before Java 5.
The return type of the subclass' method must be a subclass of the return type of the superclass' method. In other words you can make the return type of the subclass' method the same or less general.

Confused???? Lets understand with an example -
class Clothe { 
     public clothe getDesign() {
        System.out.println("clothe");
        return new Clothe();
    }
}
class Shirt extends Clothe {
    public Shirt getDesign() {  
     System.out.println("shirt");
     return new Shirt();
   }
}

An overridden method in a derived class can return a type derived from the type returned by the base-class method.

public class CovariantReturn {
    public static void main(String[] args) {
       clothe clothe = new clothe();
       System.out.println(clothe.getDesign());
       clothe = new Shirt();
       System.out.println(clothe.getDesign());
   } 
}   
Output:
clothe
clothe@923e30
shirt
Shirt@130c19b

Allowing covariant return types in Java has been in Sun's bugparade for a long time (http://bugs.sun.com/bugdatabase/view_bug.do? bug_id=4144488). Now finally, JDK 1.5 supports it.

Saturday, December 4, 2010

How to create Case Insensitive Map in Java

In order to create case insensitive map, just extend HashMap class and override get and put method. Using the below code you can give keys irrespective of case.
class CaseImap extends HashMap<String, Object>
{
// Overriding get method
public String
get(String key)
{
return (String)super.get(key.toLowerCase());
}
// Overriding put method
public Object
put(String key, Object value)
{
return super.put(key.toLowerCase(), value);
}
}

Friday, December 3, 2010

Using isInfoEnabled in Java

-Logging using log.info
 Just have the below "if" block before using log.info or log.debug or log.error
  if(log.isInfoEnabled()
 {
  log.info(yourFunctionName + " :--- " + getVeryLargeString() + someVar);
 } 
Why?
Suppose the current log4j level is set to Error then log.info messages will not be displayed, it will take only few nanoseconds to execute log method but it may take time to execute the function call, if any, in the message.
Drawback of not using if condition
1) Unnecessary call to the functions in the log.info message 
2) Many string objects will be created in the string pool
Drawback of using if condition-
1) For each and every log method call u have to have this condition, code will not be readable and not be clear much.
2) Extra if condition in many places.

Jdk1.5 has solution for this problem, it removes log calls automatically removed if they're not being used.

It is possible to do this using bytecode manipulation - searching for and removing calls to the logger as the class is loaded. The new Java 1.5 instrumentation classes make this particularly easy, by allowing ClassTransformer classes to be registered on the command line that are allowed to manipulate class files as they are loaded.

To use this code, launch Java with the arguments:
java -javaagent:net.surguy.logfilter.LogPreloader=debug [your own class]

The argument is optional, and can be one of "debug", "info", "warn", "error" or "fatal", corresponding to the standard Log4J log levels.

How does it work
As Java loads each class, it passes it to each of the registered ClassFileTransformers, which get a chance to alter its bytecode. It uses the ASM bytecode manipulation library to do so.