1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.jexl3.jexl342;
18
19 import org.apache.commons.jexl3.JexlEngine;
20 import org.apache.commons.jexl3.introspection.JexlPropertyGet;
21
22 /**
23 * Wraps a reference or optional property get executor.
24 */
25 public class ReferenceGetExecutor implements JexlPropertyGet {
26 /** The reference handler. */
27 private final ReferenceUberspect.ReferenceHandler handler;
28 /** The previous getter we did delegate to. */
29 private final JexlPropertyGet getter;
30
31 /**
32 * Creates an instance.
33 * @param referenceHandler the reference handler
34 * @param jexlGet the property getter
35 */
36 public ReferenceGetExecutor(final ReferenceUberspect.ReferenceHandler referenceHandler, final JexlPropertyGet jexlGet) {
37 if (referenceHandler == null || jexlGet == null) {
38 throw new IllegalArgumentException("handler and getter cant be null");
39 }
40 handler = referenceHandler;
41 getter = jexlGet;
42 }
43
44 /**
45 * Dereference an expected optional or reference .
46 * @param ref the reference
47 * @return the reference value or the reference
48 */
49 protected Object getReference(final Object ref) {
50 return handler.callGet(ref);
51 }
52
53 @Override
54 public Object invoke(final Object ref) throws Exception {
55 final Object obj = getReference(ref);
56 return getter.invoke(obj);
57 }
58
59 @Override
60 public boolean isCacheable() {
61 return getter.isCacheable();
62 }
63
64 @Override
65 public boolean tryFailed(final Object rval) {
66 return getter.tryFailed(rval);
67 }
68
69 @Override
70 public Object tryInvoke(final Object ref, final Object key) {
71 final Object obj = getReference(ref);
72 return obj == ref ? JexlEngine.TRY_FAILED : getter.tryInvoke(obj, key);
73 }
74 }