org.mvel2
Class ParserContext

java.lang.Object
  extended by org.mvel2.ParserContext
All Implemented Interfaces:
Serializable

public class ParserContext
extends Object
implements 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
protected  boolean variablesEscape
           
 
Constructor Summary
ParserContext()
           
ParserContext(boolean debugSymbols)
           
ParserContext(Map<String,Object> imports, Map<String,Interceptor> interceptors, String sourceFile)
           
ParserContext(Parser rootParser)
           
ParserContext(ParserConfiguration parserConfiguration)
           
 
Method Summary
 void addError(ErrorDetail errorDetail)
           
 void addImport(Class cls)
          Adds an import for the specified Class.
 void addImport(Proto proto)
           
 void addImport(String name, Class cls)
          Adds an import for a specified Class using an alias.
 void addImport(String name, Method method)
          Adds an import for a specified Method representing a static method import using an alias.
 void addImport(String name, MethodStub method)
          Adds a static import for the specified MethodStub with an alias.
 void addIndexedInput(String variable)
           
 void addIndexedInput(String[] variables)
           
 void addIndexedInputs(Collection<String> variables)
           
 void addIndexedLocals(Collection<String> variables)
           
 void addIndexedLocals(String[] variables)
           
 void addInput(String name, Class type)
           
 void addInput(String name, Class type, Class[] typeParameters)
           
 void addInputs(Map<String,Class> inputs)
           
 void addPackageImport(String packageName)
          Adds a package import to a parse session.
 void addTypeParameters(Map<String,Map<String,Type>> typeParameters)
           
 void addTypeParameters(String name, Class type)
           
 void addVariable(String name, Class type)
           
 void addVariable(String name, Class type, boolean failIfNewAssignment)
           
 void addVariables(Map<String,Class> variables)
           
static ParserContext create()
           
 ParserContext createColoringSubcontext()
           
 ParserContext createSubcontext()
           
 void declareFunction(Function function)
           
 List<ErrorDetail> getErrorList()
           
 Function getFunction(String name)
           
 Map getFunctions()
           
 Class getImport(String name)
          Get an import that has been declared, either in the parsed script or programatically
 Map<String,Object> getImports()
           
 ArrayList<String> getIndexedInputs()
           
 String[] getIndexedVarNames()
           
 Map<String,Class> getInputs()
           
 Map<String,Interceptor> getInterceptors()
           
 LineLabel getLastLineLabel()
           
 Type[] getLastTypeParameters()
           
 int getLineCount()
          Get total number of lines declared in the current context.
 int getLineFor(String sourceName, int cursor)
           
 int getLineOffset()
          Get the current line offset.
 ParserConfiguration getParserConfiguration()
           
 Proto getProtoImport(String name)
           
 Parser getRootParser()
           
 String getSourceFile()
           
 MethodStub getStaticImport(String name)
          Get a MethodStub which wraps a static method import.
 Object getStaticOrClassImport(String name)
          Returns either an instance of Class or MethodStub (whichever matches).
 Map<String,Type> getTypeParameters(String name)
           
 Type[] getTypeParametersAsArray(String name)
           
 HashMap<String,Class> getVariables()
           
 Set<String> getVariableScope()
           
 Class getVarOrInputType(String name)
          Return the variable or input type froom the current parser context.
 Class getVarOrInputTypeOrNull(String name)
           
 boolean hasFunction()
           
 boolean hasFunction(String name)
           
 boolean hasImport(String name)
          Tests to see if the specified import exists.
 boolean hasImports()
           
 boolean hasIndexedInputs()
           
 boolean hasProtoImport(String name)
           
 boolean hasVarOrInput(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 amount
 void initializeTables()
          Initializes internal Maps.
 void initLineMapping(String sourceName, char[] expr)
           
 boolean isAllowBootstrapBypass()
           
 boolean isBlockSymbols()
           
 boolean isCompiled()
           
 boolean isDebugSymbols()
           
 boolean isExecutableCodeReached()
           
 boolean isFatalError()
           
 boolean isIndexAllocation()
           
 boolean isLineMapped(String sourceName)
           
 boolean isOptimizerNotified()
           
 boolean isRetainParserState()
           
 boolean isStrictTypeEnforcement()
           
 boolean isStrongTyping()
           
 boolean isVariablesEscape()
           
 boolean isVariableVisible(String var)
           
 boolean isVisitedLine(String sourceName, int lineNumber)
           
 void makeVisible(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(List<ErrorDetail> errorList)
           
 void setExecutableCodeReached(boolean executableCodeReached)
           
 void setFatalError(boolean fatalError)
           
 void setImports(Map<String,Object> imports)
           
 void setIndexAllocation(boolean indexAllocation)
           
 void setInputs(Map<String,Class> inputs)
           
 void setInterceptors(Map<String,Interceptor> interceptors)
           
 LineLabel setLastLineLabel(LineLabel lastLineLabel)
           
 void setLastTypeParameters(Type[] lastTypeParameters)
           
 void setLineAndOffset(int lineCount, int lineOffset)
          Sets both the current line count and line offset
 int 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(String sourceFile)
           
 void setStrictTypeEnforcement(boolean strictTypeEnforcement)
          Enables strict type enforcement -
 void setStrongTyping(boolean strongTyping)
          Enables strong type enforcement.
 void setVariables(HashMap<String,Class> variables)
           
 ParserContext stronglyTyped()
           
 int variableIndexOf(String name)
           
 void visitLine(String sourceName, int lineNumber)
           
 ParserContext withImport(Class clazz)
           
 ParserContext withIndexedVars(String[] varNames)
           
 ParserContext withInput(String name, Class type)
           
 ParserContext withInputs(Map<String,Class> inputs)
           
 ParserContext withTypeParameter(String name, Class type)
           
 ParserContext withTypeParameters(Map<String,Map<String,Type>> typeParameters)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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(Map<String,Object> imports,
                     Map<String,Interceptor> interceptors,
                     String sourceFile)
Method Detail

createSubcontext

public ParserContext createSubcontext()

createColoringSubcontext

public ParserContext createColoringSubcontext()

hasVarOrInput

public boolean hasVarOrInput(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 Class getVarOrInputType(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 Class getVarOrInputTypeOrNull(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 count
lineOffset - The line offset

getImport

public Class getImport(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(String name)
Get a MethodStub which wraps a static method import.

Parameters:
name - The name identifier
Returns:
An instance of MethodStub

getStaticOrClassImport

public Object getStaticOrClassImport(String name)
Returns either an instance of Class or MethodStub (whichever matches).

Parameters:
name - The name identifier.
Returns:
An instance of Class or MethodStub

addPackageImport

public void addPackageImport(String packageName)
Adds a package import to a parse session.

Parameters:
packageName - A fully qualified package (eg. java.util.concurrent).

hasImport

public boolean hasImport(String name)
Tests to see if the specified import exists.

Parameters:
name - A name identifier
Returns:
boolean

hasProtoImport

public boolean hasProtoImport(String name)

getProtoImport

public Proto getProtoImport(String name)

addImport

public void addImport(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(String name,
                      Class cls)
Adds an import for a specified Class using an alias. For example:

 parserContext.addImport("sys", System.class);
 
... doing this would allow an MVEL script to be written as such:

 sys.currentTimeMillis();
 

Parameters:
name - The alias to use
cls - The instance of the Class which represents the imported class.

addImport

public void addImport(String name,
                      Method method)
Adds an import for a specified Method representing a static method import using an alias. For example:

 parserContext.addImport("time", MVEL.getStaticMethod(System.class, "currentTimeMillis", new Class[0]));
 
... doing this allows the System.currentTimeMillis() method to be executed in a script simply by writing time().

Parameters:
name - The alias to use
method - The instance of Method which represents the static import.

addImport

public void addImport(String name,
                      MethodStub method)
Adds a static import for the specified MethodStub with an alias.

Parameters:
name - The alias to use
method - 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(String name,
                        Class type,
                        boolean failIfNewAssignment)

addVariable

public void addVariable(String name,
                        Class type)

addVariables

public void addVariables(Map<String,Class> variables)

addInput

public void addInput(String name,
                     Class type)

addInput

public void addInput(String name,
                     Class type,
                     Class[] typeParameters)

addInputs

public void addInputs(Map<String,Class> inputs)

processTables

public void processTables()

getInputs

public Map<String,Class> getInputs()

setInputs

public void setInputs(Map<String,Class> inputs)

getErrorList

public List<ErrorDetail> getErrorList()

setErrorList

public void setErrorList(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 String getSourceFile()

setSourceFile

public void setSourceFile(String sourceFile)

getInterceptors

public Map<String,Interceptor> getInterceptors()

setInterceptors

public void setInterceptors(Map<String,Interceptor> interceptors)

getImports

public Map<String,Object> getImports()

setImports

public void setImports(Map<String,Object> imports)

pushVariableScope

public void pushVariableScope()

popVariableScope

public void popVariableScope()

makeVisible

public void makeVisible(String var)

getVariableScope

public Set<String> getVariableScope()

isVariableVisible

public boolean isVariableVisible(String var)

getVariables

public HashMap<String,Class> getVariables()

setVariables

public void setVariables(HashMap<String,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(String sourceName)

initLineMapping

public void initLineMapping(String sourceName,
                            char[] expr)

getLineFor

public int getLineFor(String sourceName,
                      int cursor)

isVisitedLine

public boolean isVisitedLine(String sourceName,
                             int lineNumber)

visitLine

public void visitLine(String sourceName,
                      int lineNumber)

getLastLineLabel

public LineLabel getLastLineLabel()

setLastLineLabel

public LineLabel setLastLineLabel(LineLabel lastLineLabel)

hasImports

public boolean hasImports()

declareFunction

public void declareFunction(Function function)

getFunction

public Function getFunction(String name)

getFunctions

public Map getFunctions()

hasFunction

public boolean hasFunction(String name)

hasFunction

public boolean hasFunction()

addTypeParameters

public void addTypeParameters(String name,
                              Class type)

addTypeParameters

public void addTypeParameters(Map<String,Map<String,Type>> typeParameters)

getTypeParameters

public Map<String,Type> getTypeParameters(String name)

getTypeParametersAsArray

public Type[] getTypeParametersAsArray(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()

getIndexedInputs

public ArrayList<String> getIndexedInputs()

addIndexedInput

public void addIndexedInput(String[] variables)

addIndexedLocals

public void addIndexedLocals(String[] variables)

addIndexedLocals

public void addIndexedLocals(Collection<String> variables)

addIndexedInput

public void addIndexedInput(String variable)

addIndexedInputs

public void addIndexedInputs(Collection<String> variables)

variableIndexOf

public int variableIndexOf(String name)

hasIndexedInputs

public boolean hasIndexedInputs()

isIndexAllocation

public boolean isIndexAllocation()

setIndexAllocation

public void setIndexAllocation(boolean indexAllocation)

getParserConfiguration

public ParserConfiguration getParserConfiguration()

getLastTypeParameters

public Type[] getLastTypeParameters()

setLastTypeParameters

public void setLastTypeParameters(Type[] lastTypeParameters)

isAllowBootstrapBypass

public boolean isAllowBootstrapBypass()

setAllowBootstrapBypass

public void setAllowBootstrapBypass(boolean allowBootstrapBypass)

getIndexedVarNames

public String[] getIndexedVarNames()

create

public static ParserContext create()

stronglyTyped

public ParserContext stronglyTyped()

withInput

public ParserContext withInput(String name,
                               Class type)

withInputs

public ParserContext withInputs(Map<String,Class> inputs)

withTypeParameter

public ParserContext withTypeParameter(String name,
                                       Class type)

withTypeParameters

public ParserContext withTypeParameters(Map<String,Map<String,Type>> typeParameters)

withImport

public ParserContext withImport(Class clazz)

withIndexedVars

public ParserContext withIndexedVars(String[] varNames)


Copyright © 2011. All Rights Reserved.