Tuesday, May 8, 2012

Ant task to find all java files in dir

                     

                   Some time we need to identify all file names in our project. For documentation purpose or for creating modification estimate etc. Following is the ant target that you can use to do the job. here the list of file name will be printed with semi colon ; as delimiter.
  
 
  
   
    
   
   
    list of files: 
   ${prop.javafiles}
  
                     
                   In above snippet:  ${dir.src} will be the location of source code Output will look like following
  
list:

  [echo]  list of files: 

  [echo]com\test\listproject\AdapterController.java;com
\test\listproject\cleanup\BppmDifferenceData.java;com\
test\listproject\cleanup\CleanupException.java; 
com\test\listproject\cleanup\CleanupManager.java

BUILD SUCCESSFUL

Total time: 0 seconds

Friday, May 4, 2012

JAVA Reflection : Reading method by reflection

Hi Folks,

      Last week while writing mapping utility, I came across requirement to invoke method on object using method name obviously I decided to use reflection. While using reflection  I came across two methods in java.lang.Class viz.:
    public Method getMethod(String name, Class... parameterTypes)
    public Method getDeclaredMethod(String name, Class... parameterTypes)
   
Though both the methods can be used to retrieve method information dynamically, they behave differently while searching

getMethod
  public Method getMethod(String name,Class... parameterTypes)
              throws NoSuchMethodException, SecurityException
           This method return Method object represented by parameter. The algorithm used for Searching method name, will search the method in Class passed as argument, if the method was not found inside it the search is carried out on all its super classes.

getDeclaredMethod      
 public Method getDeclaredMethod(String name,
                                 Class... parameterTypes)
                throws NoSuchMethodException, SecurityException 
             This method return Method object represented by searching in Class only which is provided as argument.

Note: If you use getMethods in the program instead of getDeclaredMethods, you can also obtain information for inherited methods.

Reference:
http://java.sun.com/developer/technicalArticles/ALT/Reflection/