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