Package com.sun.codemodel

Examples of com.sun.codemodel.JBlock


  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()) {
        JBlock conditionalBlock = new JBlock(false, false);
        JConditional jc = conditionalBlock._if(inputVariables[i].getIsSet().ne(JExpr.lit(0)));
        jc._then().assign(workspaceJVars[3].component(JExpr.lit(i)), workspaceJVars[2].component(JExpr.lit(i)));
        jc._then().assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), inputVariables[i].getHolder());
        jc._else().assign(workspaceJVars[3].component(JExpr.lit(i)), 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;
  }
View Full Code Here


       
        JDefinedClass readerCls = rootReader.getReaderClass();
        dtf = readerCls.field(JMod.STATIC, type_dtFactory, "dtFactory");
        JMethod constructor = readerCls.constructor(JMod.PUBLIC);
        JTryBlock tb = constructor.body()._try();
        JBlock body = tb.body();
        body.assign(dtf, JExpr.direct(DatatypeFactory.class.getName()+".newInstance()"));
        tb._catch((JClass) rootReader.getCodeModel()._ref(DatatypeConfigurationException.class));
       
        Map<QName, ? extends RuntimeElementInfo> elementMappings = set.getElementMappings(null);
        for (Map.Entry<QName, ? extends RuntimeElementInfo> e : elementMappings.entrySet()) {
            QName q = e.getKey();
View Full Code Here

                                     boolean wrap) {
        JCodeModel model = classBuilder.getCodeModel();
        CodeBody body = classBuilder.getBody();
        JType type = model._ref(cls);
       
        JBlock xsiNilReturnBlock = body.getBlock()._if(JExpr.direct("reader").invoke("isXsiNil"))._then();

        // Method factoryMethod = info.getFactoryMethod();
        JVar var = body.decl(type, info.getClazz().getSimpleName(), JExpr._new(type));
        if (wrap) {
            JType jaxbElementType = model._ref(JAXBElement.class);
            JType qnameType = model._ref(QName.class);
            JVar qname = body.getBlock().decl(qnameType, "_xname", JExpr.direct("reader").invoke("getName"));
            body._return(jaxbElementType,
                         JExpr._new(jaxbElementType)
                                .arg(qname).arg(JExpr.dotclass((JClass) type)).arg(var));
            xsiNilReturnBlock._return(JExpr._null());
        } else if (parentCls == null) {
            body._return(var);
            xsiNilReturnBlock._return(JExpr._null());
        } else {
            xsiNilReturnBlock._return();
        }
       
       
        if (info.isOrdered()) {
            // classBuilder.beginSequence();
View Full Code Here

            if (type instanceof Class) {
                c = (Class) type;
            }
           
            // type to create for this property
            JBlock block = builder.getBody().getBlock();
            JExpression toSet = handleElement(builder, target, adapter, c, nillable);

            if (toSet != null)
                doSet(block, bean, beanClass, propEl, toSet);
        } else if (target instanceof RuntimeClassInfo) {
View Full Code Here

        Class<?> c = (Class<?>)propEl.getRawType();
       
        if (target.isSimpleType()) {
            // type to create for this property
            JBlock block = builder.getBody().getBlock();
            JExpression toSet = handleAttribute(builder, target, propEl.getAdapter(), c);

            if (toSet != null)
                doSet(block, bean, beanClass, propEl, toSet);
        } else {
View Full Code Here

    private JExpression handleDuration(ParserBuilder builder, JVar value) {
        return dtf.invoke("newDuration").arg(value);
    }
   
    private JExpression handleEnum(ParserBuilder builder, JExpression value, Class c) {
        JBlock body =  builder.getBody().getBlock();
        JClass jc = (JClass) builder.getCodeModel()._ref(c);
       
        return body.staticInvoke(jc, "fromValue").arg(value);
    }
View Full Code Here

                     boolean writeXsiType) {
       
        JType type = b.getCodeModel()._ref(cls);
        ElementWriterBuilder classBuilder;
        if (info.isElement()) {
            JBlock block = b.getCurrentBlock();
            JVar var = block.decl(type, "_o", JExpr.cast(type, b.getObject()));
            JBlock nullBlock = block._if(var.ne(JExpr._null()))._then();
            b.setCurrentBlock(nullBlock);
           
            classBuilder = b.writeElement(name, type, var);

            b.setCurrentBlock(block);
View Full Code Here

//                   
                // This is all probably less than ideal
                Type rawType = propRef.getRawType();
                String propName = JaxbUtil.getGetter(parentClass, propRef.getName(), rawType);
                   
                JBlock block = classBuilder.getCurrentBlock().block();
                JType mtype = model._ref(MarshallerImpl.class);
                JVar marshaller = block.decl(mtype, "marsh",
                    JExpr.cast(mtype, JExpr.direct("context").invoke("get").arg(JExpr.lit(MarshallerImpl.MARSHALLER))));
               
                JExpression propValue = classBuilder.getObject().invoke(propName);
                if (prop.isCollection()) {
                        JForEach each = block.forEach(getGenericType(rawType), "_o", propValue);
                        JBlock newBody = each.body();
                        block = newBody;
                        propValue = each.var();
                    }
               
                block.add(marshaller.invoke("marshal").arg(propValue).arg(classBuilder.getXSW()));
View Full Code Here

        Class c = (Class) target.getType();
        JType jt = getType(c);
        JType rawJT = getType(rawType);
       
        QName name = typeRef.getTagName();
        JBlock block = b.getCurrentBlock();
        JBlock origBlock = block;

        addType(c, target.getTypeName());

        String propName = JaxbUtil.getGetter(parentClass, propEl.getName(), rawType);
       
        JVar var = block.decl(rawJT,
                              propEl.getName() + "_" + javify(name.getLocalPart()),
                              b.getObject().invoke(propName));

       
        if (!propEl.isRequired()) {
            JConditional nullCond = block._if(var.ne(JExpr._null()));
            block = nullCond._then();
            b.setCurrentBlock(block);
           
            if (typeRef.isNillable()) {
                nullCond._else().add(b.getXSW().invoke("writeXsiNil"));
            } else if (propEl.isCollection() && propEl.isCollectionNillable()) {
                nullCond._else().add(b.getXSW().invoke("writeXsiNil"));
            }
        }
       
        if (propEl.isCollection()) {
            JForEach each = block.forEach(getGenericType(rawType), "_o", var);
            JBlock newBody = each.body();
            b.setCurrentBlock(newBody);
            var = each.var();
           
            rawType = c;
            rawJT = jt;
        }

        ElementWriterBuilder valueBuilder = b.writeElement(name, rawJT, var);
       
        if (target.isSimpleType()) {
            if (rawType instanceof Class) {
                c = (Class) rawType;
            }
            writeSimpleTypeElement(valueBuilder,
                                   target,
                                   propEl.getAdapter(),
                                   typeRef.isNillable(),
                                   rawType, c, jt);
        } else if (target instanceof RuntimeClassInfo) {
            RuntimeClassInfo rci = (RuntimeClassInfo)target;

            List<Class<?>> substTypes = getSubstitutionTypes(c);
            JBlock origBlck = valueBuilder.getCurrentBlock();
           
            if (typeRef.isNillable()) {
                valueBuilder.writeNilIfNull();
            }
           
View Full Code Here

                                        Class c,
                                        JType jt) {
       
        if (adapter != null) {
            JVar adapterVar = getAdapter(adapter);
            JBlock block = b.getCurrentBlock();
            JVar valueVar = block.decl(model.ref(String.class), "value", adapterVar.invoke("marshal").arg(object));
           
            JBlock writeNil = block._if(object.eq(JExpr._null()))._then();
            if (nillable) {
                writeNil.add(b.getXSW().invoke("writeXsiNil"));
            }
            writeNil._return();
           
            block.add(b.getXSW().invoke("writeCharacters").arg(valueVar));
        } else if(c.equals(String.class)
            || c.equals(Integer.class)
            || c.equals(Double.class)
            || c.equals(Boolean.class)
            || c.equals(Float.class)
            || c.equals(Short.class)
            || c.equals(Long.class)
            || c.equals(Byte.class)
            || c.isPrimitive()) {
            JVar orig = b.getObject();
            b.setObject(object);
            b.writeAs(c, nillable);
            b.setObject(orig);
        } else if (c.equals(XMLGregorianCalendar.class)) {
            writePropertyWithMethodInvoke(b, object, jt, "toXMLFormat", nillable);
        } else if (c.isEnum()) {
            try {
                Method method = c.getMethod("value", new Class[0]);
               
                Class<?> readAs = method.getReturnType();
                JType valueType = model._ref(readAs);
               
                JBlock block = b.getCurrentBlock();
                JBlock writeNil = block._if(object.eq(JExpr._null()))._then();
                if (nillable) {
                    writeNil.add(b.getXSW().invoke("writeXsiNil"));
                }
                writeNil._return();
               
                JVar valueVar = block.decl(valueType, "_enum", object.invoke("value"));
                writeSimpleTypeElement(b, target, adapter, valueVar, nillable, readAs, readAs, valueType);
            } catch (Exception e) {
                throw new BuildException(e);
View Full Code Here

TOP

Related Classes of com.sun.codemodel.JBlock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.