Java code Analytic/Metrics tool - java

I am looking for a free Java code analysis/metrics tool that I can use to see class dependencies, method execution times, etc, and possibly to print out a diagram that shows them. I am currently using a jdepend task in ANT to achieve this, but I'm thinking there must be a better tool for that sort of thing. I would also prefer if it was integrated with Netbeans, since I'd rather not port all my projects to Eclipse for the task of analyzing. I have tried to play with the community version of Visual Paradigm, but I got sick of it really fast when it didn't offer code synchronization in the community edition. Although I can give it another shot if that is indeed the way to go. I also tried BOUML, but it seems to be more of a UML design tool than an existing code analyzer. So, my question is, what do you guys use for Java code analysis? Thanks!
EDIT: For instance, JDepend measures a few metrics and can draw a nice little dependency graph, but it doesn't exactly have a UI or anything. I guess I'm looking for a tool that can draw out all my classes and their dependencies on each other (jdepend only does packages AFAIK) as well as the methods that are called between the classes and provides a metric number of "how good the code structure is". I apologize if the question is vague, I'm just looking for a variety of options and I don't exactly have a lot of experience with code metrics tools...

As #Victor has mentioned Sonar is a very good tool for static code analysis. It produces graphs, charts, and generally allows you to slice and dice your Java code analysis in multiple ways.
However, it does run as a server, which can require privileges on your machine/network that you might not have. I use it myself, and I recommend it, but it's not always practical. So let's take a look at some of the tools that it uses underneath the hood.
First, there's FindBugs. As the name implies, it helps you find bugs in your Java source code.
Next, there's PMD. It helps find bugs in different ways from FindBugs.
Third, there's CheckStyle. It helps ensure that your code conforms to certain style guidelines.
Finally, there's Cobertura, which instruments your Java bytecodes and analyzes which source code lines are exercised by your unit tests (you do have unit tests, right?)
This is not an exhaustive list of the tools that Sonar employs, but it covers what seem to be the highlights. Inside Sonar, these are all 100% configurable. Outside Sonar, they're still configurable, but you better like XML.

Sonar. Though, personally I've never used it, so I can't answer questions about details. However, I know that our conitnuos building tool is integrated with it to make pretty pictures :)

Related

What is the difference between wala and soot?

I need to chose a framework for following tasks in Java:
extract control flow graph
interprocedural and intraprocedural analysis
dataflow analysis
PDG
different souce code analysis tasks (like method body extraction, test code extraction)
Which framework would be a good fit for my tasks?
I came across so many different tools apart from wala, soot as well like JavaParser, Spoon, to name a few.
Which framework should I chose? Ideally I would like to adopt a tool that is easier to use. Additionally, my expectation was given the popularity of Java tools should already exist for all these tasks. Or my understanding is wrong?
I will appreciate it if anyone please point me to different resources etc.
Spoon would be a good fit for many tasks on your list, and it is quite easy to use. It is primarily used for source code transformation and analysis with the official documentation at http://spoon.gforge.inria.fr/. For control and data flow analysis there is the spoon-control-flow package which is based on Spoon.
I used Soot many times, for different things. I am 100% sure that Soot extracts control-flow graphs, and performs intraprocedural and dataflow analysis. They really do have a good community and good people who are willing to help you out. However, I am not quite sure if you can extract PDG or SDG with Soot. I read papers that claim they did it with Soot, but there are no source codes or examples that I was able to find (maybe you have to do some modification, I don't know). But I also do know that you can perform an interprocedural analysis with Soot as well.
Here are some tutorials for Soot. If you have any questions I recommend you to join their mail group and ask questions if you have any.
For WALA, I was also in a need of a PDG since that in its wiki it says you can extract a PDGs. Even though when you import the WALA project there are some example codes in it that you might want to look at. However, I was never able to get it running, because there wasn't enough documentation. Most of its documentation is pretty old.
Here are some tutorials for WALA.
Here is also a dataflow analysis test case code that they have. Maybe it can give you some insight. I strongly recommend you to look into their test cases, it might help.
Briefly, I believe you can all of the things you asked with Soot and WALA. However, personally, I was mostly able to do them with Soot. I guess it is more of a matter of how familiar get with them and how much you like one of them.

How can I enforce documentation in Java code?

What options are there for enforcing that code is documented? I want something that will run as part of the build process e.g. a maven target that will check that code is documented (class level and method level) and report if any code is missing documentation.
Have looked at FindBugs, PMD, and CheckStyle, but they dont appear to offer this capability.
Well, I hate to tell you this, but useful documentation cannot be enforced using tools.
Checkstyle can check if Javadoc is present, but it cannot check that the Javadoc contains anything other then #param pMyParam the myParam and other meaningless junk. Even if you let some tool check the contents of the docs, this would produce loads of false positives and would lead to simple, but useful comments to be inflated only to please the checking tool. After some time, the developers will learn to filter out Javadocs when reading code like we filter out ads when reading a web page. So, all tooling will gain you nothing if the developers do not want to write good and helpful docs.
To say it with the words of Antoine de Saint-Exupéry: If you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea.
My recommendation is: Use checkstyle to check for the very basics, such as the fact that each class has at least a type comment, and that interfaces document their methods as well. Then, educate the developers where necessary on what makes a meaningful and useful documentation, and make it clear that in the eyes of the development lead, good quality code has good quality docs. Javadocs can be very good even if certain methods are not documented at all. The checking can then only be done by manual inspection, e.g. by peer reviews or some kind of formalized step in the quality control process.
Just my two cents.
Perhaps it is not exactly what you are looking for but you can use Sonar and call it when you compile the code.
Sonar will provide with lots of information included checkstyle
Sonar is very easy to use and integrate with a maven project.
Doc: http://docs.sonarqube.org/display/SONAR/Installing+and+Configuring+Maven
Teamscale can check whether comments are missing. It can also assess the quality of the comments and reveal trivial as well as unrelated comments.
Disclaimer: I am a Teamscale developer.
A while ago there was this doclet from Sun called DocCheck. Creates a report from the javadoc. Don't know if its still available..
Do a google search.
Ok, google search... works with version 1.2, 1.3 and 1.4. Its experimental and appears to be inactive. Is it worth using now? I don't know. That's something for you to decide.
javadoc doccheck download http://192.9.162.55/j2se/javadoc/doccheck/index.html

Tool that finds pieces of code without thorough coverage of exceptional states

I would like to find a good tool that will analyze my code and find possible problems with missing exceptional states handling, like null checking, numerical overflows ect..
Any recommendations?
Edit: this was retlated to testing too i.e. how to find spots that are weakly covered by tests.
Personally, I use PMD and Findbugs:
http://pmd.sourceforge.net/
http://findbugs.sourceforge.net/
There's a lot of overlap between those two, but you can configure custom rulesets and rules for both of them, and you can also code your own rules if you want.
Unfortunately, what any static analysis tools can do with regards to finding potential problems with exceptional states is somewhat limited. Having said that, both of these tools has pointed out problems in code I've worked on that would have wasted a lot of time to debug if a client had found the problem after we released.
[EDIT] To cover the dynamic part of the code, use EclEmma and unit tests. EclEmma uses Emma internally, which you could use without Eclipse as well.
A good IDE like Netbeans or Eclipse should do that as wide as it's possible. The Compilers messages should help, too.
The rest is up to you (that means: testing, testing and testing).

How to refactor thousands of lines of Java code? Is there any tool available?

In our application we have two or three classes which contains the entire Java Swing application logic. These two or three classes contain around 7k lines of code.
Now I have been assigned the task to refactor this Java code.
How do I start? Is there any tool available that will do the refactoring or at least guide us?
I'd recommend Eclipse - the brilliant Java IDE for the editing and refactoring. It has several tools for refactoring. An excellent tutorial on how to do it with Eclipse is located at:
http://www.cs.umanitoba.ca/~eclipse/13-Refactoring.pdf
There's a brililant article on the power of refactoring with Eclipse, if you're not yet convinced, at:
http://www.eclipse.org/articles/article.php?file=Article-Unleashing-the-Power-of-Refactoring/index.html
And finally another article on how to refactor in Eclipse, including techniques and tools, is available at:
http://www.ibm.com/developerworks/opensource/library/os-ecref/
There's also another stackoverflow question on strategies for refactoring Java code that you may be interested in:
https://stackoverflow.com/questions/128498/what-are-the-best-code-refactoring-strategies
Hope that helps, good luck!
I assume that you are trying to break up these large classes into smaller ones. The most common way to do this is with the Extract Class refactoring. It just happens that this is a major topic in my PhD thesis work.
One of the hard parts is deciding what goes into the new classes. There are two publicly available tools that I know of that help - ExtC (my tool) and JDeodorant. Both are Eclipse plug-ins, and I would classify both as being prototypes. If you want to try to use my tool, I'll be glad to help.
Once you decide what should go into the new class, you have to do the actual work of separating the class into others. Eclipse's Extract Class refactoring is misnamed and isn't really helpful. IntelliJ's IDEA is much better, but still has some bugs. JDeodorant can also perform the split, but it also has some bugs.
IntelliJ has all the smarts for understanding Java code and provides excellent refactorings. And now there is a free and open source version.
Eclipse has some built-in refactoring tools. You could refactor method's signatures, extract interfaces and classes, pull methods up and down in the hierarchy tree, move packages ... and all that just by two clicks.
Also, you could start with a Martin Fowler book "Refactoring: Improving the Design of Existing Code".
As refactoring code relies primarily on the developer (assisted by tooling), your IDE is a very important tool when it comes to refactoring.
Both Eclipse and IntelliJ IDEA have plenty of refactoring support.
For an overview, checkout:
http://www.jetbrains.com/idea/features/refactoring.html
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/concepts/concept-refactoring.htm
I have created my own refactoring tool that tries and group together methods that use the same set of variables. It is very much an early prototype. It is only available as a Windows Eclipse plugin.
Variable Usage Eclipse Plugin

Looking for Java code beautifier (or maybe not beautify, but something to make code more elegant) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I don't know how to describe it, but I'm looking for a tool that makes suggestions to my code on how to be... more elegant.
For example to make a nested if() statement a switch statement. something like this.
As I don't know how to describe my problem I don't know what to search for.
I thing code beautifier do not specifically change statements, right?
Have a look at IntelliJ IDEA "Intentions":
http://blogs.jetbrains.com/idea/tag/intentions/
http://www.jetbrains.com/idea/documentation/intentions.jsp
You might want to have a look into Netbeans. It provides code hints, which point out common practices and mistakes.
Except for the auto-formatting and style checking your IDE provides, you may be interested in a static code analysis tool like Crap4J, or something from this list of SCA tools for Java. They help you improve the readability of your code, and warn you when you use error-prone constructs. There are IDE plugins for most tools to let you find bad code as you write it.
You could use a tool like emmaecl, which integrates nicely with Eclipse.
You could also use something like Sonar which does quality analysis on your sources and provides metrics and hints on how to improve the code. Sonar is a web service you install. It would integrate nicely with an automated build tool like Hudson, but that's not required.
Have a look at FindBugs and PMD for improving the quality of your Java source codes.
Have a look at this post for beautifying your Java source codes.
You seem to be looking for a tool that will help you improve not your code's visual appearance and consistency (this is what a beautifier does), but its higher-level structure. FindBugs is your friend. Besides locating real bugs in your code, it will also identify more than 80 bad coding practices and more than 60 dodgy constructs (see this list).
You can use Artistic Style (works for C, C++, C# and Java) which has many options so you can set-up style you like.
You can use it either from command line or if you look hard you might find plugins for the mainstream IDEs (For what I know, it's integrated in KDevelop and there is plugin for Qt Creator).
Most IDEs will have formatting capability. Eclipse, which is a free, OpenSource IDE, will format Java soruce files
Eclipse can format your code.
You can customize it here: Window/Preferences/Java/Code Style
Maybe static code analysis tools like Findbugs or PMD could interesst you.
Eclipse has a code "Clean Up" functionality. You can find it under
Window -> Preferences -> Java -> Code Style - Clean Up.
The nice thing about this is that you can check this preferences in with your project in SVN. This way if you have multiple people collaborating on a project you just define your code Clean Up options and maybe your Formatter options. This goes a long way to not having to define everything in coding guidelines (they are still necessary, but there's a lot of stuff you can now define via those options).
You might also want to explore Checkstyle.
If using the IDE, then at least Netbeans and Eclipse both have functionality to clean up and reformat code.
Netbeans allows you to configure the code formatting to quite detailed level, i.e. including line break before the curly braces or not. Breaking long lines etc. Settings/Preferences->Editor->Formatting.
After configuration you just use ctrl+shift+f (cmd+shift+f) on your open file and all the crap gets automagically cleaned up.
I'm pretty certain the same thing is available for Eclipse also. Haven't dug into the configuration part, but formatting at least was available and was reformatting and cleaning up the code pretty neatly.
From commercial standalone tools jindent does the trick http://www.jindent.com/ for example.
I'd look at Sonar if I were you (http://sonarsource.org), it uses many of the aforementioned tools, such as PMD, Checkstyle and Findbugs and gives you an intuitive overview of the quality of your code.

Categories