Sunday, January 22, 2012

Own Custom Task in Ant

Many times when developing complicated Ant scripts we feel the need of custom Ant tags or need to develop our own task in Ant. Here is a step by step guide to achieve the same
Prerequisites:
Download and set up the Apache ant.
 1.Create a new Java Project in eclipse.Right click on Project->Properties->Java Build Path->Add external Jar. Browse to Ant.jar file in ANT_HOME/lib folder.



2.Make a new class say Hello which has to extend class org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

public class HelloWorld extends Task {
 private String retproperty = null;
 private String msg;

 public String getRetProperty() {
  return retproperty;
 }

 public void setRetProperty(String property) {
  this.retproperty = property;
 }

 public void setMsg(String msg) {
  this.msg = msg;
 }

 public void execute() throws BuildException {
  System.out.println(msg);
  Project project = getProject();
  project.setProperty(retproperty,
    "This is Value Returned By Custom task to Ant script");
 }
}

In this program we will learn how to pass a value to java class from ant and how to get the value as return value from Java Custom task.
3.Once you have the java code ready in eclipse click on File=>Export=>Choose Jar and expost to any suitable location on your system. as shown in below screenshot

4. Now its time to write Ant script which will call this custom task pass a value and also read the value returned by the task.So write a similar Ant script.Here classpath should be location where jar in Step 3 was exported.

  
  
   
   
  


5.Now we can run the Ant script through command prompt or eclipse as per convenience. We will get output as below
In developing Ant custom task whatever Java variables we want to be accessed through ant we should make them private with public getter and setter methods.Now when through Ant below line is executed



This executes method of the HelloWorld class is invoked and msg variable is initialised with value passed. Similarly 
project.setProperty(retproperty,
    "This is Value Returned By Custom task to Ant script");

The above line sets the value into property val  which is again defined in retproperty. So that in Ant script variable val will hold the value returned or set inside the class so we can use it for further processing.

Please comment me if you face any issue implementing it.


Tuesday, January 17, 2012

Understanding Call By Reference And Call By Value In JAVA


In java we usually pass parameters to the function but sometimes it works out to be call by value and sometimes call by reference. This behavior is dependent on what parameters are getting passed to the function.Unlike C where we choose its call by reference or value.So below are set of rules you can keep in mind to decide whether the code will implement call by value or call by reference.

Simplified Rule:
Whenever we pass any variable belonging to primitive it is always passed as pass by value. Whenever we pass object of the class it mostly happens as pass by reference except certain circumstances.Below example 
public class Example {
 public class Try {
 public static void main(String[] args) {
  int i = 0;
  String s = "In Main:String";
  StringBuffer sb = new StringBuffer("In Main:StringBuffer");
  System.out.println("**********************************************");
  System.out.println("Value of i in main before calling function i=" + i);
  primitve(i);
  System.out.println("Value of i in main after calling function i=" + i);
  System.out.println("**********************************************");
  System.out.println("Value of StringBuffer sb in main before function ="+sb);
  stringBufferProcess(sb);
  System.out.println("Value of StringBuffer sb in main before function ="+sb);
  System.out.println("**********************************************");
  System.out.println("Value of s in main before calling function s=" + s);
  stringProcess(s);
  System.out.println("Value of s in main after calling function s=" + s);
 }

 public static void stringBufferProcess(StringBuffer c) {
  System.out.println("Value of StringBuffer inside function before processing="+c);
          c.append(":::stringBufferProcess Func:::");
          System.out.println("Value of StringBuffer inside function ="+c);
 }

 public static void primitve(int i) {
  System.out.println("Value of i in function before processsing i=" + i);
  i = i + 5;
  System.out.println("Value of i in function after processsing i=" + i);
 }

 public static void stringProcess(String s) {
  System.out.println("Value of s in function before processsing s=" + s);
  s = s + "In StringProcess:";
  System.out.println("Value of s in function after processsing s=" + s);
 }

}

The above program would generate some output like this:
**********************************************
Value of i in main before calling function i=0
Value of i in function before processsing i=0
Value of i in function after processsing i=5
Value of i in main after calling function i=0
**********************************************
Value of StringBuffer sb in main before function =In Main:StringBuffer
Value of StringBuffer inside function before processing=In Main:StringBuffer
Value of StringBuffer inside function =In Main:StringBuffer:::stringBufferProcess Func:::
Value of StringBuffer sb in main before function =In Main:StringBuffer:::stringBufferProcess Func:::
**********************************************
Value of s in main before calling function s=In Main:String
Value of s in function before processsing s=In Main:String
Value of s in function after processsing s=In Main:StringIn StringProcess:
Value of s in main after calling function s=In Main:String 
Explanation:

When we pass a primitive it will be pass by value so change in value of i is not reflected in main. When we pass object reference to the function  object get modified.However this is only true till the object passed is mutable.If the object passed in immutable it becomes pass by value. In above example behave objects of 2 classes String Buffer and String but both behave differently.
This is mainly due to the fact that object of String class is immutable whereas object of StringBuffer class is mutable ie. Immutable objects are the objects whose state can not be changed once constructed. whereas object of StringBuffer class is mutable.

Conclusion:
1)When we pass a primitive to a function it is call by value
2) When we pass object as parameter to the function and if object is immutable then it is pass by value.
3) When we pass object as parameter to the function and if object is mutable then it is pass by reference.


Clarification:
Java dosent have separate call by reference or call by value. I agree with it. The post just tries to draw lines with C. and tries to explain when you pass object as parameter to function when the value is reflected back in calling function and when it is not.


I think this explaination is clear