KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > mockobjects > util > CodeUtils


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.mockobjects.util;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import xjavadoc.XClass;
13
14 /**
15  * Utility methods.
16  *
17  * @author Joe Walnes
18  * @author Stig Jørgensen
19  * @created 5. februar 2003
20  */

21 public class CodeUtils
22 {
23
24     private final static Map JavaDoc wrappers = new HashMap JavaDoc();
25
26     static {
27         wrappers.put("boolean", "Boolean");
28         wrappers.put("char", "Character");
29         wrappers.put("byte", "Byte");
30         wrappers.put("short", "Short");
31         wrappers.put("int", "Integer");
32         wrappers.put("long", "Long");
33         wrappers.put("float", "Float");
34         wrappers.put("double", "Double");
35     }
36
37     /**
38      * For a class: returns the methods for the specied class and its superclasses. <p>
39      *
40      * For an interface: returns the methods for the specified interface and all its extending interfaces.
41      *
42      * @param startClass
43      * @return
44      * @todo:unittest
45      */

46     public static List JavaDoc getAllMethods(XClass startClass)
47     {
48         return new ArrayList JavaDoc(startClass.getMethods(true));
49     }
50
51     public static String JavaDoc wrapValue(String JavaDoc name, String JavaDoc type)
52     {
53         if (wrappers.containsKey(type)) {
54             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
55
56             result.append("new ");
57             result.append(wrappers.get(type));
58             result.append('(');
59             result.append(name);
60             result.append(')');
61             return result.toString();
62         }
63         else {
64             return name;
65         }
66     }
67
68     public static String JavaDoc unwrapValue(String JavaDoc name, String JavaDoc type)
69     {
70         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
71
72         if (wrappers.containsKey(type)) {
73             result.append("((");
74             result.append(wrappers.get(type));
75             result.append(')');
76             result.append(name);
77             result.append(").");
78             result.append(type);
79             result.append("Value()");
80         }
81         else {
82             result.append('(');
83             result.append(type);
84             result.append(')');
85             result.append(name);
86         }
87         return result.toString();
88     }
89
90     public static String JavaDoc capitalize(String JavaDoc s)
91     {
92         return s.substring(0, 1).toUpperCase() + s.substring(1);
93     }
94 }
95
Popular Tags