ADC Home > Reference Library > Technical Q&As > Legacy Documents > Java >

Legacy Documentclose button

Important: This document is part of the Legacy section of the ADC Reference Library. This information should not be used for new development.

Current information on this Reference Library topic can be found here:

Hiding public methods from AppleScript in Java


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.

[May 17 1999]


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.