1 22 package org.jboss.reflect.spi; 23 24 import java.io.ObjectStreamException ; 25 import java.io.Serializable ; 26 import java.util.HashMap ; 27 28 import org.jboss.reflect.plugins.ClassInfoImpl; 29 import org.jboss.reflect.plugins.ValueConvertor; 30 import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory; 31 32 39 public class PrimitiveInfo implements TypeInfo, Serializable 40 { 41 42 private static final long serialVersionUID = 3256718498443835449L; 43 44 45 public static final PrimitiveInfo BOOLEAN = new PrimitiveInfo("boolean", 0, Boolean.TYPE); 46 47 48 public static final PrimitiveInfo BYTE = new PrimitiveInfo("byte", 1, Byte.TYPE); 49 50 51 public static final PrimitiveInfo CHAR = new PrimitiveInfo("char", 2, Character.TYPE); 52 53 54 public static final PrimitiveInfo DOUBLE = new PrimitiveInfo("double", 3, Double.TYPE); 55 56 57 public static final PrimitiveInfo FLOAT = new PrimitiveInfo("float", 4, Float.TYPE); 58 59 60 public static final PrimitiveInfo INT = new PrimitiveInfo("int", 5, Integer.TYPE); 61 62 63 public static final PrimitiveInfo LONG = new PrimitiveInfo("long", 6, Long.TYPE); 64 65 66 public static final PrimitiveInfo SHORT = new PrimitiveInfo("short", 7, Short.TYPE); 67 68 69 public static final PrimitiveInfo VOID = new PrimitiveInfo("void", 8, Void.TYPE); 70 71 72 private static final PrimitiveInfo[] values = {BOOLEAN, BYTE, CHAR, DOUBLE, FLOAT, INT, LONG, SHORT, VOID}; 73 74 75 protected static final TypeInfoFactory typeInfoFactory = new IntrospectionTypeInfoFactory(); 76 77 78 protected final transient String name; 79 80 81 protected final int ordinal; 82 83 84 protected final transient Class <? extends Object > type; 85 86 87 private static HashMap <String , PrimitiveInfo> map = new HashMap <String , PrimitiveInfo>(); 88 89 static 90 { 91 map.put("boolean", BOOLEAN); 92 map.put("byte", BYTE); 93 map.put("char", CHAR); 94 map.put("double", DOUBLE); 95 map.put("float", FLOAT); 96 map.put("int", INT); 97 map.put("long", LONG); 98 map.put("short", SHORT); 99 map.put("void", VOID); 100 } 101 102 108 public static PrimitiveInfo valueOf(String name) 109 { 110 return map.get(name); 111 } 112 113 120 protected PrimitiveInfo(String name, int ordinal, Class <? extends Object > type) 121 { 122 this.name = name; 123 this.ordinal = ordinal; 124 this.type = type; 125 } 126 127 132 public int ordinal() 133 { 134 return ordinal; 135 } 136 137 public String getName() 138 { 139 return name; 140 } 141 142 public Class getType() 143 { 144 return type; 145 } 146 147 public Object convertValue(Object value) throws Throwable 148 { 149 return ValueConvertor.convertValue(type, value); 150 } 151 152 public boolean isArray() 153 { 154 return false; 155 } 156 157 public TypeInfo getArrayType(int depth) 158 { 159 Class arrayClass = ClassInfoImpl.getArrayClass(getType(), depth); 160 return typeInfoFactory.getTypeInfo(arrayClass); 161 } 162 163 public Object [] newArrayInstance(int size) throws Throwable 164 { 165 throw new UnsupportedOperationException ("Not an array " + name); 166 } 167 168 public String toString() 169 { 170 return name; 171 } 172 173 public boolean equals(Object obj) 174 { 175 if (obj == this) 176 return true; 177 if (obj == null) 178 return false; 179 if (!(obj instanceof PrimitiveInfo)) 180 return false; 181 if (!obj.getClass().equals(this.getClass())) 182 return false; 183 PrimitiveInfo other = (PrimitiveInfo) obj; 184 return other.ordinal == this.ordinal; 185 } 186 187 public int hashCode() 188 { 189 return name.hashCode(); 190 } 191 192 Object readResolve() throws ObjectStreamException 193 { 194 return values[ordinal]; 195 } 196 } 197 | Popular Tags |