KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 public class MethodUtil extends FunctionUtil
11 {
12     private static Map methodCriterias = new HashMap();
13
14     public static ASTMethodDeclarator getDeclarator(ASTMethodDeclaration method)
15     {
16         return (ASTMethodDeclarator)SimpleNodeUtil.findChild(method, ASTMethodDeclarator.class);
17     }
18
19     public static Token getName(ASTMethodDeclaration method)
20     {
21         ASTMethodDeclarator decl = getDeclarator(method);
22         return decl.getFirstToken();
23     }
24
25     public static ASTFormalParameters getParameters(ASTMethodDeclaration method)
26     {
27         ASTMethodDeclarator decl = getDeclarator(method);
28         return (ASTFormalParameters)SimpleNodeUtil.findChild(decl, ASTFormalParameters.class);
29     }
30
31     public static String JavaDoc getFullName(ASTMethodDeclaration method)
32     {
33         Token nameTk = getName(method);
34         ASTFormalParameters params = getParameters(method);
35         String JavaDoc fullName = toFullName(nameTk, params);
36         return fullName;
37     }
38
39     public static double getMatchScore(ASTMethodDeclaration a, ASTMethodDeclaration b)
40     {
41         // caching the criteria (instead of extracting it every time) is around 25% faster.
42
MethodMatchCriteria aCriteria = getCriteria(a);
43         MethodMatchCriteria bCriteria = getCriteria(b);
44
45         return aCriteria.compare(bCriteria);
46     }
47
48     protected static MethodMatchCriteria getCriteria(ASTMethodDeclaration method)
49     {
50         MethodMatchCriteria crit = (MethodMatchCriteria)methodCriterias.get(method);
51         if (crit == null) {
52             crit = new MethodMatchCriteria(method);
53             methodCriterias.put(method, crit);
54         }
55         return crit;
56     }
57
58 }
59
Popular Tags