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.JexlMethod;
22  
23  /**
24   * Wraps a reference or optional method executor.
25   */
26  public class ReferenceMethodExecutor implements JexlMethod {
27  
28      /** The reference handler. */
29      private final ReferenceUberspect.ReferenceHandler handler;
30  
31      /** The method to delegate to. */
32      private final JexlMethod method;
33  
34      /**
35       * Creates an instance.
36       *
37       * @param referenceHandler the reference handler
38       * @param jexlMethod the method executor
39       */
40      public ReferenceMethodExecutor(final ReferenceUberspect.ReferenceHandler referenceHandler, final JexlMethod jexlMethod) {
41          if (referenceHandler == null || jexlMethod == null) {
42              throw new IllegalArgumentException("handler and method cant be null");
43          }
44          this.method = jexlMethod;
45          this.handler = referenceHandler;
46      }
47  
48      /**
49       * Dereference an expected optional or reference.
50       *
51       * @param opt the reference
52       * @return the reference value
53       */
54      protected Object getReference(final Object opt) {
55          return handler.callGet(opt);
56      }
57  
58      @Override
59      public Class<?> getReturnType() {
60          return method != null ?  method.getReturnType() : null;
61      }
62  
63      @Override
64      public Object invoke(final Object ref, final Object... args) throws Exception {
65          final Object obj = getReference(ref);
66          return obj == null ? null : method.invoke(obj, args);
67      }
68  
69      @Override
70      public boolean isCacheable() {
71          return method != null && method.isCacheable();
72      }
73  
74      @Override
75      public boolean tryFailed(final Object rval) {
76          return method == null || method.tryFailed(rval);
77      }
78  
79      @Override
80      public Object tryInvoke(final String name, final Object ref, final Object... args) throws JexlException.TryFailed {
81          final Object obj = getReference(ref);
82          if (method == null) {
83              return obj == null ? null : JexlEngine.TRY_FAILED;
84          }
85          if (obj == ref) {
86              return JexlEngine.TRY_FAILED;
87          }
88          return method.tryInvoke(name, obj, args);
89      }
90  }