Wednesday 29 February 2012

Use Static Method directly without using class name



One among lot of interesting things of  java is the use of static method , which is basically a class level method and shared by all instances

Mostly we end up using static methods by referencing class name and the require method like

                                           System.out.println(String.valueOf(true));

Other way provided by Java is STATIC IMPORTS

 Same above expression can be used as following (note : I haven't been using class name here i.e. String)


                                          System.out.println(valueOf(true));


To do this you need to add a static import  as

                                          import static java.lang.String.valueOf;

So the whole code would look like


               import static java.lang.String.valueOf;


              public class StaticTestClass {
   
                         public static void main(String args[])
                        {
                           System.out.println(valueOf(true));
                        }

              }



But , this isn't a good practice because

  1. As it leads to lot of confusion when your code goes big  and makes uderstanding trouble some
  2. Lets assume you have another class which also has valueOf as static method and they you do a static import of this custom class method . This would lead to compile time ambiguous error as compiler wont be able to know which actual method to use. Error would look like :
                 The method valueOf(boolean) is ambiguous for the type StaticTestClas

So Avoid it and use class name "." method name directly.

Monday 20 February 2012

Debug Java SDK Code on Eclipse


Few days back I got stuck into situation , in which I needed to debug Java sdk code . I found few websites on goggling but none of the gave  perfect solution . So after struggling for few more days got a way to do it an thought would be useful to blog it out  .

The ingredients required for  this work are pretty basic ,

  • Eclipse (preferably indigo but it doesn't matter for sure)
  • Java SDK (preferably 6 or above) . Mostly Java SDK comes with a source code else you could download it from Oracle website. 
** Just make sure , the sdk version and source code version should be same else it would end up conflicts while debugging

Next Step would be to create a dummy Application   , I will call it FileLoadingExample .




Creation of Projects



 As you can see in the above screen shot , I have a class with name FileLoadingFromClassLoader with our main method.  In this method I am trying to load a file using class loader .

My requirement is to debug getResourceAsStream method of Class loader .

So lets dig in

There are basically 2 steps in it

  1. Import downloaded Java source code : Now this would not be straight forward as JDK source code might not come with eclipse configuration files (There are some sources from where you can get source code with .project files which can be directly used in eclipse from Import utility). Lets assume you don't have code in eclipse format , then create a dummy project as shown in above screen shot (JavaSourceCodeProject) and copy all the source code from source of JDK to src folder of this project i.e. packages copied would be com,java,javax,launcher,org,sunw and others (if any). After building project , you might find some errors , try resolving it and if you are not able to resolve it , don't worry much as this wouldst be a show stopper.

         Once this copying is done next step would be to include this project as Referenced Project for our     
         sample project  under Projects Tab and modify Order and Export tab ( our imported java code
         java code project should be above JRE )


Add Java Code project as Reference Project



Ordering of projects for sample project . (Notice our Java Code project  comes above JRE)







            Now after this step , we would be in position to go to corresponding java class in JDK source 
            code after we click on needed method (standard ctrl + Mouse click  used in eclipse ) .

           So now I ctrl + Mouse Click on method getResourceAsStream which takes me to class 
           ClassLoader.java  and I put a break point at first line of the method.


       2. In the second Step , we need to change some debugging configuration for our project. In this step
            , we need to go to debugging Configuration of our sample project .

            On Click of this , it would take us to debugging configuration  screen and then we  need to 
            go to Class Path Tab . In this tab you need to add our Java Source project under   Bootstrap 
            entries . 

           Remember : Our Java Source Project should be the first project in Bootstrap Entries
                                   above JRE System library


Going to Debug Configuration




Adding Java Code Project to Boot Strap Entries





            Now debug your sample class using eclipse debug and put break point where ever  you want in the
            java code and your debug would stop at that point and further there , you could inspect variable 
            and dig more deep if needed . As you might have error in you java project , it would give you alert 
            box asking do we need to proceed or not , just go ahead it should be fine 


Debugging java Class Loader Class