Java 类com.sun.codemodel.JWhileLoop 实例源码

项目:kola    文件:StatementAdapter.java   
@Override
public void caseAWhileStatement(AWhileStatement node) {
    ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context);
    node.getCond().apply(ea);
    JWhileLoop wl = parent._while(ea.expr);
    JBlock tmpBlock = new JBlock();
    StatementAdapter sa = new StatementAdapter(new JBlockParent(tmpBlock, context), context);
    node.getDo().apply(sa);
    JStatement stmt = (JStatement) tmpBlock.getContents().get(0);
    wl.setBody(stmt);
}
项目:kola    文件:JBlockParent.java   
@Override
public JWhileLoop _while(JExpression exp) {
    return block._while(exp);
}
项目:LRPaGe    文件:MainGenerator.java   
/**
 * Generates a main class with main method for testing the generated parser and lexer
 * from the command line.
 * @param pckg - package in which to place the main class
 * @param model - JCodeModel
 * @param lexer - the lexer JDefinedClass
 * @param parser - the parser JDefinedClass
 * @param printVisitor - the print visitor JDefinedClass - this is used to print
 * the AST generated by the parser
 * @param abstractNode - the synatx node interface
 * @throws JClassAlreadyExistsException
 * 
 * @author Tristan Bepler
 */
public static void generateMain(String pckg, JCodeModel model,
        Framework f, NodeGenerator nodes, JDefinedClass printVisitor,
        TokenFactoryGenerator tokenFac, ParsingEngineGenerator parseEng)
        throws JClassAlreadyExistsException{
    String name = pckg == null ? MAIN : pckg+"."+MAIN;
    JDefinedClass mainClass = model._class(name);
    CodeGenerator.appendJDocHeader(mainClass);
    JMethod main = mainClass.method(JMod.PUBLIC + JMod.STATIC, void.class, "main");
    main._throws(IOException.class);
    main.param(String[].class, "args");
    JBlock body = main.body();
    JVar visitor = body.decl(nodes.getVisitorInterface(), "visitor", JExpr._new(printVisitor));
    JVar fac = body.decl(f.getTokenFactoryInterface().narrow(nodes.getVisitorInterface()),
            "factory", tokenFac.newTokenFactory());
    JVar parse = body.decl(
            f.getParserClass().narrow(nodes.getVisitorInterface()),
            "parser",
            JExpr._new(f.getParserClass().narrow(nodes.getVisitorInterface()))
                    .arg(JExpr._new(parseEng.getParsingEngine())));
    JVar str = body.decl(model.ref(String.class), "line");
    body.decl(model.ref(BufferedReader.class), "reader", JExpr._new(model.ref(BufferedReader.class))
            .arg(JExpr._new(model.ref(InputStreamReader.class)).arg(model.ref(System.class).staticRef("in"))));
    JWhileLoop loop = body._while(JExpr.direct("(line = reader.readLine()) != null"));
    loop.body()._if(JExpr.invoke(str, "equals").arg(JExpr.lit("q")))._then()._break();
    JVar lex = loop.body().decl(
            f.getLexerClass().narrow(nodes.getVisitorInterface()),
            "lexer",
            JExpr._new(f.getLexerClass().narrow(nodes.getVisitorInterface())).arg(str).arg(fac));
    loop.body()._while(JExpr.invoke(lex, "hasNext")).body()
        .invoke(model.ref(System.class).staticRef("out"), "print").arg(JExpr.invoke(lex, "nextToken").plus(JExpr.lit(" ")));
    loop.body().invoke(model.ref(System.class).staticRef("out"), "println");
    loop.body().assign(lex, JExpr._new(f.getLexerClass().narrow(nodes.getVisitorInterface())).arg(str).arg(fac));
    JVar node = loop.body().decl(
            f.getNodeInterface().narrow(nodes.getVisitorInterface()),
            "root",
            JExpr.invoke(parse, "parse").arg(lex).arg(
                    JExpr._new(f.getBurkeFischerRepairClass().narrow(nodes.getVisitorInterface()))
                            .arg(JExpr.lit(10)).arg(JExpr.lit(4))));
    loop.body()._if(node.ne(JExpr._null()))._then().invoke(node, "accept").arg(visitor);

}
项目:openhds-server    文件:IndividualTemplateBuilder.java   
public void buildAdditionalMethods(JDefinedClass jc) {

    // getCurrentResidency
    JMethod jmgCurrentResidency = jc.method(JMod.PUBLIC, org.openhds.domain.model.Residency.class, "getCurrentResidency");

    // if statement
    JBlock jBlock = jmgCurrentResidency.body();
    JConditional condition = jBlock._if(JExpr.ref(jfResidencies, "size()").eq(JExpr.lit(0)));
    condition._then()._return(JExpr._null());

    // iterator
    JClass iterator = jCodeModel.ref(java.util.Iterator.class);
    iterator = iterator.narrow(org.openhds.domain.model.Residency.class);
    JVar jvIterator = jBlock.decl(iterator, "itr");
    jvIterator.init(JExpr.ref(jfResidencies, "iterator()"));

    // residency
    JClass residencies = jCodeModel.ref(org.openhds.domain.model.Residency.class);
    JVar jvResidencies = jBlock.decl(residencies, "residency");
    jvResidencies.init(JExpr._null());

    // while loop
    JWhileLoop whileLoop = jBlock._while(JExpr.ref(jvIterator, "hasNext()"));
    JBlock whileLoopBody = whileLoop.body();
    whileLoopBody.assign(jvResidencies, JExpr.ref(jvIterator, "next()"));

    jBlock._return(jvResidencies);  

    //-------------- getCurrentMembership ------------------
    JMethod jmgCurrentMembership = jc.method(JMod.PUBLIC, org.openhds.domain.model.Membership.class, "getCurrentMembership");

    //Init method
    JBlock jBlockCurrentMembership = jmgCurrentMembership.body();

    // Membership m1
    JClass memberShipClass = jCodeModel.ref(org.openhds.domain.model.Membership.class);
    JVar jvM1 = jBlockCurrentMembership.decl(memberShipClass, "m1");
    jvM1.init(JExpr._new(memberShipClass));

    // if statement
    JConditional conditionMembership = jBlockCurrentMembership._if(JExpr.ref(jfMemberships, "size()").eq(JExpr.lit(0)));
    conditionMembership._then()._return(JExpr._null());

    // iterator
    JClass membershipIterator = jCodeModel.ref(java.util.Iterator.class);
    membershipIterator = membershipIterator.narrow(org.openhds.domain.model.Membership.class);
    JVar jvMembershipIterator = jBlockCurrentMembership.decl(membershipIterator, "itr");
    jvMembershipIterator.init(JExpr.ref(jfMemberships, "iterator()"));

    //Membership membership
    JVar jvMembership = jBlockCurrentMembership.decl(memberShipClass, "membership");
    jvMembership.init(JExpr._null());

    // while loop
    JWhileLoop membershipWhileLoop = jBlockCurrentMembership._while(JExpr.ref(jvMembershipIterator, "hasNext()"));
    JBlock membershipWhileLoopBody = membershipWhileLoop.body();
    membershipWhileLoopBody.assign(jvM1, JExpr.ref(jvMembershipIterator, "next()"));

    // if statement
    JConditional conditionEndDate = membershipWhileLoopBody._if(JExpr.ref(jvM1, "endDate").eq(JExpr._null())); 
    JBlock assignBlock = conditionEndDate._then();
    assignBlock.assign(jvMembership, jvM1);

    jBlockCurrentMembership._return(jvMembership);
}
项目:kola    文件:StatementAdapter.java   
public JWhileLoop _while(JExpression exp);