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