Search in sources :

Example 1 with JCatchBlock

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

the class JaxWsAnnotationProcessor method addEntryPointMethodImplementation.

/**
 * Creates the implementation of a entry point method.
 */
protected void addEntryPointMethodImplementation(final JCodeModel model, final JFieldVar webServiceContext, final JMethod method, final List<JClass> throwTypes, final boolean voidMethod, final String endpointInterfaceName) {
    final JBlock methodBody = method.body();
    final JInvocation runContext = JExpr.invoke(LOOKUP_RUN_CONTEXT_METHOD_NAME);
    final JTryBlock tryBlock = methodBody._try();
    // Invoke port type on behalf of RunContext.
    final JInvocation runContextInvocation = createRunContextInvocation(model, runContext, voidMethod, method, endpointInterfaceName);
    if (voidMethod) {
        tryBlock.body().add(runContextInvocation);
    } else {
        tryBlock.body()._return(runContextInvocation);
    }
    // Create exception handling.
    final JCatchBlock catchBlock = tryBlock._catch(model.ref(Exception.class));
    final JVar caughtException = catchBlock.param("e");
    final JBlock catchBody = catchBlock.body();
    if (throwTypes.isEmpty()) {
        // webservice method has no faults declared.
        catchBody._throw(JExpr.invoke(HANDLE_UNDECLARED_FAULT_METHOD_NAME).arg(caughtException));
    } else {
        // handle declared webservice faults.
        final JConditionalEx condition = new JConditionalEx(catchBody);
        for (final JClass throwType : throwTypes) {
            condition._elseif(caughtException._instanceof(throwType))._throw(JExprEx.cast(throwType, caughtException));
        }
        condition._else()._throw(JExpr.invoke(HANDLE_UNDECLARED_FAULT_METHOD_NAME).arg(caughtException));
    }
}
Also used : JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JTryBlock(com.sun.codemodel.JTryBlock) JCatchBlock(com.sun.codemodel.JCatchBlock) JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) IOException(java.io.IOException) JVar(com.sun.codemodel.JVar) JConditionalEx(org.eclipse.scout.jaxws.apt.internal.codemodel.JConditionalEx)

Example 2 with JCatchBlock

use of com.sun.codemodel.JCatchBlock in project drill by axbaretto.

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)

Example 3 with JCatchBlock

use of com.sun.codemodel.JCatchBlock in project drill by axbaretto.

the class HiveFuncHolder method generateSetup.

private void generateSetup(ClassGenerator<?> g, JVar[] workspaceJVars) {
    JCodeModel m = g.getModel();
    JBlock sub = new JBlock(true, true);
    // declare and instantiate argument ObjectInspector's
    JVar oiArray = sub.decl(m._ref(ObjectInspector[].class), "argOIs", JExpr.newArray(m._ref(ObjectInspector.class), argTypes.length));
    JClass oih = m.directClass(ObjectInspectorHelper.class.getCanonicalName());
    JClass mt = m.directClass(TypeProtos.MinorType.class.getCanonicalName());
    JClass mode = m.directClass(DataMode.class.getCanonicalName());
    for (int i = 0; i < argTypes.length; i++) {
        sub.assign(oiArray.component(JExpr.lit(i)), oih.staticInvoke("getDrillObjectInspector").arg(mode.staticInvoke("valueOf").arg(JExpr.lit(argTypes[i].getMode().getNumber()))).arg(mt.staticInvoke("valueOf").arg(JExpr.lit(argTypes[i].getMinorType().getNumber()))).arg((((PrimitiveObjectInspector) returnOI).getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.STRING) ? JExpr.lit(true) : JExpr.lit(false)));
    }
    // declare and instantiate DeferredObject array
    sub.assign(workspaceJVars[2], JExpr.newArray(m._ref(DrillDeferredObject.class), argTypes.length));
    for (int i = 0; i < argTypes.length; i++) {
        sub.assign(workspaceJVars[2].component(JExpr.lit(i)), JExpr._new(m.directClass(DrillDeferredObject.class.getCanonicalName())));
    }
    // declare empty array for argument deferred objects
    sub.assign(workspaceJVars[3], JExpr.newArray(m._ref(DrillDeferredObject.class), argTypes.length));
    // create new instance of the UDF class
    sub.assign(workspaceJVars[1], getUDFInstance(m));
    // create try..catch block to initialize the UDF instance with argument OIs
    JTryBlock udfInitTry = sub._try();
    udfInitTry.body().assign(workspaceJVars[0], workspaceJVars[1].invoke("initialize").arg(oiArray));
    JCatchBlock udfInitCatch = udfInitTry._catch(m.directClass(Exception.class.getCanonicalName()));
    JVar exVar = udfInitCatch.param("ex");
    udfInitCatch.body()._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName())).arg(JExpr.lit(String.format("Failed to initialize GenericUDF"))).arg(exVar));
    sub.add(ObjectInspectorHelper.initReturnValueHolder(g, m, workspaceJVars[4], returnOI, returnType.getMinorType()));
    // now add it to the doSetup block in Generated class
    JBlock setup = g.getBlock(ClassGenerator.BlockType.SETUP);
    setup.directStatement(String.format("/** start %s for function %s **/ ", ClassGenerator.BlockType.SETUP.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
    setup.add(sub);
    setup.directStatement(String.format("/** end %s for function %s **/ ", ClassGenerator.BlockType.SETUP.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
}
Also used : JClass(com.sun.codemodel.JClass) DataMode(org.apache.drill.common.types.TypeProtos.DataMode) JBlock(com.sun.codemodel.JBlock) 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) ObjectInspectorHelper(org.apache.drill.exec.expr.fn.impl.hive.ObjectInspectorHelper)

Example 4 with JCatchBlock

use of com.sun.codemodel.JCatchBlock in project drill by axbaretto.

the class ClassGenerator method addCtor.

/**
 * The code generator creates a method called __DRILL_INIT__ which takes the
 * place of the constructor when the code goes though the byte code merge.
 * For Plain-old Java, we call the method from a constructor created for
 * that purpose. (Generated code, fortunately, never includes a constructor,
 * so we can create one.) Since the init block throws an exception (which
 * should never occur), the generated constructor converts the checked
 * exception into an unchecked one so as to not require changes to the
 * various places that create instances of the generated classes.
 *
 * Example:<code><pre>
 * public StreamingAggregatorGen1() {
 *       try {
 *         __DRILL_INIT__();
 *     } catch (SchemaChangeException e) {
 *         throw new UnsupportedOperationException(e);
 *     }
 * }</pre></code>
 *
 * Note: in Java 8 we'd use the <tt>Parameter</tt> class defined in Java's
 * introspection package. But, Drill prefers Java 7 which only provides
 * parameter types.
 */
private void addCtor(Class<?>[] parameters) {
    JMethod ctor = clazz.constructor(JMod.PUBLIC);
    JBlock body = ctor.body();
    // If there are parameters, need to pass them to the super class.
    if (parameters.length > 0) {
        JInvocation superCall = JExpr.invoke("super");
        for (int i = 1; i < parameters.length; i++) {
            Class<?> p = parameters[i];
            superCall.arg(ctor.param(model._ref(p), "arg" + i));
        }
        body.add(superCall);
    }
    JTryBlock tryBlock = body._try();
    tryBlock.body().invoke(SignatureHolder.DRILL_INIT_METHOD);
    JCatchBlock catchBlock = tryBlock._catch(model.ref(SchemaChangeException.class));
    catchBlock.body()._throw(JExpr._new(model.ref(UnsupportedOperationException.class)).arg(catchBlock.param("e")));
}
Also used : SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod) JTryBlock(com.sun.codemodel.JTryBlock) JCatchBlock(com.sun.codemodel.JCatchBlock)

Example 5 with JCatchBlock

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

the class HiveFuncHolder method generateSetup.

private void generateSetup(ClassGenerator<?> g, JVar[] workspaceJVars) {
    JCodeModel m = g.getModel();
    JBlock sub = new JBlock(true, true);
    // declare and instantiate argument ObjectInspector's
    JVar oiArray = sub.decl(m._ref(ObjectInspector[].class), "argOIs", JExpr.newArray(m._ref(ObjectInspector.class), argTypes.length));
    JClass oih = m.directClass(ObjectInspectorHelper.class.getCanonicalName());
    JClass mt = m.directClass(TypeProtos.MinorType.class.getCanonicalName());
    JClass mode = m.directClass(DataMode.class.getCanonicalName());
    for (int i = 0; i < argTypes.length; i++) {
        sub.assign(oiArray.component(JExpr.lit(i)), oih.staticInvoke("getDrillObjectInspector").arg(mode.staticInvoke("valueOf").arg(JExpr.lit(argTypes[i].getMode().getNumber()))).arg(mt.staticInvoke("valueOf").arg(JExpr.lit(argTypes[i].getMinorType().getNumber()))).arg((((PrimitiveObjectInspector) returnOI).getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.STRING) ? JExpr.lit(true) : JExpr.lit(false)));
    }
    // declare and instantiate DeferredObject array
    sub.assign(workspaceJVars[2], JExpr.newArray(m._ref(DrillDeferredObject.class), argTypes.length));
    for (int i = 0; i < argTypes.length; i++) {
        sub.assign(workspaceJVars[2].component(JExpr.lit(i)), JExpr._new(m.directClass(DrillDeferredObject.class.getCanonicalName())));
    }
    // declare empty array for argument deferred objects
    sub.assign(workspaceJVars[3], JExpr.newArray(m._ref(DrillDeferredObject.class), argTypes.length));
    // create new instance of the UDF class
    sub.assign(workspaceJVars[1], getUDFInstance(m));
    // create try..catch block to initialize the UDF instance with argument OIs
    JTryBlock udfInitTry = sub._try();
    udfInitTry.body().assign(workspaceJVars[0], workspaceJVars[1].invoke("initialize").arg(oiArray));
    JCatchBlock udfInitCatch = udfInitTry._catch(m.directClass(Exception.class.getCanonicalName()));
    JVar exVar = udfInitCatch.param("ex");
    udfInitCatch.body()._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName())).arg(JExpr.lit(String.format("Failed to initialize GenericUDF"))).arg(exVar));
    sub.add(ObjectInspectorHelper.initReturnValueHolder(g, m, workspaceJVars[4], returnOI, returnType.getMinorType()));
    // now add it to the doSetup block in Generated class
    JBlock setup = g.getBlock(ClassGenerator.BlockType.SETUP);
    setup.directStatement(String.format("/** start %s for function %s **/ ", ClassGenerator.BlockType.SETUP.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
    setup.add(sub);
    setup.directStatement(String.format("/** end %s for function %s **/ ", ClassGenerator.BlockType.SETUP.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
}
Also used : JClass(com.sun.codemodel.JClass) DataMode(org.apache.drill.common.types.TypeProtos.DataMode) JBlock(com.sun.codemodel.JBlock) 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) ObjectInspectorHelper(org.apache.drill.exec.expr.fn.impl.hive.ObjectInspectorHelper)

Aggregations

JBlock (com.sun.codemodel.JBlock)7 JCatchBlock (com.sun.codemodel.JCatchBlock)7 JTryBlock (com.sun.codemodel.JTryBlock)7 JVar (com.sun.codemodel.JVar)5 JCodeModel (com.sun.codemodel.JCodeModel)4 DrillDeferredObject (org.apache.drill.exec.expr.fn.impl.hive.DrillDeferredObject)4 JClass (com.sun.codemodel.JClass)3 JInvocation (com.sun.codemodel.JInvocation)3 JConditional (com.sun.codemodel.JConditional)2 JMethod (com.sun.codemodel.JMethod)2 DataMode (org.apache.drill.common.types.TypeProtos.DataMode)2 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)2 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)2 ObjectInspectorHelper (org.apache.drill.exec.expr.fn.impl.hive.ObjectInspectorHelper)2 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)1 IOException (java.io.IOException)1 JConditionalEx (org.eclipse.scout.jaxws.apt.internal.codemodel.JConditionalEx)1