Skip to content

Package: ELSupport

ELSupport

nameinstructionbranchcomplexitylinemethod
ELSupport()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
checkType(Object, Class)
M: 42 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
coerceToBoolean(Object)
M: 31 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
coerceToCharacter(Object)
M: 45 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
coerceToEnum(Object, Class)
M: 20 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
coerceToNumber(Number, Class)
M: 130 C: 0
0%
M: 38 C: 0
0%
M: 20 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
coerceToNumber(Object)
M: 22 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
coerceToNumber(Object, Class)
M: 55 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
coerceToNumber(String, Class)
M: 87 C: 0
0%
M: 28 C: 0
0%
M: 15 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
coerceToString(Object)
M: 20 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
coerceToType(Object, Class)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
coerceToType(Object, Class, boolean)
M: 104 C: 0
0%
M: 38 C: 0
0%
M: 20 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%
compare(Object, Object)
M: 130 C: 0
0%
M: 24 C: 0
0%
M: 13 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
containsNulls(Object[])
M: 16 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
equals(Object, Object)
M: 133 C: 0
0%
M: 26 C: 0
0%
M: 14 C: 0
0%
M: 29 C: 0
0%
M: 1 C: 0
0%
isBigDecimalOp(Object, Object)
M: 10 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isBigIntegerOp(Object, Object)
M: 10 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isDoubleOp(Object, Object)
M: 16 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isDoubleStringOp(Object, Object)
M: 22 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isLongOp(Object, Object)
M: 34 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isStringFloat(String)
M: 25 C: 0
0%
M: 8 C: 0
0%
M: 6 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
throwUnhandled(Object, Object)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
toFloat(String)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
toNumber(String)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0, which is available at
7: * http://www.eclipse.org/legal/epl-2.0.
8: *
9: * This Source Code may also be made available under the following Secondary
10: * Licenses when the conditions for such availability set forth in the
11: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12: * version 2 with the GNU Classpath Exception, which is available at
13: * https://www.gnu.org/software/classpath/license.html.
14: *
15: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16: */
17:
18: package com.sun.el.lang;
19:
20: import java.beans.PropertyEditor;
21: import java.beans.PropertyEditorManager;
22: import java.math.BigDecimal;
23: import java.math.BigInteger;
24:
25: import jakarta.el.ELException;
26: import jakarta.el.PropertyNotFoundException;
27:
28: import com.sun.el.util.MessageFactory;
29:
30: /**
31: * A helper class that implements the Jakarta Expression Specification
32: *
33: * @author Jacob Hookom [jacob@hookom.net]
34: * @author Kin-man Chung
35: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
36: */
37: public class ELSupport {
38:
39: private final static Long ZERO = Long.valueOf(0L);
40:
41: public final static void throwUnhandled(Object base, Object property) throws ELException {
42:• if (base == null) {
43: throw new PropertyNotFoundException(MessageFactory.get("error.resolver.unhandled.null", property));
44: } else {
45: throw new PropertyNotFoundException(MessageFactory.get("error.resolver.unhandled", base.getClass(), property));
46: }
47: }
48:
49: /**
50: * @param obj0 First object to be compared
51: * @param obj1 Second object to be compared
52: * @return The result (an int with values -1, 0, or 1) of the comparison
53: * @throws ELException when something goes wrong
54: */
55: public final static int compare(final Object obj0, final Object obj1) throws ELException {
56:• if (obj0 == obj1 || equals(obj0, obj1)) {
57: return 0;
58: }
59:• if (isBigDecimalOp(obj0, obj1)) {
60: BigDecimal bd0 = (BigDecimal) coerceToNumber(obj0, BigDecimal.class);
61: BigDecimal bd1 = (BigDecimal) coerceToNumber(obj1, BigDecimal.class);
62: return bd0.compareTo(bd1);
63: }
64:• if (isDoubleOp(obj0, obj1)) {
65: Double d0 = (Double) coerceToNumber(obj0, Double.class);
66: Double d1 = (Double) coerceToNumber(obj1, Double.class);
67: return d0.compareTo(d1);
68: }
69:• if (isBigIntegerOp(obj0, obj1)) {
70: BigInteger bi0 = (BigInteger) coerceToNumber(obj0, BigInteger.class);
71: BigInteger bi1 = (BigInteger) coerceToNumber(obj1, BigInteger.class);
72: return bi0.compareTo(bi1);
73: }
74:• if (isLongOp(obj0, obj1)) {
75: Long l0 = (Long) coerceToNumber(obj0, Long.class);
76: Long l1 = (Long) coerceToNumber(obj1, Long.class);
77: return l0.compareTo(l1);
78: }
79:• if (obj0 instanceof String || obj1 instanceof String) {
80: return coerceToString(obj0).compareTo(coerceToString(obj1));
81: }
82:• if (obj0 instanceof Comparable) {
83: // Safe cast
84: @SuppressWarnings("unchecked")
85: Comparable<Object> cobj0 = (Comparable) obj0;
86:• return (obj1 != null) ? cobj0.compareTo(obj1) : 1;
87: }
88:• if (obj1 instanceof Comparable) {
89: // Safe cast
90: @SuppressWarnings("unchecked")
91: Comparable<Object> cobj1 = (Comparable) obj1;
92:• return (obj0 != null) ? -(cobj1.compareTo(obj0)) : -1;
93: }
94: throw new ELException(MessageFactory.get("error.compare", obj0, obj1));
95: }
96:
97: /**
98: * @param obj0 Fisrt object to be compared
99: * @param obj1 Second object to be compared
100: * @return true if the objects compared equal
101: * @throws ELException when something goes wrong
102: */
103: public final static boolean equals(final Object obj0, final Object obj1) throws ELException {
104:• if (obj0 == obj1) {
105: return true;
106: }
107:• if (obj0 == null || obj1 == null) {
108: return false;
109: }
110:• if (isBigDecimalOp(obj0, obj1)) {
111: BigDecimal bd0 = (BigDecimal) coerceToNumber(obj0, BigDecimal.class);
112: BigDecimal bd1 = (BigDecimal) coerceToNumber(obj1, BigDecimal.class);
113: return bd0.equals(bd1);
114: }
115:• if (isDoubleOp(obj0, obj1)) {
116: Double d0 = (Double) coerceToNumber(obj0, Double.class);
117: Double d1 = (Double) coerceToNumber(obj1, Double.class);
118: return d0.equals(d1);
119: }
120:• if (isBigIntegerOp(obj0, obj1)) {
121: BigInteger bi0 = (BigInteger) coerceToNumber(obj0, BigInteger.class);
122: BigInteger bi1 = (BigInteger) coerceToNumber(obj1, BigInteger.class);
123: return bi0.equals(bi1);
124: }
125:• if (isLongOp(obj0, obj1)) {
126: Long l0 = (Long) coerceToNumber(obj0, Long.class);
127: Long l1 = (Long) coerceToNumber(obj1, Long.class);
128: return l0.equals(l1);
129: }
130:• if (obj0 instanceof Boolean || obj1 instanceof Boolean) {
131: return coerceToBoolean(obj0).equals(coerceToBoolean(obj1));
132: }
133:• if (obj0.getClass().isEnum()) {
134: return obj0.equals(coerceToEnum(obj1, obj0.getClass()));
135: }
136:• if (obj1.getClass().isEnum()) {
137: return obj1.equals(coerceToEnum(obj0, obj1.getClass()));
138: }
139:• if (obj0 instanceof String || obj1 instanceof String) {
140: return coerceToString(obj0).equals(coerceToString(obj1));
141: }
142: return obj0.equals(obj1);
143: }
144:
145: /**
146: * @param obj Object to be coerced
147: * @return The result of coercion
148: */
149: public final static Boolean coerceToBoolean(final Object obj) throws IllegalArgumentException {
150:• if (obj == null || "".equals(obj)) {
151: return Boolean.FALSE;
152: }
153:• if (obj instanceof Boolean) {
154: return (Boolean) obj;
155: }
156:• if (obj instanceof String) {
157: return Boolean.valueOf((String) obj);
158: }
159:
160: throw new IllegalArgumentException(MessageFactory.get("error.convert", obj, obj.getClass(), Boolean.class));
161: }
162:
163: // Enum types are hard construct. We can declare this as
164: // <T extends Enum<T>> T coerceToEnum(Object, Class<T> type)
165: // but this makes it harder to get the calls right.
166: @SuppressWarnings("unchecked")
167: public final static Enum coerceToEnum(final Object obj, Class type) {
168:• if (obj == null || "".equals(obj)) {
169: return null;
170: }
171:• if (obj.getClass().isEnum()) {
172: return (Enum) obj;
173: }
174: return Enum.valueOf(type, obj.toString());
175: }
176:
177: public final static Character coerceToCharacter(final Object obj) throws IllegalArgumentException {
178:• if (obj == null || "".equals(obj)) {
179: return Character.valueOf((char) 0);
180: }
181:• if (obj instanceof String) {
182: return Character.valueOf(((String) obj).charAt(0));
183: }
184:• if (ELArithmetic.isNumber(obj)) {
185: return Character.valueOf((char) ((Number) obj).shortValue());
186: }
187: Class objType = obj.getClass();
188:• if (obj instanceof Character) {
189: return (Character) obj;
190: }
191:
192: throw new IllegalArgumentException(MessageFactory.get("error.convert", obj, objType, Character.class));
193: }
194:
195: public final static Number coerceToNumber(final Object obj) {
196:• if (obj == null) {
197: return ZERO;
198:• } else if (obj instanceof Number) {
199: return (Number) obj;
200: } else {
201: String str = coerceToString(obj);
202:• if (isStringFloat(str)) {
203: return toFloat(str);
204: } else {
205: return toNumber(str);
206: }
207: }
208: }
209:
210: protected final static Number coerceToNumber(final Number number, final Class type) throws IllegalArgumentException {
211:• if (Long.TYPE == type || Long.class.equals(type)) {
212: return Long.valueOf(number.longValue());
213: }
214:• if (Double.TYPE == type || Double.class.equals(type)) {
215: return Double.valueOf(number.doubleValue());
216: }
217:• if (Integer.TYPE == type || Integer.class.equals(type)) {
218: return Integer.valueOf(number.intValue());
219: }
220:• if (BigInteger.class.equals(type)) {
221:• if (number instanceof BigDecimal) {
222: return ((BigDecimal) number).toBigInteger();
223: }
224:• if (number instanceof BigInteger) {
225: return number;
226: }
227: return BigInteger.valueOf(number.longValue());
228: }
229:• if (BigDecimal.class.equals(type)) {
230:• if (number instanceof BigDecimal) {
231: return number;
232: }
233:• if (number instanceof BigInteger) {
234: return new BigDecimal((BigInteger) number);
235: }
236:• if (number instanceof Long) {
237: return new BigDecimal((Long) number);
238: }
239: return new BigDecimal(number.doubleValue());
240: }
241:• if (Byte.TYPE == type || Byte.class.equals(type)) {
242: return Byte.valueOf(number.byteValue());
243: }
244:• if (Short.TYPE == type || Short.class.equals(type)) {
245: return Short.valueOf(number.shortValue());
246: }
247:• if (Float.TYPE == type || Float.class.equals(type)) {
248: return Float.valueOf(number.floatValue());
249: }
250:
251: throw new IllegalArgumentException(MessageFactory.get("error.convert", number, number.getClass(), type));
252: }
253:
254: public final static Number coerceToNumber(final Object obj, final Class type) throws IllegalArgumentException {
255:• if (obj == null || "".equals(obj)) {
256: return coerceToNumber(ZERO, type);
257: }
258:• if (obj instanceof String) {
259: return coerceToNumber((String) obj, type);
260: }
261:• if (ELArithmetic.isNumber(obj)) {
262:• if (obj.getClass().equals(type)) {
263: return (Number) obj;
264: }
265: return coerceToNumber((Number) obj, type);
266: }
267:
268:• if (obj instanceof Character) {
269: return coerceToNumber(Short.valueOf((short) ((Character) obj).charValue()), type);
270: }
271:
272: throw new IllegalArgumentException(MessageFactory.get("error.convert", obj, obj.getClass(), type));
273: }
274:
275: protected final static Number coerceToNumber(final String val, final Class type) throws IllegalArgumentException {
276:• if (Long.TYPE == type || Long.class.equals(type)) {
277: return Long.valueOf(val);
278: }
279:• if (Integer.TYPE == type || Integer.class.equals(type)) {
280: return Integer.valueOf(val);
281: }
282:• if (Double.TYPE == type || Double.class.equals(type)) {
283: return Double.valueOf(val);
284: }
285:• if (BigInteger.class.equals(type)) {
286: return new BigInteger(val);
287: }
288:• if (BigDecimal.class.equals(type)) {
289: return new BigDecimal(val);
290: }
291:• if (Byte.TYPE == type || Byte.class.equals(type)) {
292: return Byte.valueOf(val);
293: }
294:• if (Short.TYPE == type || Short.class.equals(type)) {
295: return Short.valueOf(val);
296: }
297:• if (Float.TYPE == type || Float.class.equals(type)) {
298: return Float.valueOf(val);
299: }
300:
301: throw new IllegalArgumentException(MessageFactory.get("error.convert", val, String.class, type));
302: }
303:
304: /**
305: * @param obj Object to be coerced
306: * @return The result of coercion
307: */
308: public final static String coerceToString(final Object obj) {
309:• if (obj == null) {
310: return "";
311:• } else if (obj instanceof String) {
312: return (String) obj;
313:• } else if (obj instanceof Enum) {
314: return ((Enum) obj).name();
315: } else {
316: return obj.toString();
317: }
318: }
319:
320: public final static void checkType(final Object obj, final Class<?> type) throws IllegalArgumentException {
321:• if (String.class.equals(type)) {
322: coerceToString(obj);
323: }
324:• if (ELArithmetic.isNumberType(type)) {
325: coerceToNumber(obj, type);
326: }
327:• if (Character.class.equals(type) || Character.TYPE == type) {
328: coerceToCharacter(obj);
329: }
330:• if (Boolean.class.equals(type) || Boolean.TYPE == type) {
331: coerceToBoolean(obj);
332: }
333:• if (type.isEnum()) {
334: coerceToEnum(obj, type);
335: }
336: }
337:
338: public final static <T> T coerceToType(final Object obj, final Class<T> type) throws IllegalArgumentException {
339: return coerceToType(obj, type, false);
340: }
341:
342: public final static <T> T coerceToType(final Object obj, final Class<T> type, boolean isEL22Compatible) throws IllegalArgumentException {
343:
344:• if (type == null || Object.class.equals(type) || (obj != null && type.isAssignableFrom(obj.getClass()))) {
345: return (T) obj;
346: }
347:
348: // New in 3.0
349:• if (!isEL22Compatible && obj == null && !type.isPrimitive() && !String.class.equals(type)) {
350: return null;
351: }
352:
353:• if (String.class.equals(type)) {
354: return (T) coerceToString(obj);
355: }
356:• if (ELArithmetic.isNumberType(type)) {
357: return (T) coerceToNumber(obj, type);
358: }
359:• if (Character.class.equals(type) || Character.TYPE == type) {
360: return (T) coerceToCharacter(obj);
361: }
362:• if (Boolean.class.equals(type) || Boolean.TYPE == type) {
363: return (T) coerceToBoolean(obj);
364: }
365:• if (type.isEnum()) {
366: return (T) coerceToEnum(obj, type);
367: }
368:
369:• if (obj == null) {
370: return null;
371: }
372:
373:• if (obj instanceof String) {
374:• if ("".equals(obj)) {
375: return null;
376: }
377: PropertyEditor editor = PropertyEditorManager.findEditor(type);
378:• if (editor != null) {
379: editor.setAsText((String) obj);
380: return (T) editor.getValue();
381: }
382: }
383: throw new IllegalArgumentException(MessageFactory.get("error.convert", obj, obj.getClass(), type));
384: }
385:
386: /**
387: * @param obj An array of objects
388: * @return true if the array contains a null, false otherwise
389: */
390: public final static boolean containsNulls(final Object[] obj) {
391:• for (int i = 0; i < obj.length; i++) {
392:• if (obj[0] == null) {
393: return true;
394: }
395: }
396: return false;
397: }
398:
399: public final static boolean isBigDecimalOp(final Object obj0, final Object obj1) {
400:• return (obj0 instanceof BigDecimal || obj1 instanceof BigDecimal);
401: }
402:
403: public final static boolean isBigIntegerOp(final Object obj0, final Object obj1) {
404:• return (obj0 instanceof BigInteger || obj1 instanceof BigInteger);
405: }
406:
407: public final static boolean isDoubleOp(final Object obj0, final Object obj1) {
408:• return (obj0 instanceof Double || obj1 instanceof Double || obj0 instanceof Float || obj1 instanceof Float);
409: }
410:
411: public final static boolean isDoubleStringOp(final Object obj0, final Object obj1) {
412:• return (isDoubleOp(obj0, obj1) || (obj0 instanceof String && isStringFloat((String) obj0)) || (obj1 instanceof String && isStringFloat((String) obj1)));
413: }
414:
415: public final static boolean isLongOp(final Object obj0, final Object obj1) {
416:• return (obj0 instanceof Long || obj1 instanceof Long || obj0 instanceof Integer || obj1 instanceof Integer || obj0 instanceof Character
417: || obj1 instanceof Character || obj0 instanceof Short || obj1 instanceof Short || obj0 instanceof Byte || obj1 instanceof Byte);
418: }
419:
420: public final static boolean isStringFloat(final String str) {
421: int len = str.length();
422:• if (len > 1) {
423:• for (int i = 0; i < len; i++) {
424:• switch (str.charAt(i)) {
425: case 'E':
426: return true;
427: case 'e':
428: return true;
429: case '.':
430: return true;
431: }
432: }
433: }
434: return false;
435: }
436:
437: public final static Number toFloat(final String value) {
438: try {
439:• if (Double.parseDouble(value) > Double.MAX_VALUE) {
440: return new BigDecimal(value);
441: } else {
442: return Double.valueOf(value);
443: }
444: } catch (NumberFormatException e0) {
445: return new BigDecimal(value);
446: }
447: }
448:
449: public final static Number toNumber(final String value) {
450: try {
451: return Integer.valueOf(Integer.parseInt(value));
452: } catch (NumberFormatException e0) {
453: try {
454: return Long.valueOf(Long.parseLong(value));
455: } catch (NumberFormatException e1) {
456: return new BigInteger(value);
457: }
458: }
459: }
460:
461: /**
462: *
463: */
464: public ELSupport() {
465: super();
466: }
467: }