Package ognl.enhance
Class ExpressionCompiler
- java.lang.Object
-
- ognl.enhance.ExpressionCompiler
-
- All Implemented Interfaces:
OgnlExpressionCompiler
public class ExpressionCompiler extends java.lang.Object implements OgnlExpressionCompiler
Responsible for managing/providing functionality related to compiling generated java source expressions via bytecode enhancements for a given ognl expression.
-
-
Field Summary
Fields Modifier and Type Field Description protected intclassCounterprotected javassist.ClassPoolclassPoolJavassist class definition pool.protected java.util.Map<ClassResolver,EnhancedClassLoader>loadersClassLoaderinstances.static java.lang.StringPRE_CASTKey used to store any java source string casting statements in theOgnlContextduring class compilation.-
Fields inherited from interface ognl.enhance.OgnlExpressionCompiler
ROOT_TYPE
-
-
Constructor Summary
Constructors Constructor Description ExpressionCompiler()Default constructor, does nothing.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidaddCastString(OgnlContext context, java.lang.String cast)Used bycastExpression(OgnlContext, Node, String)to store the cast java source string in to the currentOgnlContext.java.lang.StringcastExpression(OgnlContext context, Node expression, java.lang.String body)Used primarily by AST types likeASTChainwherefoo.bar.idtype references may need to be cast multiple times in order to properly resolve the members in a compiled statement.voidcompileExpression(OgnlContext context, Node expression, java.lang.Object root)The core method executed to compile a specific expression.booleancontainsMethod(java.lang.reflect.Method method, java.lang.Class<?> clazz)Helper utility method used by compiler to help resolve class->method mappings during method calls toOgnlExpressionCompiler.getSuperOrInterfaceClass(java.lang.reflect.Method, Class).java.lang.StringcreateLocalReference(OgnlContext context, java.lang.String expression, java.lang.Class<?> type)Method is used for expressions where multiple inner parameter method calls in generated java source strings cause javassit failures.private voidcreateLocalReferences(OgnlContext context, javassist.ClassPool pool, javassist.CtClass clazz, javassist.CtClass[] params)protected java.lang.StringgenerateGetter(OgnlContext context, javassist.CtClass newClass, javassist.ClassPool pool, javassist.CtMethod valueGetter, Node expression, java.lang.Object root)protected java.lang.StringgenerateOgnlGetter(javassist.CtClass clazz, javassist.CtMethod valueGetter, javassist.CtField node)Fail safe getter creation when normal compilation fails.protected java.lang.StringgenerateOgnlSetter(javassist.CtClass clazz, javassist.CtMethod valueSetter, javassist.CtField node)Fail safe setter creation when normal compilation fails.protected java.lang.StringgenerateSetter(OgnlContext context, javassist.CtClass newClass, javassist.ClassPool pool, javassist.CtMethod valueSetter, Node expression, java.lang.Object root)static java.lang.StringgetCastString(java.lang.Class<?> type)Returns the appropriate casting expression (minus parens) for the specified class type.protected EnhancedClassLoadergetClassLoader(OgnlContext context)Creates aClassLoaderinstance compatible with the javassist classloader and normal OGNL class resolving semantics.java.lang.StringgetClassName(java.lang.Class<?> clazz)Gets a javassist safe class string for the given class instance.private java.lang.StringgetClassName(java.lang.Class<?> clazz, java.lang.Class<?>[] interfaces)protected javassist.ClassPoolgetClassPool(OgnlContext context, EnhancedClassLoader loader)Gets either a new or existingClassPoolfor use in compiling javassist classes.protected javassist.CtClassgetCtClass(java.lang.Class<?> searchClass)Loads a new class definition via javassist for the specified class.java.lang.Class<?>getInterfaceClass(java.lang.Class<?> clazz)Used in places where the preferredOgnlExpressionCompiler.getSuperOrInterfaceClass(java.lang.reflect.Method, Class)isn't possible because the method isn't known for a class.private java.lang.Class<?>getInterfaceClass(java.lang.Class<?> clazz, java.lang.Class<?>[] interfaces)static java.lang.StringgetRootExpression(Node expression, java.lang.Object root, OgnlContext context)Convenience method called by many different property/method resolving AST types to get a root expression resolving string for the given node.java.lang.Class<?>getRootExpressionClass(Node rootNode, OgnlContext context)For a given root object type returns the base class type to be used in root referenced expressions.java.lang.Class<?>getSuperOrInterfaceClass(java.lang.reflect.Method method, java.lang.Class<?> clazz)For the givenMethodand class finds the highest level interface class this combination can be cast to.protected java.lang.Class<?>instantiateClass(javassist.ClassPool pool, javassist.CtClass newClass)Called whennewClasshas been fully populated and is ready to be instantiated.static booleanshouldCast(Node expression)Used bygetRootExpression(Node, Object, OgnlContext)to determine if the expression needs to be cast at all.
-
-
-
Field Detail
-
PRE_CAST
public static final java.lang.String PRE_CAST
Key used to store any java source string casting statements in theOgnlContextduring class compilation.- See Also:
- Constant Field Values
-
loaders
protected java.util.Map<ClassResolver,EnhancedClassLoader> loaders
ClassLoaderinstances.
-
classPool
protected javassist.ClassPool classPool
Javassist class definition pool.
-
classCounter
protected int classCounter
-
-
Method Detail
-
addCastString
public static void addCastString(OgnlContext context, java.lang.String cast)
Used bycastExpression(OgnlContext, Node, String)to store the cast java source string in to the currentOgnlContext. This will either add to the existing string present if it already exists or create a new instance and store it using the static key ofPRE_CAST.- Parameters:
context- The current execution context.cast- The java source string to store in to the context.
-
getCastString
public static java.lang.String getCastString(java.lang.Class<?> type)
Returns the appropriate casting expression (minus parens) for the specified class type.For instance, if given an
Integerobject the string"java.lang.Integer"would be returned. For an array of primitive ints"int[]"and so on..- Parameters:
type- The class to cast a string expression for.- Returns:
- The converted raw string version of the class name.
-
getRootExpression
public static java.lang.String getRootExpression(Node expression, java.lang.Object root, OgnlContext context)
Convenience method called by many different property/method resolving AST types to get a root expression resolving string for the given node. The callers are mostly ignorant and rely on this method to properly determine if the expression should be cast at all and take the appropriate actions if it should.- Parameters:
expression- The node to check and generate a root expression to if necessary.root- The root object for this execution.context- The current execution context.- Returns:
- Either an empty string or a root path java source string compatible with javassist compilations
from the root object up to the specified
Node.
-
shouldCast
public static boolean shouldCast(Node expression)
Used bygetRootExpression(Node, Object, OgnlContext)to determine if the expression needs to be cast at all.- Parameters:
expression- The node to check against.- Returns:
- Yes if the node type should be cast - false otherwise.
-
castExpression
public java.lang.String castExpression(OgnlContext context, Node expression, java.lang.String body)
Description copied from interface:OgnlExpressionCompilerUsed primarily by AST types likeASTChainwherefoo.bar.idtype references may need to be cast multiple times in order to properly resolve the members in a compiled statement.This method should be using the various
OgnlContext.getCurrentType()/OgnlContext.getCurrentAccessor()methods to inspect the type stack and properly cast to the right classes - but only when necessary.- Specified by:
castExpressionin interfaceOgnlExpressionCompiler- Parameters:
context- The current execution context.expression- The node being checked for casting.body- The java source string generated by the given node.- Returns:
- The body string parameter plus any additional casting syntax needed to make the expression resolvable.
-
getClassName
public java.lang.String getClassName(java.lang.Class<?> clazz)
Description copied from interface:OgnlExpressionCompilerGets a javassist safe class string for the given class instance. This is especially useful for handling array vs. normal class casting strings.- Specified by:
getClassNamein interfaceOgnlExpressionCompiler- Parameters:
clazz- The class to get a string equivalent javassist compatible string reference for.- Returns:
- The string equivalent of the class.
-
getClassName
private java.lang.String getClassName(java.lang.Class<?> clazz, java.lang.Class<?>[] interfaces)
-
getSuperOrInterfaceClass
public java.lang.Class<?> getSuperOrInterfaceClass(java.lang.reflect.Method method, java.lang.Class<?> clazz)Description copied from interface:OgnlExpressionCompilerFor the givenMethodand class finds the highest level interface class this combination can be cast to.- Specified by:
getSuperOrInterfaceClassin interfaceOgnlExpressionCompiler- Parameters:
method- The method the class must implement.clazz- The current class being worked with.- Returns:
- The highest level interface / class that the referenced
Methodis declared in.
-
containsMethod
public boolean containsMethod(java.lang.reflect.Method method, java.lang.Class<?> clazz)Helper utility method used by compiler to help resolve class->method mappings during method calls toOgnlExpressionCompiler.getSuperOrInterfaceClass(java.lang.reflect.Method, Class).- Parameters:
method- The method to check for existance of.clazz- The class to check for the existance of a matching method definition to the method passed in.- Returns:
- True if the class contains the specified method, false otherwise.
-
getInterfaceClass
public java.lang.Class<?> getInterfaceClass(java.lang.Class<?> clazz)
Description copied from interface:OgnlExpressionCompilerUsed in places where the preferredOgnlExpressionCompiler.getSuperOrInterfaceClass(java.lang.reflect.Method, Class)isn't possible because the method isn't known for a class. Attempts to upcast the given class to the next available non-private accessible class so that compiled expressions can reference the interface class of an instance so as not to be compiled in to overly specific statements.- Specified by:
getInterfaceClassin interfaceOgnlExpressionCompiler- Parameters:
clazz- The class to attempt to find a compatible interface for.- Returns:
- The same class if no higher level interface could be matched against or the interface equivalent class.
-
getInterfaceClass
private java.lang.Class<?> getInterfaceClass(java.lang.Class<?> clazz, java.lang.Class<?>[] interfaces)
-
getRootExpressionClass
public java.lang.Class<?> getRootExpressionClass(Node rootNode, OgnlContext context)
Description copied from interface:OgnlExpressionCompilerFor a given root object type returns the base class type to be used in root referenced expressions. This helps in some instances where the root objects themselves are compiled javassist instances that need more generic class equivalents to cast to.- Specified by:
getRootExpressionClassin interfaceOgnlExpressionCompiler- Parameters:
rootNode- The root expression node.context- The current execution context.- Returns:
- The root expression class type to cast to for this node.
-
compileExpression
public void compileExpression(OgnlContext context, Node expression, java.lang.Object root) throws java.lang.Exception
Description copied from interface:OgnlExpressionCompilerThe core method executed to compile a specific expression. It is expected that this expression always return aNodewith a non nullNode.getAccessor()instance - unless an exception is thrown by the method or the statement wasn't compilable in this instance because of missing/null objects in the expression. These instances may in some cases continue to call this compilation method until the expression is resolvable.- Specified by:
compileExpressionin interfaceOgnlExpressionCompiler- Parameters:
context- The context of execution.expression- The pre-parsed root expression node to compile.root- The root object for the expression - may be null in many instances so some implementations may exit- Throws:
java.lang.Exception- If an error occurs compiling the expression and no strategy has been implemented to handle incremental expression compilation for incomplete expression members.
-
instantiateClass
protected java.lang.Class<?> instantiateClass(javassist.ClassPool pool, javassist.CtClass newClass) throws javassist.CannotCompileExceptionCalled whennewClasshas been fully populated and is ready to be instantiated.- Parameters:
pool- the javassist ClassPool contextnewClass- the definition of the new class- Returns:
- The compiled class
- Throws:
javassist.CannotCompileException- if thrown by javassist
-
generateGetter
protected java.lang.String generateGetter(OgnlContext context, javassist.CtClass newClass, javassist.ClassPool pool, javassist.CtMethod valueGetter, Node expression, java.lang.Object root) throws java.lang.Exception
- Throws:
java.lang.Exception
-
createLocalReference
public java.lang.String createLocalReference(OgnlContext context, java.lang.String expression, java.lang.Class<?> type)
Description copied from interface:OgnlExpressionCompilerMethod is used for expressions where multiple inner parameter method calls in generated java source strings cause javassit failures. It is hacky and cumbersome to have to generate expressions this way but it's the only current known way to make javassist happy.Takes an expression block generated by a node and creates a new method on the base object being compiled so that sufficiently complicated sub expression blocks can be broken out in to distinct methods to be referenced by the core accessor / setter methods in the base compiled root object.
- Specified by:
createLocalReferencein interfaceOgnlExpressionCompiler- Parameters:
context- The current execution context.expression- The java source expression to dump in to a seperate method reference.type- The return type that should be specified for the new method.- Returns:
- The method name that will be used to reference the sub expression in place of the actual sub expression itself.
-
createLocalReferences
private void createLocalReferences(OgnlContext context, javassist.ClassPool pool, javassist.CtClass clazz, javassist.CtClass[] params) throws javassist.CannotCompileException, javassist.NotFoundException
- Throws:
javassist.CannotCompileExceptionjavassist.NotFoundException
-
generateSetter
protected java.lang.String generateSetter(OgnlContext context, javassist.CtClass newClass, javassist.ClassPool pool, javassist.CtMethod valueSetter, Node expression, java.lang.Object root) throws java.lang.Exception
- Throws:
java.lang.Exception
-
generateOgnlGetter
protected java.lang.String generateOgnlGetter(javassist.CtClass clazz, javassist.CtMethod valueGetter, javassist.CtField node) throws java.lang.ExceptionFail safe getter creation when normal compilation fails.- Parameters:
clazz- The javassist class the new method should be attached to.valueGetter- The method definition the generated code will be contained within.node- The root expression node.- Returns:
- The generated source string for this method, the method will still be added via the javassist API either way so this is really a convenience for exception reporting / debugging.
- Throws:
java.lang.Exception- If a javassist error occurs.
-
generateOgnlSetter
protected java.lang.String generateOgnlSetter(javassist.CtClass clazz, javassist.CtMethod valueSetter, javassist.CtField node) throws java.lang.ExceptionFail safe setter creation when normal compilation fails.- Parameters:
clazz- The javassist class the new method should be attached to.valueSetter- The method definition the generated code will be contained within.node- The root expression node.- Returns:
- The generated source string for this method, the method will still be added via the javassist API either way so this is really a convenience for exception reporting / debugging.
- Throws:
java.lang.Exception- If a javassist error occurs.
-
getClassLoader
protected EnhancedClassLoader getClassLoader(OgnlContext context)
Creates aClassLoaderinstance compatible with the javassist classloader and normal OGNL class resolving semantics.- Parameters:
context- The current execution context.- Returns:
- The created
ClassLoaderinstance.
-
getCtClass
protected javassist.CtClass getCtClass(java.lang.Class<?> searchClass) throws javassist.NotFoundExceptionLoads a new class definition via javassist for the specified class.- Parameters:
searchClass- The class to load.- Returns:
- The javassist class equivalent.
- Throws:
javassist.NotFoundException- When the class definition can't be found.
-
getClassPool
protected javassist.ClassPool getClassPool(OgnlContext context, EnhancedClassLoader loader)
Gets either a new or existingClassPoolfor use in compiling javassist classes. A new class path object is inserted in to the returnedClassPoolusing the passed inloaderinstance if a new pool needs to be created.- Parameters:
context- The current execution context.loader- TheClassLoaderinstance to use - as returned bygetClassLoader(OgnlContext).- Returns:
- The existing or new
ClassPoolinstance.
-
-