Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

Java custom functions

When creating checkers with custom functions, use the --kast-library option of kwcreatechecker. This will generate a MyFunc.java file to contain your plug-in source, and a plugin_functions.xml file to register the custom functions contained in the jar.

Add your source to the MyFunc.java file

Each custom Java function should be implemented as a separate class, extending one of three abstract classes: CustomIntFunction, CustomBoolFunction or CustomTreeFunction. Every such class should have an associated name, which corresponds to the function name in the KAST expression. When calling a custom function in a KAST expression, you can either specify a single argument of type 'tree' or specify no arguments at all. In the latter case, the current node will be processed.

  • If you need an integer custom function, define a new class that extends com.klocwork.jxpath.CustomIntFunction abstract class overriding its method:
    public long evaluate(com.klocwork.javaapi.jtree.TreeNode node);
  • If you need a boolean custom function, define a new class that extends com.klocwork.jxpath.CustomBoolFunction abstract class overriding its method:
    public boolean evaluate(com.klocwork.javaapi.jtree.TreeNode node);
  • If you need a custom function that returns a value of type 'tree', define a new class that extends com.klocwork.jxpath.CustomTreeFunction abstract class overriding its method:
    public com.klocwork.javaapi.jtree.TreeNode evaluate(com.klocwork.javaapi.jtree.TreeNode node);

Your custom function should be implemented in this 'evaluate' method. AST nodes can be accessed using classes from the com.klocwork.javaapi package.

plugin_functions.xml file

Classes implementing custom functions should be packed into one or several jars (plug-ins). Each jar must have a plugin_functions.xml file at the top level of its inner file hierarchy. This file registers all custom functions contained in the corresponding jar.

Note: If you will be using the default function name ("myFunc") in your KAST expression and class ("MyFunc") specified in MyFunc.java, you don't need to update these values in the plugin_functions.xml file.

The plugin_functions.xml file has the following format:

<plugin> <function name="name1" class="class1"/> <function name="name2" class="class2"/> ... </plugin>

Here name1, name2, etc. are the names used to refer to the custom function in the KAST expression, while class1, class2, etc. are fully qualified names of associated classes. For example:

<plugin> <function name="isAssignment" class="com.klocwork.plug-ins.IsAssignment"/> <function name="isShift" class="com.klocwork.plug-ins.IsShift"/> <function name="getMostLeftArg" class="com.klocwork.plug-ins.GetMostLeftArg"/> </plugin>

Test your KAST expressions

KAST expressions containing custom functions must be tested using kwjava. Checker Studio does not support custom functions.

Use the --xpath-library option to specify the name of the library that contains the functions.

The example below specifies the text file containing the KAST expression and the library name:

kwjava --xpath-file myJKAST.txt --xpath-library "myJKASTCustomFunctions.so" Testcase.java

Build the checker

To build the checker library from the checker directory (<CHECKER.CODE>), run:

ant install

This generates a <CHECKER.CODE>.zip file.

Desktop testing

Once the jars are generated, they should be placed into your <User_install>/plugins directory, where you can test them locally before they are formally deployed.

What's next?

The checker now can be deployed to the Klocwork server, in which case it will be made available to all Klocwork connected desktops when they synchronize with the server. It can also be deployed to individual desktop installations.

To learn how, see: