How do I call setField on inherited public fields?

1 view
Skip to first unread message

eching

unread,
Aug 7, 2008, 1:54:12 PM8/7/08
to Sleep Developers
When I try to set fields on a class that extends another class and has
inherited public fields, I get something like this:

Warning: no field named fname in class com.eric.Eric at test.sl:6

But if I instantiate an instance of the parent, I can set the field's
value fine. How do I do this?

Also, what is the syntax for accessing a field in java objects?

Thanks,
Eric

Raphael Mudge

unread,
Aug 7, 2008, 3:05:06 PM8/7/08
to sleep-de...@googlegroups.com
Hi Eric,
Good questions. First off the Sleep 2.1 Manual chapter on
interfacing with Java will be a big help. Chapter 7. Java Objects -
http://sleep.dashnine.org/manual/

If you could show the code to test.sl, that will help. However here
is what you do:

# to get a static field

$field = [com.eric.Eric fname]

# to set a static field

setField(^com.eric.Eric, "fname", $value);

# an instance field

$field = [$eric fname];
setField($eric, "fname", $value);

You can also import your package with:

import com.eric.Eric;

$field = [Eric fname];

The ^ character is used to get a Scalar that contains Class object
for the literal class. Its the same as [Class forName: "somePath"]

-- Raphael

Eric Ching

unread,
Aug 7, 2008, 3:38:50 PM8/7/08
to sleep-de...@googlegroups.com
This is a dumb example, but serves the purpose I suppose.

Here are my java classes:

package com.eric;
class Person{
  public String fname;
  public String lname;
 
 public Person(String fname, String lname){
   this.fname = fname;
   this.lname = lname;
 }
}

package com.eric;
class Eric extends Person{
 
  public Eric(String fname, String lname){
    super(fname, lname);
  }
}


Then test.sl looks like this:

=========
import com.eric.* from: com.eric.jar;

$eric = [new Eric: "Eric", "Ching"];

setField($eric, "fname",  "Dave");
=========

When I run that I get:

Warning: attempted to pass a malformed key value pair: fname at test.sl:6

Originally test.sl looked like this (which is based off the sleep doc):

=========
import com.eric.* from: com.eric.jar;

$eric = [new Eric: "Eric", "Ching"];

setField($eric, fname => "Dave");
=========

And I get the original error:

Warning: no field named fname in class com.eric.Eric at test.sl:7

Raphael Mudge

unread,
Aug 7, 2008, 4:03:20 PM8/7/08
to sleep-de...@googlegroups.com
You're right about the fname => "Dave" part for specifying the field.  I was wrong about setField($object, "name", $value).  I don't know my own language.

I took a look at the code for &setField versus the ObjectAccess interpreter op.  &setField uses getDeclaredField() to resolved the field.  This method doesn't walk up the inherited hierarchy of stuff.

ObjectAccess uses getField().  So there is our problem.  I'll post a fix for it soon.

-- Raphael

On Aug 7, 2008, at 3:38 PM, Eric Ching wrote:



Eric Ching

unread,
Aug 7, 2008, 4:16:55 PM8/7/08
to sleep-de...@googlegroups.com
Excellent, much appreciated.

Raphael Mudge

unread,
Aug 7, 2008, 5:17:47 PM8/7/08
to sleep-de...@googlegroups.com
The change is now in the SVN.  You can get it with:

svn export svn://svn.berlios.de/sleep/sleep

Then use ant to compile it.   I used your Eric/Person example as the unit test so you can be sure that much works.  

Apparently getDeclaredField works with protected/public Field members and getField only works with public members.  For now I put in a hack that makes Sleep try getDeclaredField first and then getField if necessary.  If someone eventually says "I can't access protected members declared in the parent class" then I'll rewrite the code to walk the class hierarchy. 

Any requests for this?

-- Raphael

Eric Ching

unread,
Aug 7, 2008, 5:26:29 PM8/7/08
to sleep-de...@googlegroups.com
Works great!  Thanks for the speedy response.

My java apps tend to be simple and quite unsophisticated (that or limited in scope).  I tend to declare fields as private or public(not so much public) so I'm not the best person for feedback =p
Reply all
Reply to author
Forward
0 new messages