AppPerfect Code Analyzer 


Tutorial : Static Code Analysis of a Project

This tutorial provides a brief introduction to the Code Analyzer product of the AppPerfect DevTest4J using a set of hands-on practice exercises. This tutorial assumes you have successfully downloaded and installed AppPerfect DevTest4J on your machine with the default options. Apart from this pre-requisite, this tutorial is self contained. Instructions given below are Windows-specific; if you are using a non-Windows machine, please use equivalent commands/instructions.

This document is divided into following sections

  1. Creating Common Project
  2. AppPerfect Code Analyzer

Within each section, multiple exercises are listed. Each exercise forms a logical unit which is expected to take no more than a few minutes to perform. All exercises use the PetStore demo application running inside Apache Tomcat. The modified PetStore application and Tomcat are both bundled with this product.

All exercises assume you have installed the product in C:\AppPerfect\DevTest folder and will be referred as DEVTEST_HOME henceforth in tutorial. If you have installed the product in some other folder, modify the instructions below appropriately.

This tutorial is not intended as a comprehensive training guide. Please refer to the product documentation for detailed information. However, this tutorial will give you a very good overview of the products and substantially improve your productivity with the product.

Creating Common Project

In AppPerfect DevTest4J, you can create a common project and use the same project to perform tests in all the bundled products.

Exercise 1: Launch AppPerfect DevTest4J

Action:

  1. Click on Start -> Programs ->AppPerfect DevTest4J x.x.x -> AppPerfect DevTest4J
  2. On launching AppPerfect DevTest4J a Welcome page will be displayed. Go through the brief description given for each product and click on the product icon to view its perspective.
    NB: Welcome page is displayed only when DevTest4J x.x.x is launched and last time no project was opened.
  3. To switch between different product perspectives click on corresponding project button in toolsbar.

Exercise 2: Creating a Common Project

Action:

  1. Launch the Common Project Wizard by clicking File ->New... menu option. The New Project wizard will be launched.
  2. Go through the instruction provided on top of the General tab.
  3. Keep the default project name and location for the purpose of this exercise. We don't have to provide "Notification" settings or "Remote Application/AppServer" settings for this exercise. Click on the Next button.
  4. For this exercise we don't need to import any IDE project. Click on the Next button.
  5. In the Source tab click on the "Add File" button on the r.h.s., select "Files of type:" as *.jsp in the Open dialog and add jsp files from the location "DEVTEST_HOME\tomcat\webapps\petstore"
  6. Now click on the "Add Folder" button on the r.h.s. and select the folder "DEVTEST_HOME\tomcat\webapps\petstore\src". Click the Next button.
  7. Use the default JDK which is bundled with AppPerfect DevTest4J and click on the Next button.
  8. In the "Environment" tab we will provide the classpath for the sources added in the project.
  9. Click on the "Add File" button on the r.h.s. and navigate to the location - "DEVTEST_HOME\tomcat\webapps\petstore\WEB-INF\lib" - select all the jars present in the "lib" folder.
  10. Click on "Verify Classpath" button to validate the classpath.
  11. Classpath validation dialog will be launched and the classpath will be verified. A message saying that the classpath specified is correct should be displayed. Click on the "OK" button. Click on the Next button.
  12. In the "Target" tab select project type as "SERVER".
  13. Keep the default URL in the "Starting URL" field i.e., "http://localhost:8080/petstore". Select the "AppServers Settings" tab.
  14. Select the "Specify AppServer Settings" checkbox and select "Tomcat_5.x" from the drop down menu.
    NB: This is required only if you want to launch AppServer automatically while profiling, recording and replaying in AppPerfect Unit Tester or if you want to profile AppServer in AppPerfect Java Profiler.
  15. Specify the Server Home path - "DEVTEST_HOME\tomcat" and the "Startup file" for the server - "DEVTEST_HOME\tomcat\bin\catalina.bat".
  16. Select the "Launch server automatically (When required)" checkbox. Click on the Finish button.
  17. If applications asks for Starting URL validation, click on 'No' button. Normally Confirmation for Starting URL validation is not asked when "Launch server automatically (When required)" is checked. A confirmation message saying that the project is saved will be displayed. Click on the OK button.

Now using this common project we will create tests to demonstrate the functionalities of AppPerfect Code Analyzer product.

AppPerfect Code Analyzer

NB:Please follow the steps provided in the "Creating Common Project" section to first create a common project, then proceed further.

Exercise 1: Define a Code Analyzer project

Action:

  1. Once the common project is successfully created another dialog - Define Project Properties - will be displayed.
  2. Read the instructions at top of each tab.
  3. In the General tab select analyze JSP files and specify following path - "DEVTEST_HOME\tomcat\webapps\petstore" as Document Root folder.
  4. In the Rules tab, we need to choose the rules that need to be applied to the selected source files. Scroll through and review all the rules. For this excercise, apart from the selected default rules we will select one more rule viz.,'Declare_variable_final'. To do this, right click on any of the rule and select 'Find...' option from the popup menu. Type the rule name 'Declare_variable_final' in the 'Find Node:' text field. The rule will be listed below. Select the rule from the list. The rule will be highlighted in the Rules tree. Click the Close button to close the Find dialog. Mark check the rule for it to be applied for the test. Select Auto-Fix tab.
  5. In this tab, we need to choose the rules which should auto fix their violations. Right click on any of the rule and select 'Find...' option from the popup menu. Type the rule name 'Declare_variable_final' in the 'Find Node:' text field. The rule will be listed below. Select the rule from the list. The rule will be highlighted in the Rules tree. Click the Close button to close the Find dialog. Mark check the rule for it to be applied for the test. Keep the default options selected related to prompting before auto-fixing the rule and backup of your original code. The backup of your code is stored as a .java.BAK file in the same folder as the original file. Click OK.
  6. Now all the JSP & Java files which were added to the Source in the Common Project Wizard will be displayed in the Source tab.
  7. Click through all the menu items to familiarize yourself with the available features and how to access them. Viewing through all menu items will provide a reasonable overview of the application.
  8. The central concept of code analysis is Java coding rules. AppPerfect Code Analyzer defines over 750 coding rules that are applied to your source code to identify issues. These rules can be viewed by clicking on Project -> Rules Manager... menu item. Detailed description, sample code, etc. are provided.
  9. Click on Tools ->Options... menu item. Click on the JDKs tab and ensure that the JDK path has been set correctly. This is the path provided for JDK during installation of AppPerfect DevTest4J. You may modify the path or add new JDK through this dialog box. It is critical that a correct version of JDK is available for AppPerfect DevTest4J to perform correctly.
  10. Click Help -> Table of Contents menu item to see AppPerfect DevTest4J product documentation.

Exercise 2: Perform Code Analysis, View and Analyze results

Action:

  1. To analyze the project, click on Project -> Run. As source files are being analyzed, output is displayed in the "Code Analyzer Console" screen.
  2. Auto-Fix confirmation dialog will be displayed showing the Rule being violated, a short description for the rule, Original violated code and a suggested fix for the violation. Click on "Stop auto-fixing" button of the dialog. We will not Auto-Fix for this project.
  3. As rules violations are detected, they are added to the list in the main Window. Once the analysis is completed, you will see 'Finished Analyzing' in the status bar at the bottom.
  4. To view rules violations based on source files, click on Source tab at top left and select any of the files in the tree on the left. Files with rule violations are marked with a red flag mark. Files with no rule violations are marked with a green flag mark.
  5. To view rules violations based on rules, click on Rules tab at top left and select any of the rules in the tree on the left. Rules which were violated are marked with a red flag mark. Rules which were not violated are marked with a green flag mark. Click on any of the rules to see details about which files violated that rule.
  6. To view other available results, click on the other tabs shown on the right hand side view.
  7. We recommend that users fix rules violations based on the rules violation severity. Hence, fix "Critical" category rules violations first, then move on to "High", etc.

Exercise 3: Perform Auto-Fix

Action:

  1. Auto-Fix can be done only for Java files, it is not supported for JSP files.
  2. Select CheckForm.java in the Source tab. The Violated rules are displayed in the Violations tab of the main panel on the r.h.s. frame.
  3. Right click on the rule "Declare_variable_final" and select Fix Rule Violations -> This Line.
  4. A Auto-fix confirmation dialog will be displayed showing the Rule being violated, a short description for the rule, Original violated code and a suggested fix for the violation. Click Ok.
  5. Now select the Auto fixes tab. The auto fix details will be displayed here.
  6. Right click on the Auto fixed code in the Auto fix tab. Select Restore File option to restore the original file. This will revert the changes done by Auto fix. Now, you can see the violated rule for the line in the violations tab.

Exercise 4: AppPerfect Analytics

Action:

  1. All AppPerfect DevTest4J products (except AppPerfect Java Profiler) comes bundled with a data reporting/analysis tool called AppPerfect Analytics.
  2. Click on Tools ->Analytics... to invoke AppPerfect Analytics
  3. Each product has a number of useful, pre-defined reports already available to analyze results data. To view any of the reports, click on the report name on the left side of the window and select File -> Preview from the menubar.
  4. You can also define custom reports by clicking on File -> New Report... in the menubar. AppPerfect Analytics usage is not currently covered by this tutorial. Please refer to product documentation for additional details.