Posts Tagged ‘Object’

Get the hash of a string

You can get a quick hash code for a string (and all other objects) in Groovy by simply calling it’s hashCode() method. def message = ‘hello’ println message.hashCode() Which prints 99162322

Check if an object is in a class hierarchy

You can check if an object is in a class hierarchy using Groovy’s in operator. class Animal { } class Dog extends Animal { } class Cat extends Animal { }   dog = new Dog() cat = new Cat()   assert dog in Dog assert dog in Animal assert cat in Cat assert cat [...]

Dynamically add methods to a class

You can dynamically add methods to a class by adding them to the class’s metaClass. The delegate is a reference to the instance of the object that has been created. For example, to add a squared() method to the Integer class: Integer.metaClass.squared = {-> delegate.multiply(delegate) }   assert 7.squared() == 49 To add a shout() [...]

Find the properties on an object

The properties of an object can be found by examining it’s metaClass.properties class Person { String first String last }   Person.metaClass.properties.each { p -> println "${p.name} = ${p.type}" } Which prints class = class java.lang.Class first = class java.lang.String last = class java.lang.String metaClass = interface groovy.lang.MetaClass To extract more details about each property [...]

Create a mock object for an abstract class

We can create a mock object for an abstract class by creating the class but and extending it at the same time abstract class Person { def first def last abstract def type() }   // Create our mock object by providing an implementation of the abstract method def mock = new Person() { def [...]

Safely dereference null objects

Groovy provides a shortcut technique for dereferencing null objects. Suppose we have the following code: class Person { Address address }   class Address { String street }   person = new Person() To access a person’s street we might write street = null if (person && person.address) { street = person.address.street } Using the [...]

Create a mock object for an abstract class using a map

We can create a mock object for an abstract class by coercing a map into the abstract class by providing the abstract methods in the map as closure functions. abstract class Person { def first def last abstract def getType() }   // Create our mock object by providing an implementation of the abstract method [...]

Create a mock object from a map

You can create a mock version of an object for use in testing by coercing a map into your target object. For example, suppose you had a Person object defined as follows. class Person { def first = ” def last = ” def fullname() { "$first $last".trim() } }   // Create a person [...]

Create an object from a map or list

A nice feature in Groovy allows us to coerce Maps and Lists into objects. This is done using the as operator. Coercing a Map class Person { def first def last }   // Coerce a map into a Person object p = [first:’Alfred’,last:’Hitchcock’] as Person assert p.last == ‘Hitchcock’ Corecing a List If we [...]

Create an object

A trivial Groovy object contains only properties. It can be created using the default constructor or with named parameters. class Person { def first def last }   // Create using the default constructor p = new Person()   // Create using default parameters p = new Person(first:’Alfred’,last:’Hitchcock’) If we add a constructor to our [...]