Wednesday, February 18, 2009

Groovy import aliasing

One of the Groovy features I've discovered recently is "import aliasing". For instance, You have two classes with the same name located in two different packages. When You would like to use them together there will be conflict and to resolve it You have to use one of the mentioned classed with full package name. If someone faced such situation You know how inconvenient it is. Groovy helps to solve such situation using "import aliasing": there is an opportunity to set an alias for every import in Your class. For instance, You want to use two different Calendar classes. Let's create alias for own Calendar entity:

package com.mycompany.myorganization.core.service

import java.util.Calendar
import com.mycompany.myorganization.core.domain.Calendar
as MyCalendar

// Some other imports.

class UserService {
// Using both Calendars.
// Exampe of using own Calendar entity:
// MyCalendar myCalendar = new MyCalendar()

}

Another situation when You can use aliasing is long package names.
That's all. Enjoy it!

7 comments:

  1. Great way to change the namespace thank you

    ReplyDelete
  2. NAYETH!! I HAVETH NOT SHARTETH MINE OWN PANTALOONS!

    ReplyDelete
  3. hmmm yes, perhaps allowing users to comment anonymously was not the best of choices hmm yessss.
    Penis 4

    ReplyDelete