jastrachan | 6 May 09:19 2004
Picon

Re: Accessing Java Instance Variables

BTW have we raised an issue for this? Sounds like a fairly easy fix to 
the MetaClass, to use reflection to find a field if there's no 
property.

On 6 May 2004, at 06:54, bing ran wrote:
> Owen,
>
> You need a getter in the super class.
>
> Groovy cannot access a public field using ".". It has to be a JavaBeans
> property.
>
> Bing
>
>
> -----Original Message-----
> From: groovy-user-admin@...
> [mailto:groovy-user-admin@...] On Behalf Of
Owen 
> Densmore
> Sent: Thursday, May 06, 2004 1:28 PM
> To: Groovy Users
> Subject: [groovy-user] Accessing Java Instance Variables
>
> I'm trying to integrate groovy with existing java frameworks.  One 
> need I
> have is to subclass, in groovy, and existing java class.  I need to 
> use the
> java instance variables from groovy, but that appears to fail; groovy
> reports that it cannot find the variable.
>
> Here's an example:
>
> SimpleModel.java:
> import java.util.ArrayList;
> public class SimpleModel {
>    public ArrayList agents;
>    public SimpleModel (ArrayList a) {
> 	this.agents=a;
>    }
>    public void show() {
> 	for (int i=0; i < agents.size(); i++)
> 		System.out.println(agents.get(i).toString());
>    }
> }
>
> test.groovy:
> #!/usr/bin/env groovy
>
> SimpleModel a = new SimpleModel(["asdf", 4])
> a.show()
>
> public class GroovyModel extends SimpleModel {
> 	public GroovyModel(java.util.ArrayList a) {
> 		super(a);
> 	}
> 	public void cat(java.util.ArrayList a) {
> 		agents.addAll(a);
> 	}
> }
>
> GroovyModel b = new GroovyModel(["groovy", 4])
> b.show()
> b.cat([1.5, "more"])
> b.show()
>
> When this is run, I get:
> owen|~/src/groovy[530]: ./test.groovy
> asdf
> 4
> groovy
> 4
> Caught: groovy.lang.MissingPropertyException: No such property: agents 
> for
> class: GroovyModel
> groovy.lang.MissingPropertyException: No such property: agents for
> class: GroovyModel
>          at groovy.lang.MetaClass.getProperty(MetaClass.java:540)
>          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>
> .. and using "super.agents" also fails.
>
> How should I access java instance variables?  .. or is this impossible 
> in
> groovy?
>
> 	-- Owen
>
> Owen Densmore         908 Camino Santander   Santa Fe, NM 87505
> Cell: 505-570-0168    Home: 505-988-3787     http://backspaces.net
>
> _______________________________________________
> groovy-user mailing list
> groovy-user@...
> http://lists.codehaus.org/mailman/listinfo/groovy-user
>
>
>
> _______________________________________________
> groovy-user mailing list
> groovy-user@...
> http://lists.codehaus.org/mailman/listinfo/groovy-user
>
>

James
-------
http://radio.weblogs.com/0112098/

Gmane