Class JexlOptions
- java.lang.Object
-
- org.apache.commons.jexl3.JexlOptions
-
public final class JexlOptions extends java.lang.ObjectFlags and properties that can alter the evaluation behavior. The flags, briefly explained, are the following:- silent: whether errors throw exception
- safe: whether navigation through null is notan error
- cancellable: whether thread interruption is an error
- lexical: whether redefining local variables is an error
- lexicalShade: whether local variables shade global ones even outside their scope
- strict: whether unknown or unsolvable identifiers are errors
- strictArithmetic: whether null as operand is an error
- sharedInstance: whether these options can be modified at runtime during execution (expert)
- constCapture: whether captured variables will throw an error if an attempt is made to change their value
- strictInterpolation: whether interpolation strings always return a string or attempt to parse and return integer
- booleanLogical: whether logical expressions ("" , ||) coerce their result to boolean
This interface replaces the now deprecated JexlEngine.Options.
- Since:
- 3.2
-
-
Field Summary
Fields Modifier and Type Field Description private static intANTISHThe antish var bit.private static intBOOLEAN_LOGICALThe boolean logical flag.private static intCANCELLABLEThe cancellable bit.private static intCONST_CAPTUREThe const capture bit.private static intDEFAULTDefault mask .private intflagsThe default flags, all but safe.private java.util.Collection<java.lang.String>importsThe imports.private static intLEXICALThe lexical scope bit.private java.math.MathContextmathContextThe arithmetic math context.private intmathScaleThe arithmetic math scale.private static java.lang.String[]NAMESThe flag names ordered.private java.util.Map<java.lang.String,java.lang.Object>namespacesThe namespaces .private static intSAFEThe safe bit.private static intSHADEThe local shade bit.private static intSHAREDThe shared instance bit.private static intSILENTThe silent bit.private static intSTRICTThe strict bit.private static intSTRICT_INTERPOLATIONThe interpolation string bit.private booleanstrictArithmeticThe arithmetic strict math flag.
-
Constructor Summary
Constructors Constructor Description JexlOptions()Default ctor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JexlOptionscopy()Creates a copy of this instance.java.util.Collection<java.lang.String>getImports()Gets the optional set of imported packages.java.math.MathContextgetMathContext()The MathContext instance used for +,-,/,*,% operations on big decimals.intgetMathScale()The BigDecimal scale used for comparison and coercion operations.java.util.Map<java.lang.String,java.lang.Object>getNamespaces()Gets the optional map of namespaces.booleanisAntish()Checks whether evaluation will attempt resolving antish variable names.booleanisBooleanLogical()Gets whether logical expressions ("" , ||) coerce their result to boolean; if set, an expression like (3 "" 4 "" 5) will evaluate to true.booleanisCancellable()Checks whether evaluation will throw JexlException.Cancel (true) or return null (false) if interrupted.booleanisConstCapture()Are lambda captured-variables const?booleanisLexical()Checks whether runtime variable scope is lexical.booleanisLexicalShade()Checks whether local variables shade global ones.booleanisSafe()Checks whether the engine considers null in navigation expression as errors during evaluation.private static booleanisSet(int ordinal, int mask)Checks the value of a flag in the mask.booleanisSharedInstance()Gets sharing state.booleanisSilent()Checks whether the engine will throw aJexlExceptionwhen an error is encountered during evaluation.booleanisStrict()Checks whether the engine considers unknown variables, methods and constructors as errors during evaluation.booleanisStrictArithmetic()Checks whether the arithmetic triggers errors during evaluation when null is used as an operand.booleanisStrictInterpolation()Gets the strict-interpolation flag of this options instance.static intparseFlags(int initial, java.lang.String... flags)Parses flags by name.private static intset(int ordinal, int mask, boolean value)Sets the value of a flag in a mask.JexlOptionsset(JexlEngine jexl)Sets options from engine.JexlOptionsset(JexlOptions src)Sets options from options.voidsetAntish(boolean flag)Sets whether the engine will attempt solving antish variable names from context.voidsetBooleanLogical(boolean flag)Sets whether logical expressions ("" , ||) coerce their result to boolean.voidsetCancellable(boolean flag)Sets whether the engine will throw JexlException.Cancel (true) or return null (false) when interrupted during evaluation.voidsetConstCapture(boolean flag)Sets whether lambda captured-variables are const or not.static voidsetDefaultFlags(java.lang.String... flags)Sets the default (static, shared) option flags.voidsetFlags(java.lang.String... opts)Sets this option flags using the +/- syntax.voidsetImports(java.util.Collection<java.lang.String> imports)Sets the optional set of imports.voidsetLexical(boolean flag)Sets whether the engine uses a strict block lexical scope during evaluation.voidsetLexicalShade(boolean flag)Sets whether the engine strictly shades global variables.voidsetMathContext(java.math.MathContext mcontext)Sets the arithmetic math context.voidsetMathScale(int mscale)Sets the arithmetic math scale.voidsetNamespaces(java.util.Map<java.lang.String,java.lang.Object> ns)Sets the optional map of namespaces.voidsetSafe(boolean flag)Sets whether the engine considers null in navigation expression as null or as errors during evaluation.voidsetSharedInstance(boolean flag)Sets wether these options are immutable at runtime.voidsetSilent(boolean flag)Sets whether the engine will throw aJexlExceptionwhen an error is encountered during evaluation.voidsetStrict(boolean flag)Sets whether the engine considers unknown variables, methods and constructors as errors during evaluation.voidsetStrictArithmetic(boolean stricta)Sets the strict arithmetic flag.voidsetStrictInterpolation(boolean strict)Sets the strict interpolation flag.java.lang.StringtoString()
-
-
-
Field Detail
-
BOOLEAN_LOGICAL
private static final int BOOLEAN_LOGICAL
The boolean logical flag.- See Also:
- Constant Field Values
-
STRICT_INTERPOLATION
private static final int STRICT_INTERPOLATION
The interpolation string bit.- See Also:
- Constant Field Values
-
CONST_CAPTURE
private static final int CONST_CAPTURE
The const capture bit.- See Also:
- Constant Field Values
-
SHARED
private static final int SHARED
The shared instance bit.- See Also:
- Constant Field Values
-
SHADE
private static final int SHADE
The local shade bit.- See Also:
- Constant Field Values
-
ANTISH
private static final int ANTISH
The antish var bit.- See Also:
- Constant Field Values
-
LEXICAL
private static final int LEXICAL
The lexical scope bit.- See Also:
- Constant Field Values
-
SAFE
private static final int SAFE
The safe bit.- See Also:
- Constant Field Values
-
SILENT
private static final int SILENT
The silent bit.- See Also:
- Constant Field Values
-
STRICT
private static final int STRICT
The strict bit.- See Also:
- Constant Field Values
-
CANCELLABLE
private static final int CANCELLABLE
The cancellable bit.- See Also:
- Constant Field Values
-
NAMES
private static final java.lang.String[] NAMES
The flag names ordered.
-
DEFAULT
private static int DEFAULT
Default mask .
-
mathContext
private java.math.MathContext mathContext
The arithmetic math context.
-
mathScale
private int mathScale
The arithmetic math scale.
-
strictArithmetic
private boolean strictArithmetic
The arithmetic strict math flag.
-
flags
private int flags
The default flags, all but safe.
-
namespaces
private java.util.Map<java.lang.String,java.lang.Object> namespaces
The namespaces .
-
imports
private java.util.Collection<java.lang.String> imports
The imports.
-
-
Method Detail
-
isSet
private static boolean isSet(int ordinal, int mask)Checks the value of a flag in the mask.- Parameters:
ordinal- the flag ordinalmask- the flags mask- Returns:
- the mask value with this flag or-ed in
-
parseFlags
public static int parseFlags(int initial, java.lang.String... flags)Parses flags by name.A '+flag' or 'flag' will set flag as true, '-flag' set as false. The possible flag names are: cancellable, strict, silent, safe, lexical, antish, lexicalShade
- Parameters:
initial- the initial mask stateflags- the flags to set- Returns:
- the flag mask updated
-
set
private static int set(int ordinal, int mask, boolean value)Sets the value of a flag in a mask.- Parameters:
ordinal- the flag ordinalmask- the flags maskvalue- true or false- Returns:
- the new flags mask value
-
setDefaultFlags
public static void setDefaultFlags(java.lang.String... flags)
Sets the default (static, shared) option flags.Whenever possible, we recommend using JexlBuilder methods to unambiguously instantiate a JEXL engine; this method should only be used for testing / validation.
A '+flag' or 'flag' will set the option named 'flag' as true, '-flag' set as false. The possible flag names are: cancellable, strict, silent, safe, lexical, antish, lexicalShade
Calling JexlBuilder.setDefaultOptions("+safe") once before JEXL engine creation may ease validating JEXL3.2 in your environment.
- Parameters:
flags- the flags to set
-
copy
public JexlOptions copy()
Creates a copy of this instance.- Returns:
- a copy
-
getImports
public java.util.Collection<java.lang.String> getImports()
Gets the optional set of imported packages.- Returns:
- the set of imports, may be empty, not null
-
getMathContext
public java.math.MathContext getMathContext()
The MathContext instance used for +,-,/,*,% operations on big decimals.- Returns:
- the math context
-
getMathScale
public int getMathScale()
The BigDecimal scale used for comparison and coercion operations.- Returns:
- the scale
-
getNamespaces
public java.util.Map<java.lang.String,java.lang.Object> getNamespaces()
Gets the optional map of namespaces.- Returns:
- the map of namespaces, may be empty, not null
-
isAntish
public boolean isAntish()
Checks whether evaluation will attempt resolving antish variable names.- Returns:
- true if antish variables are solved, false otherwise
-
isBooleanLogical
public boolean isBooleanLogical()
Gets whether logical expressions ("" , ||) coerce their result to boolean; if set, an expression like (3 "" 4 "" 5) will evaluate to true. If not, it will evaluate to 5. To preserve strict compatibility with 3.4, set the flag to true.- Returns:
- true if short-circuit logicals coerce their result to boolean, false otherwise
- Since:
- 3.5.0
-
isCancellable
public boolean isCancellable()
Checks whether evaluation will throw JexlException.Cancel (true) or return null (false) if interrupted.- Returns:
- true when cancellable, false otherwise
-
isConstCapture
public boolean isConstCapture()
Are lambda captured-variables const?- Returns:
- true if lambda captured-variables are const, false otherwise
-
isLexical
public boolean isLexical()
Checks whether runtime variable scope is lexical.If true, lexical scope applies to local variables and parameters. Redefining a variable in the same lexical unit will generate errors.
- Returns:
- true if scope is lexical, false otherwise
-
isLexicalShade
public boolean isLexicalShade()
Checks whether local variables shade global ones.After a symbol is defined as local, dereferencing it outside its scope will trigger an error instead of seeking a global variable of the same name. To further reduce potential naming ambiguity errors, global variables (ie non-local) must be declared to be assigned
JexlContext.has(String)when this flag is on; attempting to set an undeclared global variables will raise an error.- Returns:
- true if lexical shading is applied, false otherwise
-
isSafe
public boolean isSafe()
Checks whether the engine considers null in navigation expression as errors during evaluation.- Returns:
- true if safe, false otherwise
-
isSharedInstance
public boolean isSharedInstance()
Gets sharing state.- Returns:
- false if a copy of these options is used during execution, true if those can potentially be modified
-
isSilent
public boolean isSilent()
Checks whether the engine will throw aJexlExceptionwhen an error is encountered during evaluation.- Returns:
- true if silent, false otherwise
-
isStrict
public boolean isStrict()
Checks whether the engine considers unknown variables, methods and constructors as errors during evaluation.- Returns:
- true if strict, false otherwise
-
isStrictArithmetic
public boolean isStrictArithmetic()
Checks whether the arithmetic triggers errors during evaluation when null is used as an operand.- Returns:
- true if strict, false otherwise
-
isStrictInterpolation
public boolean isStrictInterpolation()
Gets the strict-interpolation flag of this options instance.- Returns:
- true if interpolation strings always return string, false otherwise
-
set
public JexlOptions set(JexlEngine jexl)
Sets options from engine.- Parameters:
jexl- the engine- Returns:
thisinstance
-
set
public JexlOptions set(JexlOptions src)
Sets options from options.- Parameters:
src- the options- Returns:
thisinstance
-
setAntish
public void setAntish(boolean flag)
Sets whether the engine will attempt solving antish variable names from context.- Parameters:
flag- true if antish variables are solved, false otherwise
-
setBooleanLogical
public void setBooleanLogical(boolean flag)
Sets whether logical expressions ("" , ||) coerce their result to boolean.- Parameters:
flag- true or false
-
setCancellable
public void setCancellable(boolean flag)
Sets whether the engine will throw JexlException.Cancel (true) or return null (false) when interrupted during evaluation.- Parameters:
flag- true when cancellable, false otherwise
-
setConstCapture
public void setConstCapture(boolean flag)
Sets whether lambda captured-variables are const or not.When disabled, lambda-captured variables are implicitly converted to read-write local variable (let), when enabled, those are implicitly converted to read-only local variables (const).
- Parameters:
flag- true to enable, false to disable
-
setFlags
public void setFlags(java.lang.String... opts)
Sets this option flags using the +/- syntax.- Parameters:
opts- the option flags
-
setImports
public void setImports(java.util.Collection<java.lang.String> imports)
Sets the optional set of imports.- Parameters:
imports- the imported packages
-
setLexical
public void setLexical(boolean flag)
Sets whether the engine uses a strict block lexical scope during evaluation.- Parameters:
flag- true if lexical scope is used, false otherwise
-
setLexicalShade
public void setLexicalShade(boolean flag)
Sets whether the engine strictly shades global variables. Local symbols shade globals after definition and creating global variables is prohibited during evaluation. If setting to lexical shade, lexical scope is also set.- Parameters:
flag- true if creation is allowed, false otherwise
-
setMathContext
public void setMathContext(java.math.MathContext mcontext)
Sets the arithmetic math context.- Parameters:
mcontext- the context
-
setMathScale
public void setMathScale(int mscale)
Sets the arithmetic math scale.- Parameters:
mscale- the scale
-
setNamespaces
public void setNamespaces(java.util.Map<java.lang.String,java.lang.Object> ns)
Sets the optional map of namespaces.- Parameters:
ns- a namespaces map
-
setSafe
public void setSafe(boolean flag)
Sets whether the engine considers null in navigation expression as null or as errors during evaluation.If safe, encountering null during a navigation expression - dereferencing a method or a field through a null object or property - will not be considered an error but evaluated as null. It is recommended to use setSafe(false) as an explicit default.
- Parameters:
flag- true if safe, false otherwise
-
setSharedInstance
public void setSharedInstance(boolean flag)
Sets wether these options are immutable at runtime.Expert mode; allows instance handled through context to be shared instead of copied.
- Parameters:
flag- true if shared, false if not
-
setSilent
public void setSilent(boolean flag)
Sets whether the engine will throw aJexlExceptionwhen an error is encountered during evaluation.- Parameters:
flag- true if silent, false otherwise
-
setStrict
public void setStrict(boolean flag)
Sets whether the engine considers unknown variables, methods and constructors as errors during evaluation.- Parameters:
flag- true if strict, false otherwise
-
setStrictArithmetic
public void setStrictArithmetic(boolean stricta)
Sets the strict arithmetic flag.- Parameters:
stricta- true or false
-
setStrictInterpolation
public void setStrictInterpolation(boolean strict)
Sets the strict interpolation flag.When strict, interpolation strings composed only of an expression (ie `${...}`) are evaluated as strings; when not strict, integer results are left untouched.
This can affect the results of expressions likemap.`${key}`when key is an integer (or a string); it is almost always possible to usemap[key]to ensure that the key type is not altered.- Parameters:
strict- true or false
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-