Created
October 10, 2012 11:03
-
-
Save EwanDawson/3864796 to your computer and use it in GitHub Desktop.
Extend Groovy asType(Class) method to handle additional classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.joda.time.DateTime | |
def oldAsType = String.metaClass.getMetaMethod("asType", [Class] as Class[]) | |
String.metaClass.asType = { Class type -> | |
type.isAssignableFrom(DateTime) ? | |
new DateTime(delegate) : | |
oldAsType.invoke(delegate, [type] as Class[]) | |
} | |
assert "2012-01-01T10:10:10" as DateTime == new DateTime("2012-01-01T10:10:10") | |
assert "1234" as Integer == 1234 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment