KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jxpath > functions > ConstructorFunction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.jxpath.functions;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20
21 import org.apache.commons.jxpath.ExpressionContext;
22 import org.apache.commons.jxpath.Function;
23 import org.apache.commons.jxpath.JXPathException;
24 import org.apache.commons.jxpath.util.TypeUtils;
25
26 /**
27  * An extension function that creates an instance using a constructor.
28  *
29  * @author Dmitri Plotnikov
30  * @version $Revision: 1.11 $ $Date: 2004/02/29 14:17:44 $
31  */

32 public class ConstructorFunction implements Function {
33
34     private Constructor JavaDoc constructor;
35     private static final Object JavaDoc EMPTY_ARRAY[] = new Object JavaDoc[0];
36
37     public ConstructorFunction(Constructor JavaDoc constructor) {
38         this.constructor = constructor;
39     }
40
41     /**
42      * Converts parameters to suitable types and invokes the constructor.
43      */

44     public Object JavaDoc invoke(ExpressionContext context, Object JavaDoc[] parameters) {
45         try {
46             Object JavaDoc[] args;
47             if (parameters == null) {
48                 parameters = EMPTY_ARRAY;
49             }
50             int pi = 0;
51             Class JavaDoc types[] = constructor.getParameterTypes();
52             if (types.length > 0
53                 && ExpressionContext.class.isAssignableFrom(types[0])) {
54                 pi = 1;
55             }
56             args = new Object JavaDoc[parameters.length + pi];
57             if (pi == 1) {
58                 args[0] = context;
59             }
60             for (int i = 0; i < parameters.length; i++) {
61                 args[i + pi] = TypeUtils.convert(parameters[i], types[i + pi]);
62             }
63             return constructor.newInstance(args);
64         }
65         catch (Throwable JavaDoc ex) {
66             if (ex instanceof InvocationTargetException JavaDoc) {
67                 ex = ((InvocationTargetException JavaDoc) ex).getTargetException();
68             }
69             throw new JXPathException(
70                 "Cannot invoke constructor " + constructor,
71                 ex);
72         }
73     }
74 }
Popular Tags