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    *      http://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      /** The Uberspect to discover the method. */
31      private final JexlUberspect uberspect;
32      /** The method we are trying to discover. */
33      private final String methodName;
34      /** The result when we solve it. */
35      private JexlMethod delegate;
36  
37      OptionalNullMethod(final JexlUberspect jexlUberspect, final String name) {
38          uberspect = jexlUberspect;
39          methodName = name;
40      }
41  
42      @Override
43      public Class<?> getReturnType() {
44          return delegate != null ? delegate.getReturnType() : null;
45      }
46  
47      @Override
48      public Object invoke(final Object obj, final Object... params) throws Exception {
49          if (obj == null) {
50              return null;
51          }
52          if (delegate == null) {
53              delegate = uberspect.getMethod(obj, methodName, params);
54              if (delegate == null) {
55                  throw new JexlException.Method((JexlInfo) null, methodName, params);
56              }
57          }
58          return delegate.invoke(obj, params);
59      }
60  
61      @Override
62      public boolean isCacheable() {
63          return false;
64      }
65  
66      @Override
67      public boolean tryFailed(final Object rval) {
68          return delegate != null ? delegate.tryFailed(rval) : JexlEngine.TRY_FAILED == rval;
69      }
70  
71      @Override
72      public Object tryInvoke(final String name, final Object obj, final Object... params) throws JexlException.TryFailed {
73          if (obj == null) {
74              return null;
75          }
76          return delegate != null ? delegate.tryInvoke(name, obj, params) : JexlEngine.TRY_FAILED;
77      }
78  }