use of com.google.template.soy.soytree.defn.TemplateParam in project closure-templates by google.
the class TemplateNodeTest method testParseSoyDoc.
@Test
public void testParseSoyDoc() {
String soyDoc = "" + "/**\n" + " * Test template.\n" + " *\n" + " * @param foo Foo to print.\n" + " * @param? goo\n" + " * Goo to print.\n" + " */";
TemplateNode tn = parse("{namespace ns}\n" + "/**\n" + " * Test template.\n" + " *\n" + " * @param foo Foo to print.\n" + " * @param? goo\n" + " * Goo to print.\n" + " */" + "{template .boo}{$foo}{$goo}{/template}");
assertEquals(soyDoc, tn.getSoyDoc());
assertEquals("Test template.", tn.getSoyDocDesc());
List<TemplateParam> params = tn.getParams();
assertEquals(2, params.size());
SoyDocParam soyDocParam0 = (SoyDocParam) params.get(0);
assertEquals(DeclLoc.SOY_DOC, soyDocParam0.declLoc());
assertEquals("foo", soyDocParam0.name());
assertEquals(true, soyDocParam0.isRequired());
assertEquals("Foo to print.", soyDocParam0.desc());
SoyDocParam soyDocParam1 = (SoyDocParam) params.get(1);
assertEquals(DeclLoc.SOY_DOC, soyDocParam1.declLoc());
assertEquals("goo", soyDocParam1.name());
assertEquals(false, soyDocParam1.isRequired());
assertEquals("Goo to print.", soyDocParam1.desc());
}
use of com.google.template.soy.soytree.defn.TemplateParam in project closure-templates by google.
the class TemplateNodeTest method testParseHeaderDecls.
@Test
public void testParseHeaderDecls() {
TemplateNode tn = parse("{namespace ns}\n" + "/**@param foo */\n" + "{template .boo}{$foo}{/template}");
List<TemplateParam> params = tn.getParams();
assertThat(params).hasSize(1);
SoyDocParam soyDocParam0 = (SoyDocParam) params.get(0);
assertEquals("foo", soyDocParam0.name());
assertThat(ImmutableList.copyOf(tn.getAllParams())).hasSize(1);
}
use of com.google.template.soy.soytree.defn.TemplateParam in project closure-templates by google.
the class TemplateCompiler method generateConstructor.
/**
* Generate a public constructor that assigns our final field and checks for missing required
* params.
*
* <p>This constructor is called by the generate factory classes.
*
* @param fieldInitializers additional statements to initialize fields (other than params)
*/
private void generateConstructor(Statement fieldInitializers) {
final Label start = new Label();
final Label end = new Label();
final LocalVariable thisVar = createThisVar(template.typeInfo(), start, end);
final LocalVariable paramsVar = createLocal("params", 1, SOY_RECORD_TYPE, start, end);
final LocalVariable ijVar = createLocal("ij", 2, SOY_RECORD_TYPE, start, end);
final List<Statement> assignments = new ArrayList<>();
// for other fields needed by the compiler.
assignments.add(fieldInitializers);
assignments.add(paramsField.putInstanceField(thisVar, paramsVar));
assignments.add(ijField.putInstanceField(thisVar, ijVar));
for (TemplateParam param : template.node().getAllParams()) {
Expression paramProvider = getParam(paramsVar, ijVar, param);
assignments.add(paramFields.get(param.name()).putInstanceField(thisVar, paramProvider));
}
Statement constructorBody = new Statement() {
@Override
protected void doGen(CodeBuilder ga) {
ga.mark(start);
// call super()
thisVar.gen(ga);
ga.invokeConstructor(OBJECT.type(), NULLARY_INIT);
for (Statement assignment : assignments) {
assignment.gen(ga);
}
ga.visitInsn(Opcodes.RETURN);
ga.visitLabel(end);
thisVar.tableEntry(ga);
paramsVar.tableEntry(ga);
ijVar.tableEntry(ga);
}
};
constructorBody.writeMethod(Opcodes.ACC_PUBLIC, template.constructor().method(), writer);
}
Aggregations