KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > CodegenUtils


1 /*
2  * CodegenUtils.java
3  *
4  * Created on January 31, 2007, 11:54 AM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.jruby.util;
11
12 /**
13  *
14  * @author headius
15  */

16 public class CodegenUtils {
17     public static final CodegenUtils instance = new CodegenUtils();
18     
19     /**
20      * Creates a dotted class name from a path/package name
21      */

22     public static String JavaDoc c(String JavaDoc p) {
23         return p.replace('/', '.');
24     }
25
26     /**
27      * Creates a class path name, from a Class.
28      */

29     public static String JavaDoc p(Class JavaDoc n) {
30         return n.getName().replace('.','/');
31     }
32
33     /**
34      * Creates a class identifier of form Labc/abc;, from a Class.
35      */

36     public static String JavaDoc ci(Class JavaDoc n) {
37         if (n.isArray()) {
38             n = n.getComponentType();
39             if (n.isPrimitive()) {
40                 if (n == Byte.TYPE) {
41                     return "[B";
42                 } else if (n == Boolean.TYPE) {
43                     return "[Z";
44                 } else if (n == Integer.TYPE) {
45                     return "[I";
46                 } else if (n == Double.TYPE) {
47                     return "[D";
48                 } else if (n == Long.TYPE) {
49                     return "[J";
50                 } else {
51                     throw new RuntimeException JavaDoc("Unrecognized type in compiler: " + n.getName());
52                 }
53             } else {
54                 return "[" + ci(n);
55             }
56         } else {
57             if (n.isPrimitive()) {
58                 if (n == Byte.TYPE) {
59                     return "B";
60                 } else if (n == Boolean.TYPE) {
61                     return "Z";
62                 } else if (n == Integer.TYPE) {
63                     return "I";
64                 } else if (n == Double.TYPE) {
65                     return "D";
66                 } else if (n == Long.TYPE) {
67                     return "J";
68                 } else if (n == Void.TYPE) {
69                     return "V";
70                 } else {
71                     throw new RuntimeException JavaDoc("Unrecognized type in compiler: " + n.getName());
72                 }
73             } else {
74                 return "L" + p(n) + ";";
75             }
76         }
77     }
78     
79     /**
80      * Create a method signature from the given param types and return values
81      */

82     public static String JavaDoc sig(Class JavaDoc retval, Class JavaDoc[] params) {
83         StringBuffer JavaDoc signature = new StringBuffer JavaDoc("(");
84         
85         for (int i = 0; i < params.length; i++) {
86             signature.append(ci(params[i]));
87         }
88         
89         signature.append(")").append(ci(retval));
90         
91         return signature.toString();
92     }
93     
94     /**
95      * Create a method signature with just a return value
96      */

97     public static String JavaDoc sig(Class JavaDoc retval) {
98         StringBuffer JavaDoc signature = new StringBuffer JavaDoc("()");
99         
100         signature.append(ci(retval));
101         
102         return signature.toString();
103     }
104     
105     // TODO: Wouldn't it be nice to replace this all with a single varargs?
106
public static Class JavaDoc[] params() {
107         return new Class JavaDoc[0];
108     }
109     public static Class JavaDoc[] params(Class JavaDoc a) {
110         return new Class JavaDoc[] {a};
111     }
112     public static Class JavaDoc[] params(Class JavaDoc a, Class JavaDoc b) {
113         return new Class JavaDoc[] {a,b};
114     }
115     public static Class JavaDoc[] params(Class JavaDoc a, Class JavaDoc b, Class JavaDoc c) {
116         return new Class JavaDoc[] {a,b,c};
117     }
118     public static Class JavaDoc[] params(Class JavaDoc a, Class JavaDoc b, Class JavaDoc c, Class JavaDoc d) {
119         return new Class JavaDoc[] {a,b,c,d};
120     }
121     public static Class JavaDoc[] params(Class JavaDoc a, Class JavaDoc b, Class JavaDoc c, Class JavaDoc d, Class JavaDoc e) {
122         return new Class JavaDoc[] {a,b,c,d,e};
123     }
124     public static Class JavaDoc[] params(Class JavaDoc a, Class JavaDoc b, Class JavaDoc c, Class JavaDoc d, Class JavaDoc e, Class JavaDoc f) {
125         return new Class JavaDoc[] {a,b,c,d,e,f};
126     }
127     
128 }
129
Popular Tags