Search in sources :

Example 76 with JCodeModel

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

the class HandlerArtifactProcessor method createAndPersistHandlerDelegate.

/**
 * Generates the entry point for a handler.
 */
public String createAndPersistHandlerDelegate(final JClass portTypeEntryPoint, final EntryPointDefinition entryPointDefinition, final HandlerDefinition handler, final int idx, final ProcessingEnvironment env) throws IOException, JClassAlreadyExistsException {
    final JCodeModel model = new JCodeModel();
    final String fullName = entryPointDefinition.getEntryPointQualifiedName() + "_" + handler.getHandlerSimpleName() + HANDLER_SUFFIX;
    final JDefinedClass handlerDelegate = model._class(fullName);
    // Add 'Generated' annotation
    final JAnnotationUse generatedAnnotation = handlerDelegate.annotate(Generated.class);
    generatedAnnotation.param("value", JaxWsAnnotationProcessor.class.getName());
    generatedAnnotation.param("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSSZ").format(new Date()));
    generatedAnnotation.param("comments", "Handler delegate for " + handler.getHandlerQualifiedName());
    AptUtil.addJavaDoc(handlerDelegate, format("This class is auto-generated by APT triggered by Maven build and is based on the handler configuration declared in {@link %s}.", entryPointDefinition.getSimpleName()));
    switch(handler.getHandlerType()) {
        case SOAP:
            handlerDelegate._extends(model.ref(SOAPHandlerDelegate.class));
            break;
        case LOGICAL:
            handlerDelegate._extends(model.ref(HandlerDelegate.class).narrow(LogicalMessageContext.class));
            handlerDelegate._implements(model.ref(LogicalHandler.class).narrow(LogicalMessageContext.class));
            break;
        default:
            handlerDelegate._extends(model.ref(HandlerDelegate.class).narrow(MessageContext.class));
            break;
    }
    // Add default constructor with super call to provide handler annotation.
    final JClass entryPointDefinitionClass = model.ref(entryPointDefinition.getQualifiedName());
    final JMethod defaultConstructor = handlerDelegate.constructor(JMod.PUBLIC);
    defaultConstructor.body().invoke("super").arg(JExpr.dotclass(entryPointDefinitionClass).invoke("getAnnotation").arg(JExpr.dotclass(model.ref(WebServiceEntryPoint.class))).invoke("handlerChain").component(JExpr.lit(idx)));
    AptUtil.buildAndPersist(model, env.getFiler());
    return handlerDelegate.fullName();
}
Also used : JaxWsAnnotationProcessor(org.eclipse.scout.jaxws.apt.JaxWsAnnotationProcessor) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) SOAPHandlerDelegate(org.eclipse.scout.rt.server.jaxws.provider.handler.SOAPHandlerDelegate) JCodeModel(com.sun.codemodel.JCodeModel) Date(java.util.Date) JAnnotationUse(com.sun.codemodel.JAnnotationUse) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) JMethod(com.sun.codemodel.JMethod) WebServiceEntryPoint(org.eclipse.scout.rt.server.jaxws.provider.annotation.WebServiceEntryPoint) SimpleDateFormat(java.text.SimpleDateFormat)

Example 77 with JCodeModel

use of com.sun.codemodel.JCodeModel in project drill by apache.

the class HiveFuncHolder method generateEval.

private HoldingContainer generateEval(ClassGenerator<?> g, HoldingContainer[] inputVariables, JVar[] workspaceJVars) {
    HoldingContainer out = g.declare(returnType);
    JCodeModel m = g.getModel();
    JBlock sub = new JBlock(true, true);
    // initialize DeferredObject's. For an optional type, assign the value holder only if it is not null
    for (int i = 0; i < argTypes.length; i++) {
        if (inputVariables[i].isOptional()) {
            sub.assign(workspaceJVars[3].component(JExpr.lit(i)), workspaceJVars[2].component(JExpr.lit(i)));
            JBlock conditionalBlock = new JBlock(false, false);
            JConditional jc = conditionalBlock._if(inputVariables[i].getIsSet().ne(JExpr.lit(0)));
            jc._then().assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), inputVariables[i].getHolder());
            jc._else().assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), JExpr._null());
            sub.add(conditionalBlock);
        } else {
            sub.assign(workspaceJVars[3].component(JExpr.lit(i)), workspaceJVars[2].component(JExpr.lit(i)));
            sub.assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), inputVariables[i].getHolder());
        }
    }
    // declare generic object for storing return value from GenericUDF.evaluate
    JVar retVal = sub.decl(m._ref(Object.class), "ret");
    // create try..catch block to call the GenericUDF instance with given input
    JTryBlock udfEvalTry = sub._try();
    udfEvalTry.body().assign(retVal, workspaceJVars[1].invoke("evaluate").arg(workspaceJVars[3]));
    JCatchBlock udfEvalCatch = udfEvalTry._catch(m.directClass(Exception.class.getCanonicalName()));
    JVar exVar = udfEvalCatch.param("ex");
    udfEvalCatch.body()._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName())).arg(JExpr.lit(String.format("GenericUDF.evaluate method failed"))).arg(exVar));
    // get the ValueHolder from retVal and return ObjectInspector
    sub.add(ObjectInspectorHelper.getDrillObject(m, returnOI, workspaceJVars[0], workspaceJVars[4], retVal));
    sub.assign(out.getHolder(), workspaceJVars[4]);
    // now add it to the doEval block in Generated class
    JBlock setup = g.getBlock(ClassGenerator.BlockType.EVAL);
    setup.directStatement(String.format("/** start %s for function %s **/ ", ClassGenerator.BlockType.EVAL.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
    setup.add(sub);
    setup.directStatement(String.format("/** end %s for function %s **/ ", ClassGenerator.BlockType.EVAL.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
    return out;
}
Also used : HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) DrillDeferredObject(org.apache.drill.exec.expr.fn.impl.hive.DrillDeferredObject) JTryBlock(com.sun.codemodel.JTryBlock) JCatchBlock(com.sun.codemodel.JCatchBlock) JCodeModel(com.sun.codemodel.JCodeModel) JVar(com.sun.codemodel.JVar)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)77 Test (org.junit.Test)59 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)52 JPackage (com.sun.codemodel.JPackage)47 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)45 JType (com.sun.codemodel.JType)41 JDefinedClass (com.sun.codemodel.JDefinedClass)15 JClass (com.sun.codemodel.JClass)12 Schema (org.jsonschema2pojo.Schema)11 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 TextNode (com.fasterxml.jackson.databind.node.TextNode)6 File (java.io.File)6 IOException (java.io.IOException)5 JBlock (com.sun.codemodel.JBlock)4 JCatchBlock (com.sun.codemodel.JCatchBlock)4 JMethod (com.sun.codemodel.JMethod)4 JTryBlock (com.sun.codemodel.JTryBlock)4 JVar (com.sun.codemodel.JVar)4 URL (java.net.URL)4