KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > bytecode > ChainedParamReader


1 /*
2  * Copyright 2002-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.axis.utils.bytecode;
17
18 import java.io.IOException JavaDoc;
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.*;
22
23 /**
24  * Description: In ParamReader class, user can not get inherited method parameter
25  * from the class they passed in. This is done because of performance. This class
26  * is intended to setup the inheritant chain. If the method could not be found in
27  * the derived class, it will try to search it from super class, if not in the
28  * immedidate super class it will search super class's super class, until it reaches
29  * the root which is java.lang.Object. This is not an eager load since it only
30  * start searching the super class when it is asked to
31  * User: pengyu
32  * Date: Sep 6, 2003
33  * Time: 11:43:24 PM
34  *
35  */

36 public class ChainedParamReader {
37     private List chain = new ArrayList();
38     private List clsChain = new ArrayList();
39     private Map methodToParamMap = new HashMap();
40
41     /**
42      * Process a given class's parameter names
43      * @param cls the class which user wants to get parameter info from
44      * @throws IOException
45      */

46     public ChainedParamReader(Class JavaDoc cls) throws IOException JavaDoc {
47         ParamReader reader = new ParamReader(cls);
48         chain.add(reader);
49         clsChain.add(cls);
50     }
51
52     //now I need to create deligate methods
53
/**
54      * return the names of the declared parameters for the given constructor.
55      * If we cannot determine the names, return null. The returned array will
56      * have one name per parameter. The length of the array will be the same
57      * as the length of the Class[] array returned by Constructor.getParameterTypes().
58      * @param ctor
59      * @return array of names, one per parameter, or null
60      */

61     public String JavaDoc[] getParameterNames(Constructor JavaDoc ctor) {
62         //there is no need for the constructor chaining.
63
return ((ParamReader) chain.get(0)).getParameterNames(ctor);
64     }
65
66     /**
67      * return the names of the declared parameters for the given method.
68      * If we cannot determine the names in the current class, we will try
69      * to search its parent class until we reach java.lang.Object. If we
70      * still can not find the method we will return null. The returned array
71      * will have one name per parameter. The length of the array will be the same
72      * as the length of the Class[] array returned by Method.getParameterTypes().
73      * @param method
74      * @return String[] array of names, one per parameter, or null
75      **/

76     public String JavaDoc[] getParameterNames(Method JavaDoc method) {
77         //go find the one from the cache first
78
if (methodToParamMap.containsKey(method)) {
79             return (String JavaDoc[]) methodToParamMap.get(method);
80         }
81
82         String JavaDoc[] ret = null;
83         for (Iterator it = chain.iterator(); it.hasNext();) {
84             ParamReader reader = (ParamReader) it.next();
85             ret = reader.getParameterNames(method);
86             if (ret != null) {
87                 methodToParamMap.put(method, ret);
88                 return ret;
89             }
90         }
91         //if we here, it means we need to create new chain.
92
Class JavaDoc cls = (Class JavaDoc) clsChain.get(chain.size() - 1);
93         while (cls.getSuperclass() != null) {
94             Class JavaDoc superClass = cls.getSuperclass();
95             try {
96                 ParamReader _reader = new ParamReader(superClass);
97                 chain.add(_reader);
98                 clsChain.add(cls);
99                 ret = _reader.getParameterNames(method);
100                 if (ret != null) { //we found it so just return it.
101
methodToParamMap.put(method, ret);
102                     return ret;
103                 }
104             } catch (IOException JavaDoc e) {
105                 //can not find the super class in the class path, abort here
106
return null;
107             }
108         }
109         methodToParamMap.put(method, ret);
110         return null;
111     }
112 }
113
Popular Tags