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.internal.introspection;
18  
19  import java.beans.IntrospectionException;
20  import java.lang.reflect.Constructor;
21  import java.lang.reflect.InvocationTargetException;
22  
23  import org.apache.commons.jexl3.JexlException;
24  import org.apache.commons.jexl3.introspection.JexlMethod;
25  
26  /**
27   * A JexlMethod that wraps a constructor.
28   */
29  public final class ConstructorMethod implements JexlMethod {
30  
31      /**
32       * Discovers a class constructor and wrap it as a JexlMethod.
33       *
34       * @param is the introspector
35       * @param ctorHandle a class or class name
36       * @param args constructor arguments
37       * @return a {@link JexlMethod}
38       */
39      public static ConstructorMethod discover(final Introspector is, final Object ctorHandle, final Object... args) {
40          String className;
41          Class<?> clazz = null;
42          if (ctorHandle instanceof Class<?>) {
43              clazz = (Class<?>) ctorHandle;
44              className = clazz.getName();
45          } else if (ctorHandle != null) {
46              className = ctorHandle.toString();
47          } else {
48              return null;
49          }
50          final Constructor<?> ctor = is.getConstructor(clazz, new MethodKey(className, args));
51          if (ctor != null) {
52              return new ConstructorMethod(ctor);
53          }
54          return null;
55      }
56  
57      /** The wrapped constructor. */
58      private final Constructor<?> ctor;
59  
60      /**
61       * Creates a constructor method.
62       *
63       * @param theCtor the constructor to wrap
64       */
65      ConstructorMethod(final Constructor<?> theCtor) {
66          this.ctor = theCtor;
67      }
68  
69      @Override
70      public Class<?> getReturnType() {
71          return ctor.getDeclaringClass();
72      }
73  
74      @Override
75      public Object invoke(final Object obj, final Object... params) throws Exception {
76          final Class<?> ctorClass = ctor.getDeclaringClass();
77          boolean invoke = true;
78          if (obj != null) {
79              if (obj instanceof Class<?>) {
80                  invoke = ctorClass.equals(obj);
81              } else {
82                  invoke = ctorClass.getName().equals(obj.toString());
83              }
84          }
85          if (invoke) {
86                  return ctor.newInstance(params);
87              }
88          throw new IntrospectionException("constructor resolution error");
89      }
90  
91      @Override
92      public boolean isCacheable() {
93          return true;
94      }
95  
96      @Override
97      public boolean tryFailed(final Object rval) {
98          return rval == Uberspect.TRY_FAILED;
99      }
100 
101     @Override
102     public Object tryInvoke(final String name, final Object obj, final Object... args) {
103         // Don't try to invoke if no parameter but call has arguments
104         if (ctor.getParameterCount() > 0 || args.length == 0) {
105             try {
106                 final Class<?> ctorClass = ctor.getDeclaringClass();
107                 boolean invoke = true;
108                 if (obj != null) {
109                     if (obj instanceof Class<?>) {
110                         invoke = ctorClass.equals(obj);
111                     } else {
112                         invoke = ctorClass.getName().equals(obj.toString());
113                     }
114                 }
115                 invoke &= name == null || ctorClass.getName().equals(name);
116                 if (invoke) {
117                     return ctor.newInstance(args);
118                 }
119             } catch (InstantiationException | IllegalArgumentException | IllegalAccessException xinstance) {
120                 return Uberspect.TRY_FAILED;
121             } catch (final InvocationTargetException xinvoke) {
122                 throw JexlException.tryFailed(xinvoke); // throw
123             }
124         }
125         return Uberspect.TRY_FAILED;
126     }
127 
128 }