KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > java > ParameterUtil


1 package org.incava.java;
2
3 import java.util.*;
4 import net.sourceforge.pmd.ast.*;
5
6
7 /**
8  * Miscellaneous routines for parameters.
9  */

10 public class ParameterUtil extends SimpleNodeUtil
11 {
12     public static ASTFormalParameter[] getParameters(ASTFormalParameters params)
13     {
14         return (ASTFormalParameter[])findChildren(params, ASTFormalParameter.class);
15     }
16
17     public static Token[] getParameterNames(ASTFormalParameters params)
18     {
19         ASTFormalParameter[] fps = getParameters(params);
20         List names = new ArrayList();
21         
22         for (int pi = 0; pi < fps.length; ++pi) {
23             ASTFormalParameter fp = fps[pi];
24             Token name = getParameterName(fp);
25             names.add(name);
26         }
27
28         return (Token[])names.toArray(new Token[0]);
29     }
30
31     public static ASTFormalParameter getParameter(ASTFormalParameters params, int index)
32     {
33         return (ASTFormalParameter)findChild(params, ASTFormalParameter.class, index);
34     }
35
36     public static Token getParameterName(ASTFormalParameters params, int index)
37     {
38         ASTFormalParameter param = getParameter(params, index);
39         return getParameterName(param);
40     }
41
42     public static String JavaDoc getParameterType(ASTFormalParameters params, int index)
43     {
44         ASTFormalParameter param = getParameter(params, index);
45         return getParameterType(param);
46     }
47
48     public static List getParameterTypes(ASTFormalParameters params)
49     {
50         List types = new ArrayList();
51         int nParams = params.jjtGetNumChildren();
52         for (int i = 0; i < nParams; ++i) {
53             ASTFormalParameter param = (ASTFormalParameter)params.jjtGetChild(i);
54             String JavaDoc type = getParameterType(param);
55             types.add(type);
56         }
57         return types;
58     }
59
60     public static List getParameterList(ASTFormalParameters params)
61     {
62         List paramList = new ArrayList();
63         int nParams = params.jjtGetNumChildren();
64
65         for (int i = 0; i < nParams; ++i) {
66             ASTFormalParameter param = getParameter(params, i);
67             String JavaDoc name = getParameterName(param).image;
68             String JavaDoc type = getParameterType(param);
69             // tr.Ace.log("type: " + type + "; name: " + name);
70
Object JavaDoc[] values = new Object JavaDoc[] { param, type, name };
71             paramList.add(values);
72         }
73
74         return paramList;
75     }
76
77     public static Token getParameterName(ASTFormalParameter param)
78     {
79         if (param == null) {
80             return null;
81         }
82         else {
83             ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId)param.jjtGetChild(1);
84             return vid.getFirstToken();
85         }
86     }
87
88     public static String JavaDoc getParameterType(ASTFormalParameter param)
89     {
90         if (param == null) {
91             return null;
92         }
93         else {
94             // type is the first child, but we also have to look for the
95
// variable ID including brackets, for arrays
96
StringBuffer JavaDoc typeBuf = new StringBuffer JavaDoc();
97             ASTType type = (ASTType)SimpleNodeUtil.findChild(param, ASTType.class);
98             Token ttk = type.getFirstToken();
99         
100             while (true) {
101                 typeBuf.append(ttk.image);
102                 if (ttk == type.getLastToken()) {
103                     break;
104                 }
105                 else {
106                     ttk = ttk.next;
107                 }
108             }
109             
110             ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId)SimpleNodeUtil.findChild(param, ASTVariableDeclaratorId.class);
111             
112             Token vtk = vid.getFirstToken();
113             while (vtk != vid.getLastToken()) {
114                 vtk = vtk.next;
115                 typeBuf.append(vtk.image);
116             }
117
118             return typeBuf.toString();
119         }
120     }
121
122     public static int[] getMatch(List aParameters, int aIndex, List bParameters)
123     {
124         int typeMatch = -1;
125         int nameMatch = -1;
126         
127         Object JavaDoc[] aValues = (Object JavaDoc[])aParameters.get(aIndex);
128
129         for (int bi = 0; bi < bParameters.size(); ++bi) {
130             Object JavaDoc[] bValues = (Object JavaDoc[])bParameters.get(bi);
131
132             if (bValues == null) {
133                 // tr.Ace.log("already consumed");
134
}
135             else {
136                 if (aValues[1].equals(bValues[1])) {
137                     typeMatch = bi;
138                 }
139
140                 if (aValues[2].equals(bValues[2])) {
141                     nameMatch = bi;
142                 }
143
144                 if (typeMatch == bi && nameMatch == bi) {
145                     aParameters.set(aIndex, null);
146                     bParameters.set(bi, null);
147                     return new int[] { typeMatch, nameMatch };
148                 }
149             }
150         }
151
152         int bestMatch = typeMatch;
153         if (bestMatch < 0) {
154             bestMatch = nameMatch;
155         }
156
157         if (bestMatch >= 0) {
158             // make sure there isn't an exact match for this somewhere else in
159
// aParameters
160
Object JavaDoc[] bValues = (Object JavaDoc[])bParameters.get(bestMatch);
161             int aMatch = getExactMatch(aParameters, bValues);
162             if (aMatch >= 0) {
163                 return new int[] { -1, -1 };
164             }
165             else {
166                 aParameters.set(aIndex, null);
167                 bParameters.set(bestMatch, null);
168                 return new int[] { typeMatch, nameMatch };
169             }
170         }
171         else {
172             return new int[] { -1, -1 };
173         }
174     }
175
176     public static double getMatchScore(ASTFormalParameters a, ASTFormalParameters b)
177     {
178         double score;
179         
180         if (a.jjtGetNumChildren() == 0 && b.jjtGetNumChildren() == 0) {
181             score = 1.0;
182         }
183         else {
184             // (int[], double, String) <=> (int[], double, String) ==> 100% (3 of 3)
185
// (int[], double, String) <=> (double, int[], String) ==> 80% (3 of 3 - 10% * misordered)
186
// (int[], double) <=> (double, int[], String) ==> 46% (2 of 3 - 10% * misordered)
187
// (int[], double, String) <=> (String) ==> 33% (1 of 3 params)
188
// (int[], double) <=> (String) ==> 0 (0 of 3)
189

190             List aParamTypes = ParameterUtil.getParameterTypes(a);
191             List bParamTypes = ParameterUtil.getParameterTypes(b);
192
193             int aSize = aParamTypes.size();
194             int bSize = bParamTypes.size();
195
196             int exactMatches = 0;
197             int misorderedMatches = 0;
198             
199             for (int ai = 0; ai < aSize; ++ai) {
200                 int paramMatch = getListMatch(aParamTypes, ai, bParamTypes);
201                 if (paramMatch == ai) {
202                     ++exactMatches;
203                 }
204                 else if (paramMatch >= 0) {
205                     ++misorderedMatches;
206                 }
207             }
208
209             for (int bi = 0; bi < bSize; ++bi) {
210                 int paramMatch = getListMatch(bParamTypes, bi, aParamTypes);
211                 if (paramMatch == bi) {
212                     ++exactMatches;
213                 }
214                 else if (paramMatch >= 0) {
215                     ++misorderedMatches;
216                 }
217             }
218
219             int numParams = Math.max(aSize, bSize);
220             double match = (double)exactMatches / numParams;
221             match += (double)misorderedMatches / (2 * numParams);
222
223             score = 0.5 + (match / 2.0);
224         }
225         
226         return score;
227     }
228
229     /**
230      * Returns 0 for exact match, +1 for misordered match, -1 for no match.
231      */

232     protected static int getListMatch(List aList, int aIndex, List bList)
233     {
234         int aSize = aList.size();
235         int bSize = bList.size();
236         String JavaDoc aStr = aIndex < aSize ? (String JavaDoc)aList.get(aIndex) : null;
237         String JavaDoc bStr = aIndex < bSize ? (String JavaDoc)bList.get(aIndex) : null;
238         
239         if (aStr == null) {
240             return -1;
241         }
242         if (aStr.equals(bStr)) {
243             aList.set(aIndex, null);
244             bList.set(aIndex, null);
245             return aIndex;
246         }
247         else {
248             for (int bi = 0; bi < bSize; ++bi) {
249                 bStr = (String JavaDoc)bList.get(bi);
250                 if (aStr.equals(bStr)) {
251                     aList.set(aIndex, null);
252                     bList.set(bi, null);
253                     return bi;
254                 }
255             }
256             return -1;
257         }
258     }
259
260     protected static int getExactMatch(List parameters, Object JavaDoc[] values)
261     {
262         for (int i = 0; i < parameters.size(); ++i) {
263             Object JavaDoc[] pv = (Object JavaDoc[])parameters.get(i);
264             if (pv == null) {
265                 // tr.Ace.log("null parameters");
266
}
267             else if (pv[1].equals(values[1]) && pv[2].equals(values[2])) {
268                 return i;
269             }
270         }
271         return -1;
272     }
273
274 }
275
Popular Tags