Posts

Showing posts from 2012

Create new Java Class files at runtime

When we write programs usual method is to write all the code compile it and run. But when we try to complex things some times we may want to generate a new class and use it at runtime. As an example I wanted to create a class which parse a xml message and create a new Java object from that data. This is used in web server. So that result object varies according to the web service which is deployed. The solution was to inspect the service at deployment time and create a new parser class according to that in the service deployment time. It has to be noted that this parser had to be generated as a .class file and dynamically added to the classpath. So the result was to use a bytecode manupulation library. There are few bytecode manuplulation libraries available as ASM and JavaAssist.
I used JavaAssist for this. The reason for using JavaAssist is it is simple to create a new class from the scratch using JavaAssit. When we create new class files using JavaAssit we have to create all the…

How to get the base class of a Array using java reflection.

If we use Java reflection to get the class of a object like String[][] strings; Java reflection will return [[Ljava.lang.String. But sometimes you may want to get the bases class e.g in this case java.lang.String. Following method can be used to do that. If you want to get the bass class of array object a you use this as Class c=getTypeOfArray(a.getClass()); public static Class getTypeOfArray(Class c){ Class cls=null; while(true){ if(c.isArray()){ c=c.getComponentType(); }else{ cls=c; break; } } return cls; }

GSoC 2012 with Apache Photark, A great experience.

Image
Google summer of code is one of the worlds leading opensource development programs. It is held anually by Google inc. Many enthusiastic students participate each year for this. In every year many number of students try to take part of this program. But nearly 1000 students only get selected for this event.  I was lucky to take part in 2012 program for the first time. I took part on GSoC 2012 under Apache Software Foundation for their incubator project Photark.
The interesting thing was my initial plan was to participate on another organization which develops a CMS. I also worked with them nearly one month. But then I realized what they are expecting and what I am interested is not matching. So I looked for another organization and found this interesting project. But then there was little time for application closing period.  So I quickly checcout the code base play with it for little time, drafted a proposal and submitted it. Then Mr. Suhothayan Sriskandarajah who was happened to my …

How to get current time and date using JavaScript.

Getting system date and time using JavaScript is easy. But some steps has to follow to convert it to a readable format. These functions will help you to get rid of the hassle .

//returns date in format 01/01/2012

function getCurrentDate() {
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
return (month + "/" + day + "/" + year);
}

//returns time in format 05:45 AM
function getCurrentTime() {
var time = ""; var currentTime = new Date(); var hours = currentTime.getHours(); var minutes = currentTime.getMinutes(); if (minutes < 10) { minutes = "0" + minutes; } var h; if (hours > 12) { h = hours - 12; } if (h < 10) { h = '0' + h; } time += (h + ":" + minutes + " "); if (hours > 11) { time += ("PM"); } else { time += ("AM"); } return time;
}

How to build a maven project without tests

When we build a large project using maven it takes a lot of time because of tests  runs. So we can build quickly by disabling the tests. We can do that using the following command.
mvn -Dmaven.test.skip=true install

Using Geolocation API in phone gap

I am currently developing a Phonegap application for Google summer of Code 2012. I encountered some problems when using it and this is how I corrected them.

Location not available- In android emulator does not automatically get the location. So we have to send a moc location to the emulator using DDMS perspective in eclipse.

Phonegap documentation says to use the below method to get location.

navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError);
But this gave me an error with error code 2 unable to start geolocation service. I corrected it using the below method.
navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError,{ enableHighAccuracy: true });
Note - This is only for testing in the emulator.

How to make a phone call in a Android application

This may a looks like a simple task. Actually it is a simple task. You can do it using few lines of code. But there are only few places which shows how to do it. So I decided to mention it here.


public void onCallButtonClicked(View view) {
String url = "tel:" + tpNo;
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
intent.setData(Uri.parse(url));
startActivity(intent);
}

Android: Using Google maps app to show places in your application

Image
When we are developing android application with maps functionality normally developers tries to use Google maps API for this. But if your requirement is simple it may be more easy and effective to use existing maps application by a intent. In this application I'm going to show how to show the nearest hospital in your application.
Note- To make this work your app and emulator must be created using Google APIs not android APIs. (Both are the same but if you are not using Google maps API map application functionality is not available to you).
Here is the code,
LocationManager  locMgr  = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location recentLoc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double lat = recentLoc.getLatitude();
double lon = recentLoc.getLongitude();
String geoURI = String.format("geo:%f,%f?q=hospital", lat, lon);
Uri geo = Uri.parse(geoURI);
Intent geoMap = new Intent(Intent.ACTION_VIEW, geo);
startActivity(geoMap);

In the geoUR…