1 28 29 package org.jibx.binding.model; 30 31 import java.util.ArrayList ; 32 33 import org.jibx.binding.util.StringArray; 34 import org.jibx.runtime.EnumSet; 35 36 42 43 public class PropertyAttributes extends AttributeBase 44 { 45 46 public static final StringArray s_allowedAttributes = 47 new StringArray(new String [] { "field", "get-method", "set-method", 48 "test-method", "type", "usage" }); 49 50 53 public static final int REQUIRED_USAGE = 0; 54 public static final int OPTIONAL_USAGE = 1; 55 public static final int OPTIONAL_IN_USAGE = 2; 56 public static final int OPTIONAL_OUT_USAGE = 3; 57 private static final EnumSet s_usageEnum = new EnumSet(REQUIRED_USAGE, 58 new String [] { "required", "optional", "opt-in", "opt-out" }); 59 60 63 64 private int m_usage; 65 66 67 private String m_usageName = s_usageEnum.getName(REQUIRED_USAGE); 68 69 70 private String m_declaredType; 71 72 73 private String m_fieldName; 74 75 76 private String m_testName; 77 78 79 private String m_getName; 80 81 82 private String m_setName; 83 84 85 private IClass m_getType; 86 87 88 private IClass m_setType; 89 90 91 private IClass m_type; 92 93 94 private IClassItem m_fieldItem; 95 96 97 private IClassItem m_testItem; 98 99 100 private IClassItem m_getItem; 101 102 103 private IClassItem m_setItem; 104 105 106 private boolean m_isImplicit; 107 108 113 public String getUsageName() { 114 return s_usageEnum.getName(m_usage); 115 } 116 117 123 public int getUsage() { 124 return m_usage; 125 } 126 127 132 public void setUsageName(String name) { 133 m_usageName = name; 134 } 135 136 142 public boolean hasProperty() { 143 return !m_isImplicit && m_type != null; 144 } 145 146 151 public String getDeclaredType() { 152 return m_declaredType; 153 } 154 155 161 public IClass getType() { 162 return m_type; 163 } 164 165 170 public void setDeclaredType(String type) { 171 m_declaredType = type; 172 } 173 174 179 public String getFieldName() { 180 return m_fieldName; 181 } 182 183 189 public IClassItem getField() { 190 return m_fieldItem; 191 } 192 193 198 public void setFieldName(String field) { 199 m_fieldName = field; 200 } 201 202 207 public String getTestName() { 208 return m_testName; 209 } 210 211 217 public IClassItem getTest() { 218 return m_testItem; 219 } 220 221 226 public void setTestName(String test) { 227 m_testName = test; 228 } 229 230 235 public String getGetName() { 236 return m_getName; 237 } 238 239 245 public IClassItem getGet() { 246 return m_getItem; 247 } 248 249 255 public IClass getGetType() { 256 return m_getType; 257 } 258 259 264 public void setGetName(String get) { 265 m_getName = get; 266 } 267 268 273 public String getSetName() { 274 return m_setName; 275 } 276 277 283 public IClassItem getSet() { 284 return m_setItem; 285 } 286 287 293 public IClass getSetType() { 294 return m_setType; 295 } 296 297 302 public void setSetName(String set) { 303 m_setName = set; 304 } 305 306 315 public boolean isImplicit() { 316 return m_isImplicit; 317 } 318 319 322 public void prevalidate(ValidationContext vctx) { 323 324 if (m_usageName != null) { 326 m_usage = s_usageEnum.getValue(m_usageName); 327 if (m_usage < 0) { 328 vctx.addError("Value \"" + m_usageName + 329 "\" is not a valid choice for usage"); 330 } 331 } else { 332 m_usage = vctx.getParentElement().getDefaultStyle(); 333 } 334 335 ContainerElementBase parent = vctx.getParentContainer(); 337 IClass cobj = parent.getEffectiveType(); 338 String dtype = null; 339 String gtype = null; 340 String stype = null; 341 boolean err = false; 342 m_isImplicit = true; 343 if (m_fieldName != null) { 344 345 m_isImplicit = false; 347 348 m_fieldItem = cobj.getField(m_fieldName); 350 if (m_fieldItem == null) { 351 vctx.addFatal("Nonstatic field " + m_fieldName + 352 " not found in class " + cobj.getName()); 353 err = true; 354 } else { 355 dtype = gtype = stype = m_fieldItem.getTypeName(); 356 } 357 358 } 359 if (m_testName != null) { 360 361 m_testItem = cobj.getMethod(m_testName, "()Z"); 363 if (m_testItem == null) { 364 vctx.addError("Nonstatic test-method " + m_testName + 365 " not found in class " + cobj.getName()); 366 } 367 368 } 369 if (m_getName != null) { 370 371 m_isImplicit = false; 373 374 m_getItem = cobj.getMethod(m_getName, "()"); 376 if (m_getItem == null) { 377 vctx.addFatal("Nonstatic get-method " + m_getName + 378 " not found in class " + cobj.getName()); 379 err = true; 380 } else { 381 gtype = m_getItem.getTypeName(); 382 if (dtype == null) { 383 dtype = gtype; 384 } 385 } 386 } 387 if (m_setName != null) { 388 389 m_isImplicit = false; 391 392 ArrayList sigs = new ArrayList (); 394 if (m_getItem != null) { 395 sigs.add("(" + ClassUtils.getSignature(gtype) + ")V"); 396 } 397 if (m_declaredType != null) { 398 sigs.add("(" + ClassUtils.getSignature(m_declaredType) + ")V"); 399 } 400 if (m_fieldItem != null) { 401 sigs.add("(" + m_fieldItem.getSignature() + ")V"); 402 } 403 sigs.add("(Ljava/lang/Object;)V"); 404 405 m_setItem = cobj.getMethod(m_setName, 407 (String [])sigs.toArray(new String [0])); 408 if (m_setItem == null) { 409 vctx.addFatal("Nonstatic set-method " + m_setName + 410 " with argument of appropriate type not found in class " + 411 cobj.getName()); 412 err = true; 413 } else { 414 stype = m_setItem.getArgumentType(0); 415 if (dtype == null) { 416 dtype = stype; 417 } 418 } 419 } 420 421 String tname = m_declaredType; 423 if (tname == null) { 424 tname = dtype; 425 if (tname == null) { 426 tname = cobj.getName(); 427 } 428 } else if (dtype == null) { 429 dtype = gtype = stype = tname; 431 } 432 m_type = vctx.getClassInfo(tname); 433 if (m_type == null) { 434 vctx.addFatal("Unable to load class " + tname); 435 } else if (vctx.getContextObject() instanceof CollectionElement) { 436 437 if (m_fieldName != null || m_testName != null || 439 m_getName != null || m_setName != null) { 440 vctx.addWarning("Property access attributes (field, " + 441 "get-method, set-method, test-method) ignored " + 442 "for collection item"); 443 } 444 445 } else if (!err && !m_isImplicit) { 446 447 boolean valid = true; 449 450 if (vctx.isInBinding()) { 452 if (stype == null) { 453 vctx.addError("No way to set property value"); 454 } else { 455 valid = ClassUtils.isAssignable(tname, stype, vctx); 456 } 457 } 458 if (!vctx.isInBinding()) { 459 if (gtype == null) { 460 vctx.addError("No way to get property value"); 461 } else if (valid) { 462 valid = ClassUtils.isAssignable(tname, gtype, vctx) || 463 ClassUtils.isAssignable(gtype, tname, vctx); 464 } 465 } 466 if (!valid) { 467 vctx.addError("Incompatible types used in property definition"); 468 } 469 } 470 super.prevalidate(vctx); 471 } 472 } | Popular Tags |