1 /*
2  * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v. 2.0, which is available at
6  * http://www.eclipse.org/legal/epl-2.0.
7  *
8  * This Source Code may also be made available under the following Secondary
9  * Licenses when the conditions for such availability set forth in the
10  * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11  * version 2 with the GNU Classpath Exception, which is available at
12  * https://www.gnu.org/software/classpath/license.html.
13  *
14  * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15  */

16
17 package com.sun.el;
18
19 import java.lang.reflect.Method;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Properties;
23
24 import javax.el.ELContext;
25 import javax.el.ELException;
26 import javax.el.ELResolver;
27 import javax.el.ExpressionFactory;
28 import javax.el.MethodExpression;
29 import javax.el.ValueExpression;
30
31 import com.sun.el.lang.ELSupport;
32 import com.sun.el.lang.ExpressionBuilder;
33 import com.sun.el.stream.StreamELResolver;
34 import com.sun.el.util.MessageFactory;
35
36 /**
37  * @see javax.el.ExpressionFactory
38  *
39  * @author Jacob Hookom [jacob@hookom.net]
40  * @author Kin-man Chung
41  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
42  */

43 public class ExpressionFactoryImpl extends ExpressionFactory {
44
45     private Properties properties;
46     private boolean isBackwardCompatible22;
47
48     public ExpressionFactoryImpl() {
49         super();
50     }
51
52     public ExpressionFactoryImpl(Properties properties) {
53         super();
54         this.properties = properties;
55         this.isBackwardCompatible22 = "true".equals(getProperty("javax.el.bc2.2"));
56     }
57
58     @Override
59     public Object coerceToType(Object obj, Class<?> type) {
60         try {
61             return ELSupport.coerceToType(obj, type, isBackwardCompatible22);
62         } catch (IllegalArgumentException ex) {
63             throw new ELException(ex);
64         }
65     }
66
67     @Override
68     public MethodExpression createMethodExpression(ELContext context, String expression, Class<?> expectedReturnType, Class<?>[] expectedParamTypes) {
69         MethodExpression methodExpression =
70                 new ExpressionBuilder(expression, context)
71                     .createMethodExpression(expectedReturnType, expectedParamTypes);
72
73         if (expectedParamTypes == null && !methodExpression.isParametersProvided()) {
74             throw new NullPointerException(MessageFactory.get("error.method.nullParms"));
75         }
76
77         return methodExpression;
78     }
79
80     @Override
81     public ValueExpression createValueExpression(ELContext context, String expression, Class<?> expectedType) {
82         if (expectedType == null) {
83             throw new NullPointerException(MessageFactory.get("error.value.expectedType"));
84         }
85
86         return new ExpressionBuilder(expression, context).createValueExpression(expectedType);
87     }
88
89     @Override
90     public ValueExpression createValueExpression(Object instance, Class<?> expectedType) {
91         if (expectedType == null) {
92             throw new NullPointerException(MessageFactory.get("error.value.expectedType"));
93         }
94
95         return new ValueExpressionLiteral(instance, expectedType);
96     }
97
98     public String getProperty(String key) {
99         if (properties == null) {
100             return null;
101         }
102
103         return properties.getProperty(key);
104     }
105
106     @Override
107     public ELResolver getStreamELResolver() {
108         return new StreamELResolver();
109     }
110
111     @Override
112     public Map<String, Method> getInitFunctionMap() {
113         return new HashMap<String, Method>();
114     }
115 }
116