use of spoon.reflect.code.CtNewArray in project spoon by INRIA.
the class RoleHandlersGenerator method process.
@Override
public void process() {
SpoonMetaModel metaModel = new SpoonMetaModel(getFactory());
// all root super MMFields
List<MetamodelProperty> superFields = new ArrayList<>();
metaModel.getConcepts().forEach(mmConcept -> {
mmConcept.getRoleToProperty().forEach((role, rim) -> {
addUniqueObject(superFields, rim.getRootSuperField());
});
});
superFields.sort((a, b) -> {
int d = a.getRole().name().compareTo(b.getRole().name());
if (d != 0) {
return d;
}
return a.getOwnerConcept().getName().compareTo(b.getOwnerConcept().getName());
});
PrinterHelper concept = new PrinterHelper(getFactory().getEnvironment());
superFields.forEach(mmField -> {
concept.write(mmField.getOwnerConcept().getName() + " CtRole." + mmField.getRole().name()).writeln().incTab().write("ItemType: ").write(mmField.getValueType().toString()).writeln();
for (MMMethodKind mk : MMMethodKind.values()) {
MMMethod mmMethod = mmField.getMethod(mk);
if (mmMethod != null) {
concept.write(mk.name()).write(": ").write(mmMethod.getSignature()).write(" : ").write(mmMethod.getReturnType().toString()).writeln();
}
}
concept.decTab();
concept.write("----------------------------------------------------------").writeln();
});
try (Writer w = new OutputStreamWriter(new FileOutputStream(file("target/report/concept.txt")))) {
w.write(concept.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
CtType<?> template = getTemplate("spoon.generating.meta.ModelRoleHandlerTemplate");
CtClass<?> modelRoleHandlersClass = Substitution.createTypeFromTemplate(TARGET_PACKAGE + ".ModelRoleHandlers", template, new HashMap<>());
CtNewArray<?> roleHandlersFieldExpr = (CtNewArray<?>) modelRoleHandlersClass.getField("roleHandlers").getDefaultExpression();
superFields.forEach(rim -> {
Map<String, Object> params = new HashMap<>();
params.put("$getterName$", rim.getMethod(MMMethodKind.GET).getName());
if (rim.getMethod(MMMethodKind.SET) != null) {
params.put("$setterName$", rim.getMethod(MMMethodKind.SET).getName());
}
params.put("$Role$", getFactory().Type().createReference(CtRole.class));
params.put("ROLE", rim.getRole().name());
params.put("$TargetType$", rim.getOwnerConcept().getModelInterface().getReference());
// params.put("AbstractHandler", getFactory().Type().createReference("spoon.reflect.meta.impl.AbstractRoleHandler"));
params.put("AbstractHandler", getRoleHandlerSuperTypeQName(rim));
params.put("Node", rim.getOwnerConcept().getModelInterface().getReference());
params.put("ValueType", fixMainValueType(getRoleHandlerSuperTypeQName(rim).endsWith("SingleHandler") ? rim.getValueType() : rim.getItemValueType()));
CtClass<?> modelRoleHandlerClass = Substitution.createTypeFromTemplate(getHandlerName(rim), getTemplate("spoon.generating.meta.RoleHandlerTemplate"), params);
if (rim.getMethod(MMMethodKind.SET) == null) {
modelRoleHandlerClass.getMethodsByName("setValue").forEach(m -> m.delete());
}
modelRoleHandlerClass.addModifier(ModifierKind.STATIC);
modelRoleHandlersClass.addNestedType(modelRoleHandlerClass);
roleHandlersFieldExpr.addElement(getFactory().createCodeSnippetExpression("new " + modelRoleHandlerClass.getSimpleName() + "()"));
});
}
Aggregations