Enum Class ZeroArgumentNonVoidMethodPolicy

java.lang.Object
java.lang.Enum<ZeroArgumentNonVoidMethodPolicy>
freemarker.ext.beans.ZeroArgumentNonVoidMethodPolicy
All Implemented Interfaces:
Serializable, Comparable<ZeroArgumentNonVoidMethodPolicy>, Constable

public enum ZeroArgumentNonVoidMethodPolicy extends Enum<ZeroArgumentNonVoidMethodPolicy>
How to show 0 argument non-void public methods to templates. Used in BeansWrapper, and therefore in DefaultObjectWrapper. This policy doesn't apply to methods that Java Beans introspector discovers as a property read method (which typically look like getSomething(), or isSomething()). It's only applicable to methods like something(), including the component read methods of Java records.
Since:
2.3.33
See Also:
BeansWrapperConfiguration.setDefaultZeroArgumentNonVoidMethodPolicy(ZeroArgumentNonVoidMethodPolicy), BeansWrapperConfiguration.setRecordZeroArgumentNonVoidMethodPolicy(ZeroArgumentNonVoidMethodPolicy), BeansWrapper.MethodAppearanceDecision.setMethodInsteadOfPropertyValueBeforeCall(boolean), MethodCallAwareTemplateHashModel
  • Enum Constant Details

    • BOTH_METHOD_AND_PROPERTY_UNLESS_BEAN_PROPERTY_READ_METHOD

      public static final ZeroArgumentNonVoidMethodPolicy BOTH_METHOD_AND_PROPERTY_UNLESS_BEAN_PROPERTY_READ_METHOD
      Both obj.m, and obj.m() gives back the value that the m Java method returns, and it's not possible to get the method itself. But, it's not applicable for Java Bean property read methods (like int getX()), which remain just simple methods (because the Java Bean property is visible regardless of the ZeroArgumentNonVoidMethodPolicy, like obj.x, for which the Java Bean property read method is getX()).

      This is a parse-time trick that only works when the result of the dot operator (like obj.m), or of the square bracket key operator (like obj["m"]) is called immediately in a template (like obj.m(), or like obj["m"]()), and therefore the dot, or square bracket key operator knows that you will call the result of it. In such case, if the wrapped obj implements MethodCallAwareTemplateHashModel, the operator will call MethodCallAwareTemplateHashModel.getBeforeMethodCall(String) instead of TemplateHashModel.get(String). Also note that at least in 2.3.33 it's only done if the method call has 0 arguments.

      The practical reason for this feature is that the convention of using SomeType something() instead of SomeType getSomething() spreads in the Java ecosystem (and is a standard in some other JVM languages), and thus we can't tell anymore if SomeType something() just reads a value, and hence should be accessed like obj.something, or it's more like an operation that has side effect, and therefore should be accessed like obj.something(). So with allowing both, the template author is free to decide which is the more fitting. Also, for accessing Java records components, the proper way is obj.something, but before FreeMarker was aware of records (and hence that those methods are like property read methods), the only way that worked was obj.something(), so to be more backward compatible, we have to support both.

    • METHOD_ONLY

      public static final ZeroArgumentNonVoidMethodPolicy METHOD_ONLY
      Only obj.m() gives back the value in a template, obj.m in a template just gives the method itself.
    • PROPERTY_ONLY_UNLESS_BEAN_PROPERTY_READ_METHOD

      public static final ZeroArgumentNonVoidMethodPolicy PROPERTY_ONLY_UNLESS_BEAN_PROPERTY_READ_METHOD
      obj.m in a template gives back the value, and you can't get the method itself. But, it's not applicable for Java Bean property read methods, which will remain normals methods, just like with BOTH_METHOD_AND_PROPERTY_UNLESS_BEAN_PROPERTY_READ_METHOD.
  • Method Details

    • values

      public static ZeroArgumentNonVoidMethodPolicy[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static ZeroArgumentNonVoidMethodPolicy valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null