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.JexlException;
21  import org.apache.commons.jexl3.introspection.JexlPropertySet;
22  
23  /**
24   * Wraps a reference or optional property set executor.
25   */
26  public class ReferenceSetExecutor implements JexlPropertySet {
27  
28      /** The reference handler. */
29      private final ReferenceUberspect.ReferenceHandler handler;
30  
31      /** The previous setter we did delegate to. */
32      private final JexlPropertySet setter;
33  
34      /**
35       * Creates an instance.
36       *
37       * @param referenceHandler the reference handler
38       * @param jexlSet the property setter
39       */
40      public ReferenceSetExecutor(final ReferenceUberspect.ReferenceHandler referenceHandler, final JexlPropertySet jexlSet) {
41          if (referenceHandler == null || jexlSet == null) {
42              throw new IllegalArgumentException("handler and setter cant be null");
43          }
44          this.handler = referenceHandler;
45          this.setter = jexlSet;
46      }
47  
48      /**
49       * Dereference an expected optional or reference.
50       *
51       * @param opt the reference
52       * @return the reference value, TRY_FAILED if null
53       */
54      protected Object getReference(final Object opt) {
55          return handler.callGet(opt);
56      }
57  
58      @Override
59      public Object invoke(final Object opt, final Object arg) throws Exception {
60          final Object obj = getReference(opt);
61          return setter.invoke(obj, arg);
62      }
63  
64      @Override
65      public boolean isCacheable() {
66          return setter.isCacheable();
67      }
68  
69      @Override
70      public boolean tryFailed(final Object rval) {
71          return setter.tryFailed(rval);
72      }
73  
74      @Override
75      public Object tryInvoke(final Object opt, final Object key, final Object arg) throws JexlException.TryFailed {
76          final Object obj = getReference(opt);
77          return obj == opt? JexlEngine.TRY_FAILED : obj == null ? null : setter.tryInvoke(key, obj, arg);
78      }
79  }