Skip to content

Resolving path expressions

dietmarz edited this page Nov 27, 2012 · 14 revisions

To illustrate the usage of path expressions the @PmAttrCfg annotation will be discussed as an example in more detail. The PmAttrCfg annotation has a valuePath attribute @PmAttrCfg(valuePath = "<expression>") which allows to a access a property of the backing bean and assign it to the annotated attribute. There is a list of constructiv JUnit tests in the org.pm4j.core.pm.impl.pathresolver.PathResolverTest class. Feel invited to experiment with these examples.

Syntax

valuePath expression

Dot Operator

To separate fiels an methods, a dot '.' character is used. e.g.

  • myBean.name returns the public String name from myBean. myBean is a property of the current class. It is allowed to return null.

  • myBean.getName() returns the value auf the public String getName() call of myBean. It is allowed to return null.

Modifiers

Modifiers are placed in front of fields or methods to control the existence or value expectation while resolving.

  • (o) means value optional. "myBean.(o)address.street" address is optional, means if address is null, the street property will not be resolved preventing a NullPointerException. But the address property of myBean must exist. "myBean.address.(o)street" means address is not optional. Because nothing is called on street, the '(o)' modifier is unneccessary as it is placed as last modifier in the chain.
  • (x) means existing optional. "myShip.(x)getContainers().size()" If the ship is a passenger ship, it has probably no getContainers() method. But if the method exists, size() will be called, even if getContainers() returns null.
  • (x,o) means value and existing optional. "myShip.(x,o)getContainers().size()" If the ship is a containership, but the container list is not initialized, the size() call will be prevented.

Accessing named objects

if the expression starts with a hash '#' character, the referenced object is searched within the PmConversation. The sign is allowed only at the beginnig of the expression.

  • "#myService.(x)getVessel()" The named object myService will be resolved from the conversation, and the optional existing getVessel() method is called if the method exists.
Clone this wiki locally