Hidden Annotation in Groovy Validators

There is another update to the Groovy Validators. (I know I keep saying I am done, but like The Godfather, I keep getting pulled back in.)

I made a new annotation @Hidden. Here is the summary from the groovydoc:

The purpose of this annotation is to help you keep your private fields private.

Suppose you have a field in an object you want to change. In that case, making it final will not work. But you also do not want code outside the object to change it. Making it private won’t work in Groovy either. You could write a setter that takes an argument and does nothing with it, over and over. Or you could use this annotation.

Here is an example on how to use it:

It is said you are only as old as you feel. Suppose going on a yoga retreat helps you feel a year younger, but visiting your in-laws makes you feel a year older. (One way to avoid this is to not marry a Scala programmer.)

class AgeHolder
    @Hidden
    int perceivedAge
    
    AgeHolder( argAge ) {
        this.perceivedAge = argAge
    }
    
    def visitYogaRetreat() {
        perceivedAge--
    }
    
    def visitInLaws() {
        perceivedAge++
    }
}

You’re welcome.

Image from Wikimedia, assumed allowed under Fair Use. Image from the Rabbula Gospels, a 6th century Syriac Gospel manuscript.

 

2 thoughts on “Hidden Annotation in Groovy Validators”

  1. Nice one, but I have a few questions:
    1. What if I modify the variable in code. Will it fail during compilation or execution?
    2. IDE support. Will IDEA code complete suggest me the filed or it will hidden?

    • Thanks for the response.

      Honestly, you might have to try this yourself to see what happens. You should be able to modify the variable. It’s not a final field, so I don’t think anything bad should happen. The field should be modifiable from within the object itself. You could still call the setter from outside the object, but the field should not be changed.

      WRT IDE support: I have just been working on this with Gradle. I don’t have or use an IDE at the moment. If this gains any traction, I might look into it.

Comments are closed.