Adding new functions to expression editor according to the tutorial

0

I tried to add new function to my Report in Jaspersoft Studiop according to the tutorial: Expression Editor how to extend it and contribute your own functions.

With this Method Jaspersoft studio adds some example Function too and I wanted to begin by adding my functionen to the .java files provided but it doesn't work.

Either my expressions don't show up or the expression editor does not work at all. I even had to reinstall the JRE to fix the expression editor after one attempt.

 

What I tried:

-rebuilding the whole Report directory by pressing ctrl+b

-constructing it completely from scratch

-Adding it to one of the files provided by the wizard (New->other->Funtion package) in the source folder

 

I think it has something to do with the compiling process but I#m not sure and I don't know how to make Jaspersoft studio to compile my files.

 

My code and my outline below. ROUND_MW is the function I wanted to add. The function is far from complete but it should be valid code.

package unilimeFunctions;
 
import net.sf.jasperreports.functions.annotations.Function;
import net.sf.jasperreports.functions.annotations.FunctionCategories;
import net.sf.jasperreports.functions.annotations.FunctionParameter;
import net.sf.jasperreports.functions.annotations.FunctionParameters;
import java.text.DecimalFormat;
 
@FunctionCategories({ UnilimeReportFunctions.class })
public class UnilimeFunctions {
 
    @Function("ROUND_MW")
    @FunctionParameters({ @FunctionParameter("DecimalNumber"), @FunctionParameter("DecimalPlaces")})
    public static Double ROUND_MW(Double DecimalNumber, Integer DecimalPlaces) {
        DecimalFormat df = new DecimalFormat();
        return 15.0;    
    }
 
    @Function("HELLO_WORLD_MSG")
    public static String HELLO_WORLD_MSG() {
        return "Hello World!";
    }
 
    @Function("REPEAT_MSG")
    @FunctionParameters({ @FunctionParameter("message"), @FunctionParameter("times"),
            @FunctionParameter("forceUpperCase") })
    public static String REPEAT_MSG(String message, Integer times) {
        return REPEAT_MSG(message, times, false);
    }
 
    public static String REPEAT_MSG(String message, Integer times, Boolean forceUpperCase) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < times; i++) {
            sb.append(message).append("\n");
        }
        String txt = sb.toString();
        if (forceUpperCase) {
            txt = txt.toUpperCase();
        }
        return txt;
    }
}

I would really aprecciate any kind of help. Even where I have to start looking

anderl's picture
2
Joined: Mar 8 2018 - 4:50am
Last seen: 2 months 6 days ago

Thank you for your response. I'm using Jaspersoft Studio 6.5.1.

The first java file named unilimeFunctions:

package unilimeFunctions;
 
import net.sf.jasperreports.functions.annotations.Function;
import net.sf.jasperreports.functions.annotations.FunctionCategories;
import net.sf.jasperreports.functions.annotations.FunctionParameter;
import net.sf.jasperreports.functions.annotations.FunctionParameters;
import java.text.DecimalFormat;
 
@FunctionCategories({ UnilimeReportFunctions.class })
public class UnilimeFunctions {
 
    @Function("ROUND_MW")
    @FunctionParameters({ @FunctionParameter("DecimalNumber"), @FunctionParameter("DecimalPlaces")})
    public static Double ROUND_MW(Double DecimalNumber, Integer DecimalPlaces) {
        DecimalFormat df = new DecimalFormat();
        return 15.0;    
    }
 
    @Function("HELLO_WORLD_MSG")
    public static String HELLO_WORLD_MSG() {
        return "Hello World!";
    }
 
    @Function("REPEAT_MSG")
    @FunctionParameters({ @FunctionParameter("message"), @FunctionParameter("times"),
            @FunctionParameter("forceUpperCase") })
    public static String REPEAT_MSG(String message, Integer times) {
        return REPEAT_MSG(message, times, false);
    }
 
    public static String REPEAT_MSG(String message, Integer times, Boolean forceUpperCase) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < times; i++) {
            sb.append(message).append("\n");
        }
        String txt = sb.toString();
        if (forceUpperCase) {
            txt = txt.toUpperCase();
        }
        return txt;
    }
}

The second one, unilimeReportFunctions, is empty.

As mentioned before: This is the output that I got from following the tutorial. I only added the function ROUND_MW

anderl - 1 year 8 months ago

Did you modify the dedicated message properties? What do you mean with "The second one, unilimeReportFunctions, is empty."?
I think it's better you attach a zip with your sample project. 

Thanks.
Regards,
Massimo.

mrabbi - 1 year 8 months ago

Can you detail which version of Studio you are using? Could you provide both the Java files. I see errors in the category one too.

Regards,
Massimo.

mrabbi - 1 year 8 months ago
show 1 more...

Here is a link to the zip Archive:

https://we.tl/yQRoQ8re1f

anderl - 1 year 8 months ago

0 Answers:

No answers yet
Feedback
randomness