View Javadoc
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    *      https://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       *
34       * @param referenceHandler the reference handler
35       * @param jexlGet the property getter
36       */
37      public ReferenceGetExecutor(final ReferenceUberspect.ReferenceHandler referenceHandler, final JexlPropertyGet jexlGet) {
38          if (referenceHandler == null || jexlGet == null) {
39              throw new IllegalArgumentException("handler and getter cant be null");
40          }
41          handler = referenceHandler;
42          getter = jexlGet;
43      }
44  
45      /**
46       * Dereference an expected optional or reference.
47       *
48       * @param ref the reference
49       * @return the reference value or the reference
50       */
51      protected Object getReference(final Object ref) {
52          return handler.callGet(ref);
53      }
54  
55      @Override
56      public Object invoke(final Object ref) throws Exception {
57          final Object obj = getReference(ref);
58          return getter.invoke(obj);
59      }
60  
61      @Override
62      public boolean isCacheable() {
63          return getter.isCacheable();
64      }
65  
66      @Override
67      public boolean tryFailed(final Object rval) {
68          return getter.tryFailed(rval);
69      }
70  
71      @Override
72      public Object tryInvoke(final Object ref, final Object key) {
73          final Object obj = getReference(ref);
74          return obj == ref ? JexlEngine.TRY_FAILED : getter.tryInvoke(obj, key);
75      }
76  }