Search in sources :

Example 26 with JExpression

use of com.sun.codemodel.JExpression 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)

Example 27 with JExpression

use of com.sun.codemodel.JExpression in project jsonschema2pojo by joelittlejohn.

the class DynamicPropertiesRule method addInternalGetMethodJava6.

private JMethod addInternalGetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
    JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
    JBlock body = method.body();
    JConditional propertyConditional = null;
    if (propertiesNode != null) {
        for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext(); ) {
            Map.Entry<String, JsonNode> property = properties.next();
            String propertyName = property.getKey();
            JsonNode node = property.getValue();
            String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
            JType propertyType = jclass.fields().get(fieldName).type();
            JExpression condition = lit(propertyName).invoke("equals").arg(nameParam);
            if (propertyConditional == null) {
                propertyConditional = body._if(condition);
            } else {
                propertyConditional = propertyConditional._elseif(condition);
            }
            JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {});
            propertyConditional._then()._return(invoke(propertyGetter));
        }
    }
    JClass extendsType = jclass._extends();
    JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else();
    if (extendsType != null && extendsType instanceof JDefinedClass) {
        JDefinedClass parentClass = (JDefinedClass) extendsType;
        JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
        lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
    } else {
        lastBlock._return(notFoundParam);
    }
    return method;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JsonNode(com.fasterxml.jackson.databind.JsonNode) JExpression(com.sun.codemodel.JExpression) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JMethod(com.sun.codemodel.JMethod) Map(java.util.Map) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 28 with JExpression

use of com.sun.codemodel.JExpression in project jsonschema2pojo by joelittlejohn.

the class EnumRule method addFactoryMethod.

protected void addFactoryMethod(EnumDefinition enumDefinition, JDefinedClass _enum) {
    JType backingType = enumDefinition.getBackingType();
    JFieldVar quickLookupMap = addQuickLookupMap(enumDefinition, _enum);
    JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
    JVar valueParam = fromValue.param(backingType, "value");
    JBlock body = fromValue.body();
    JVar constant = body.decl(_enum, "constant");
    constant.init(quickLookupMap.invoke("get").arg(valueParam));
    JConditional _if = body._if(constant.eq(JExpr._null()));
    JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class));
    JExpression expr = valueParam;
    // if string no need to add ""
    if (!isString(backingType)) {
        expr = expr.plus(JExpr.lit(""));
    }
    illegalArgumentException.arg(expr);
    _if._then()._throw(illegalArgumentException);
    _if._else()._return(constant);
    ruleFactory.getAnnotator().enumCreatorMethod(_enum, fromValue);
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JInvocation(com.sun.codemodel.JInvocation) JExpression(com.sun.codemodel.JExpression) JMethod(com.sun.codemodel.JMethod) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 29 with JExpression

use of com.sun.codemodel.JExpression in project jsonschema2pojo by joelittlejohn.

the class ObjectRule method addEquals.

private void addEquals(JDefinedClass jclass, JsonNode node) {
    Map<String, JFieldVar> fields = removeFieldsExcludedFromEqualsAndHashCode(jclass.fields(), node);
    JMethod equals = jclass.method(JMod.PUBLIC, boolean.class, "equals");
    JVar otherObject = equals.param(Object.class, "other");
    JBlock body = equals.body();
    body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE);
    body._if(otherObject._instanceof(jclass).eq(JExpr.FALSE))._then()._return(JExpr.FALSE);
    JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject));
    JExpression result = JExpr.lit(true);
    // First, check super.equals(other)
    if (!jclass._extends().fullName().equals(Object.class.getName())) {
        result = result.cand(JExpr._super().invoke("equals").arg(rhsVar));
    }
    // Chain the results of checking all other fields
    for (JFieldVar fieldVar : fields.values()) {
        if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
            continue;
        }
        JFieldRef thisFieldRef = JExpr.refthis(fieldVar.name());
        JFieldRef otherFieldRef = JExpr.ref(rhsVar, fieldVar.name());
        JExpression fieldEquals;
        if (fieldVar.type().isPrimitive()) {
            if ("double".equals(fieldVar.type().name())) {
                JClass doubleClass = jclass.owner().ref(Double.class);
                fieldEquals = doubleClass.staticInvoke("doubleToLongBits").arg(thisFieldRef).eq(doubleClass.staticInvoke("doubleToLongBits").arg(otherFieldRef));
            } else if ("float".equals(fieldVar.type().name())) {
                JClass floatClass = jclass.owner().ref(Float.class);
                fieldEquals = floatClass.staticInvoke("floatToIntBits").arg(thisFieldRef).eq(floatClass.staticInvoke("floatToIntBits").arg(otherFieldRef));
            } else {
                fieldEquals = thisFieldRef.eq(otherFieldRef);
            }
        } else if (fieldVar.type().isArray()) {
            if (!fieldVar.type().elementType().isPrimitive()) {
                throw new UnsupportedOperationException("Only primitive arrays are supported");
            }
            fieldEquals = jclass.owner().ref(Arrays.class).staticInvoke("equals").arg(thisFieldRef).arg(otherFieldRef);
        } else {
            fieldEquals = thisFieldRef.eq(otherFieldRef).cor(thisFieldRef.ne(JExpr._null()).cand(thisFieldRef.invoke("equals").arg(otherFieldRef)));
        }
        // Chain the equality of this field with the previous comparisons
        result = result.cand(fieldEquals);
    }
    body._return(result);
    equals.annotate(Override.class);
}
Also used : JFieldRef(com.sun.codemodel.JFieldRef) JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JExpression(com.sun.codemodel.JExpression) JMethod(com.sun.codemodel.JMethod) Arrays(java.util.Arrays) JVar(com.sun.codemodel.JVar)

Example 30 with JExpression

use of com.sun.codemodel.JExpression in project scout.rt by eclipse.

the class JaxWsAnnotationProcessor method createRunContextInvocation.

/**
 * Creates code to invoke the port type on behalf of the RunContext.
 */
protected JInvocation createRunContextInvocation(final JCodeModel model, final JExpression runContext, final boolean voidMethod, final JMethod portTypeMethod, final String endpointInterfaceName) {
    final JType returnType;
    final JDefinedClass runContextCallable;
    final String runMethodName;
    if (voidMethod) {
        returnType = model.ref(Void.class).unboxify();
        runContextCallable = model.anonymousClass(IRunnable.class);
        runMethodName = "run";
    } else {
        returnType = portTypeMethod.type().boxify();
        runContextCallable = model.anonymousClass(model.ref(Callable.class).narrow(returnType));
        runMethodName = "call";
    }
    // Implement RunContext callable.
    final JMethod runContextRunMethod = runContextCallable.method(JMod.PUBLIC | JMod.FINAL, returnType, runMethodName)._throws(Exception.class);
    runContextRunMethod.annotate(Override.class);
    // Invoke the bean method.
    final JInvocation beanInvocation = model.ref(BEANS.class).staticInvoke("get").arg(model.ref(endpointInterfaceName).dotclass()).invoke(portTypeMethod.name());
    for (final JVar parameter : portTypeMethod.listParams()) {
        beanInvocation.arg(parameter);
    }
    if (voidMethod) {
        runContextRunMethod.body().add(beanInvocation);
    } else {
        runContextRunMethod.body()._return(beanInvocation);
    }
    // Create RunContext invocations.
    final JExpression exceptionTranslator = model.ref(DefaultExceptionTranslator.class).dotclass();
    return runContext.invoke(runMethodName).arg(JExpr._new(runContextCallable)).arg(exceptionTranslator);
}
Also used : BEANS(org.eclipse.scout.rt.platform.BEANS) JDefinedClass(com.sun.codemodel.JDefinedClass) DefaultExceptionTranslator(org.eclipse.scout.rt.platform.exception.DefaultExceptionTranslator) JInvocation(com.sun.codemodel.JInvocation) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) JExpression(com.sun.codemodel.JExpression) JMethod(com.sun.codemodel.JMethod) JType(com.sun.codemodel.JType) Callable(java.util.concurrent.Callable) JVar(com.sun.codemodel.JVar)

Aggregations

JExpression (com.sun.codemodel.JExpression)39 JVar (com.sun.codemodel.JVar)28 JBlock (com.sun.codemodel.JBlock)22 JMethod (com.sun.codemodel.JMethod)16 JClass (com.sun.codemodel.JClass)13 JConditional (com.sun.codemodel.JConditional)13 JInvocation (com.sun.codemodel.JInvocation)10 JFieldVar (com.sun.codemodel.JFieldVar)9 JDefinedClass (com.sun.codemodel.JDefinedClass)8 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)6 JType (com.sun.codemodel.JType)5 Map (java.util.Map)5 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)5 JFieldRef (com.sun.codemodel.JFieldRef)4 IOException (java.io.IOException)4 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)4 JPackage (com.sun.codemodel.JPackage)3 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)3 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)3 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)3