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  
18  package org.apache.commons.jxpath.functions;
19  
20  import java.lang.reflect.Constructor;
21  import java.lang.reflect.InvocationTargetException;
22  
23  import org.apache.commons.jxpath.ExpressionContext;
24  import org.apache.commons.jxpath.Function;
25  import org.apache.commons.jxpath.JXPathInvalidAccessException;
26  import org.apache.commons.jxpath.util.TypeUtils;
27  
28  /**
29   * An extension function that creates an instance using a constructor.
30   */
31  public class ConstructorFunction implements Function {
32  
33      private static final Object[] EMPTY_ARRAY = {};
34      private final Constructor constructor;
35  
36      /**
37       * Constructs a new ConstructorFunction.
38       *
39       * @param constructor the constructor to call.
40       */
41      public ConstructorFunction(final Constructor constructor) {
42          this.constructor = constructor;
43      }
44  
45      /**
46       * Converts parameters to suitable types and invokes the constructor.
47       *
48       * @param context    evaluation context
49       * @param parameters constructor args
50       * @return new instance
51       */
52      @Override
53      public Object invoke(final ExpressionContext context, Object[] parameters) {
54          try {
55              Object[] args;
56              if (parameters == null) {
57                  parameters = EMPTY_ARRAY;
58              }
59              int pi = 0;
60              final Class[] types = constructor.getParameterTypes();
61              if (types.length > 0 && ExpressionContext.class.isAssignableFrom(types[0])) {
62                  pi = 1;
63              }
64              args = new Object[parameters.length + pi];
65              if (pi == 1) {
66                  args[0] = context;
67              }
68              for (int i = 0; i < parameters.length; i++) {
69                  args[i + pi] = TypeUtils.convert(parameters[i], types[i + pi]);
70              }
71              return constructor.newInstance(args);
72          } catch (Throwable ex) {
73              if (ex instanceof InvocationTargetException) {
74                  ex = ((InvocationTargetException) ex).getTargetException();
75              }
76              throw new JXPathInvalidAccessException("Cannot invoke constructor " + constructor, ex);
77          }
78      }
79  }