Expression Editor: how to extend it and contribute your own functions


Overview

In this section, we will cover some of the internal workings of the Functions Library. We will explain how it is implemented and how a developer can extend it in order to contribute his own functions in the environment.

Implementation details

Since their introduction, the expression functions APIs have been refactored. When first introduced they were a standalone project and were added to the Jaspersoft Studio distribution as jars.
Starting from the JasperReports version 5.1.2 they were moved to the JasperReport library. The main classes are part of the core library but the additional ones are contributed as separate jar produced from the jasperreports/demo/functions folder.
All implementation details and additional examples are good covered in the official JasperReports documentation, therefore give a look at the following reference page before proceed. It will help you in having a good understanding of what we will cover in the next section.

Creating a Functions Library skeleton

In order to make the developer's life more easy, we created a dedicated wizard in Jaspersoft Studio which has the purpose of preparing the base skeleton for having a new functions library ready to use.

There are some requirements in order to proceed:

  • be sure to have in your JasperReport project Build-Path the "JasperReports Library"

    Figure 1. JasperReports Library classpath entry

    JasperReports Library classpath entry

  • be sure to have a Source Folder in your JasperReports project. If you don't have it create a new one, and if necessary unlock the Java Capabilities from the Jaspersoft Studio preferences.

    Figure 2. Enabling Java Capabilities

    Enabling Java Capabilities

    Figure 3. Adding a source folder to the project

    Adding a source folder to the project

After having verified these few requirements, you can proceed by invoking the wizard File > New > Other... > Jaspersoft Studio > Functions Library.

Figure 4. Invoking 'Functions Library' wizard

Invoking 'Functions Library' wizard


In the first page you will have to enter some basic information:

Figure 5. 'Function Library' wizard information page

'Function Library' wizard information page


  1. the Library Name should be a class-name like identifier and it will be used to generate the class file containing your functions;
  2. the Source folder is the chosen Build-Path entry;
  3. the Package is the Java package where most of the generated output files will be placed;
  4. the Category Label will be the text shown in the Expression Editor categories list;
  5. the Category Description is an additional details regarding the category
  6. the Category Class is automatically suggested based on the chosen label;
  7. the automatic creation of sample methods and an example report;

The second (optional) page allows the user to define more additional categories to which the library functions will belong to.

Figure 6. Additional categories definition page

Additional categories definition page


After completing the wizard (click on Finish button) you will end-up having the skeleton that is useful to start implementing your own functions. The sample methods serve only as a quick and simple example. Again, for all the details please refer to the official JasperReports documentation as exposed in the above section.

Figure 7. Generated files

Generated files


If you double click on a text-field in order to bring-up the expression editor you can see how the sample functions are presented.

Figure 8. New samples functions in expression editor

New samples functions in expression editor


Conclusions

As you can see the creation of new functions library was made more easy introducing this wizard. All the foundation files will be ready just after one simple step. Moreover having the ability to edit Java classes and properties files directly inside your Jaspersoft Studio installation, allows you to quickly test your functions. You can develop without the need to generate every time the jar package to be included on the Build-Path. You can modify your library and proceed with internationalization support, test and once all is done export the jar file using the dedicated standard wizard.