Search in sources :

Example 1 with JConditional

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

the class DynamicPropertiesRule method addPublicGetMethod.

private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) {
    JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME);
    JTypeVar returnType = method.generify("T");
    method.type(returnType);
    Models.suppressWarnings(method, "unchecked");
    JVar nameParam = method.param(String.class, "name");
    JBlock body = method.body();
    JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value", invoke(internalGetMethod).arg(nameParam).arg(notFoundValue));
    JConditional found = method.body()._if(notFoundValue.ne(valueVar));
    found._then()._return(cast(returnType, valueVar));
    JBlock notFound = found._else();
    JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
    if (getAdditionalProperties != null) {
        notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam)));
    } else {
        notFound._throw(illegalArgumentInvocation(jclass, nameParam));
    }
    return method;
}
Also used : JTypeVar(com.sun.codemodel.JTypeVar) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 2 with JConditional

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

the class DynamicPropertiesRule method addSetProperty.

private void addSetProperty(JDefinedClass jclass, JBlock callSite, String propertyName, JType propertyType, JVar valueVar, JsonNode node) {
    JMethod propertySetter = jclass.getMethod(getSetterName(propertyName, node), new JType[] { propertyType });
    JConditional isInstance = callSite._if(valueVar._instanceof(propertyType.boxify().erasure()));
    isInstance._then().invoke(propertySetter).arg(cast(propertyType.boxify(), valueVar));
    isInstance._else()._throw(illegalArgumentInvocation(jclass, propertyName, propertyType, valueVar));
}
Also used : JConditional(com.sun.codemodel.JConditional) JMethod(com.sun.codemodel.JMethod)

Example 3 with JConditional

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

the class DynamicPropertiesRule method addInternalSetMethodJava6.

private JMethod addInternalSetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
    JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar valueParam = method.param(Object.class, "value");
    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);
            propertyConditional = propertyConditional == null ? propertyConditional = body._if(condition) : propertyConditional._elseif(condition);
            JBlock callSite = propertyConditional._then();
            addSetProperty(jclass, callSite, propertyName, propertyType, valueParam, node);
            callSite._return(TRUE);
        }
    }
    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_SETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
        lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
    } else {
        lastBlock._return(FALSE);
    }
    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 4 with JConditional

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

the class ChainedHashTable method setupIsKeyMatchInternal.

private void setupIsKeyMatchInternal(ClassGenerator<HashTable> cg, MappingSet incomingMapping, MappingSet htableMapping, LogicalExpression[] keyExprs, List<Comparator> comparators, TypedFieldId[] htKeyFieldIds) throws SchemaChangeException {
    cg.setMappingSet(incomingMapping);
    if (keyExprs == null || keyExprs.length == 0) {
        cg.getEvalBlock()._return(JExpr.FALSE);
        return;
    }
    for (int i = 0; i < keyExprs.length; i++) {
        final LogicalExpression expr = keyExprs[i];
        cg.setMappingSet(incomingMapping);
        HoldingContainer left = cg.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        cg.setMappingSet(htableMapping);
        ValueVectorReadExpression vvrExpr = new ValueVectorReadExpression(htKeyFieldIds[i]);
        HoldingContainer right = cg.addExpr(vvrExpr, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc;
        // codegen for nullable columns if nulls are not equal
        if (comparators.get(i) == Comparator.EQUALS && left.isOptional() && right.isOptional()) {
            jc = cg.getEvalBlock()._if(left.getIsSet().eq(JExpr.lit(0)).cand(right.getIsSet().eq(JExpr.lit(0))));
            jc._then()._return(JExpr.FALSE);
        }
        final LogicalExpression f = FunctionGenerationHelper.getOrderingComparatorNullsHigh(left, right, context.getFunctionRegistry());
        HoldingContainer out = cg.addExpr(f, ClassGenerator.BlkCreateMode.FALSE);
        // check if two values are not equal (comparator result != 0)
        jc = cg.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
        jc._then()._return(JExpr.FALSE);
    }
    // All key expressions compared equal, so return TRUE
    cg.getEvalBlock()._return(JExpr.TRUE);
}
Also used : ValueVectorReadExpression(org.apache.drill.exec.expr.ValueVectorReadExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) JConditional(com.sun.codemodel.JConditional)

Example 5 with JConditional

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

the class OperatorCodeGenerator method generateComparisons.

protected void generateComparisons(ClassGenerator<?> g, VectorAccessible batch) {
    g.setMappingSet(MAIN_MAPPING);
    for (Ordering od : popConfig.getOrderings()) {
        // first, we rewrite the evaluation stack for each side of the comparison.
        ErrorCollector collector = new ErrorCollectorImpl();
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFunctionRegistry());
        if (collector.hasErrors()) {
            throw UserException.unsupportedError().message("Failure while materializing expression. " + collector.toErrorString()).build(logger);
        }
        g.setMappingSet(LEFT_MAPPING);
        HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(RIGHT_MAPPING);
        HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(MAIN_MAPPING);
        // next we wrap the two comparison sides and add the expression block for the comparison.
        LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFunctionRegistry());
        HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
        if (od.getDirection() == Direction.ASCENDING) {
            jc._then()._return(out.getValue());
        } else {
            jc._then()._return(out.getValue().minus());
        }
        g.rotateBlock();
    }
    g.rotateBlock();
    g.getEvalBlock()._return(JExpr.lit(0));
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) Ordering(org.apache.drill.common.logical.data.Order.Ordering) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JConditional(com.sun.codemodel.JConditional)

Aggregations

JConditional (com.sun.codemodel.JConditional)18 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)12 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)10 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)8 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)8 Ordering (org.apache.drill.common.logical.data.Order.Ordering)8 JBlock (com.sun.codemodel.JBlock)7 JVar (com.sun.codemodel.JVar)7 JMethod (com.sun.codemodel.JMethod)6 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)6 JExpression (com.sun.codemodel.JExpression)5 JClass (com.sun.codemodel.JClass)3 JDefinedClass (com.sun.codemodel.JDefinedClass)3 Map (java.util.Map)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JFieldVar (com.sun.codemodel.JFieldVar)2 JInvocation (com.sun.codemodel.JInvocation)2 JType (com.sun.codemodel.JType)2 ClassGenerator (org.apache.drill.exec.expr.ClassGenerator)2 ValueVectorReadExpression (org.apache.drill.exec.expr.ValueVectorReadExpression)2