KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > generator > JType


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.generator;
19
20 import java.util.HashMap JavaDoc;
21
22 public class JType extends JEntity {
23
24     private static HashMap JavaDoc typeMap = new HashMap JavaDoc(60);
25     private Class JavaDoc type;
26     private String JavaDoc typeDecl;
27
28     public JType(Class JavaDoc type) {
29         super(null);
30         setType(type);
31     }
32
33     public void setType(Class JavaDoc type) {
34         this.type = type;
35         calculateTypeDecl();
36     }
37
38     public Class JavaDoc getType() {
39         return type;
40     }
41
42     public String JavaDoc getTypeDecl() {
43         return typeDecl;
44     }
45
46     public int hashCode() {
47         return type.hashCode();
48     }
49
50     public boolean equals(Object JavaDoc other) {
51         boolean rc = false;
52
53         if (other == this) {
54             rc = true;
55         } else if (other instanceof JType) {
56             JType t = (JType) other;
57
58             rc = t.type.equals(type);
59         }
60
61         return rc;
62     }
63
64     protected void calculateTypeDecl() {
65         if (type == null) {
66             return;
67         }
68
69         typeDecl = (String JavaDoc) typeMap.get(type);
70
71         if (typeDecl == null) {
72             synchronized (typeMap) {
73                 typeDecl = type.getName();
74
75                 if (type.isArray()) {
76                     typeDecl = convertToTypeDecl(typeDecl);
77                 }
78
79                 typeMap.put(type, typeDecl);
80             }
81         }
82     }
83
84     protected String JavaDoc convertToTypeDecl(String JavaDoc typeName) {
85         String JavaDoc rc = "";
86         char charAt = 0;
87         int i;
88
89         if (typeName != null && typeName.length() > 0) {
90             for (i = 0; i < typeName.length(); i++) {
91                 charAt = typeName.charAt(i);
92
93                 if (charAt == '[') {
94                     rc = rc + "[]";
95                 } else if (charAt == 'Z') {
96                     rc = "boolean" + rc;
97                 } else if (charAt == 'B') {
98                     rc = "byte" + rc;
99                 } else if (charAt == 'C') {
100                     rc = "char" + rc;
101                 } else if (charAt == 'L') {
102                     int semiIndex = typeName.indexOf(";");
103                     rc = typeName.substring(i + 1, semiIndex) + rc;
104                     i = semiIndex;
105                 } else if (charAt == 'D') {
106                     rc = "double" + rc;
107                 } else if (charAt == 'F') {
108                     rc = "float" + rc;
109                 } else if (charAt == 'I') {
110                     rc = "int" + rc;
111                 } else if (charAt == 'J') {
112                     rc = "long" + rc;
113                 } else if (charAt == 'S') {
114                     rc = "short" + rc;
115                 } else {
116                     System.out.println("Error: Invalid signature. typeName = " + typeName + ", charAt = " + charAt + ", i = " + i);
117                 }
118             }
119         }
120
121         return rc;
122     }
123
124     protected void showTypeInfo() {
125         System.out.println("\tisArray() = " + type.isArray());
126         System.out.println("\tisPrimitive() = " + type.isPrimitive());
127         System.out.println("\ttoString() = " + type.toString());
128         System.out.println("\ttypeDecl = " + getTypeDecl());
129         System.out.println("");
130     }
131
132     protected void validateDeclType(String JavaDoc t) {
133         String JavaDoc ct = getTypeDecl();
134         if (!t.equals(ct)) {
135             System.out.println("Class Decl Type: '" + ct + "' does not match expected type: '" + t + "'");
136         }
137     }
138
139     public static void main(String JavaDoc args[])
140             throws Exception JavaDoc {
141         (new JType(java.lang.String JavaDoc.class)).showTypeInfo();
142         (new JType(java.lang.String JavaDoc[].class)).showTypeInfo();
143         (new JType(java.lang.String JavaDoc[][].class)).showTypeInfo();
144
145         (new JType(int.class)).showTypeInfo();
146         (new JType(int[].class)).showTypeInfo();
147         (new JType(int[][].class)).showTypeInfo();
148
149         (new JType(java.lang.String JavaDoc.class)).validateDeclType("java.lang.String");
150         (new JType(java.lang.String JavaDoc[].class)).validateDeclType("java.lang.String[]");
151         (new JType(java.lang.String JavaDoc[][].class)).validateDeclType("java.lang.String[][]");
152
153         (new JType(int.class)).validateDeclType("int");
154         (new JType(int[].class)).validateDeclType("int[]");
155         (new JType(int[][].class)).validateDeclType("int[][]");
156     }
157 }
158
Popular Tags