Questions tagged [expandometaclass]

25 questions
121
votes
8 answers

shortcut for creating a Map from a List in groovy?

I'd like some sorthand for this: Map rowToMap(row) { def rowMap = [:]; row.columns.each{ rowMap[it.name] = it.val } return rowMap; } given the way the GDK stuff is, I'd expect to be able to do something like: Map rowToMap(row) { …
danb
  • 10,239
  • 14
  • 60
  • 76
19
votes
4 answers

Copy Groovy class properties

I want to copy object properties to another object in a generic way (if a property exists on target object, I copy it from the source object). My code works fine using ExpandoMetaClass, but I don't like the solution. Are there any other ways to do…
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
7
votes
1 answer

Scope of Groovy's ExpandoMetaClass?

Groovy exposes an ExpandoMetaClass that allows you to dynamically add instance and class methods/properties to a POJO. I would like to use it to add an instance method to one of my Java classes: public class Fizz { // ...etc. } Fizz fizz = new…
user1768830
5
votes
1 answer

Where to properly initialize Groovy metaclasses?

In a Groovy application, if you want to initialize metaclasses, where is the best place to put those initializations? In Grails apps, I've used the Bootstrap.groovy file. Is there something similar for an arbitrary Groovy app? Edit: To clarify,…
Alan Krueger
  • 4,701
  • 4
  • 35
  • 48
4
votes
1 answer

Getting org.codehaus.groovy.control.MultipleCompilationErrorsException using gmaven plugin

This is my sample program, while compiling using mvn it throws me the compilation error, i'm trying to add a Static Methods using ExpandoMetaClass - @Singleton class ThrowError { def parse () { …
anish
  • 6,884
  • 13
  • 74
  • 140
4
votes
1 answer

Groovy expando metaclass

I've developed a Class that has some methods that augment Integer, it mainly lets me do this: def total = 100.dollars + 50.euros Now I have to extend Integer.metaClass doing something like this: Integer.metaClass.getDollars = {-> …
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
4
votes
4 answers

Can you use Groovy meta programming to override a private method on a Java class

I'm trying to override a private method on a Java class using meta programming. The code looks something like this: // Java class public class MyClass{ private ClassOfSomeSort property1; private ClassOfSomeOtherSort property2; public…
Jay Shark
  • 635
  • 1
  • 11
  • 21
3
votes
1 answer

In Groovy, When Does It Makes Sense To Use An Expando vs the 'as' operator and Closures?

Groovy is a wonderful language that offers lots of different choices. In thinking about unit tests, when does it make sense to use an Expando object vs. the "as" operator with…
finneycanhelp
  • 9,018
  • 12
  • 53
  • 77
3
votes
3 answers

Overriding event closure on Grails GORM domain class for unit testing

I'm working on a fresh Grails project and recently noticed the default convention in the Spring Security Core generated User class now auto-encodes the password via a beforeInsert/Update event. That's a nice, clean, DRY way of doing the encode, and…
James
  • 1,391
  • 2
  • 14
  • 20
2
votes
2 answers

Passing delegate through nested closures in Groovy

I am creating a builder which accepts Groovy closures as markup. However I am having trouble catching method calls with nested closures. Closure nested = { foo () //will throw missingMethod exception } Closure root = { foo () …
Akusete
  • 10,704
  • 7
  • 57
  • 73
2
votes
1 answer

ExpandoMetaClass in Grails to override behavior in a Java library?

I'm using Grails with third-party java libraries, and I'd like to override behavior in one of those libraries. I've attempted to do this in Bootstrap.groovy, like so: // class overrides ExpandoMetaClass.enableGlobally() …
jbwiv
  • 1,015
  • 9
  • 21
2
votes
2 answers

Grails: ExpandoMetaClass for a method

Consider a method def public Set getAgeRanges(boolean excludeSenior) { -- something --- } how to write ExpandoMetaClass for this like ClassName.metaClass.methodName << { boolean excludeSenior-> --…
Antony Prince
  • 319
  • 4
  • 17
2
votes
1 answer

Result of Replaced/Overriden Parameterless Method Not Passed to the Constructor

I'm trying to change the behaviour of the constructor of a groovy class by replacing a method in that class which is used to set a property but the properties are not getting set with the expected values. class TestClass { def noParam def…
ld_pvl
  • 295
  • 2
  • 17
1
vote
2 answers

Partial Mocking Class with Multiple Static Methods with GMock

I'm using GMock to add some unit testing to our existing Java projects. We have multiple places where the methods needing to be tested are static methods, which utilize additional static methods within the method we want to test. I would like to be…
Patrick
  • 13
  • 3
1
vote
1 answer

Grails how to change a metaclass Method

I'm trying to remove the square brackets that appear in toString off all collections in Grails. These square brackets are added in the toString of the java.util.AbstractCollection class. So the goal is to replace this toString method and I'm trying…
Victor Soares
  • 757
  • 1
  • 8
  • 34
1
2