1 28 29 package org.jibx.binding.def; 30 31 import org.jibx.binding.classes.*; 32 import org.jibx.runtime.JiBXException; 33 34 50 51 public class ObjectStringConversion extends StringConversion 52 { 53 56 private static final String TOSTRING_METHOD = "toString"; 57 private static final String TOSTRING_SIGNATURE = 58 "()Ljava/lang/String;"; 59 private static final String FROMSTRING_SIGNATURE = 60 "(Ljava/lang/String;)V"; 61 62 65 67 private boolean m_needDeserialize; 68 69 72 private ClassItem m_initFromString; 73 74 76 private boolean m_needSerialize; 77 78 81 private ClassItem m_instToString; 82 83 91 92 ObjectStringConversion(String type, ObjectStringConversion inherit) 93 throws JiBXException { 94 super(type, inherit); 95 if (type.equals(inherit.m_typeName)) { 96 m_needDeserialize = inherit.m_needDeserialize; 97 m_initFromString = inherit.m_initFromString; 98 m_needSerialize = inherit.m_needSerialize; 99 m_instToString = inherit.m_instToString; 100 } else { 101 initMethods(); 102 } 103 } 104 105 116 117 ObjectStringConversion(Object dflt, String ser, String deser, 118 String type) { 119 super(dflt, ser, deser, type); 120 try { 121 initMethods(); 122 } catch (JiBXException ex) { 123 throw new IllegalArgumentException (ex.getMessage()); 124 } 125 } 126 127 135 136 private void initMethods() throws JiBXException { 137 if (!"java.lang.String".equals(m_typeName)) { 138 m_needSerialize = true; 139 m_needDeserialize = !"java.lang.Object".equals(m_typeName); 140 ClassFile cf = ClassCache.getClassFile(m_typeName); 141 m_initFromString = cf.getInitializerMethod(FROMSTRING_SIGNATURE); 142 m_instToString = cf.getMethod(TOSTRING_METHOD, TOSTRING_SIGNATURE); 143 } 144 } 145 146 154 155 public void genFromText(MethodBuilder mb) throws JiBXException { 156 157 if (m_deserializer != null) { 159 160 mb.addMethodExceptions(m_deserializer); 164 mb.appendCall(m_deserializer); 165 166 } else if (m_initFromString != null) { 167 168 mb.appendDUP(); 173 BranchWrapper ifnnull = mb.appendIFNONNULL(this); 174 mb.appendPOP(); 175 mb.appendACONST_NULL(); 176 BranchWrapper toend = mb.appendUnconditionalBranch(this); 177 178 mb.targetNext(ifnnull); 181 mb.appendCreateNew(m_typeName); 182 mb.appendDUP_X1(); 183 mb.appendSWAP(); 184 mb.appendCallInit(m_typeName, FROMSTRING_SIGNATURE); 185 mb.targetNext(toend); 186 187 } else if (m_needDeserialize) { 188 throw new JiBXException("No deserializer for " + m_typeName + 189 "; define deserializer or constructor from java.lang.String"); 190 } 191 } 192 193 205 206 public void genParseOptional(boolean attr, MethodBuilder mb) 207 throws JiBXException { 208 209 mb.appendLoadConstant((String )m_default); 213 String name = attr ? UNMARSHAL_OPT_ATTRIBUTE : UNMARSHAL_OPT_ELEMENT; 214 mb.appendCallVirtual(name, UNMARSHAL_OPT_SIGNATURE); 215 216 genFromText(mb); 218 } 219 220 231 232 public void genParseRequired(boolean attr, MethodBuilder mb) 233 throws JiBXException { 234 235 String name = attr ? UNMARSHAL_REQ_ATTRIBUTE : UNMARSHAL_REQ_ELEMENT; 239 mb.appendCallVirtual(name, UNMARSHAL_REQ_SIGNATURE); 240 241 genFromText(mb); 243 } 244 245 258 259 public void genToText(String type, MethodBuilder mb) 260 throws JiBXException { 261 if (m_serializer == null && m_needSerialize) { 262 263 if (m_instToString == null) { 265 throw new JiBXException("No serializer for " + m_typeName + 266 "; define serializer or toString() method"); 267 } else { 268 269 mb.addMethodExceptions(m_instToString); 273 mb.appendCall(m_instToString); 274 275 } 276 } else { 277 super.genToText(type, mb); 278 } 279 } 280 281 295 296 protected BranchWrapper genToOptionalText(String type, MethodBuilder mb, 297 int extra) throws JiBXException { 298 299 if (m_default == null) { 301 302 genToText(type, mb); 305 return null; 306 307 } else { 308 309 genToText(type, mb); 312 313 mb.appendDUP(); 319 mb.appendLoadConstant((String )m_default); 320 mb.appendCallStatic(COMPARE_OBJECTS_METHOD, 321 COMPARE_OBJECTS_SIGNATURE); 322 BranchWrapper iffalse = mb.appendIFEQ(this); 323 324 genPopValues(extra+1, mb); 327 BranchWrapper toend = mb.appendUnconditionalBranch(this); 328 mb.targetNext(iffalse); 329 return toend; 330 331 } 332 } 333 334 339 340 public boolean isPrimitive() { 341 return false; 342 } 343 344 352 353 protected Object convertDefault(String text) throws JiBXException { 354 return text; 355 } 356 357 372 373 public StringConversion derive(String type, String ser, String dser, 374 String dflt) throws JiBXException { 375 if (type == null) { 376 type = m_typeName; 377 } 378 StringConversion inst = new ObjectStringConversion(type, this); 379 if (ser != null) { 380 inst.setSerializer(ser); 381 } 382 if (dser != null) { 383 inst.setDeserializer(dser); 384 } 385 if (dflt != null) { 386 inst.m_default = inst.convertDefault(dflt); 387 } 388 return inst; 389 } 390 } 391 | Popular Tags |