net.jakubholy.jeeutils.jsfelcheck
Class JsfStaticAnalyzer

java.lang.Object
  extended by net.jakubholy.jeeutils.jsfelcheck.JsfStaticAnalyzer

public class JsfStaticAnalyzer
extends Object

Perform analysis of (selected) JSF 1.1 JSP files and validate that all EL expressions reference only existing managed beans and their properties/action methods.

For local variables, such as the var produced by h:dataTable, you must first declare of what type they are as this cannot be determined based on the code, see DataTableVariableResolver.declareTypeFor(String, Class).

If there are some EL variables aside of managed beans in faces-config (and perhaps Spring config) and the local variables you can declare them to the validator via JsfElValidator.declareVariable(String, Object).

If there are other tags than h:dataTable that can create local variables, you must create and register an appropriate resolver for them as is done with the dataTable.

How it works

We use "fake value resolvers" for a real JSF resolver; those resolvers do not retrieve variables and property values from the context as JSF normally does but instead produce a new fake value of the expected type using Mockito - thus we can check that expressions are valid. When the type of a variable/property cannot be determined (which is often the case for Collections, which can contain nay Object) and isn't defined via property override etc. then we use MockObjectOfUnknownType - if you see it in a failed JSF EL check then you need to declare the type to use.

Limitations

 - JSF 1.1 (switching to another one requires replacing the Sun's ValueBindingFactory and
 MethodBindingFactory used by the JsfElValidator by appropriate alternatives).
 - Currently it's assumed that a tag can declare only 1 local variable (var in dataTable).
 - Included files (statically or dynamically) are processed without taking their inclusion
 into account, i.e. variables defined in the including page aren't available when checking them.
 We have though all the information for taking inclusions into account available, it would just
 require more work (filter the included files out not to be processed as top-level files and
 when an inclusion tag is encountered, process the included file passing the current context on;
 see JsfElCheckingVisitor and
 org.apache.jasper.compiler.Node.Visitor.visit(IncludeAction)
 , org.apache.jasper.compiler.Node.Visitor.visit(IncludeDirective)).
 

TO DO

- Perform a separate run using the RegExp EL extractor to verify that the Jasper-based one has found all EL expressions.

Author:
jholy

Nested Class Summary
static class JsfStaticAnalyzer.ExpressionFailure
           
 
Constructor Summary
JsfStaticAnalyzer()
           
 
Method Summary
 void addElExpressionFilter(ElExpressionFilter elExpressionFilter)
          Used to ignore some expressions, i.e. not to validate them.
 Collection<File> getFacesConfigFiles()
           
 String getJspsToIncludeCommaSeparated()
           
 Collection<File> getSpringConfigFiles()
           
 boolean isPrintCorrectExpressions()
           
 boolean isSuppressOutput()
           
static void main(String[] args)
           
 void setFacesConfigFiles(Collection<File> facesConfigFiles)
          The faces-config.xml files to read managed beans from.
 void setJspsToIncludeCommaSeparated(String jspsToIncludeCommaSeparated)
          Process only the given files; set to null to process all.
 void setPrintCorrectExpressions(boolean printCorrectExpressions)
           
 void setSpringConfigFiles(Collection<File> springConfigFiles)
          The Spring application context XML files to read managed beans from.
 void setSuppressOutput(boolean suppressOutput)
          True - do not print results to the standard output / error stream.
 CollectedValidationResults validateElExpressions(String jspDir, Map<String,Class<?>> localVariableTypes, Map<String,Class<?>> extraVariables, Map<String,Class<?>> propertyTypeOverrides)
          Check expressions in all JSP files under the jspDir and print the failed (or all) ones to System out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsfStaticAnalyzer

public JsfStaticAnalyzer()
Method Detail

validateElExpressions

public CollectedValidationResults validateElExpressions(String jspDir,
                                                        Map<String,Class<?>> localVariableTypes,
                                                        Map<String,Class<?>> extraVariables,
                                                        Map<String,Class<?>> propertyTypeOverrides)
Check expressions in all JSP files under the jspDir and print the failed (or all) ones to System out.

Notion: Variable - the first element in an EL expression; property: any but the first element. Example: #{variable.propert1.property2['some.key']}

Parameters:
jspDir - (required) where to search for JSP pages
localVariableTypes - (required) type definitions for local EL variables such as produced by h:dataTable, see DataTableVariableResolver.declareTypeFor(String, Class)
extraVariables - (required) extra variables/managed beans not defined in faces-context, see JsfElValidator.declareVariable(String, Object)
propertyTypeOverrides - (required) override the type to use for a property; mostly useful for properties where the proper type cannot be derived such as a Collection, see JsfElValidator.definePropertyTypeOverride(String, Class)
Returns:
Throws:
Exception

addElExpressionFilter

public void addElExpressionFilter(ElExpressionFilter elExpressionFilter)
Used to ignore some expressions, i.e. not to validate them.

Parameters:
elExpressionFilter - (required)

main

public static void main(String[] args)
                 throws Exception
Throws:
Exception

setPrintCorrectExpressions

public void setPrintCorrectExpressions(boolean printCorrectExpressions)

isPrintCorrectExpressions

public boolean isPrintCorrectExpressions()

setJspsToIncludeCommaSeparated

public void setJspsToIncludeCommaSeparated(String jspsToIncludeCommaSeparated)
Process only the given files; set to null to process all.


getJspsToIncludeCommaSeparated

public String getJspsToIncludeCommaSeparated()

setFacesConfigFiles

public void setFacesConfigFiles(Collection<File> facesConfigFiles)
The faces-config.xml files to read managed beans from. Default: empty. Set to empty or null not to process any.


getFacesConfigFiles

public Collection<File> getFacesConfigFiles()

setSpringConfigFiles

public void setSpringConfigFiles(Collection<File> springConfigFiles)
The Spring application context XML files to read managed beans from. Default: empty. Set to empty or null not to process any.


getSpringConfigFiles

public Collection<File> getSpringConfigFiles()

setSuppressOutput

public void setSuppressOutput(boolean suppressOutput)
True - do not print results to the standard output / error stream. Defaul: false.


isSuppressOutput

public boolean isSuppressOutput()


Copyright © 2011. All Rights Reserved.