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.JexlInfo;
22  import org.apache.commons.jexl3.introspection.JexlMethod;
23  import org.apache.commons.jexl3.introspection.JexlUberspect;
24  
25  /**
26   * JexlMethod on a reference that pointed null.
27   * <p>Performs a late discovery of the actual method.</p>
28   */
29  public class OptionalNullMethod implements JexlMethod {
30  
31      /** The Uberspect to discover the method. */
32      private final JexlUberspect uberspect;
33  
34      /** The method we are trying to discover. */
35      private final String methodName;
36  
37      /** The result when we solve it. */
38      private JexlMethod delegate;
39  
40      OptionalNullMethod(final JexlUberspect jexlUberspect, final String name) {
41          uberspect = jexlUberspect;
42          methodName = name;
43      }
44  
45      @Override
46      public Class<?> getReturnType() {
47          return delegate != null ? delegate.getReturnType() : null;
48      }
49  
50      @Override
51      public Object invoke(final Object obj, final Object... params) throws Exception {
52          if (obj == null) {
53              return null;
54          }
55          if (delegate == null) {
56              delegate = uberspect.getMethod(obj, methodName, params);
57              if (delegate == null) {
58                  throw new JexlException.Method((JexlInfo) null, methodName, params);
59              }
60          }
61          return delegate.invoke(obj, params);
62      }
63  
64      @Override
65      public boolean isCacheable() {
66          return false;
67      }
68  
69      @Override
70      public boolean tryFailed(final Object rval) {
71          return delegate != null ? delegate.tryFailed(rval) : JexlEngine.TRY_FAILED == rval;
72      }
73  
74      @Override
75      public Object tryInvoke(final String name, final Object obj, final Object... params) throws JexlException.TryFailed {
76          if (obj == null) {
77              return null;
78          }
79          return delegate != null ? delegate.tryInvoke(name, obj, params) : JexlEngine.TRY_FAILED;
80      }
81  }