Customizing the Behavior of Basic Expressions

The behavior of field access, index access, and method/constructor call, and binary/unary operations can be customized by changing an attribute of a Context object.

Field Access
target . field
target :: field
Index Access target [ index ]
Method Call
target . method (...)
target :: method (...)
Constructor
target (...)
Binary/Unary Operations +,-,*,/,&,|,^,<<,>>,>>>,&&,||,++,--

Setting up a configuration

A Configuration of a context can be set up with Context.setConfiguration(Configuration) method.

Context context = new Context();
context.setConfiguration(new NonPublicMemberAccessor());
...

pnuts.lang.Configuration class

pnuts.lang.Configuration defines the default behavior of basic expressions. A subclass may override its methods to define its own behavior.

Field Access

public Object getField( Context context , Object target , String name );
public void putField( Context context, Object target , String name, Object value );
public Object getStaticField( Context context, Class target , String name );
public void putStaticField( Context context, Class target , String name, Object value );

Index Access

public Object getElement( Context context, Object target , Object key );
public void setElement( Context context , Object target , Object key, Object value);

Constuctor

public Object callConstructor( Context context, Class c , Object[] args , Class[] types );

Method Call

public Object callMethod( Context context , Object target , Object[] args , Class[] types );

Binary/Unary Operations

Binary/unary operations can be customized by setting the following protected variables in the constructor.
protected BinaryOperator _add, _subtract, _multiply, _divide, _mod, _and, _or, _xor, _shiftLeft, _shiftRight, _shiftArithmetic;
protected UnaryOperator _add1, _subtract1, _negate, _not;
protected BooleanOperator _lt, _le, _gt, _ge, _eq;

Predefined Configuration

The following classes are defined in pnuts.lang package, each of which defines a behavior of Java API access.

pnuts.lang.Configuration
The default configuration, which can access Beans proeprties, Beans methods, context-local variables, and pnuts.lang.Property's attributes. On Java2 platform, it can access Map entries as well.
pnuts.ext.PublicMemberAccessor
Same as the default configuration, except that it can access public members instead of Beans properties.
pnuts.ext.NonPublicMemberAccessor
It can access non-public methods and fields. It requires Java2 environment.
pnuts.awt.EventQueueConfiguration
Use AWT Event thread to access Java API.

The pnuts.lang.defaultConfiguration Property

If the system property, pnuts.lang.defaultConfiguraion, is a valid Configuration subclass when Pnuts API is first loaded, the specified Configuration is used as the default configuration.

For example, if the Pnuts interpreter is started as the following command line, all field access in the scripts will be interpreted as access to public members.

% java -Dpnuts.lang.defaultConfiguration=pnuts.lang.PublicMemberConfiguration pnuts.tools.Main
See also 'Setting up a Context'.

Function definition and Configuration

Since Java API accesses are likely to assume a particular configuration, scripts should be executed in the same configuration as the one which was used when the scripts were loaded. Therefore, function defined in a script is executed in the configuration in which the function is defined.