Thursday, February 7, 2013

ADF BC Groovy with Java Imports

ADF BC Groovy expressions support is really great and often saves a lot of time in development of complex functionalities. It comes very handy when retrieving referenced data or calling custom ADF BC methods. There is one more hidden gem offered by ADF BC Groovy - option to import Java packages directly into Groovy and use Java functionality.

Here is the example - I'm importing java.text.SimpleDateFormat directly from Groovy expression and then using Java formatting to retrieve month and year portion from current date. This expression is defined for default value of the transient attribute:


Simple and no headache - it just works:


Here you can download sample application for this post - AdvancedGroovyApp.zip.

8 comments:

Anonymous said...

thank you

Anonymous said...

Great discovery Andrejus, never would have thought of that. Can be very helpfull.

Thx.

Filip Huysmans

Andrej Baranovskij said...

Sounds good Filip ! :)

Andrejus

Anonymous said...

Hi! wonderful post. does this works on any jdev version ? we are not currently working with the last release. we are on 11.1.1.4

Andrej Baranovskij said...

I think it should work with any version. But I was testing only with the latest JDev.

Andrejus

Unknown said...

Hi Andrejus,

This doesn't work with JDeveloper 12c (12.1.3.0.0) - you get the following error:

Unexpected exception caught: org.codehaus.groovy.control.MultipleCompilationErrorsException, msg=startup failed:
General error during semantic analysis: JBO-25152: Calling the constructor for class java.text.SimpleDateFormat is not permitted.

Andrej Baranovskij said...

It could be a bug in 12c. I will need to test, on my todo list.

Regards,
Andrejus

Neha said...

I am also getting
General error during semantic analysis: JBO-25152: Calling the constructor for class java.text.SimpleDateFormat is not permitted when using below in the expression in the EO attribute
Date firstDayofMonth = calendar.getTime();
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
return sdf.format(firstDayofMonth);
I am in Jdev12.2.1
Did you get a chance to see if it is a bug?