use of abs.frontend.ast.AddMethodModifier in project abstools by abstools.
the class DeltaForGetSetFieldsBuilder method updateDelta.
/**
* Add a delta that adds Getters and Setters for clazz.
*
* @param extensions
* @param interf
* @param clazz
*/
void updateDelta(Map<String, String> typeHierarchy, InterfaceTypeUse interf, ClassDecl clazz) {
String className = clazz.getName();
String deltaOnClassName = testCaseNameBuilder.deltaOnClass(className);
String interfaceForModifyingClassFieldName = testCaseNameBuilder.interfaceForModifyingFieldOfClass(className);
DeltaDecl dd = getDelta(deltaOnClassName);
if (dd != null) {
typeHierarchy.put(interf.getName(), interfaceForModifyingClassFieldName);
return;
}
dd = new DeltaDecl();
dd.setName(deltaOnClassName);
dd.addDeltaAccess(new DeltaAccess(clazz.getModuleDecl().getName()));
// add Setters and Getters
ModifyClassModifier mcm = new ModifyClassModifier();
mcm.setName(className);
InterfaceDecl ai = new InterfaceDecl();
ai.setName(interfaceForModifyingClassFieldName);
// extends the existing interface
InterfaceTypeUse inf = interf.treeCopyNoTransform();
ai.addExtendedInterfaceUse(inf.treeCopyNoTransform());
mcm.addAddedInterface(new InterfaceTypeUse(ai.getName(), new abs.frontend.ast.List<abs.frontend.ast.Annotation>()));
typeHierarchy.put(inf.getName(), interfaceForModifyingClassFieldName);
for (MethodImpl m : clazz.getMethodList()) {
if (RUN_METHOD.equals(m.getMethodSig().getName())) {
mcm.addModifier(removeRun());
break;
}
}
for (FieldDecl fd : clazz.getFieldList()) {
AddMethodModifier smm = addSetter(fd.getName(), fd.getAccess().treeCopyNoTransform());
AddMethodModifier gmm = addGetter(fd.getName(), fd.getAccess().treeCopyNoTransform());
mcm.addModifier(smm);
mcm.addModifier(gmm);
ai.addBody(smm.getMethodImpl().getMethodSig());
ai.addBody(gmm.getMethodImpl().getMethodSig());
}
dd.addModuleModifier(new AddInterfaceModifier(ai));
dd.addModuleModifier(mcm);
deltas.add(new DeltaWrapper(dd, false));
}
use of abs.frontend.ast.AddMethodModifier in project abstools by abstools.
the class TraitTest method collapseTest2.
@Test
public void collapseTest2() {
Model model = assertParseOk("module TestMod;" + "interface Inter {}" + "trait T = { " + " Unit printLine_1(){println(\"I'm 1!\");}" + " Unit printLine_2(){println(\"I'm 2!\");}" + "}" + "trait T2 = { " + " Unit printLine_2(){println(\"I'm 2!\");}" + "}" + "trait T3 = { " + " Unit printLine_5(){println(\"I'm 5!\");}" + " Unit printLine_6(){println(\"I'm 6!\");}" + "}" + "trait T4 = { " + " Unit printLine_7(){println(\"I'm 7!\");}" + " Unit printLine_8(){println(\"I'm 8!\");}" + "}" + "class InterImpl(Inter inter) implements Inter { }" + "" + "delta D3;" + "modifies class TestMod.InterImpl{" + " adds T modifies T2 removes Unit printLine_2();" + " adds Int i = 0;" + " modifies T3 adds T4 " + "}");
ClassDecl cls = (ClassDecl) findDecl(model, "TestMod", "InterImpl");
assertNotNull(cls);
assertTrue(cls.getMethods().getNumChild() == 0);
model.applyTraits();
model.collapseTraitModifiers();
DeltaDecl delta = findDelta(model, "D3");
assertNotNull(delta);
assertThat(delta, instanceOf(DeltaDecl.class));
ModifyClassModifier mm = (ModifyClassModifier) delta.getModuleModifier(0);
assertTrue(mm.getModifierList().getNumChild() == 6);
DeltaTraitModifier dml = (DeltaTraitModifier) mm.getModifier(1);
AddMethodModifier mcl = (AddMethodModifier) dml.getMethodModifier();
TraitExpr set = mcl.getTraitExpr();
assertTrue(set.getChild(0).getNumChild() == 2);
assertThat(set, instanceOf(TraitSetExpr.class));
DeltaTraitModifier dml2 = (DeltaTraitModifier) mm.getModifier(2);
ModifyMethodModifier mcl2 = (ModifyMethodModifier) dml2.getMethodModifier();
TraitExpr set2 = mcl2.getTraitExpr();
assertTrue(set2.getChild(0).getNumChild() == 1);
assertThat(set2, instanceOf(TraitSetExpr.class));
}
use of abs.frontend.ast.AddMethodModifier in project abstools by abstools.
the class DeltaForGetSetFieldsBuilder method addGetter.
AddMethodModifier addGetter(Exp returnValue, String fieldName, Access returnType) {
MethodSig sig = new MethodSig(testCaseNameBuilder.getterMethodName(fieldName), new abs.frontend.ast.List<Annotation>(), returnType, new abs.frontend.ast.List<ParamDecl>());
Block block = new Block();
ReturnStmt rs = new ReturnStmt();
rs.setRetExp(new FieldUse(fieldName));
block.addStmtNoTransform(rs);
MethodImpl method = new MethodImpl(sig, block, false);
AddMethodModifier modifier = new AddMethodModifier(method);
return modifier;
}
Aggregations