Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

No Format
public Class getPropertyClass(OgnlContext context, Object target, Object name)
{
  IBeanProvider provider = (IBeanProvider)target;
  String beanName = ((String)name).replaceAll("\"", "");
        
  if (provider.canProvideBean(beanName))
    return provider.getBean(beanName).getClass();
        
  return super.getPropertyClass(context, target, name);
}
    
public String getSourceAccessor(OgnlContext context, Object target, Object name)
{
   IBeanProvider provider = (IBeanProvider)target;
   String beanName = ((String)name).replaceAll("\"", "");
        
   if (provider.canProvideBean(beanName)) {

       Class type = OgnlRuntime.getCompiler().getInterfaceClass(provider.getBean(beanName).getClass());
            
       ExpressionCompiler.addCastString(context, "((" + type.getName() + ")");
            
       context.setCurrentAccessor(IBeanProvider.class);
       context.setCurrentType(type);
            
       return ".getBean(" + name + "))";
   }
        
   return super.getSourceAccessor(context, target, name);
}
    
public String getSourceSetter(OgnlContext context, Object target, Object name)
{
  throw new UnsupportedCompilationException("Can't set beans on IBeanProvider.");
}

Although this example may not provide with all of the possible use cases you may need to learn to properly implement these methods in your own PropertyAccessor implementations - the built in OGNL versions like ObjectPropertyAccessor, MapPropertyAccessor, ListPropertyAccessor, etc should provide more than enough data to work from. http://svn.opensymphony.com/svn/ognl/trunk/

The most important part of the above logic you will want to look at is in how the new OgnlContext methods for setting object/accessor types are being set:

No Format

context.setCurrentAccessor(IBeanProvider.class);
context.setCurrentType(type);

This meta information is used by the OgnlExpressionCompiler to correctly cast your specific expression object types during compilation. This process of casting/converting in to and out of native types is the most complicated part of this new logic and also the source of the greatest number of bugs reported in the OGNL jira. http://jira.opensymphony.com/browse/OGNL

In this property accessor example the goal is to turn general statements like beans.emailValidator in to their pure source form - which would look something like this when all is said and done:

No Format

((ValidatingBean)beanProvider.getBean("emailValidator"))

There is also the ever important cast handling which you must do:

No Format

Class type = OgnlRuntime.getCompiler().getInterfaceClass(provider.getBean(beanName).getClass());

ExpressionCompiler.addCastString(context, "((" + type.getName() + ")");

In this example the PropertyAccessor is trying to determine the class type and manually adding the cast string for the specific type to the overall statement by invoking the utility method addCastString(OgnlContext, String) on ExpressionCompiler. In many instances of expression compilation you might also be dealing with unknown method calls, where the more preferred way to do this kind of logic would be something like this: (taken from the OGNL ObjectPropertyAccessor implementation)

No Format

Method m = ...(various reflection gynamistics used to find a java.reflect.Method instance)

context.setCurrentType(m.getReturnType());
context.setCurrentAccessor(OgnlRuntime.getCompiler().getSuperOrInterfaceClass(m, m.getDeclaringClass()));

When dealing with method calls it is very important that you do this specific kind of type setting on the OgnlContext class so that the casting done on your statements (which happens outside of the ObjectPropertyAccessor in this instance) can be done on the highest level interface defining that method. This becomes important when you are dealing with expressions that you would like to re-use against different object instances. For example, suppose we had an ognl expression like this (for Tapestry):

No Format

user.firstName

and the object it was compiled against was an instance of something looking like this:

No Format

public abstract LoginPage extends BasePage implements UserPermissions {

  public abstract User getUser();

}

..
/**
 * Interface for any page/component that holds references to the current system
 * User. 
 */
public interface UserPermissions {
   User getUser();
}

BasePage is a Tapestry specific class which is unimportant in this example. What is important to know is that if we had done something like this in the previous context setting example:

No Format

context.setCurrentType(m.getReturnType());
context.setCurrentAccessor(m.getDeclaringClass());

It would have resulted in a compiled expression of:

No Format

public void get(OgnlContext context, Object root)
{
  return ((LoginPage)root).getUser();
}

This is undesirable in situations where you would like to re-use OGNL expressions across many different class instances (which is what Tapestry does via the ExpressionCacheImpl listed above). The better/more re-usable compiled version should really look like:

No Format

public void get(OgnlContext context, Object root)
{
  return ((UserPermissions)root).getUser();
}

These are the more delicate parts of the compiler API that the majority of people will need to worry about during any integration efforts.