Tuesday, October 8, 2013

Eclipse, Ant, CVS with Windows 7

I got a new machine with Windows 7, and started to setup my build environment.

We have an ant script to update from CVS and tag and etc...

Simply adding following ant task I could do this.

<target name="cvs_update">
<cvs command="update -dP">
</cvs>
</target>
<target name="cvs_tag">
<cvs command="tag ABC_${build_time}">
</cvs>
</target>

But, we should install WinCVS/CVSNT to run this task.

In Windows 7 I got a first issue, ant couldn't find the cvs.exe even if the cvs.exe in PATH variable. And if you check the in command prompt by type cvs it will run. But in Eclipse when you run above ant task it will keep saying application cvs not found.

[cvs] Caught exception: Cannot run program "cvs": CreateProcess error=2, The system cannot find the file specified

Only solution I found is:
uninstall and install CVSNT into a folder, which name doesn't have any spaces (Example Program_Files_(x86))

The 2nd I faced in login

 [cvs] cvs update: Empty password used - try 'cvs login' with a real password [cvs]

The only solution I found is:
Run CVSNT Password Agent.

Saturday, May 18, 2013

Eclipse, Java : Inserting Single line "if block"

If we run our program after completing coding, those will not as excepted mostly, or at-least in testing we will find places where we have missed some null check or etc. So we may have to add a if condition to check and to avoid those Exceptions. Mostly those will be a single line "if blocks".

I am talking about cases like below. Say you have following line:

myObject.myMethod();

Here we got a NullPointerException because myObject is null, I wanted added a null check before this:

if(myObject!=null)
     myObject.myMethod();

This is correct, but I mostly don't like this, for future coding and because of the readability, I prefer to add a block as follows. Sometime reader will think that, all the statements which following the above statement also inside the if condition.

if(myObject!=null){
     myObject.myMethod();
}

It was hard for me always, because I had to write if block and copy paste the statement inside. But i found a method to do in Eclipse without  doing  copy paste.

Just go infront of the statement and start typing your if condition and write up to { and press enter key, then you will get what you want.

if(myObject!=null){|myObject.myMethod(); <-- font="">Keep the curser at the | position and press enter.

Okey now you try and let me know your comments.

Wednesday, January 30, 2013

Part 2:Useful Eclipse Templates for Android Development

Part 1:Useful Eclipse Templates for Android Development

Weak Reference for Views in Activity.

Inside onCreate of each Android Activity we used to setContentView with layout xml. Then to access each view from the activity we use:

View view = findViewById(R.id..........);

to avoid calling  findViewById multiple time,  call this only in onCreate and keep this view as member variable inside activity.

View mView

.....

mView = findViewById(R.id..........);

Then, question is why we need to overload the onCreate method, we call findViewById only when needed and keep as member variable. So introduce following get method.

View getView(){
     if(mView==null){
         mView = findViewById(R.id..........);
     }
     return mView;
}

But in some cases, to avoid memory leakage issues, we need to remove all view from content view and need to null all these references in activity. But rather find each reference and assign to null, if we make a weak reference then it should all clear as we remove view from activity. For Example:


private WeakReference mView;
public View getView() {
if(!(mView!=null && mView.get()!=null)){
mView = new WeakReference((View) findViewById(R.id.....));
}
return mFrameAddStyleView.get();
}
Now, Let see how to write a Eclipse template to auto generate this:

Eclipse - > Windows -> Preferences -> Java -> Editor -> Templates -> New ...
Name:- wfind
Pattern:-
private WeakReference<${type}> m${new_name};
public ${type} get${new_name}() {
if(!(m${new_name}!=null && m${new_name}.get()!=null)){
m${new_name} = new WeakReference<${type}>((${type}) findViewById(R.id.${cursor}));
}
return m${new_name}.get();
}


Now go to your activity class and type wfind and press Ctrl+Space you will get what you want.