1 18 package org.apache.geronimo.interop.generator; 19 20 import java.util.HashMap ; 21 22 public class JType extends JEntity { 23 24 private static HashMap typeMap = new HashMap (60); 25 private Class type; 26 private String typeDecl; 27 28 public JType(Class type) { 29 super(null); 30 setType(type); 31 } 32 33 public void setType(Class type) { 34 this.type = type; 35 calculateTypeDecl(); 36 } 37 38 public Class getType() { 39 return type; 40 } 41 42 public String getTypeDecl() { 43 return typeDecl; 44 } 45 46 public int hashCode() { 47 return type.hashCode(); 48 } 49 50 public boolean equals(Object 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 ) 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 convertToTypeDecl(String typeName) { 85 String 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 t) { 133 String 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 args[]) 140 throws Exception { 141 (new JType(java.lang.String .class)).showTypeInfo(); 142 (new JType(java.lang.String [].class)).showTypeInfo(); 143 (new JType(java.lang.String [][].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 .class)).validateDeclType("java.lang.String"); 150 (new JType(java.lang.String [].class)).validateDeclType("java.lang.String[]"); 151 (new JType(java.lang.String [][].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 |