Skip to content

Commit

Permalink
EmptyStringToNullELResolver fixed for Tomcat
Browse files Browse the repository at this point in the history
Signed-off-by: pizzi80 <[email protected]>
  • Loading branch information
pizzi80 committed Jun 22, 2023
1 parent 2fe3e42 commit 7dba707
Showing 1 changed file with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,53 @@

package com.sun.faces.el;

import java.beans.FeatureDescriptor;
import java.util.Iterator;

import jakarta.el.ELContext;
import jakarta.el.ELResolver;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EmptyStringToNullELResolver extends ELResolver {

@Override
public Class<?> getCommonPropertyType(ELContext context, Object base) {
return String.class;
}
static final String EVALUATION_CONTEXT_CLASS_NAME = "EvaluationContext";

static final Map<Class<? extends ELContext>,Boolean> isEvaluationContext = new ConcurrentHashMap<>();

@Override
@SuppressWarnings("unchecked")
public <T> T convertToType(ELContext context, Object value, Class<T> targetType) {
if (value == null && targetType == String.class) {

// if ( String.class == targetType && context instanceof org.apache.el.lang.EvaluationContext ) // && context instanceof com.sun.faces.el.ELContextImpl
// System.out.println("value:["+value+"] - targetType:["+targetType+"]" + " {"+context+"}");

// NOTA: la soluzione proposta da BalusC ad oggi crea dei malfunzionamenti a JSF durante la valutazione di espressioni EL
// ad esempio se faccio #{'hello'.concat(null)} darebbe errore perché null verrebbe trattato appunto come null e String.concat(null)
// lancia un NullPointer, invece EL fa una conversione null -> '' e ti salva la vita

// Invece per ottenere il comportamento desiderato in fase di input
// sembra che EL-context in questione sia EvaluationContext

if ( value == null &&
String.class.equals(targetType) &&
isEvaluationContext.computeIfAbsent( context.getClass() , clazz -> clazz.getName().endsWith(EVALUATION_CONTEXT_CLASS_NAME) )
) {
context.setPropertyResolved(true);
return (T) null;
}

//try {
return (T) value;
//} catch (ClassCastException e) {
// return null;
//}
}

@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
return null;
public Class<?> getCommonPropertyType(ELContext context, Object base) {
return String.class;
}

@Override
public Class<?> getType(ELContext context, Object base, Object property) {
return null;
return String.class;
}

@Override
Expand Down

0 comments on commit 7dba707

Please sign in to comment.