How to check the correctness of EL-expressions?

In our web application, we did some refactoring on Java beans, and because of this refactoring, some actions / getters are no longer available. For example, in the following example:

public class MyBean implements Serializable { // Old name // public String getFoo() { return "foo"; } // New name public String getBar() { return "bar"; } } 

If my JSF code still looks like this:

 <h:inputText value="#{myBean.foo}" .../> 

then it will throw an exception.

My main problem is that some of the EL expressions can now point to more advanced Java bean methods, and I can only see this when viewing the corresponding page. It becomes more difficult when the expression EL is located in the part that is displayed under certain conditions and / or is written by Ajax.

So my question is, how can I check statically the correctness of these EL expressions?

I checked the JSFUnit tool as it provides such a utility. So I tried this example , but it does not work as expected. On a simple test page with several EL expressions (some correct, some others erroneous using non-existing beans and / or methods), the test succeeds, which is incorrect.

Important Note: My beans are defined in my Spring configuration and not in faces-config.xml .

Also note that I just need to check if the EL expression applies to the existing bean and method, and it is not necessary that this action is performed correctly ...

Technical information:

Java 1.6, Spring 2.5, JSF 1.2, EL Functor , Facelets, and Richfaces 3.3

+4
source share
1 answer

I did not find a tool that matched my requirements. So I wrote a little bean in my application. This bean will provide an action that will read the directory and parse every .xhtml file in it (and do it recursively). Of course, I will have to run my application (therefore, the analysis is not really static), but the advantage of this solution is that I will have access to the ELContext and ELExpressionFactory that I really use in my web application, both related to my Spring context.

Here is an interesting piece of code:

 // Analyse a XHTML file private void analyzeXHTMLFile(File f) throws IOException { List<String> lines = FileUtils.readLines(f); for (String l : lines) { List<String> els = getEL(l); for (String el : els) { evaluateEL(el); } } } // Get a List of EL on the line. private List<String> getEL(String line) { List<String> el = new ArrayList<String>(); int i = line.indexOf("#{"); if (i == -1) { return el; } while (i != -1) { int j = line.indexOf("}", i); if (j == -1) { return el; } el.add(line.substring(i, j + 1)); i = line.indexOf("#{", i + 1); } return el; } // Evaluate the EL private void evaluateEL(String el) { FacesContext context = FacesContext.getCurrentInstance(); ELContext elContext = context.getELContext(); ValueExpression ve = context.getApplication().getExpressionFactory().createValueExpression(elContext, el, Object.class); if (ve != null) { Object o = null; try { o = ve.getValue(elContext); } catch (PropertyNotFoundException pnfe) { // Handle this error } if (o == null) { // Handle another error } } } 
+1
source

All Articles