}
annotate(facadeClass, sourceFile);
final JFieldVar baseUriField;
final JFieldVar requestOptionsField;
final JExpression baseUriGetter = JExpr.invoke("getBaseUriTemplate");
final JExpression requestOptionsGetter = JExpr.invoke("getRequestOptions");
if (_config.isRestli2Format())
{
baseUriField = null;
requestOptionsField = null;
facadeClass._extends(BuilderBase.class);
}
else
{
// for old builder, instead of extending from RequestBuilderBase, add fields and getters in the class
baseUriField = facadeClass.field(JMod.PRIVATE | JMod.FINAL, String.class, "_baseUriTemplate");
requestOptionsField = facadeClass.field(JMod.PRIVATE, RestliRequestOptions.class, "_requestOptions");
facadeClass.method(JMod.PRIVATE, String.class, "getBaseUriTemplate").body()._return(baseUriField);
facadeClass.method(JMod.PUBLIC, RestliRequestOptions.class, "getRequestOptions").body()._return(requestOptionsField);
}
// make the original resource path available via a private final static variable.
JFieldVar originalResourceField = facadeClass.field(JMod.PRIVATE | JMod.STATIC |JMod.FINAL, String.class,
"ORIGINAL_RESOURCE_PATH");
String resourcePath = getResourcePath(resource.getPath());
originalResourceField.init(JExpr.lit(resourcePath));
// create reference to RestliRequestOptions.DEFAULT_OPTIONS
final JClass restliRequestOptionsClass = getCodeModel().ref(RestliRequestOptions.class);
JFieldRef defaultOptionsField = restliRequestOptionsClass.staticRef("DEFAULT_OPTIONS");
if (!_config.isRestli2Format())
{
// same getPathComponents() logic as in RequestBuilderBase
JMethod pathComponentsGetter = facadeClass.method(JMod.PUBLIC, String[].class, "getPathComponents");
pathComponentsGetter.body()._return(getCodeModel().ref(URIParamUtils.class).staticInvoke("extractPathComponentsFromUriTemplate").arg(baseUriField));
// method that expresses the following logic
// (requestOptions == null) ? return RestliRequestOptions.DEFAULT_OPTIONS : requestOptions;
JMethod requestOptionsAssigner = facadeClass.method(JMod.PRIVATE | JMod.STATIC, RestliRequestOptions.class, "assignRequestOptions");
JVar requestOptionsAssignerParam = requestOptionsAssigner.param(RestliRequestOptions.class, "requestOptions");
JConditional requestNullCheck = requestOptionsAssigner.body()._if(requestOptionsAssignerParam.eq(JExpr._null()));
requestNullCheck._then().block()._return(defaultOptionsField);
requestNullCheck._else().block()._return(requestOptionsAssignerParam);
}
/*
There will be 4 constructors:
()
(RestliRequestOptions)
(String)
(String, RestliRequestOptions)
*/
JMethod noArgConstructor = facadeClass.constructor(JMod.PUBLIC);
JMethod requestOptionsOverrideConstructor = facadeClass.constructor(JMod.PUBLIC);
JMethod resourceNameOverrideConstructor = facadeClass.constructor(JMod.PUBLIC);
JMethod mainConstructor = facadeClass.constructor(JMod.PUBLIC);
// no-argument constructor, delegates to the request options override constructor
noArgConstructor.body().invoke(THIS).arg(defaultOptionsField);
// request options override constructor
JVar requestOptionsOverrideOptionsParam = requestOptionsOverrideConstructor.param(RestliRequestOptions.class, "requestOptions");
if (_config.isRestli2Format())
{
requestOptionsOverrideConstructor.body().invoke(SUPER).arg(originalResourceField).arg(requestOptionsOverrideOptionsParam);
}
else
{
requestOptionsOverrideConstructor.body().assign(baseUriField, originalResourceField);
final JInvocation requestOptionsOverrideAssignRequestOptions = new JBlock().invoke("assignRequestOptions").arg(requestOptionsOverrideOptionsParam);
requestOptionsOverrideConstructor.body().assign(requestOptionsField, requestOptionsOverrideAssignRequestOptions);
}
// primary resource name override constructor, delegates to the main constructor
JVar resourceNameOverrideResourceNameParam = resourceNameOverrideConstructor.param(_stringClass, "primaryResourceName");
resourceNameOverrideConstructor.body().invoke(THIS).arg(resourceNameOverrideResourceNameParam).arg(defaultOptionsField);
// main constructor
JVar mainConsResourceNameParam = mainConstructor.param(_stringClass, "primaryResourceName");
JVar mainConsOptionsParam = mainConstructor.param(RestliRequestOptions.class, "requestOptions");
JExpression baseUriExpr;
if (resourcePath.contains("/"))
{
baseUriExpr = originalResourceField.invoke("replaceFirst").arg(JExpr.lit("[^/]*/")).arg(mainConsResourceNameParam.plus(JExpr.lit("/")));
}
else