Package org.mvel2
Class ParserContext
- java.lang.Object
-
- org.mvel2.ParserContext
-
- All Implemented Interfaces:
java.io.Serializable
public class ParserContext extends java.lang.Object implements java.io.Serializable
The ParserContext is the main environment object used for sharing state throughout the entire parser/compileShared process. The ParserContext is used to configure the parser/compiler. For example:ParserContext parserContext = new ParserContext(); parserContext.setStrongTyping(true); // turn on strong typing. Serializable comp = MVEL.compileExpression("foo.bar", parserContext);
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
blockSymbols
private boolean
compiled
private java.util.Map<java.lang.String,CompiledExpression>
compiledExpressionCache
private boolean
debugSymbols
private java.util.List<ErrorDetail>
errorList
private java.lang.Object
evaluationContext
private boolean
executableCodeReached
private boolean
fatalError
private boolean
functionContext
private java.util.HashMap<java.lang.String,Function>
globalFunctions
private boolean
indexAllocation
private java.util.ArrayList<java.lang.String>
indexedInputs
private java.util.ArrayList<java.lang.String>
indexedLocals
private java.util.Map<java.lang.String,java.lang.Class>
inputs
private LineLabel
lastLineLabel
private java.lang.reflect.Type[]
lastTypeParameters
private int
lineCount
private int
lineOffset
private boolean
optimizationMode
private ParserContext
parent
private ParserConfiguration
parserConfiguration
private boolean
retainParserState
private java.util.Map<java.lang.String,java.lang.Class>
returnTypeCache
private Parser
rootParser
private java.lang.String
sourceFile
private java.util.Map<java.lang.String,LineMapper.LineLookup>
sourceLineLookups
private boolean
strictTypeEnforcement
private boolean
strongTyping
private java.util.HashMap<java.lang.String,java.util.Map<java.lang.String,java.lang.reflect.Type>>
typeParameters
private java.util.HashMap<java.lang.String,java.lang.Class>
variables
protected boolean
variablesEscape
private java.util.ArrayList<java.util.Set<java.lang.String>>
variableVisibility
private java.util.Map<java.lang.String,java.util.Set<java.lang.Integer>>
visitedLines
-
Constructor Summary
Constructors Constructor Description ParserContext()
ParserContext(boolean debugSymbols)
ParserContext(java.util.Map<java.lang.String,java.lang.Object> imports, java.util.Map<java.lang.String,Interceptor> interceptors, java.lang.String sourceFile)
ParserContext(Parser rootParser)
ParserContext(ParserConfiguration parserConfiguration)
ParserContext(ParserConfiguration parserConfiguration, java.lang.Object evaluationContext)
ParserContext(ParserConfiguration parserConfiguration, ParserContext parent, boolean functionContext)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addError(ErrorDetail errorDetail)
void
addImport(java.lang.Class cls)
Adds an import for the specified Class.void
addImport(java.lang.String name, java.lang.Class cls)
Adds an import for a specified Class using an alias.void
addImport(java.lang.String name, java.lang.reflect.Method method)
Adds an import for a specified Method representing a static method import using an alias.void
addImport(java.lang.String name, MethodStub method)
Adds a static import for the specifiedMethodStub
with an alias.void
addImport(Proto proto)
void
addIndexedInput(java.lang.String variable)
void
addIndexedInput(java.lang.String[] variables)
void
addIndexedInputs(java.util.Collection<java.lang.String> variables)
void
addIndexedLocals(java.lang.String[] variables)
void
addIndexedLocals(java.util.Collection<java.lang.String> variables)
void
addInput(java.lang.String name, java.lang.Class type)
void
addInput(java.lang.String name, java.lang.Class type, java.lang.Class[] typeParameters)
void
addInputs(java.util.Map<java.lang.String,java.lang.Class> inputs)
void
addPackageImport(java.lang.String packageName)
Adds a package import to a parse session.void
addTypeParameters(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.reflect.Type>> typeParameters)
void
addVariable(java.lang.String name, java.lang.Class type)
void
addVariable(java.lang.String name, java.lang.Class type, boolean failIfNewAssignment)
void
addVariables(java.util.Map<java.lang.String,java.lang.Class> variables)
static ParserContext
create()
ParserContext
createColoringSubcontext()
ParserContext
createSubcontext()
void
declareFunction(Function function)
java.lang.ClassLoader
getClassLoader()
java.util.Map<java.lang.String,CompiledExpression>
getCompiledExpressionCache()
java.util.List<ErrorDetail>
getErrorList()
java.lang.Object
getEvaluationContext()
Function
getFunction(java.lang.String name)
java.util.Map
getFunctions()
java.lang.Class
getImport(java.lang.String name)
Get an import that has been declared, either in the parsed script or programaticallyjava.util.Map<java.lang.String,java.lang.Object>
getImports()
java.util.ArrayList<java.lang.String>
getIndexedInputs()
java.lang.String[]
getIndexedVarNames()
java.util.Map<java.lang.String,java.lang.Class>
getInputs()
java.util.Map<java.lang.String,Interceptor>
getInterceptors()
LineLabel
getLastLineLabel()
java.lang.reflect.Type[]
getLastTypeParameters()
int
getLineCount()
Get total number of lines declared in the current context.int
getLineFor(java.lang.String sourceName, int cursor)
int
getLineOffset()
Get the current line offset.ParserConfiguration
getParserConfiguration()
Proto
getProtoImport(java.lang.String name)
java.util.Map<java.lang.String,java.lang.Class>
getReturnTypeCache()
Parser
getRootParser()
java.lang.String
getSourceFile()
MethodStub
getStaticImport(java.lang.String name)
Get aMethodStub
which wraps a static method import.java.lang.Object
getStaticOrClassImport(java.lang.String name)
Returns either an instance of Class orMethodStub
(whichever matches).java.util.Map<java.lang.String,java.lang.reflect.Type>
getTypeParameters(java.lang.String name)
java.lang.reflect.Type[]
getTypeParametersAsArray(java.lang.String name)
java.util.HashMap<java.lang.String,java.lang.Class>
getVariables()
java.util.Set<java.lang.String>
getVariableScope()
java.lang.Class
getVarOrInputType(java.lang.String name)
Return the variable or input type froom the current parser context.java.lang.Class
getVarOrInputTypeOrNull(java.lang.String name)
boolean
hasFunction()
boolean
hasFunction(java.lang.String name)
boolean
hasImport(java.lang.String name)
Tests to see if the specified import exists.boolean
hasImports()
boolean
hasIndexedInputs()
boolean
hasProtoImport(java.lang.String name)
boolean
hasVarOrInput(java.lang.String name)
Tests whether or not a variable or input exists in the current parser context.int
incrementLineCount(int increment)
Increments the current line count by the specified amountvoid
initializeTables()
Initializes internal Maps.private void
initIndexedVariables()
void
initLineMapping(java.lang.String sourceName, char[] expr)
private void
initVariableVisibility()
boolean
isAllowBootstrapBypass()
boolean
isBlockSymbols()
boolean
isCompiled()
boolean
isDebugSymbols()
boolean
isExecutableCodeReached()
boolean
isFatalError()
boolean
isFunctionContext()
boolean
isIndexAllocation()
boolean
isLineMapped(java.lang.String sourceName)
boolean
isOptimizerNotified()
boolean
isRetainParserState()
boolean
isStrictTypeEnforcement()
boolean
isStrongTyping()
boolean
isVariablesEscape()
boolean
isVariableVisible(java.lang.String var)
boolean
isVisitedLine(java.lang.String sourceName, int lineNumber)
void
makeVisible(java.lang.String var)
void
optimizationNotify()
void
popVariableScope()
void
processTables()
void
pushVariableScope()
void
setAllowBootstrapBypass(boolean allowBootstrapBypass)
void
setBlockSymbols(boolean blockSymbols)
void
setCompiled(boolean compiled)
void
setDebugSymbols(boolean debugSymbols)
void
setErrorList(java.util.List<ErrorDetail> errorList)
void
setExecutableCodeReached(boolean executableCodeReached)
void
setFatalError(boolean fatalError)
void
setImports(java.util.Map<java.lang.String,java.lang.Object> imports)
void
setIndexAllocation(boolean indexAllocation)
void
setInputs(java.util.Map<java.lang.String,java.lang.Class> inputs)
void
setInterceptors(java.util.Map<java.lang.String,Interceptor> interceptors)
LineLabel
setLastLineLabel(LineLabel lastLineLabel)
void
setLastTypeParameters(java.lang.reflect.Type[] lastTypeParameters)
void
setLineAndOffset(int lineCount, int lineOffset)
Sets both the current line count and line offsetint
setLineCount(int lineCount)
Set the current number of lines in the current context.void
setLineOffset(int lineOffset)
Sets the current line offset.void
setRetainParserState(boolean retainParserState)
void
setRootParser(Parser rootParser)
void
setSourceFile(java.lang.String sourceFile)
void
setStrictTypeEnforcement(boolean strictTypeEnforcement)
Enables strict type enforcement -void
setStrongTyping(boolean strongTyping)
Enables strong type enforcement.void
setVariables(java.util.HashMap<java.lang.String,java.lang.Class> variables)
ParserContext
stronglyTyped()
int
variableIndexOf(java.lang.String name)
void
visitLine(java.lang.String sourceName, int lineNumber)
ParserContext
withImport(java.lang.Class clazz)
ParserContext
withIndexedVars(java.lang.String[] varNames)
ParserContext
withInput(java.lang.String name, java.lang.Class type)
ParserContext
withInputs(java.util.Map<java.lang.String,java.lang.Class> inputs)
ParserContext
withTypeParameters(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.reflect.Type>> typeParameters)
-
-
-
Field Detail
-
sourceFile
private java.lang.String sourceFile
-
lineCount
private int lineCount
-
lineOffset
private int lineOffset
-
parent
private ParserContext parent
-
parserConfiguration
private ParserConfiguration parserConfiguration
-
evaluationContext
private java.lang.Object evaluationContext
-
indexedInputs
private java.util.ArrayList<java.lang.String> indexedInputs
-
indexedLocals
private java.util.ArrayList<java.lang.String> indexedLocals
-
variableVisibility
private java.util.ArrayList<java.util.Set<java.lang.String>> variableVisibility
-
variables
private java.util.HashMap<java.lang.String,java.lang.Class> variables
-
inputs
private java.util.Map<java.lang.String,java.lang.Class> inputs
-
typeParameters
private transient java.util.HashMap<java.lang.String,java.util.Map<java.lang.String,java.lang.reflect.Type>> typeParameters
-
lastTypeParameters
private transient java.lang.reflect.Type[] lastTypeParameters
-
globalFunctions
private java.util.HashMap<java.lang.String,Function> globalFunctions
-
errorList
private transient java.util.List<ErrorDetail> errorList
-
sourceLineLookups
private transient java.util.Map<java.lang.String,LineMapper.LineLookup> sourceLineLookups
-
visitedLines
private transient java.util.Map<java.lang.String,java.util.Set<java.lang.Integer>> visitedLines
-
lastLineLabel
private LineLabel lastLineLabel
-
rootParser
private transient Parser rootParser
-
compiledExpressionCache
private transient java.util.Map<java.lang.String,CompiledExpression> compiledExpressionCache
-
returnTypeCache
private transient java.util.Map<java.lang.String,java.lang.Class> returnTypeCache
-
functionContext
private boolean functionContext
-
compiled
private boolean compiled
-
strictTypeEnforcement
private boolean strictTypeEnforcement
-
strongTyping
private boolean strongTyping
-
optimizationMode
private boolean optimizationMode
-
fatalError
private boolean fatalError
-
retainParserState
private boolean retainParserState
-
debugSymbols
private boolean debugSymbols
-
blockSymbols
private boolean blockSymbols
-
executableCodeReached
private boolean executableCodeReached
-
indexAllocation
private boolean indexAllocation
-
variablesEscape
protected boolean variablesEscape
-
-
Constructor Detail
-
ParserContext
public ParserContext()
-
ParserContext
public ParserContext(boolean debugSymbols)
-
ParserContext
public ParserContext(Parser rootParser)
-
ParserContext
public ParserContext(ParserConfiguration parserConfiguration)
-
ParserContext
public ParserContext(ParserConfiguration parserConfiguration, java.lang.Object evaluationContext)
-
ParserContext
public ParserContext(ParserConfiguration parserConfiguration, ParserContext parent, boolean functionContext)
-
ParserContext
public ParserContext(java.util.Map<java.lang.String,java.lang.Object> imports, java.util.Map<java.lang.String,Interceptor> interceptors, java.lang.String sourceFile)
-
-
Method Detail
-
createSubcontext
public ParserContext createSubcontext()
-
createColoringSubcontext
public ParserContext createColoringSubcontext()
-
hasVarOrInput
public boolean hasVarOrInput(java.lang.String name)
Tests whether or not a variable or input exists in the current parser context.- Parameters:
name
- The name of the identifier.- Returns:
- boolean
-
getVarOrInputType
public java.lang.Class getVarOrInputType(java.lang.String name)
Return the variable or input type froom the current parser context. Returns Object.class if the type cannot be determined.- Parameters:
name
- The name of the identifier- Returns:
- boolean
-
getVarOrInputTypeOrNull
public java.lang.Class getVarOrInputTypeOrNull(java.lang.String name)
-
getLineCount
public int getLineCount()
Get total number of lines declared in the current context.- Returns:
- int of lines
-
setLineCount
public int setLineCount(int lineCount)
Set the current number of lines in the current context. (Generally only used by the compiler)- Parameters:
lineCount
- The number of lines- Returns:
- int of lines
-
incrementLineCount
public int incrementLineCount(int increment)
Increments the current line count by the specified amount- Parameters:
increment
- The number of lines to increment- Returns:
- int of lines
-
getLineOffset
public int getLineOffset()
Get the current line offset. This measures the number of cursor positions back to the beginning of the line.- Returns:
- int offset
-
setLineOffset
public void setLineOffset(int lineOffset)
Sets the current line offset. (Generally only used by the compiler)- Parameters:
lineOffset
- The offset amount
-
setLineAndOffset
public void setLineAndOffset(int lineCount, int lineOffset)
Sets both the current line count and line offset- Parameters:
lineCount
- The line countlineOffset
- The line offset
-
getImport
public java.lang.Class getImport(java.lang.String name)
Get an import that has been declared, either in the parsed script or programatically- Parameters:
name
- The name identifier for the imported class (ie. "HashMap")- Returns:
- An instance of Class denoting the imported class.
-
getStaticImport
public MethodStub getStaticImport(java.lang.String name)
Get aMethodStub
which wraps a static method import.- Parameters:
name
- The name identifier- Returns:
- An instance of
MethodStub
-
getStaticOrClassImport
public java.lang.Object getStaticOrClassImport(java.lang.String name)
Returns either an instance of Class orMethodStub
(whichever matches).- Parameters:
name
- The name identifier.- Returns:
- An instance of Class or
MethodStub
-
addPackageImport
public void addPackageImport(java.lang.String packageName)
Adds a package import to a parse session.- Parameters:
packageName
- A fully qualified package (eg. java.util.concurrent).
-
hasImport
public boolean hasImport(java.lang.String name)
Tests to see if the specified import exists.- Parameters:
name
- A name identifier- Returns:
- boolean
-
hasProtoImport
public boolean hasProtoImport(java.lang.String name)
-
getProtoImport
public Proto getProtoImport(java.lang.String name)
-
addImport
public void addImport(java.lang.Class cls)
Adds an import for the specified Class.- Parameters:
cls
- The instance of the Class which represents the imported class.
-
addImport
public void addImport(Proto proto)
-
addImport
public void addImport(java.lang.String name, java.lang.Class cls)
Adds an import for a specified Class using an alias. For example:
... doing this would allow an MVEL script to be written as such:parserContext.addImport("sys", System.class);
sys.currentTimeMillis();
- Parameters:
name
- The alias to usecls
- The instance of the Class which represents the imported class.
-
addImport
public void addImport(java.lang.String name, java.lang.reflect.Method method)
Adds an import for a specified Method representing a static method import using an alias. For example:
... doing this allows the System.currentTimeMillis() method to be executed in a script simply by writing time().parserContext.addImport("time", MVEL.getStaticMethod(System.class, "currentTimeMillis", new Class[0]));
- Parameters:
name
- The alias to usemethod
- The instance of Method which represents the static import.
-
addImport
public void addImport(java.lang.String name, MethodStub method)
Adds a static import for the specifiedMethodStub
with an alias.- Parameters:
name
- The alias to usemethod
- The instance of Method which represents the static import.- See Also:
addImport(String, org.mvel2.util.MethodStub)
-
initializeTables
public void initializeTables()
Initializes internal Maps. Called by the compiler.
-
addVariable
public void addVariable(java.lang.String name, java.lang.Class type, boolean failIfNewAssignment)
-
addVariable
public void addVariable(java.lang.String name, java.lang.Class type)
-
addVariables
public void addVariables(java.util.Map<java.lang.String,java.lang.Class> variables)
-
addInput
public void addInput(java.lang.String name, java.lang.Class type)
-
addInput
public void addInput(java.lang.String name, java.lang.Class type, java.lang.Class[] typeParameters)
-
addInputs
public void addInputs(java.util.Map<java.lang.String,java.lang.Class> inputs)
-
processTables
public void processTables()
-
getInputs
public java.util.Map<java.lang.String,java.lang.Class> getInputs()
-
setInputs
public void setInputs(java.util.Map<java.lang.String,java.lang.Class> inputs)
-
getErrorList
public java.util.List<ErrorDetail> getErrorList()
-
setErrorList
public void setErrorList(java.util.List<ErrorDetail> errorList)
-
addError
public void addError(ErrorDetail errorDetail)
-
isFatalError
public boolean isFatalError()
-
setFatalError
public void setFatalError(boolean fatalError)
-
isStrictTypeEnforcement
public boolean isStrictTypeEnforcement()
-
setStrictTypeEnforcement
public void setStrictTypeEnforcement(boolean strictTypeEnforcement)
Enables strict type enforcement -- Parameters:
strictTypeEnforcement
- -
-
isStrongTyping
public boolean isStrongTyping()
-
setStrongTyping
public void setStrongTyping(boolean strongTyping)
Enables strong type enforcement.- Parameters:
strongTyping
- -
-
isRetainParserState
public boolean isRetainParserState()
-
setRetainParserState
public void setRetainParserState(boolean retainParserState)
-
getRootParser
public Parser getRootParser()
-
setRootParser
public void setRootParser(Parser rootParser)
-
getSourceFile
public java.lang.String getSourceFile()
-
setSourceFile
public void setSourceFile(java.lang.String sourceFile)
-
getInterceptors
public java.util.Map<java.lang.String,Interceptor> getInterceptors()
-
setInterceptors
public void setInterceptors(java.util.Map<java.lang.String,Interceptor> interceptors)
-
getImports
public java.util.Map<java.lang.String,java.lang.Object> getImports()
-
setImports
public void setImports(java.util.Map<java.lang.String,java.lang.Object> imports)
-
initVariableVisibility
private void initVariableVisibility()
-
pushVariableScope
public void pushVariableScope()
-
popVariableScope
public void popVariableScope()
-
makeVisible
public void makeVisible(java.lang.String var)
-
getVariableScope
public java.util.Set<java.lang.String> getVariableScope()
-
isVariableVisible
public boolean isVariableVisible(java.lang.String var)
-
getVariables
public java.util.HashMap<java.lang.String,java.lang.Class> getVariables()
-
setVariables
public void setVariables(java.util.HashMap<java.lang.String,java.lang.Class> variables)
-
isCompiled
public boolean isCompiled()
-
setCompiled
public void setCompiled(boolean compiled)
-
isDebugSymbols
public boolean isDebugSymbols()
-
setDebugSymbols
public void setDebugSymbols(boolean debugSymbols)
-
isLineMapped
public boolean isLineMapped(java.lang.String sourceName)
-
initLineMapping
public void initLineMapping(java.lang.String sourceName, char[] expr)
-
getLineFor
public int getLineFor(java.lang.String sourceName, int cursor)
-
isVisitedLine
public boolean isVisitedLine(java.lang.String sourceName, int lineNumber)
-
visitLine
public void visitLine(java.lang.String sourceName, int lineNumber)
-
getLastLineLabel
public LineLabel getLastLineLabel()
-
hasImports
public boolean hasImports()
-
declareFunction
public void declareFunction(Function function)
-
getFunction
public Function getFunction(java.lang.String name)
-
getFunctions
public java.util.Map getFunctions()
-
hasFunction
public boolean hasFunction(java.lang.String name)
-
hasFunction
public boolean hasFunction()
-
addTypeParameters
public void addTypeParameters(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.reflect.Type>> typeParameters)
-
getTypeParameters
public java.util.Map<java.lang.String,java.lang.reflect.Type> getTypeParameters(java.lang.String name)
-
getTypeParametersAsArray
public java.lang.reflect.Type[] getTypeParametersAsArray(java.lang.String name)
-
isBlockSymbols
public boolean isBlockSymbols()
-
setBlockSymbols
public void setBlockSymbols(boolean blockSymbols)
-
isVariablesEscape
public boolean isVariablesEscape()
-
isExecutableCodeReached
public boolean isExecutableCodeReached()
-
setExecutableCodeReached
public void setExecutableCodeReached(boolean executableCodeReached)
-
optimizationNotify
public void optimizationNotify()
-
isOptimizerNotified
public boolean isOptimizerNotified()
-
initIndexedVariables
private void initIndexedVariables()
-
getIndexedInputs
public java.util.ArrayList<java.lang.String> getIndexedInputs()
-
addIndexedInput
public void addIndexedInput(java.lang.String[] variables)
-
addIndexedLocals
public void addIndexedLocals(java.lang.String[] variables)
-
addIndexedLocals
public void addIndexedLocals(java.util.Collection<java.lang.String> variables)
-
addIndexedInput
public void addIndexedInput(java.lang.String variable)
-
addIndexedInputs
public void addIndexedInputs(java.util.Collection<java.lang.String> variables)
-
variableIndexOf
public int variableIndexOf(java.lang.String name)
-
getEvaluationContext
public java.lang.Object getEvaluationContext()
-
hasIndexedInputs
public boolean hasIndexedInputs()
-
isIndexAllocation
public boolean isIndexAllocation()
-
setIndexAllocation
public void setIndexAllocation(boolean indexAllocation)
-
isFunctionContext
public boolean isFunctionContext()
-
getParserConfiguration
public ParserConfiguration getParserConfiguration()
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
-
getLastTypeParameters
public java.lang.reflect.Type[] getLastTypeParameters()
-
setLastTypeParameters
public void setLastTypeParameters(java.lang.reflect.Type[] lastTypeParameters)
-
isAllowBootstrapBypass
public boolean isAllowBootstrapBypass()
-
setAllowBootstrapBypass
public void setAllowBootstrapBypass(boolean allowBootstrapBypass)
-
getIndexedVarNames
public java.lang.String[] getIndexedVarNames()
-
getCompiledExpressionCache
public java.util.Map<java.lang.String,CompiledExpression> getCompiledExpressionCache()
-
getReturnTypeCache
public java.util.Map<java.lang.String,java.lang.Class> getReturnTypeCache()
-
create
public static ParserContext create()
-
stronglyTyped
public ParserContext stronglyTyped()
-
withInput
public ParserContext withInput(java.lang.String name, java.lang.Class type)
-
withInputs
public ParserContext withInputs(java.util.Map<java.lang.String,java.lang.Class> inputs)
-
withTypeParameters
public ParserContext withTypeParameters(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.reflect.Type>> typeParameters)
-
withImport
public ParserContext withImport(java.lang.Class clazz)
-
withIndexedVars
public ParserContext withIndexedVars(java.lang.String[] varNames)
-
-