read

I’ve talked before about enhancing the Groovy language through the metaclass extension abilities. Here’s a short post that adds a handy snippet to the Groovy String class to URL encode strings. Normally this is done through the java.net.URLEncoder class, but we are going to add it to the string class so we can do something like this.

def someValue = "This is some value"
def someKey = "key"
def result = "${someKey.encodeURL()}=${someValue.encodeURL()}"

Seems like a pretty handy feature. And doing this is super easy.

String.metaClass.encodeURL = {
   java.net.URLEncoder.encode(delegate)
}

There are a couple of tricks behind this.

  1. The metaClass attribute in Groovy is what allows us to extend the language. Pimp.
    • The encodeURL is a new method, attached as a closure.
    • The encode() method does the real work. Notice no return statement. Groovy returns the last value in the method. That would be the encoded string in this case.

And there you have it. Short, simple, and to the point. Happy coding!

Blog Logo

Adam Presley


Published

Image

Adam.Blog()

Adam Presley's blog

Back to Overview