01: package org.incava.java;
02:
03: import java.util.*;
04: import net.sourceforge.pmd.ast.*;
05:
06: /**
07: * Miscellaneous routines for method declarations.
08: */
09: public class MethodUtil extends FunctionUtil {
10: private static Map methodCriterias = new HashMap();
11:
12: public static ASTMethodDeclarator getDeclarator(
13: ASTMethodDeclaration method) {
14: return (ASTMethodDeclarator) SimpleNodeUtil.findChild(method,
15: ASTMethodDeclarator.class);
16: }
17:
18: public static Token getName(ASTMethodDeclaration method) {
19: ASTMethodDeclarator decl = getDeclarator(method);
20: return decl.getFirstToken();
21: }
22:
23: public static ASTFormalParameters getParameters(
24: ASTMethodDeclaration method) {
25: ASTMethodDeclarator decl = getDeclarator(method);
26: return (ASTFormalParameters) SimpleNodeUtil.findChild(decl,
27: ASTFormalParameters.class);
28: }
29:
30: public static String getFullName(ASTMethodDeclaration method) {
31: Token nameTk = getName(method);
32: ASTFormalParameters params = getParameters(method);
33: String fullName = toFullName(nameTk, params);
34: return fullName;
35: }
36:
37: public static double getMatchScore(ASTMethodDeclaration a,
38: ASTMethodDeclaration b) {
39: // caching the criteria (instead of extracting it every time) is around 25% faster.
40: MethodMatchCriteria aCriteria = getCriteria(a);
41: MethodMatchCriteria bCriteria = getCriteria(b);
42:
43: return aCriteria.compare(bCriteria);
44: }
45:
46: protected static MethodMatchCriteria getCriteria(
47: ASTMethodDeclaration method) {
48: MethodMatchCriteria crit = (MethodMatchCriteria) methodCriterias
49: .get(method);
50: if (crit == null) {
51: crit = new MethodMatchCriteria(method);
52: methodCriterias.put(method, crit);
53: }
54: return crit;
55: }
56:
57: }
|