use of com.sun.codemodel.JDocComment in project raml-module-builder by folio-org.
the class Raml2Java method onAddResourceMethod.
@Override
public void onAddResourceMethod(JMethod method, Action action, MimeType bodyMimeType, Collection<MimeType> uniqueResponseMimeTypes) {
super.onAddResourceMethod(method, action, bodyMimeType, uniqueResponseMimeTypes);
// jdoc the new parameters in all functions
JDocComment methodDoc = method.javadoc();
// document params
JCommentPart paramDoc = methodDoc.addParam("asyncResultHandler");
paramDoc.append("A <code>Handler<AsyncResult<Response>>></code> handler ");
paramDoc.append("{@link " + Handler.class.getName() + "}");
paramDoc.append(" which must be called as follows - Note the 'GetPatronsResponse' should be replaced with '[nameOfYourFunction]Response': (example only) <code>asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetPatronsResponse.withJsonOK( new ObjectMapper().readValue(reply.result().body().toString(), Patron.class))));</code> in the final callback (most internal callback) of the function.");
paramDoc = methodDoc.addParam("vertxContext");
paramDoc.append(" The Vertx Context Object <code>io.vertx.core.Context</code> ");
// add routingContext param if indicated in generate runner plugin in pom
// String endpoints2addRoutingContext = System.getProperty("generate_routing_context");
String endpoints2addRoutingContext = PomReader.INSTANCE.getProps().getProperty("generate_routing_context");
if (endpoints2addRoutingContext != null) {
String[] rcFuncs = endpoints2addRoutingContext.split(",");
for (int i = 0; i < rcFuncs.length; i++) {
try {
// ", current path = " + action.getResource().getUri());
if (rcFuncs[i].equalsIgnoreCase(action.getResource().getUri())) {
Class classRoutingContext = io.vertx.ext.web.RoutingContext.class;
method.param(classRoutingContext, "routingContext");
JCommentPart paramDoc1 = methodDoc.addParam("routingContext");
paramDoc1.append("RoutingContext of the request. Note that the RMB framework handles all routing." + "This should only be used if a third party add-on to vertx needs the RC as input ");
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
// add okapi headers to all interfaces generated from the raml
String genericOKapiMap = "java.util.Map<String, String>";
JClass genericOkapiMapRef = getCodeModel().ref(genericOKapiMap);
method.param(genericOkapiMapRef, "okapiHeaders");
// add parameter to all functions
String genericTypeName = "io.vertx.core.Handler<io.vertx.core.AsyncResult<Response>>";
JClass genericT = getCodeModel().ref(genericTypeName);
method.param(genericT, "asyncResultHandler");
Class classContext = io.vertx.core.Context.class;
method.param(classContext, "vertxContext");
// change return type to void for all functions
method.type(getCodeModel().VOID);
// annotate with validate class so that aspect picks it up for param
// validation
method.annotate(Validate.class);
}
use of com.sun.codemodel.JDocComment in project jsonschema2pojo by joelittlejohn.
the class CommentRuleTest method applyAddsCommentToJavadoc.
@Test
public void applyAddsCommentToJavadoc() throws JClassAlreadyExistsException {
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
ObjectMapper mapper = new ObjectMapper();
TextNode commentNode = mapper.createObjectNode().textNode("some comment");
JDocComment result = rule.apply("fooBar", commentNode, null, jclass, null);
assertThat(result, sameInstance(jclass.javadoc()));
assertThat(result.size(), is(1));
assertThat((String) result.get(0), is("some comment"));
}
use of com.sun.codemodel.JDocComment in project jsonschema2pojo by joelittlejohn.
the class TitleRule method apply.
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* When a title node is found and applied with this rule, the value of the
* title is added as a JavaDoc comment. This rule is typically applied to
* the generated field, generated getter and generated setter for the
* property.
* <p>
* Note that the title is always inserted at the top of the JavaDoc comment.
*
* @param nodeName
* the name of the property to which this title applies
* @param node
* the "title" schema node
* @param parent
* the parent node
* @param generatableType
* comment-able code generation construct, usually a field or
* method, which should have this title applied
* @return the JavaDoc comment created to contain the title
*/
@Override
public JDocComment apply(String nodeName, JsonNode node, JsonNode parent, JDocCommentable generatableType, Schema schema) {
JDocComment javadoc = generatableType.javadoc();
javadoc.add(0, node.asText() + "\n<p>\n");
return javadoc;
}
use of com.sun.codemodel.JDocComment in project jsonschema2pojo by joelittlejohn.
the class RequiredArrayRuleTest method shouldUpdateJavaDoc.
@Test
public void shouldUpdateJavaDoc() throws JClassAlreadyExistsException {
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "fooBar");
jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "foo");
ObjectMapper mapper = new ObjectMapper();
ArrayNode requiredNode = mapper.createArrayNode().add("fooBar");
rule.apply("Class", requiredNode, null, jclass, new Schema(null, requiredNode, null));
JDocComment fooBarJavaDoc = jclass.fields().get("fooBar").javadoc();
JDocComment fooJavaDoc = jclass.fields().get("foo").javadoc();
assertThat(fooBarJavaDoc.size(), is(1));
assertThat((String) fooBarJavaDoc.get(0), is("\n(Required)"));
assertThat(fooJavaDoc.size(), is(0));
}
use of com.sun.codemodel.JDocComment in project raml-module-builder by folio-org.
the class ClientGenerator method generateClassMeta.
public void generateClassMeta(String className, Object globalPath) {
String mapType = System.getProperty("json.type");
if (mapType != null) {
if (mapType.equals("mongo")) {
mappingType = "mongo";
}
}
this.globalPath = "GLOBAL_PATH";
/* Adding packages here */
JPackage jp = jCodeModel._package(RTFConsts.CLIENT_GEN_PACKAGE);
try {
/* Giving Class Name to Generate */
this.className = className.substring(RTFConsts.INTERFACE_PACKAGE.length() + 1, className.indexOf("Resource"));
jc = jp._class(this.className + CLIENT_CLASS_SUFFIX);
JDocComment com = jc.javadoc();
com.add("Auto-generated code - based on class " + className);
/* class variable to root url path to this interface */
JFieldVar globalPathVar = jc.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, String.class, "GLOBAL_PATH");
globalPathVar.init(JExpr.lit("/" + (String) globalPath));
/* class variable tenant id */
tenantId = jc.field(JMod.PRIVATE, String.class, "tenantId");
token = jc.field(JMod.PRIVATE, String.class, "token");
/* class variable to http options */
JFieldVar options = jc.field(JMod.PRIVATE, HttpClientOptions.class, "options");
/* class variable to http client */
httpClient = jc.field(JMod.PRIVATE, HttpClient.class, "httpClient");
/* constructor, init the httpClient - allow to pass keep alive option */
JMethod consructor = constructor(JMod.PUBLIC);
JVar host = consructor.param(String.class, "host");
JVar port = consructor.param(int.class, "port");
JVar param = consructor.param(String.class, "tenantId");
JVar token = consructor.param(String.class, "token");
JVar keepAlive = consructor.param(boolean.class, "keepAlive");
JVar connTO = consructor.param(int.class, "connTO");
JVar idleTO = consructor.param(int.class, "idleTO");
/* populate constructor */
JBlock conBody = consructor.body();
conBody.assign(JExpr._this().ref(tenantId), param);
conBody.assign(JExpr._this().ref(token), token);
conBody.assign(options, JExpr._new(jCodeModel.ref(HttpClientOptions.class)));
conBody.invoke(options, "setLogActivity").arg(JExpr.TRUE);
conBody.invoke(options, "setKeepAlive").arg(keepAlive);
conBody.invoke(options, "setDefaultHost").arg(host);
conBody.invoke(options, "setDefaultPort").arg(port);
conBody.invoke(options, "setConnectTimeout").arg(connTO);
conBody.invoke(options, "setIdleTimeout").arg(idleTO);
JExpression vertx = jCodeModel.ref("org.folio.rest.tools.utils.VertxUtils").staticInvoke("getVertxFromContextOrNew");
conBody.assign(httpClient, vertx.invoke("createHttpClient").arg(options));
/* constructor, init the httpClient */
JMethod consructor2 = constructor(JMod.PUBLIC);
JVar hostVar = consructor2.param(String.class, "host");
JVar portVar = consructor2.param(int.class, "port");
JVar tenantIdVar = consructor2.param(String.class, "tenantId");
JVar tokenVar = consructor2.param(String.class, "token");
JBlock conBody2 = consructor2.body();
conBody2.invoke("this").arg(hostVar).arg(portVar).arg(tenantIdVar).arg(tokenVar).arg(JExpr.TRUE).arg(JExpr.lit(2000)).arg(JExpr.lit(5000));
JMethod consructor1 = constructor(JMod.PUBLIC);
JVar hostVar1 = consructor1.param(String.class, "host");
JVar portVar1 = consructor1.param(int.class, "port");
JVar tenantIdVar1 = consructor1.param(String.class, "tenantId");
JVar tokenVar1 = consructor1.param(String.class, "token");
JVar keepAlive1 = consructor1.param(boolean.class, "keepAlive");
JBlock conBody1 = consructor1.body();
conBody1.invoke("this").arg(hostVar1).arg(portVar1).arg(tenantIdVar1).arg(tokenVar1).arg(keepAlive1).arg(JExpr.lit(2000)).arg(JExpr.lit(5000));
/* constructor, init the httpClient */
JMethod consructor3 = constructor(JMod.PUBLIC);
JBlock conBody3 = consructor3.body();
conBody3.invoke("this").arg("localhost").arg(JExpr.lit(8081)).arg("folio_demo").arg("folio_demo").arg(JExpr.FALSE).arg(JExpr.lit(2000)).arg(JExpr.lit(5000));
consructor3.javadoc().add("Convenience constructor for tests ONLY!<br>Connect to localhost on 8081 as folio_demo tenant.");
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
Aggregations