Q:
I have several public methods in my class that I do not want exposed to AppleScript. Is there any way that I can specify this in my Java code, or do I have to do this by editing the 'aete '?
A: You can do this easily in code. By creating a BeanInfo for your class, you may exclude both properties and methods from the automatically generated 'aete' resource.
Given a class Salad.java :
public class Salad
{
public int msgAmount;
// we don't want this in our ingredients list
public void setMsgAmount( int amount ) { msgAmount
= amount; }
public int getMsgAmount( ) { return msgAmount; }
public void addMSG( int amount ){ ... }
// hide this too
...
};
|
You can make a SaladBeanInfo.java :
import
java.beans.*;
import java.lang.reflect.*;
public class
SaladBeanInfo extends SimpleBeanInfo
{
public MethodDescriptor[ ] getMethodDescriptors()
{
MethodDescriptor md[ ] = new MethodDescriptor[1];
try
{
md[0] = new MethodDescriptor( getMethod( Salad.class,
"addMSG" ), pd );
md[0].setHidden( true );
return md;
}
catch ( IntrospectionException e )
{
e.printStackTrace();
}
return null;
}
public PropertyDescriptor[ ] getPropertyDescriptors()
{
PropertyDescriptor pd[ ] = new PropertyDescriptor[1];
try
{
pd[0] = new PropertyDescriptor(
"msgAmount", Salad.class );
pd[0].setHidden( true );
return pd;
}
catch ( Exception e )
{
e.printStackTrace();
}
return null;
}
Method getMethod( Class c, String methodName ) throws
IntrospectionException
{
Method methods[ ] = c.getMethods();
for ( int i = 0; i < methods.length; i++ )
{
if ( methods[i].getName().equals( methodName ))
{
return methods[i];
}
}
throw new IntrospectionException(
"No such method \"" + methodName + "\"" );
}
}
|
We first create a method descriptor and set the isHidden property to true by calling setHidden( true ) . This will tell the introspector not to return information to
the 'aete' generator for this method. Thus, this method will not appear in the AppleScript dictionary for this application.
Hiding properties is a slightly different process, but is nonetheless quite straightforward. We create a PropertyDescriptor and call the inherited setHidden( ) method from java.beans.FeatureDescriptor . In this manner, we can also prevent the scriptor (and health inspector) from knowing about this hidden property.
|