Usually it happens that when you are writing a program, improper access specifier is used for a method or variable. This problem is more so because of the fact that a developer is more concerned about achieving the task at hand. If your mind is too busy in writing the correct logic for the problem, you can easily forget to check for proper access level for member variables and methods.

While manual code reviews do help in detecting and fixing these issues but it is time consuming and error prone. With the help of few tools, one can easily get suggestion for whether a variable/method should be made default, private, protected or public.

ucdetector to the rescue

ucdetector is one of such tool which gets integrated with Eclipse IDE and gives suggestions in the problem view about the code where the visibility could be changed to protected, default or private.

I got a chance to evaluate ucdetector tool. For example, consider the following code sample:

public class Sample {
	
	public int a;

	public int getA() {
		return a;
	}

	public void setA(int a) {
		this.a = a;
	}
}

In the above code, we didn’t want the variable a to be public but private. We have already provided public getter and setter for this variable hence we should have declared the variable a as private. By setting it as public int a, we are breaking OOPS concept about data hiding.
Now let us see how can ucdetector help us. Note: ucdetector stands for Unnecessary code detector.

Steps to install ucdetector

  • Download the unnecessary code detector plugin from http://sourceforge.net/project/showfiles.php?group_id=219599
  • The above step will give you a .jar file named as org.ucdetector_x.y.z.jar
  • Paste this jar into ECLIPSE_HOME/dropins directory.
  • Restart Eclipse

Other means for installing this Eclipse plugin are listed on ucdetector website. This tool can be beneficial for all kind of Java based projects. It is recommended to use ucdetector in addition to Static analysis tools like checksytyle, PMD or FindBugs.

ucdetector in action

After installing the eclipse plugin for ucdetector, Eclipse will show clear recommendations for the above program as shown below:

As expected, we are getting a suggestion to make Sample.a as private variable.

In addition to suggesting access specifiers, ucdetector also provides suggestions for dead code and methods/fields which can be final. Plus it works for Android projects too in Eclipse IDE.

Reference

1) http://stackoverflow.com/questions/2922342/dealing-with-java-default-level-access-specifiers
2) http://www.ucdetector.org/

Related Posts

Get access modifier suggestions using Eclipse ucdetector plugin admin Eclipse
Usually it happens that when you are writing a program, improper access specifier is used for a method or variable. This problem is more so because of the fact that a developer is more concerned about achieving the task at hand. If your mind is too busy in writing...
<p>Usually it happens that when you are writing a program, improper access specifier is used for a method or variable. This problem is more so because of the fact that a developer is more concerned about achieving the task at hand. If your mind is too busy in writing the correct logic for the problem, you can easily forget to check for proper access level for member variables and methods. While manual code reviews do help in detecting and fixing these issues but it is time consuming and error prone. With the help of few tools, one can easily get suggestion for whether a variable/method should be made default, private, protected or public. </p> <p> <h2>ucdetector to the rescue</h2> ucdetector is one of such tool which gets integrated with Eclipse IDE and gives suggestions in the problem view about the code where the visibility could be changed to protected, default or private.</p> I got a chance to evaluate ucdetector tool. For example, consider the following code sample: 1 <p>In the above code, we didn't want the variable a to be public but private. We have already provided public getter and setter for this variable hence we should have declared the variable a as private. By setting it as public int a, we are breaking OOPS concept about data hiding. Now let us see how can ucdetector help us. Note: ucdetector stands for Unnecessary code detector.</p> <p> <h2>Steps to install ucdetector</h2> <ul> <li> Download the unnecessary code detector plugin from http://sourceforge.net/project/showfiles.php?group_id=219599</li> <li> The above step will give you a .jar file named as org.ucdetector_x.y.z.jar</li> <li> Paste this jar into ECLIPSE_HOME/dropins directory.</li> <li> Restart Eclipse</li> </ul> Other means for installing this Eclipse plugin are listed on <a href="http://www.ucdetector.org/">ucdetector</a> website. This tool can be beneficial for all kind of Java based projects. It is recommended to use ucdetector in addition to Static analysis tools like checksytyle, PMD or FindBugs. </p> <p> <h2>ucdetector in action</h2> After installing the eclipse plugin for ucdetector, Eclipse will show clear recommendations for the above program as shown below: <img src="http://www.javaexperience.com/wp-content/uploads/2012/10/ucdetector-warnings.jpg" alt="" title="ucdetector warnings" width="343" height="414" class="alignnone size-full wp-image-1160" /> As expected, we are getting a suggestion to make Sample.a as private variable. In addition to suggesting access specifiers, ucdetector also provides suggestions for dead code and methods/fields which can be final. Plus it works for Android projects too in Eclipse IDE. </p> <h2>Reference</h2> 1) http://stackoverflow.com/questions/2922342/dealing-with-java-default-level-access-specifiers 2) http://www.ucdetector.org/
Attach JDK source in Java application in Eclipse
Profiler for Java Eclipse
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments