Tuning Java analysisTuning Java analysisA knowledge base file is the mechanism used to customize specific checkers by providing Java Path checkers with more context about your code system in order to:
Using a Java knowledge base file (.jkb), you 'mark up' your source to provide these checkers with the context they need to:
Java Path knowledge base files are written in JKB format, which is a Java-like declarative language for knowledge base specification. To reduce false positives, the traceback feature in Klocwork analysis results helps you pinpoint the source to include in your .jkb file in order to achieve your objective. Tuning to reduce false positives is by far the most common tuning scenario. Tuning to reduce false negatives is more of an extensibility activity. For more information about creating your own Java Path checkers, see Creating Java Path checkers. Note: Tuning is available only for Java Path checkers.
For your tuning changes to take effect, you need to run a full build analysis. Java knowledge base (JKB)A Java knowledge base (.jkb) file is the mechanism that tunes specified Klocwork issue checkers to reduce false positives (or less commonly false negatives) in your Java analysis results. You'll also come across JKB files when you create your own Java Path checker. The JKB file contains code paths that should be added to or eliminated from the checker's consideration. Klocwork traceback can help you determine the criteria by which code paths are to be added to or eliminated from the checker's consideration. Once you identify the criteria, then you must communicate them to the engine using a JKB file that contains:
Note: Since the JKB file can be created from your project workspace, you don't have to import it.
For more information about annotations, see Java knowledge base reference. Java knowledge base tuning workflow diagram Creating a JKB file
Editing a JKB fileYou can edit a JKB file from within your project (without having to export and import it) in either of the following ways:
Tuning Java analysis in EclipseThis article shows you how to tune the analysis to your source code within Eclipse. To learn why or when to customize your analysis, see Customizing your desktop analysis. For general tuning information, see Create a Java Knowledge Base file in EclipseYou can add methods to the Java Knowledge Base (JKB) file from the editor, the Package Explorer or the Outline view.
Example: Tuning in EclipseIn the example below, NPE.RET is detected where null comes from the get() and is dereferenced at o.toString(). Klocwork doesn't recognize the call to assertNotNull as the validation method. Traceback provides more information about the issue and can help you better identify the method(s) of interest. To tune NPE.RET in Eclipse:
package junit.framework; @Bind("NPE.RET") public class Assert { public static void assertNotNull(@Check java.lang.Object object); } Next step: Re-run the analysis by selecting the project and clicking Tuning Java analysis in IntelliJ IDEAThis article explains how you can tune the Klocwork analysis from IntelliJ IDEA. To learn more about when and why to tune the analysis, see Customizing your desktop analysis. Walk-through: Handling an NPE.RET false positiveIn the example below, NPE.RET is detected where null comes from the 'get()' and is dereferenced at 'o.toString()'. NPE.RET is detected because Klocwork doesn't recognize the call to 'assertNotNull' as the validation method. Traceback provides more information about the issue and can help identify the method(s) of interest: To deal with this false positive, we add two Java Knowledge Base annotations: @Bind and @Check.
Troubleshooting JKB annotation errorsIf you see the error message, "Build contains errors", double-click the message line to open the build log in the editor. (You may need to use the horizontal scroll bar to see the detailed message in the build log). Build log messages identify JKB annotation errors. For example, "Trying to create a record not bound to a defect" indicates that the @Bind annotation is missing or improperly placed. For more information about annotations, see Java knowledge base reference. Tutorials - Tuning Java analysisTutorials 1 to 3 demonstrate the most common reason for tuning, that is, to reduce the detection of false positives. These three tutorials include:
Tutorial 4 demonstrates the least common scenario, that is, tuning a checker to find more issues than originally reported. @Source, @Sink, and @Prop are the knowledge base annotations used in this tutorial to detect a runtime exception that was otherwise going unreported. |