protected override string GenerateCode(ITypeDefinition currentClass)
{
List<PropertyOrFieldWrapper> filtered = parameterList
.Where(p => p.IsIncluded)
.OrderBy(p => p.Index)
.ToList();
var test = refactoringContext.GetNode();
var insertedConstructor = refactoringContext.GetNode().PrevSibling as ConstructorDeclaration;
if (insertedConstructor == null)
{
// We are not inside of a constructor declaration
return null;
}
using (Script script = refactoringContext.StartScript()) {
BlockStatement originalCtorBody = insertedConstructor.Body;
foreach (PropertyOrFieldWrapper w in filtered) {
if (w.AddCheckForNull) {
// true = reference, null = generic or unknown
if (w.Type.IsReferenceType != false)
script.AddTo(originalCtorBody,
new IfElseStatement(
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.Equality, new PrimitiveExpression(null)),
new ThrowStatement(new ObjectCreateExpression(new SimpleType("ArgumentNullException"), new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"') }))
)
);
else
script.AddTo(originalCtorBody,
new IfElseStatement(
new UnaryOperatorExpression(UnaryOperatorType.Not, new MemberReferenceExpression(new IdentifierExpression(w.MemberName), "HasValue")),
new ThrowStatement(new ObjectCreateExpression(new SimpleType("ArgumentNullException"), new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"') }))
)
);
}
if (w.AddRangeCheck) {
script.AddTo(originalCtorBody,
new IfElseStatement(
new BinaryOperatorExpression(
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.LessThan, new IdentifierExpression("lower")),
BinaryOperatorType.ConditionalOr,
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.GreaterThan, new IdentifierExpression("upper"))
),
new ThrowStatement(
new ObjectCreateExpression(
new SimpleType("ArgumentOutOfRangeException"),
new List<Expression>() { new PrimitiveExpression(w.ParameterName, '"' + w.ParameterName + '"'), new IdentifierExpression(w.ParameterName), new BinaryOperatorExpression(new PrimitiveExpression("Value must be between "), BinaryOperatorType.Add, new BinaryOperatorExpression(new IdentifierExpression("lower"), BinaryOperatorType.Add, new BinaryOperatorExpression(new PrimitiveExpression(" and "), BinaryOperatorType.Add, new IdentifierExpression("upper")))) }
)
)
)
);
}
}
foreach (PropertyOrFieldWrapper w in filtered) {
script.AddTo(originalCtorBody,
new ExpressionStatement(new AssignmentExpression(new MemberReferenceExpression(new ThisReferenceExpression(), w.MemberName), AssignmentOperatorType.Assign, new IdentifierExpression(w.ParameterName)))
);
}
}
AnchorElement parameterListElement = insertionContext.ActiveElements
.OfType<AnchorElement>()
.FirstOrDefault(item => item.Name.Equals("parameterList", StringComparison.OrdinalIgnoreCase));
if (parameterListElement != null) {
StringBuilder pList = new StringBuilder();
var parameters = filtered
.Select(p => new ParameterDeclaration(refactoringContext.CreateShortType(p.Type), p.ParameterName))
.ToList();
using (StringWriter textWriter = new StringWriter(pList)) {
// Output parameter list as string
var formattingOptions = FormattingOptionsFactory.CreateMono();
CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor(textWriter, formattingOptions);
for (int i = 0; i < parameters.Count; i++) {
if (i > 0)
textWriter.Write(",");
outputVisitor.VisitParameterDeclaration(parameters[i]);
}
}
parameterListElement.Text = pList.ToString();
}
return null;
}