cbuschka / beanshell2

Automatically exported from code.google.com/p/beanshell2
0 stars 0 forks source link

can't access public fields #101

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago

bsh % setAccessibility(true);
bsh % board.players_in_game;
<2>
bsh % board.tempCell;
// Error: EvalError: Cannot access field: tempCell, on object: 
oneday.OnedayBoard@1c3508c0 : at Line: 1 : in file: <unknown file> : board 
.tempCell 

bsh % 

As seen in the above, accessing a private field works, 
but accessing a public field does not.

This is with bsh-2.1.8.jar and java 1.7_051 under eclipse.
I tracked this in the sources to getFieldValue in reflect.java

Original issue reported on code.google.com by ddyer-go...@real-me.net on 27 Mar 2014 at 12:08

GoogleCodeExporter commented 8 years ago
This same example works correctly when linked with bsh-2.0b4.jar

Original comment by ddyer-go...@real-me.net on 27 Mar 2014 at 12:20

GoogleCodeExporter commented 8 years ago
So "players_in_game" is defined as "private" and "tempCell" is defined as 
"public"?

What do you see without calling "setAccessibility(true)" or when calling 
"setAccessibility(false)"?

Original comment by pejob...@gmail.com on 27 Mar 2014 at 8:52

GoogleCodeExporter commented 8 years ago
>>So "players_in_game" is defined as "private" and "tempCell" is defined as 
"public"?
Correct.
What do you see without calling "setAccessibility(true)" or when calling 
"setAccessibility(false)"?
No difference.

Original comment by ddyer-go...@real-me.net on 27 Mar 2014 at 4:40

GoogleCodeExporter commented 8 years ago
I found another relevant bit of information.  The class of "board" has
unspecified access.  If I change it to public, then the access works.
Perhaps someone used to wrangling java reflection can explain that!

that is, the declaration for the class of the board object is

class OnedayBoard extends RBoard<OnedayCell> implements 
BoardProtocol,OnedayConstants 

and making it 

public class OnedayBoard extends RBoard<OnedayCell> implements 
BoardProtocol,OnedayConstants

fixes the problem.

Original comment by ddyer-go...@real-me.net on 27 Mar 2014 at 6:42

GoogleCodeExporter commented 8 years ago
Ah, with this bit I can reproduce the behavior. My test class looks now like 
this:

class Board {
   public int tempCell = 13;
   private int players_in_game = 42;
}

Original comment by pejob...@gmail.com on 2 Apr 2014 at 9:51

GoogleCodeExporter commented 8 years ago

Original comment by pejob...@gmail.com on 2 Apr 2014 at 9:51