Discussion:
propertyMissing difficulties
Alan Krueger
2012-09-16 04:30:54 UTC
Permalink
On the Groovy "Using methodMissing and propertyMissing
<http://groovy.codehaus.org/Using+methodMissing+and+propertyMissing>"
page, it has this sample code:

class Foo {defstorage = [:] defpropertyMissing(String name, value) {
storage[name] = value } defpropertyMissing(String name) {
storage[name] } } deff = newFoo() f.foo = "bar"assertEquals "bar", f.foo

Plugging this into foo.groovy and running that as a Groovy script
(changing the assertEquals to a println), it works as expected. Because
I've been having trouble getting propertyMissing to work properly for
me, I tried to turn the above into a JUnit test:

import static org.junit.Assert.assertEquals

import org.junit.Test

class PropertyMissingTest {
class Foo {
def storage = [:]
def propertyMissing(String name, value) { storage[name] = value }
def propertyMissing(String name) { storage[name] }
}

@Test
void test() {
def f = new Foo()
f.foo = "bar"
assertEquals "bar", f.foo
}
}

Unfortunately, when I run that I get this:

java.lang.VerifyError: (class: PropertyMissingTest$Foo, method:
propertyMissing signature:
(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;) Wrong
return type in function
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at PropertyMissingTest.class$(PropertyMissingTest.groovy)
at
PropertyMissingTest.$get$$class$PropertyMissingTest$Foo(PropertyMissingTest.groovy)
at PropertyMissingTest.test(PropertyMissingTest.groovy:14)
[...]

I've gone over this multiple times and am a bit stumped. Any
suggestions on why this isn't working?
Luke Daley
2012-09-17 11:42:15 UTC
Permalink
You've sent this to the Gradle mailing list. Did you mean the Groovy mailing list?
Post by Alan Krueger
class Foo
{
def
storage = [:]
def
propertyMissing(String name, value)
{ storage[name] = value }
def
propertyMissing(String name) { storage[name] }
}
def f = new
Foo()
f.foo =
"bar"
assertEquals
"bar", f.foo
import static
org.junit.Assert.assertEquals
import org.junit.Test
class PropertyMissingTest {
class Foo {
def storage = [:]
def propertyMissing(String name, value) { storage[name] = value }
def propertyMissing(String name) { storage[name] }
}
@Test
void test() {
def f = new Foo()
f.foo = "bar"
assertEquals "bar", f.foo
}
}
java.lang.VerifyError: (class: PropertyMissingTest$Foo, method: propertyMissing signature: (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;) Wrong return type in function
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at PropertyMissingTest.class$(PropertyMissingTest.groovy)
at PropertyMissingTest.$get$$class$PropertyMissingTest$Foo(PropertyMissingTest.groovy)
at PropertyMissingTest.test(PropertyMissingTest.groovy:14)
[...]
I've gone over this multiple times and am a bit stumped. Any suggestions on why this isn't working?
--
Luke Daley
Principal Engineer, Gradleware
http://gradleware.com
Alan Krueger
2012-09-17 18:41:58 UTC
Permalink
Post by Luke Daley
You've sent this to the Gradle mailing list. Did you mean the Groovy mailing list?
Blast, yes I did. (facepalm) Sorry about that.

Loading...