Limiting Accessible Methods/Classes

Imagine that you have the following class and you want to expose only test() method; you don't want to expose other methods.

public class MyClass {
       public void test(){
	      System.out.println("test");
       }
       public String getName(){
	      return "foo";
       }
       ...
}

BeanInfo is used to specify exposed methods. For example, BeanInfo of MyClass class can be made by defining MyClassBeanInfo class, which is a subclass of java.beans.SimpleBeanInfo class, as follows.

import java.beans.*;

public class MyClassBeanInfo extends SimpleBeanInfo {
    public MethodDescriptor[] getMethodDescriptors(){
	try{
	    return new MethodDescriptor[]{
		new MethodDescriptor(MyClass.class.getMethod("test", new Class[]{}))
	    };
	} catch (NoSuchMethodException e){
	    return new MethodDescriptor[]{};
	}
    }
}

In order to prepare a context such that only MyClass.test() can be called, register MyClass to a pnuts.ext.LimitedClassesConfiguration object with registerClass() method, then call Context.setConfiguration() method with the LimitedClassesConfiguration object.

import pnuts.ext.LimitedClassesConfiguration;

Configuration conf = new LimitedClassesConfiguration(false);
conf.registerClass(MyClass.class);
Context c = new Context();
c.setConfiguration(conf);

Pnuts.load(file, c);

In a context that a pnuts.ext.LimitedClassesConfiguration object is set, a field access operation is interpreted as an access to a Bean property.

myobj = MyClass()
myobj.test()  ===> "test" is printed
myobj.getName() ===> an exception is thrown
myobj.name    ===> "foo"