@Override
void writeMethodImplementationJava(ClassScope classScope, Method m) {
writeMethodDeclarationJava(classScope, m, Collections.<String>emptyList());
classScope.startLine("{\n");
MethodScope scope = classScope.newMethodScope();
scope.indentRight();
scope.startLine("");
quickParser.appendFinishedValueTypeNameJava(scope);
scope.append(" result;\n");
boolean wrap = quickParser.javaCodeThrowsException() || !isOptional;
if (wrap) {
scope.startLine("try {\n");
scope.indentRight();
}
String valueRef = scope.newMethodScopedName("value");
String hasValueRef = scope.newMethodScopedName("hasValue");
Util.writeReadValueAndHasValue(scope, fieldName, "underlying", valueRef, hasValueRef);
scope.startLine("if (" + hasValueRef + ") {\n");
scope.indentRight();
if (quickParser.javaCodeThrowsException()) {
scope.startLine("try {\n");
scope.indentRight();
quickParser.writeParseQuickCode(scope, valueRef, "r1");
scope.startLine("result = r1;\n");
scope.indentLeft();
scope.startLine("} catch (" + Util.BASE_PACKAGE + ".JsonProtocolParseException e) {\n");
scope.startLine(" throw new " + Util.BASE_PACKAGE + ".JsonProtocolParseException(" +
"\"Failed to parse field " + fieldName + " in type ");
scope.append(typeClass.getName() + "\", e);\n");
scope.startLine("}\n");
} else {
quickParser.writeParseQuickCode(scope, valueRef, "r1");
scope.startLine("result = r1;\n");
}
scope.indentLeft();
scope.startLine("} else {\n");
scope.indentRight();
if (isOptional) {
scope.startLine("result = null;\n");
} else {
scope.startLine("throw new " + Util.BASE_PACKAGE + ".JsonProtocolParseException(" +
"\"Field is not optional: " + fieldName + "\");\n");
}
scope.indentLeft();
scope.startLine("}\n");
if (wrap) {
scope.indentLeft();
scope.startLine("} catch (" + Util.BASE_PACKAGE + ".JsonProtocolParseException e) {\n");
scope.startLine(" throw new " + Util.BASE_PACKAGE +
".implutil.CommonImpl.ParseRuntimeException(" +
"\"On demand parsing failed for \" + underlying, e);\n");
scope.startLine("}\n");
}
scope.startLine("return result;\n");
scope.indentLeft();
scope.startLine("}\n");
}