* @param useJava50 true if source code is supposed to be generated for Java 5
*/
private void createSetterMethod(final FieldInfo fieldInfo,
final JClass jClass, final boolean useJava50) {
JMethod method = null;
JSourceCode jsc = null;
XMLInfoNature xmlNature = new XMLInfoNature(fieldInfo);
String mname = fieldInfo.getMethodSuffix();
XSType xsType = xmlNature.getSchemaType();
JType jType = xsType.getJType();
//-- create set method
/*
* fieldInfo.getWriteMethodName() will either prefix the method
* with 'add' (for multivalued fields) or 'set'!
* @see FieldInfo#getWriteMethodeName()
*/
method = new JMethod(fieldInfo.getWriteMethodName());
jClass.addMethod(method);
String paramName = fieldInfo.getName();
//-- make parameter name pretty,
//-- simply for aesthetic beauty
if (paramName.indexOf('_') == 0) {
String tempName = paramName.substring(1);
if (_javaNaming.isValidJavaIdentifier(tempName)) {
paramName = tempName;
}
}
method.addParameter(new JParameter(jType, paramName));
// if (useJava50) {
// Java5HacksHelper.addOverrideAnnotations(method.getSignature()); // DAB Java 5.0 hack
// }
createSetterComment(fieldInfo, method.getJDocComment());
jsc = method.getSourceCode();
String fieldName = fieldInfo.getName();
//-- bound properties
if (fieldInfo.isBound()) {
// save old value
jsc.add("java.lang.Object old");
jsc.append(mname);
jsc.append(" = ");
//-- 'this.' ensures this refers to the class member not the parameter
jsc.append(xsType.createToJavaObjectCode("this." + fieldName));
jsc.append(";");
}
//-- set new value
jsc.add("this.");
jsc.append(fieldName);
jsc.append(" = ");
jsc.append(paramName);
jsc.append(";");
if (fieldInfo.getFieldInfoReference() != null) {
jsc.add("this.");
jsc.append(fieldInfo.getFieldInfoReference().getName());
jsc.append(" = ");
JType referencedJType = new XMLInfoNature(fieldInfo.getFieldInfoReference()).getSchemaType().getJType();
if (referencedJType.isPrimitive()) {
jsc.append(paramName);
} else if (jType.isPrimitive()) {
JPrimitiveType primitive = (JPrimitiveType) jType;
jsc.append("new ");
jsc.append(primitive.getWrapperName());
jsc.append("(");
jsc.append(paramName);
jsc.append(")");
} else {
jsc.append(paramName);
}
jsc.append(";");
}
//-- hasProperty
if (fieldInfo.requiresHasAndDeleteMethods()) {
jsc.add("this._has");
jsc.append(fieldName);
jsc.append(" = true;");
}
//-- bound properties
if (fieldInfo.isBound()) {
//notify listeners
jsc.add("notifyPropertyChangeListeners(\"");
if (fieldName.startsWith("_")) {
jsc.append(fieldName.substring(1));
} else {
jsc.append(fieldName);
}
jsc.append("\", old");
jsc.append(mname);
jsc.append(", ");
//-- 'this.' ensures this refers to the class member not the parameter
jsc.append(xsType.createToJavaObjectCode("this." + fieldName));
jsc.append(");");
}
} //-- createSetterMethod