Java Code Examples for org.apache.el.lang.ELSupport#coerceToType()

The following examples show how to use org.apache.el.lang.ELSupport#coerceToType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReflectionUtil.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private static boolean isCoercibleFrom(EvaluationContext ctx, Object src, Class<?> target) {
    // TODO: This isn't pretty but it works. Significant refactoring would
    //       be required to avoid the exception.
    try {
        ELSupport.coerceToType(ctx, src, target);
    } catch (ELException e) {
        return false;
    }
    return true;
}
 
Example 2
Source File: ValueExpressionLiteral.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(ELContext context) {
    if (this.expectedType != null) {
        return ELSupport.coerceToType(this.value, this.expectedType);
    }
    return this.value;
}
 
Example 3
Source File: ValueExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(ELContext context) throws PropertyNotFoundException,
        ELException {
    EvaluationContext ctx = new EvaluationContext(context, this.fnMapper,
            this.varMapper);
    Object value = this.getNode().getValue(ctx);
    if (this.expectedType != null) {
        return ELSupport.coerceToType(value, this.expectedType);
    }
    return value;
}
 
Example 4
Source File: ReflectionUtil.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private static boolean isCoercibleFrom(Object src, Class<?> target) {
    // TODO: This isn't pretty but it works. Significant refactoring would
    //       be required to avoid the exception.
    try {
        ELSupport.coerceToType(src, target);
    } catch (ELException e) {
        return false;
    }
    return true;
}
 
Example 5
Source File: MethodExpressionLiteral.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(ELContext context, Object[] params) throws ELException {
    if (this.expectedType != null) {
        return ELSupport.coerceToType(this.expr, this.expectedType);
    } else {
        return this.expr;
    }
}
 
Example 6
Source File: ValueExpressionLiteral.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(ELContext context) {
    if (this.expectedType != null) {
        return ELSupport.coerceToType(this.value, this.expectedType);
    }
    return this.value;
}
 
Example 7
Source File: ValueExpressionImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(ELContext context) throws PropertyNotFoundException,
        ELException {
    EvaluationContext ctx = new EvaluationContext(context, this.fnMapper,
            this.varMapper);
    Object value = this.getNode().getValue(ctx);
    if (this.expectedType != null) {
        return ELSupport.coerceToType(value, this.expectedType);
    }
    return value;
}
 
Example 8
Source File: ReflectionUtil.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private static boolean isCoercibleFrom(Object src, Class<?> target) {
    // TODO: This isn't pretty but it works. Significant refactoring would
    //       be required to avoid the exception.
    try {
        ELSupport.coerceToType(src, target);
    } catch (ELException e) {
        return false;
    }
    return true;
}
 
Example 9
Source File: MethodExpressionLiteral.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(ELContext context, Object[] params) throws ELException {
    if (this.expectedType != null) {
        return ELSupport.coerceToType(this.expr, this.expectedType);
    } else {
        return this.expr;
    }
}
 
Example 10
Source File: AstValue.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private Object[] convertArgs(EvaluationContext ctx, Object[] src, Method m) {
    Class<?>[] types = m.getParameterTypes();
    if (types.length == 0) {
        // Treated as if parameters have been provided so src is ignored
        return EMPTY_ARRAY;
    }

    int paramCount = types.length;

    if (m.isVarArgs() && paramCount > 1 && (src == null || paramCount > src.length) ||
            !m.isVarArgs() && (paramCount > 0 && src == null ||
                    src != null && src.length != paramCount)) {
        String srcCount = null;
        if (src != null) {
            srcCount = Integer.toString(src.length);
        }
        String msg;
        if (m.isVarArgs()) {
            msg = MessageFactory.get("error.invoke.tooFewParams",
                    m.getName(), srcCount, Integer.toString(paramCount));
        } else {
            msg = MessageFactory.get("error.invoke.wrongParams",
                    m.getName(), srcCount, Integer.toString(paramCount));
        }
        throw new IllegalArgumentException(msg);
    }

    if (src == null) {
        // Must be a varargs method with a single parameter.
        // Use a new array every time since the called code could modify the
        // contents of the array
        return new Object[1];
    }

    Object[] dest = new Object[paramCount];

    for (int i = 0; i < paramCount - 1; i++) {
        dest[i] = ELSupport.coerceToType(ctx, src[i], types[i]);
    }

    if (m.isVarArgs()) {
        Class<?> varArgType = m.getParameterTypes()[paramCount - 1].getComponentType();
        Object[] varArgs =
                (Object[]) Array.newInstance(varArgType, src.length - (paramCount - 1));
        for (int i = 0; i < src.length - (paramCount - 1); i ++) {
            varArgs[i] = ELSupport.coerceToType(ctx, src[paramCount - 1 + i], varArgType);
        }
        dest[paramCount - 1] = varArgs;
    } else {
        dest[paramCount - 1] = ELSupport.coerceToType(
                ctx, src[paramCount - 1], types[paramCount - 1]);
    }

    return dest;
}
 
Example 11
Source File: ExpressionFactoryImpl.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public Object coerceToType(Object obj, Class<?> type) {
    return ELSupport.coerceToType(null, obj, type);
}
 
Example 12
Source File: AstValue.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private Object[] convertArgs(Object[] src, Method m) {
    Class<?>[] types = m.getParameterTypes();
    if (types.length == 0) {
        // Treated as if parameters have been provided so src is ignored
        return EMPTY_ARRAY;
    }
    
    int paramCount = types.length;

    if (m.isVarArgs() && paramCount > 1 && (src == null || paramCount > src.length) ||
            !m.isVarArgs() && (paramCount > 0 && src == null ||
                    src != null && src.length != paramCount)) {
        String srcCount = null;
        if (src != null) {
            srcCount = Integer.toString(src.length);
        }
        String msg;
        if (m.isVarArgs()) {
            msg = MessageFactory.get("error.invoke.tooFewParams",
                    m.getName(), srcCount, Integer.toString(paramCount));
        } else {
            msg = MessageFactory.get("error.invoke.wrongParams",
                    m.getName(), srcCount, Integer.toString(paramCount));
        }
        throw new IllegalArgumentException(msg);
    }

    if (src == null) {
        // Must be a varargs method with a single parameter.
        // Use a new array every time since the called code could modify the
        // contents of the array
        return new Object[1];
    }

    Object[] dest = new Object[paramCount];

    for (int i = 0; i < paramCount - 1; i++) {
        dest[i] = ELSupport.coerceToType(src[i], types[i]);
    }

    if (m.isVarArgs()) {
        Object[] varArgs = (Object[]) Array.newInstance(
                m.getParameterTypes()[paramCount - 1].getComponentType(),
                src.length - (paramCount - 1));
        for (int i = 0; i < src.length - (paramCount - 1); i ++) {
            varArgs[i] = ELSupport.coerceToType(src[paramCount - 1 + i],
                    types[paramCount - 1].getComponentType());
        }
        dest[paramCount - 1] = varArgs;
    } else {
        dest[paramCount - 1] = ELSupport.coerceToType(
                src[paramCount - 1], types[paramCount - 1]);
    }

    return dest;
}
 
Example 13
Source File: ExpressionFactoryImpl.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public Object coerceToType(Object obj, Class<?> type) {
    return ELSupport.coerceToType(obj, type);
}
 
Example 14
Source File: AstValue.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private Object[] convertArgs(Object[] src, Method m) {
    Class<?>[] types = m.getParameterTypes();
    if (types.length == 0) {
        // Treated as if parameters have been provided so src is ignored
        return EMPTY_ARRAY;
    }
    
    int paramCount = types.length;

    if (m.isVarArgs() && paramCount > 1 && (src == null || paramCount > src.length) ||
            !m.isVarArgs() && (paramCount > 0 && src == null ||
                    src != null && src.length != paramCount)) {
        String srcCount = null;
        if (src != null) {
            srcCount = Integer.toString(src.length);
        }
        String msg;
        if (m.isVarArgs()) {
            msg = MessageFactory.get("error.invoke.tooFewParams",
                    m.getName(), srcCount, Integer.toString(paramCount));
        } else {
            msg = MessageFactory.get("error.invoke.wrongParams",
                    m.getName(), srcCount, Integer.toString(paramCount));
        }
        throw new IllegalArgumentException(msg);
    }

    if (src == null) {
        // Must be a varargs method with a single parameter.
        // Use a new array every time since the called code could modify the
        // contents of the array
        return new Object[1];
    }

    Object[] dest = new Object[paramCount];

    for (int i = 0; i < paramCount - 1; i++) {
        dest[i] = ELSupport.coerceToType(src[i], types[i]);
    }

    if (m.isVarArgs()) {
        Object[] varArgs = (Object[]) Array.newInstance(
                m.getParameterTypes()[paramCount - 1].getComponentType(),
                src.length - (paramCount - 1));
        for (int i = 0; i < src.length - (paramCount - 1); i ++) {
            varArgs[i] = ELSupport.coerceToType(src[paramCount - 1 + i],
                    types[paramCount - 1].getComponentType());
        }
        dest[paramCount - 1] = varArgs;
    } else {
        dest[paramCount - 1] = ELSupport.coerceToType(
                src[paramCount - 1], types[paramCount - 1]);
    }

    return dest;
}
 
Example 15
Source File: ExpressionFactoryImpl.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public Object coerceToType(Object obj, Class<?> type) {
    return ELSupport.coerceToType(obj, type);
}