Skip to contentPackage: StreamELResolver$1
StreamELResolver$1
Coverage
1: /*
2: * Copyright (c) 2012, 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.stream;
18:
19: import java.beans.FeatureDescriptor;
20: import java.lang.reflect.Array;
21: import java.util.Collection;
22: import java.util.Iterator;
23:
24: import jakarta.el.ELContext;
25: import jakarta.el.ELResolver;
26:
27: /*
28: * This ELResolver intercepts method calls to a Collections, to provide
29: * support for collection operations.
30: */
31:
32: public class StreamELResolver extends ELResolver {
33:
34: @Override
35: public Object invoke(final ELContext context, final Object base, final Object method, final Class<?>[] paramTypes, final Object[] params) {
36:
37: if (context == null) {
38: throw new NullPointerException();
39: }
40:
41: if (base instanceof Collection) {
42: @SuppressWarnings("unchecked")
43: Collection<Object> c = (Collection<Object>) base;
44: if ("stream".equals(method) && params.length == 0) {
45: context.setPropertyResolved(true);
46: return new Stream(c.iterator());
47: }
48: }
49: if (base.getClass().isArray()) {
50: if ("stream".equals(method) && params.length == 0) {
51: context.setPropertyResolved(true);
52: return new Stream(arrayIterator(base));
53: }
54: }
55: return null;
56: }
57:
58: private static Iterator<Object> arrayIterator(final Object base) {
59: final int size = Array.getLength(base);
60: return new Iterator<Object>() {
61: int index = 0;
62: boolean yielded;
63: Object current;
64:
65: @Override
66: public boolean hasNext() {
67:• if ((!yielded) && index < size) {
68: current = Array.get(base, index++);
69: yielded = true;
70: }
71: return yielded;
72: }
73:
74: @Override
75: public Object next() {
76: yielded = false;
77: return current;
78: }
79:
80: @Override
81: public void remove() {
82: throw new UnsupportedOperationException();
83: }
84: };
85: }
86:
87: /*
88: * private LambdaExpression getLambda(Object obj, String method) { if (obj == null || ! (obj instanceof
89: * LambdaExpression)) { throw new ELException ("When calling " + method + ", expecting an " +
90: * "EL lambda expression, but found " + obj); } return (LambdaExpression) obj; }
91: */
92: @Override
93: public Object getValue(ELContext context, Object base, Object property) {
94: return null;
95: }
96:
97: @Override
98: public Class<?> getType(ELContext context, Object base, Object property) {
99: return null;
100: }
101:
102: @Override
103: public void setValue(ELContext context, Object base, Object property, Object value) {
104: }
105:
106: @Override
107: public boolean isReadOnly(ELContext context, Object base, Object property) {
108: return false;
109: }
110:
111: @Override
112: public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
113: return null;
114: }
115:
116: @Override
117: public Class<?> getCommonPropertyType(ELContext context, Object base) {
118: return String.class;
119: }
120: }