*/
private void createGetAsArrayMethod(final CollectionInfo fieldInfo,
final JClass jClass, final boolean useJava50,
AnnotationBuilder[] annotationBuilders) {
JType baseType = fieldInfo.getContentType().getJType();
JType arrayType = new JArrayType(baseType, useJava50);
JMethod method = new JMethod(fieldInfo.getReadMethodName(), arrayType,
"this collection as an Array");
JSourceCode sourceCode = method.getSourceCode();
// create Javadoc
JDocComment comment = method.getJDocComment();
comment.appendComment("Returns the contents of the collection in an Array. ");
if (!(baseType.isPrimitive())) {
// For non-primitive types, we use the API method made for this purpose
comment.appendComment("<p>");
comment.appendComment("Note: Just in case the collection contents are changing in ");
comment.appendComment("another thread, we pass a 0-length Array of the correct type ");
comment.appendComment("into the API call. This way we <i>know</i> that the Array ");
comment.appendComment("returned is of exactly the correct length.");
String baseTypeName = baseType.toString();
if (baseType.isArray()) {
sourceCode.add(arrayType.toString() + " array = new ");
sourceCode.append(baseTypeName.substring(0, baseTypeName.length() - 2) + "[0][];");
} else {
sourceCode.add(arrayType.toString() + " array = new ");
sourceCode.append(baseTypeName + "[0];");
}
sourceCode.add("return (" + arrayType.toString() + ") ");
sourceCode.append("this." + fieldInfo.getName() + ".toArray(array);");
} else {
// For primitive types, we have to do this the hard way
sourceCode.add("int size = this.");
sourceCode.append(fieldInfo.getName());
sourceCode.append(".size();");
sourceCode.add(arrayType.toString());
sourceCode.append(" array = new ");
// the first brackets must contain the size...
int brackets = arrayType.toString().indexOf("[]");
sourceCode.append(arrayType.toString().substring(0, brackets));
sourceCode.append("[size]");
sourceCode.append(";");
sourceCode.add("java.util.Iterator iter = " + fieldInfo.getName() + ".iterator();");
String value = "iter.next()";
sourceCode.add("for (int index = 0; index < size; index++) {");
sourceCode.indent();
sourceCode.add("array[index] = ");
if (fieldInfo.getContentType().getType() == XSType.CLASS) {
sourceCode.append("(");
sourceCode.append(arrayType.getName());
sourceCode.append(") ");
sourceCode.append(value);
} else {
sourceCode.append(fieldInfo.getContentType().createFromJavaObjectCode(value));
}