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.jxpath.functions;
18  
19  import java.lang.reflect.Constructor;
20  import java.lang.reflect.InvocationTargetException;
21  
22  import org.apache.commons.jxpath.ExpressionContext;
23  import org.apache.commons.jxpath.Function;
24  import org.apache.commons.jxpath.JXPathInvalidAccessException;
25  import org.apache.commons.jxpath.util.TypeUtils;
26  
27  /**
28   * An extension function that creates an instance using a constructor.
29   *
30   * @author Dmitri Plotnikov
31   * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
32   */
33  public class ConstructorFunction implements Function {
34      private static final Object[] EMPTY_ARRAY = new Object[0];
35  
36      private Constructor constructor;
37  
38      /**
39       * Create a new ConstructorFunction.
40       * @param constructor the constructor to call.
41       */
42      public ConstructorFunction(Constructor constructor) {
43          this.constructor = constructor;
44      }
45  
46      /**
47       * Converts parameters to suitable types and invokes the constructor.
48       * @param context evaluation context
49       * @param parameters constructor args
50       * @return new instance
51       */
52      public Object invoke(ExpressionContext context, Object[] parameters) {
53          try {
54              Object[] args;
55              if (parameters == null) {
56                  parameters = EMPTY_ARRAY;
57              }
58              int pi = 0;
59              Class[] types = constructor.getParameterTypes();
60              if (types.length > 0
61                  && 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          }
73          catch (Throwable ex) {
74              if (ex instanceof InvocationTargetException) {
75                  ex = ((InvocationTargetException) ex).getTargetException();
76              }
77              throw new JXPathInvalidAccessException(
78                  "Cannot invoke constructor " + constructor,
79                  ex);
80          }
81      }
82  }