Hidden Gems of Faktor-IPS: @Generated REDIRECT

We are beginning a new series of blog post highlighting little known features of Faktor-IPS that might come in handy in one or another project. In this first installment, I want to introduce you to @generated REDIRECT. Although not technically a Faktor-IPS feature but one of JMerge, the tool Faktor-IPS uses to merge the Java code it generates with handwritten changes.

If you ever had the pleasure of Faktor-IPS generating most of the previously handwritten boilerplate code for you, you can probably remember some cases where you had to add your own code to the generated code (getEffectiveFromAsCalendar() anyone?). Then you saved the file, edited some unrelated part of the Faktor-IPS object and the handwritten code was gone. Just because you forgot to add a NOT to @generated. And if you wanted to actually use the generated code, but just add your own code before or after (like additional checks or listener calls) it was fine as long as you didn’t change the underlying Faktor-IPS model. But if you did change it, not even the originally generated parts in your @generated NOT method would be adapted.

Well, here comes @generated REDIRECT to the rescue. It lets you keep the generated code intact and changeable by the Faktor-IPS code generator and still add your own code. To use it, just follow these four easy steps:

  1. Copy the whole generated method (including it’s @generated)
  2. Add GeneratedRedirection to the name of the copy; you may want to make it private
  3. Change @generated to @generated REDIRECT for the original method
  4. In the original method, write your own code and call the ~GeneratedRedirection where you need it

Example

Here’s an example how that might look in the model from our tutorial. There you have an attribute zipcode that is used to calculate a derived attribute ratingDistrict. So whenever setZipcode is called you want to notify all change listeners that the rating district might have changed as well.

1 Comment

  1. Philipp

    That came in handy right now… Great find at the perfect time. Cheers!

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *

*