How to use Java 5 expressions in a report?

Java 5 enhancements such as autoboxing/unboxing can be leveraged to make report expressions more concise and simpler to understand.

For instance, the following JRXML fragment

<parameter name="A" class="java.lang.Integer">
  <defaultValueExpression>
    new Integer(3)
  </defaultValueExpression>
</parameter>
...
<textField>
  <reportElement x="280" y="150" width="200" height="35">
    <printWhenExpression>
      Boolean.valueOf($P{A} != null)
    </printWhenExpression>
  </reportElement>
  <textFieldExpression class="java.lang.Integer">
    new Integer($P{A}.intValue() * 2)
  </textFieldExpression>
</textField>

would look like this when using autoboxing/unboxing:

<parameter name="A" class="java.lang.Integer">
  <defaultValueExpression>3</defaultValueExpression>
</parameter>
...
<textField>
  <reportElement x="280" y="150" width="200" height="35">
    <printWhenExpression>
      $P{A} != null
    </printWhenExpression>
  </reportElement>
  <textFieldExpression class="java.lang.Integer">
    $P{A} * 2
  </textFieldExpression>
</textField>

To use Java 5 syntax in the report expressions, you need to make sure that the report compiler compiles Java 5 code.

JDK-based report compilers automatically compile Java 5 code when using JDK 5 or newer.

The JDT-based report compiler, which is recommended when compiling reports at runtime (see this FAQ), needs to be configured to compile Java 5 code. This can be done by including the following properties in the jasperreports.properties file (which needs to be placed on the application's classpath):

org.eclipse.jdt.core.compiler.source=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.codegen.TargetPlatform=1.5
Feedback
randomness