public override void Exec(vsCommandExecOption executeOption, ref object varIn, ref object varOut) { CodeFunction2 currentMethod = CodeModelUtils.GetCurrentMethod(m_application); if (IsCommandAvailable(currentMethod)) { AndroMDA.VS80AddIn.Dialogs.PropertyMapperDialog propertyMapper = new AndroMDA.VS80AddIn.Dialogs.PropertyMapperDialog(currentMethod, m_addInSettings); if (propertyMapper.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // User clicked OK AddInUtils.InsertCodeInMethod(currentMethod, propertyMapper.GeneratedCode); m_application.StatusBar.Text = "Android/VS: Code inserted"; } } }
public string GenerateCode(CodeFunction2 method) { CodeParameter2 param = method.Parameters.Item(1) as CodeParameter2; CodeClass2 containingClass = method.Parent as CodeClass2; string returnType = method.Type.AsFullName; string paramType = param.Type.AsFullName; string paramName = param.Name; string returnVariableName = string.Empty; string codeToInsert = string.Empty; bool convertingToEntity = CodeModelUtils.IsEntityClass(method.Type.CodeType); // If we are converting to an entity if (convertingToEntity) { returnVariableName = "entity"; codeToInsert += "// VO to entity conversion\n"; // Add code to create a new entity with the Factory.NewInstance() method codeToInsert += returnType + " " + returnVariableName + " = " + returnType + ".Factory.NewInstance();\n\n"; } else { returnVariableName = "valueObject"; codeToInsert += "// Entity to VO conversion\n\n"; // Add code to create a new VO with a new statement codeToInsert += returnType + " " + returnVariableName + " = new " + returnType + "();\n\n"; } ArrayList unmappedProperties = new ArrayList(); foreach (Property prop in m_properties) { if (prop.SourceProperty != null) { if (prop.IsNullableType) { codeToInsert += "if (" + paramName + "." + prop.Name + ".HasValue)\n{\n"; codeToInsert += returnVariableName + "." + prop.Name + " = " + paramName + "." + prop.SourceProperty.Name + ".Value;\n}\n"; } else { codeToInsert += returnVariableName + "." + prop.Name + " = " + paramName + "." + prop.SourceProperty.Name + ";\n"; } } else { unmappedProperties.Add(prop); } } foreach (Property unmappedProp in unmappedProperties) { codeToInsert += "// " + returnVariableName + "." + unmappedProp.Name + "\n"; } // Add the return statement codeToInsert += "\nreturn " + returnVariableName + ";\n\n"; return(codeToInsert); }
private bool IsCommandAvailable(bool fullCheck) { CodeFunction2 currentMethod = CodeModelUtils.GetCurrentMethod(m_application); return(IsCommandAvailable(currentMethod, fullCheck)); }
public override void Exec(vsCommandExecOption executeOption, ref object varIn, ref object varOut) { CodeFunction2 currentMethod = CodeModelUtils.GetCurrentMethod(m_application); if (IsCommandAvailable(currentMethod)) { try { ConversionCodeGenerator codeGenerator = new ConversionCodeGenerator(); CodeParameter2 param = currentMethod.Parameters.Item(1) as CodeParameter2; ArrayList toProperties; ArrayList fromProperties; try { toProperties = CodeModelUtils.GetPropertiesFromType(currentMethod.Type.CodeType); } catch (NotImplementedException) { throw new Exception("Method return type '" + currentMethod.Type.AsString + "' could not be resolved."); } try { fromProperties = CodeModelUtils.GetPropertiesFromType(param.Type.CodeType); } catch (NotImplementedException) { throw new Exception("Method parameter type '" + param.Type.AsString + "' could not be resolved."); } foreach (CodeProperty toProperty in toProperties) { bool mapped = false; string toName = toProperty.Name; string toNameNoDots = toName.Replace(".", string.Empty); string toType = toProperty.Type.AsFullName; foreach (CodeProperty fromProperty in fromProperties) { string fromName = fromProperty.Name; string fromNameNoDots = fromName.Replace(".", string.Empty); if (fromName == toName || fromName == toNameNoDots || fromNameNoDots == toNameNoDots) { string fromType = fromProperty.Type.AsFullName; if (fromType.Replace("?", string.Empty) == toType.Replace("?", string.Empty)) { codeGenerator.AddProperty(toName, toType, fromName, fromType); mapped = true; break; } } } if (!mapped) { codeGenerator.AddProperty(toName, toType); } } AddInUtils.InsertCodeInMethod(currentMethod, codeGenerator.GenerateCode(currentMethod)); m_application.StatusBar.Text = "Android/VS: Code inserted"; } catch (Exception e) { m_application.StatusBar.Text = "Android/VS: Unable to insert code: " + e.Message; m_application.StatusBar.Highlight(true); } } else { m_application.StatusBar.Text = "Android/VS: Unable to insert code"; } }