Search in sources :

Example 1 with JDocComment

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);
}
Also used : JClass(com.sun.codemodel.JClass) Handler(io.vertx.core.Handler) JCommentPart(com.sun.codemodel.JCommentPart) JDocComment(com.sun.codemodel.JDocComment) JClass(com.sun.codemodel.JClass)

Example 2 with JDocComment

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"));
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) TextNode(com.fasterxml.jackson.databind.node.TextNode) JCodeModel(com.sun.codemodel.JCodeModel) JDocComment(com.sun.codemodel.JDocComment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with JDocComment

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;
}
Also used : JDocComment(com.sun.codemodel.JDocComment)

Example 4 with JDocComment

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));
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) Schema(org.jsonschema2pojo.Schema) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JCodeModel(com.sun.codemodel.JCodeModel) JDocComment(com.sun.codemodel.JDocComment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with JDocComment

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);
    }
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) HttpClient(io.vertx.core.http.HttpClient) JPackage(com.sun.codemodel.JPackage) JBlock(com.sun.codemodel.JBlock) JExpression(com.sun.codemodel.JExpression) JMethod(com.sun.codemodel.JMethod) JDocComment(com.sun.codemodel.JDocComment) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JVar(com.sun.codemodel.JVar)

Aggregations

JDocComment (com.sun.codemodel.JDocComment)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 JCodeModel (com.sun.codemodel.JCodeModel)4 JDefinedClass (com.sun.codemodel.JDefinedClass)4 Test (org.junit.Test)4 TextNode (com.fasterxml.jackson.databind.node.TextNode)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JBlock (com.sun.codemodel.JBlock)1 JClass (com.sun.codemodel.JClass)1 JCommentPart (com.sun.codemodel.JCommentPart)1 JExpression (com.sun.codemodel.JExpression)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JMethod (com.sun.codemodel.JMethod)1 JPackage (com.sun.codemodel.JPackage)1 JVar (com.sun.codemodel.JVar)1 Handler (io.vertx.core.Handler)1 HttpClient (io.vertx.core.http.HttpClient)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MessagingException (javax.mail.MessagingException)1