Search in sources :

Example 1 with JAssignmentTarget

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

the class DrillFuncHolder method convertReaderToHolder.

/**
 * Convert an input value holder (in the generated code) into a value
 * holder as declared on the input parameter.
 */
private void convertReaderToHolder(JCodeModel model, JBlock jBlock, HoldingContainer inputVariable, int currentIndex, ValueReference parameter) {
    JVar inputHolder = inputVariable.getHolder();
    MajorType inputSqlType = inputVariable.getMajorType();
    if (Types.isComplex(parameter.getType())) {
        // For complex data-types (repeated maps/lists/dicts) the input to the
        // aggregate will be a FieldReader. However, aggregate
        // functions like ANY_VALUE, will assume the input to be a
        // RepeatedMapHolder etc. Generate boilerplate code, to map
        // from FieldReader to respective Holder.
        // FooHolder param = new FooHolder();
        // param.reader = inputVar;
        JType holderClass = getParamClass(model, parameter, inputHolder.type());
        JAssignmentTarget holderVar = declare(jBlock, parameter, holderClass, JExpr._new(holderClass), currentIndex);
        jBlock.assign(holderVar.ref("reader"), inputHolder);
    } else if (Types.isUnion(inputSqlType)) {
        // Normally unions are generated as a UnionHolder. However, if a parameter
        // is a FieldReader, then we must generate the union as a UnionReader.
        // Then, if there is another function that use a holder, we can convert
        // from the UnionReader to a UnionHolder.
        // 
        // UnionHolder param = new UnionHolder();
        // inputVar.read(param);
        JType holderClass = model._ref(UnionHolder.class);
        JAssignmentTarget paramVar = jBlock.decl(holderClass, parameter.getName(), JExpr._new(holderClass));
        JInvocation readCall = inputHolder.invoke("read");
        readCall.arg(paramVar);
        jBlock.add(readCall);
    } else {
        throw new UnsupportedOperationException(String.format("Cannot convert values of type %s from a reader to a holder", inputSqlType.getMinorType().name()));
    }
}
Also used : UnionHolder(org.apache.drill.exec.expr.holders.UnionHolder) JAssignmentTarget(com.sun.codemodel.JAssignmentTarget) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) JInvocation(com.sun.codemodel.JInvocation) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 2 with JAssignmentTarget

use of com.sun.codemodel.JAssignmentTarget in project jaxb-ri by eclipse-ee4j.

the class ArrayField method generateAccessors.

@Override
public void generateAccessors() {
    MethodWriter writer = outline.createMethodWriter();
    Accessor acc = create(JExpr._this());
    JVar $idx, $value;
    JBlock body;
    // [RESULT] T[] getX() {
    // if( <var>==null )    return new T[0];
    // T[] retVal = new T[this._return.length] ;
    // System.arraycopy(this._return, 0, "retVal", 0, this._return.length);
    // return (retVal);
    // }
    $getAll = writer.declareMethod(exposedType.array(), "get" + prop.getName(true));
    writer.javadoc().append(prop.javadoc);
    body = $getAll.body();
    body._if(acc.ref(true).eq(JExpr._null()))._then()._return(JExpr.newArray(exposedType, 0));
    JVar var = body.decl(exposedType.array(), "retVal", JExpr.newArray(implType, acc.ref(true).ref("length")));
    body.add(codeModel.ref(System.class).staticInvoke("arraycopy").arg(acc.ref(true)).arg(JExpr.lit(0)).arg(var).arg(JExpr.lit(0)).arg(acc.ref(true).ref("length")));
    body._return(JExpr.direct("retVal"));
    List<Object> returnTypes = listPossibleTypes(prop);
    writer.javadoc().addReturn().append("array of\n").append(returnTypes);
    // [RESULT]
    // ET getX(int idx) {
    // if( <var>==null )    throw new IndexOutOfBoundsException();
    // return unbox(<var>.get(idx));
    // }
    JMethod $get = writer.declareMethod(exposedType, "get" + prop.getName(true));
    $idx = writer.addParameter(codeModel.INT, "idx");
    $get.body()._if(acc.ref(true).eq(JExpr._null()))._then()._throw(JExpr._new(codeModel.ref(IndexOutOfBoundsException.class)));
    writer.javadoc().append(prop.javadoc);
    $get.body()._return(acc.ref(true).component($idx));
    writer.javadoc().addReturn().append("one of\n").append(returnTypes);
    // [RESULT] int getXLength() {
    // if( <var>==null )    throw new IndexOutOfBoundsException();
    // return <ref>.length;
    // }
    JMethod $getLength = writer.declareMethod(codeModel.INT, "get" + prop.getName(true) + "Length");
    $getLength.body()._if(acc.ref(true).eq(JExpr._null()))._then()._return(JExpr.lit(0));
    $getLength.body()._return(acc.ref(true).ref("length"));
    // [RESULT] void setX(ET[] values) {
    // if (values == null) {
    // <ref> = null;
    // return;
    // }
    // int len = values.length;
    // for( int i=0; i<len; i++ )
    // <ref>[i] = values[i];
    // }
    $setAll = writer.declareMethod(codeModel.VOID, "set" + prop.getName(true));
    writer.javadoc().append(prop.javadoc);
    $value = writer.addParameter(exposedType.array(), "values");
    $setAll.body()._if($value.eq(JExpr._null()))._then().assign((JAssignmentTarget) acc.ref(true), JExpr._null())._return();
    JVar $len = $setAll.body().decl(codeModel.INT, "len", $value.ref("length"));
    $setAll.body().assign((JAssignmentTarget) acc.ref(true), castToImplTypeArray(JExpr.newArray(implType, $len)));
    JForLoop _for = $setAll.body()._for();
    JVar $i = _for.init(codeModel.INT, "i", JExpr.lit(0));
    _for.test(JOp.lt($i, $len));
    _for.update($i.incr());
    _for.body().assign(acc.ref(true).component($i), castToImplType(acc.box($value.component($i))));
    writer.javadoc().addParam($value).append("allowed objects are\n").append(returnTypes);
    // [RESULT] ET setX(int idx, ET value)
    // <ref>[idx] = value
    JMethod $set = writer.declareMethod(exposedType, "set" + prop.getName(true));
    $idx = writer.addParameter(codeModel.INT, "idx");
    $value = writer.addParameter(exposedType, "value");
    writer.javadoc().append(prop.javadoc);
    body = $set.body();
    body._return(JExpr.assign(acc.ref(true).component($idx), castToImplType(acc.box($value))));
    writer.javadoc().addParam($value).append("allowed object is\n").append(returnTypes);
}
Also used : JAssignmentTarget(com.sun.codemodel.JAssignmentTarget) MethodWriter(com.sun.tools.xjc.generator.bean.MethodWriter) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JForLoop(com.sun.codemodel.JForLoop) JVar(com.sun.codemodel.JVar)

Example 3 with JAssignmentTarget

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

the class DrillFuncHolder method declare.

/**
 * Declares specified {@code paramExpression} in specified {@code jBlock}
 * and assigns it to the array component if required and / or returns declared expression.
 *
 * @param jBlock          target block where declaration is added
 * @param parameter       function parameter which should be declared
 * @param paramClass      type of the declared variable
 * @param paramExpression expression to be declared
 * @param currentIndex    index of current parameter
 * @return declared expression
 */
protected JAssignmentTarget declare(JBlock jBlock, ValueReference parameter, JType paramClass, JExpression paramExpression, int currentIndex) {
    if (parameter.isVarArg()) {
        // For VarArg, an array has already been generated:
        // FieldReader[] in = new FieldReader[ 4 ] ;
        // Here we assign the input value to the array:
        // in[ 0 ] = new VarCharHolderReaderImpl(constant1);
        JAssignmentTarget arrayComponent = JExpr.ref(parameter.getName()).component(JExpr.lit(currentIndex - getParamCount() + 1));
        jBlock.assign(arrayComponent, paramExpression);
        return arrayComponent;
    } else {
        return jBlock.decl(paramClass, parameter.getName(), paramExpression);
    }
}
Also used : JAssignmentTarget(com.sun.codemodel.JAssignmentTarget)

Aggregations

JAssignmentTarget (com.sun.codemodel.JAssignmentTarget)3 JVar (com.sun.codemodel.JVar)2 JBlock (com.sun.codemodel.JBlock)1 JForLoop (com.sun.codemodel.JForLoop)1 JInvocation (com.sun.codemodel.JInvocation)1 JMethod (com.sun.codemodel.JMethod)1 JType (com.sun.codemodel.JType)1 MethodWriter (com.sun.tools.xjc.generator.bean.MethodWriter)1 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)1 UnionHolder (org.apache.drill.exec.expr.holders.UnionHolder)1