public void GetterWithInvalidSetterDoesNotGenerateProperty() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var returnType = TypeRegistry.Int32; var getMethod = new CsMethod(null, "GetActive") { ReturnValue = new CsReturnValue(null) { PublicType = returnType, MarshalType = returnType }, }; var invalidSetMethod = new CsMethod(null, "SetActive") { ReturnValue = new CsReturnValue(null) { PublicType = TypeRegistry.Void } }; Assert.Empty(propertyBuilder.CreateProperties(new[] { getMethod, invalidSetMethod })); Assert.Empty(propertyBuilder.CreateProperties(new[] { invalidSetMethod, getMethod })); }
public void GetterMethodReturningStatusCodeWithOutParamGeneratesProperty() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = new CsFundamentalType(typeof(int)); var getMethod = new CsMethod { Name = "GetActive", ReturnValue = new CsReturnValue { PublicType = new CsStruct { Name = "SharpGen.Runtime.Result" } } }; getMethod.Add(new CsParameter { PublicType = paramType, Attribute = CsParameterAttribute.Out }); var properties = propertyBuilder.CreateProperties(new[] { getMethod }); Assert.True(properties.ContainsKey("Active")); var prop = properties["Active"]; Assert.True(prop.IsPropertyParam); Assert.Equal(paramType, prop.PublicType); }
public void MethodWithNameStartingWithSetCreatesProperty() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = TypeRegistry.Int32; var setMethod = new CsMethod(null, "SetActive") { ReturnValue = new CsReturnValue(null) { PublicType = TypeRegistry.Void } }; setMethod.Add(new CsParameter(null, null) { PublicType = paramType }); var properties = propertyBuilder.CreateProperties(new[] { setMethod }); Assert.True(properties.ContainsKey("Active")); var prop = properties["Active"]; Assert.Equal(paramType, prop.PublicType); }
public void PersistentGetterGeneratesPersistentProperty() { CppMethod cppGetMethod = new("GetActive") { Rule = { Property = true, Persist = true } }; var paramType = TypeRegistry.Int32; var getMethod = new CsMethod(cppGetMethod, cppGetMethod.Name) { ReturnValue = new CsReturnValue(null) { PublicType = paramType, MarshalType = paramType } }; var iface = new CsInterface(null, null); iface.Add(getMethod); var prop = new CsProperty(null, "Active", getMethod, null); PropertyBuilder.AttachPropertyToParent(prop); Assert.True(prop.IsPersistent); }
public void PersistentGetterGeneratesPersistentProperty() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = new CsFundamentalType(typeof(int)); var getMethod = new CsMethod { Name = "GetActive", ReturnValue = new CsReturnValue { PublicType = paramType, MarshalType = paramType }, AllowProperty = true, IsPersistent = true }; var iface = new CsInterface(); iface.Add(getMethod); var prop = new CsProperty("Active") { Getter = getMethod }; propertyBuilder.AttachPropertyToParent(prop); Assert.True(prop.IsPersistent); }
public void PropertyAttachedToGetterType() { var paramType = TypeRegistry.Int32; CppMethod cppGetMethod = new("GetActive") { Rule = { Property = true } }; var getMethod = new CsMethod(cppGetMethod, cppGetMethod.Name) { ReturnValue = new CsReturnValue(null) { PublicType = paramType, MarshalType = paramType } }; var iface = new CsInterface(null, null); iface.Add(getMethod); var prop = new CsProperty(null, "Active", getMethod, null); PropertyBuilder.AttachPropertyToParent(prop); Assert.Equal(iface, prop.Parent); }
public void PropertyNotAttachedWhenSetterAllowPropertyIsFalse() { CppMethod cppSetMethod = new("SetActive") { Rule = { Property = false } }; var paramType = TypeRegistry.Int32; var setMethod = new CsMethod(cppSetMethod, cppSetMethod.Name) { ReturnValue = new CsReturnValue(null) { PublicType = TypeRegistry.Void } }; setMethod.Add(new CsParameter(null, null) { PublicType = paramType }); var iface = new CsInterface(null, null); iface.Add(setMethod); var prop = new CsProperty(null, "Active", null, setMethod); PropertyBuilder.AttachPropertyToParent(prop); Assert.Null(prop.Parent); }
public void DoesNotGeneratePropertyIfGetterAndSetterMismatch() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = new CsFundamentalType(typeof(int)); var getMethod = new CsMethod { Name = "GetActive", ReturnValue = new CsReturnValue { PublicType = paramType, MarshalType = paramType }, }; var setMethod = new CsMethod { Name = "SetActive", ReturnValue = new CsReturnValue { PublicType = new CsFundamentalType(typeof(void)) } }; setMethod.Add(new CsParameter { PublicType = new CsFundamentalType(typeof(short)) }); var props = propertyBuilder.CreateProperties(new[] { getMethod, setMethod }); Assert.Empty(props); }
public void GetterWithInvalidSetterDoesNotGenerateProperty() { var returnType = TypeRegistry.Int32; CsMethod getMethod = new(Ioc, null, "GetActive") { ReturnValue = new CsReturnValue(Ioc, null) { PublicType = returnType, MarshalType = returnType } }; var invalidSetMethod = new CsMethod(Ioc, null, "SetActive") { ReturnValue = new CsReturnValue(Ioc, null) { PublicType = TypeRegistry.Void } }; Assert.Empty(propertyBuilder.CreateProperties(new[] { getMethod, invalidSetMethod })); Assert.Empty(propertyBuilder.CreateProperties(new[] { invalidSetMethod, getMethod })); }
public void SetterVisibilityInternal() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = new CsFundamentalType(typeof(int)); var setMethod = new CsMethod { Name = "SetActive", ReturnValue = new CsReturnValue { PublicType = new CsFundamentalType(typeof(void)) }, AllowProperty = true }; setMethod.Add(new CsParameter { PublicType = paramType }); var iface = new CsInterface(); iface.Add(setMethod); var prop = new CsProperty("Active") { Setter = setMethod }; propertyBuilder.AttachPropertyToParent(prop); Assert.Equal(Visibility.Internal, setMethod.Visibility); }
public void GetterMethodReturningStatusCodeWithOutParamGeneratesProperty() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = TypeRegistry.Int32; var getMethod = new CsMethod(null, "GetActive") { ReturnValue = new CsReturnValue(null) { PublicType = new CsStruct(null, "SharpGen.Runtime.Result") } }; getMethod.Add(new CsParameter(null, null) { PublicType = paramType, Attribute = CsParameterAttribute.Out }); var properties = propertyBuilder.CreateProperties(new[] { getMethod }); Assert.True(properties.ContainsKey("Active")); var prop = properties["Active"]; Assert.True(prop.IsPropertyParam); Assert.Equal(paramType, prop.PublicType); }
public void PropertyNotAttachedWhenGetterAllowPropertyIsFalse() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider("SharpGen.Runtime")); var paramType = new CsFundamentalType(typeof(int)); var getMethod = new CsMethod { Name = "GetActive", ReturnValue = new CsReturnValue { PublicType = paramType, MarshalType = paramType }, AllowProperty = false }; var iface = new CsInterface(); iface.Add(getMethod); var prop = new CsProperty("Active") { Getter = getMethod }; propertyBuilder.AttachPropertyToParent(prop); Assert.Null(prop.Parent); }
public void GetterVisibiltyInternal() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider("SharpGen.Runtime")); var paramType = new CsFundamentalType(typeof(int)); var getMethod = new CsMethod { Name = "GetActive", ReturnValue = new CsReturnValue { PublicType = paramType, MarshalType = paramType }, AllowProperty = true }; var iface = new CsInterface(); iface.Add(getMethod); var prop = new CsProperty("Active") { Getter = getMethod }; propertyBuilder.AttachPropertyToParent(prop); Assert.Equal(Visibility.Internal, getMethod.Visibility); }
public void SetOnlyPropertyAttachedToSetterType() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider("SharpGen.Runtime")); var paramType = new CsFundamentalType(typeof(int)); var setMethod = new CsMethod { Name = "SetActive", ReturnValue = new CsReturnValue { PublicType = new CsFundamentalType(typeof(void)) }, AllowProperty = true }; setMethod.Add(new CsParameter { PublicType = paramType }); var iface = new CsInterface(); iface.Add(setMethod); var prop = new CsProperty("Active") { Setter = setMethod }; propertyBuilder.AttachPropertyToParent(prop); Assert.Equal(iface, prop.Parent); }
public void MethodWithNameStartingWithSetCreatesProperty() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = new CsFundamentalType(typeof(int)); var setMethod = new CsMethod { Name = "SetActive", ReturnValue = new CsReturnValue { PublicType = new CsFundamentalType(typeof(void)) } }; setMethod.Add(new CsParameter { PublicType = paramType }); var properties = propertyBuilder.CreateProperties(new[] { setMethod }); Assert.True(properties.ContainsKey("Active")); var prop = properties["Active"]; Assert.Equal(paramType, prop.PublicType); }
public void MethodWithNameStartingWithSetAndReturningResultGeneratesProperty() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = new CsFundamentalType(typeof(int)); var setMethod = new CsMethod { Name = "SetActive", ReturnValue = new CsReturnValue { PublicType = new CsStruct { Name = "SharpGen.Runtime.Result" } } }; setMethod.Add(new CsParameter { PublicType = paramType }); var properties = propertyBuilder.CreateProperties(new[] { setMethod }); Assert.True(properties.ContainsKey("Active"), "Property not created"); var prop = properties["Active"]; Assert.Equal(paramType, prop.PublicType); }
public void MethodWithNameStartingWithSetAndReturningResultGeneratesProperty() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = TypeRegistry.Int32; var setMethod = new CsMethod(null, "SetActive") { ReturnValue = new CsReturnValue(null) { PublicType = new CsStruct(null, "SharpGen.Runtime.Result") } }; setMethod.Add(new CsParameter(null, null) { PublicType = paramType }); var properties = propertyBuilder.CreateProperties(new[] { setMethod }); Assert.True(properties.ContainsKey("Active"), "Property not created"); var prop = properties["Active"]; Assert.Equal(paramType, prop.PublicType); }
public void GetterWithInvalidSetterDoesNotGenerateProperty() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var returnType = new CsFundamentalType(typeof(int)); var getMethod = new CsMethod { Name = "GetActive", ReturnValue = new CsReturnValue { PublicType = returnType, MarshalType = returnType }, }; var invalidSetMethod = new CsMethod { Name = "SetActive", ReturnValue = new CsReturnValue { PublicType = new CsFundamentalType(typeof(void)) } }; Assert.Empty(propertyBuilder.CreateProperties(new[] { getMethod, invalidSetMethod })); Assert.Empty(propertyBuilder.CreateProperties(new[] { invalidSetMethod, getMethod })); }
public void PropertyNotAttachedWhenSetterAllowPropertyIsFalse() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = new CsFundamentalType(typeof(int)); var setMethod = new CsMethod { Name = "SetActive", ReturnValue = new CsReturnValue { PublicType = new CsFundamentalType(typeof(void)) }, AllowProperty = false }; setMethod.Add(new CsParameter { PublicType = paramType }); var iface = new CsInterface(); iface.Add(setMethod); var prop = new CsProperty("Active") { Setter = setMethod }; propertyBuilder.AttachPropertyToParent(prop); Assert.Null(prop.Parent); }
public void DoesNotGeneratePropertyIfGetterAndSetterMismatch() { var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider()); var paramType = TypeRegistry.Int32; var getMethod = new CsMethod(null, "GetActive") { ReturnValue = new CsReturnValue(null) { PublicType = paramType, MarshalType = paramType }, }; var setMethod = new CsMethod(null, "SetActive") { ReturnValue = new CsReturnValue(null) { PublicType = TypeRegistry.Void } }; setMethod.Add(new CsParameter(null, null) { PublicType = TypeRegistry.Int16 }); var props = propertyBuilder.CreateProperties(new[] { getMethod, setMethod }); Assert.Empty(props); }
public void SetterVisibilityInternal() { CppMethod cppSetMethod = new("SetActive") { Rule = { Property = true } }; var paramType = TypeRegistry.Int32; var setMethod = new CsMethod(cppSetMethod, cppSetMethod.Name) { ReturnValue = new CsReturnValue(null) { PublicType = TypeRegistry.Void } }; setMethod.Add(new CsParameter(null, null) { PublicType = paramType }); var iface = new CsInterface(null, null); iface.Add(setMethod); var prop = new CsProperty(null, "Active", null, setMethod); PropertyBuilder.AttachPropertyToParent(prop); Assert.Equal(Visibility.Internal, setMethod.Visibility); }
public static CsMethod WithLeftRightArguments(this CsMethod method, string leftType, string rightType, string leftName = "left", string rightName = "right") { method.AddParam(leftName, leftType); method.AddParam(rightName, rightType); return(method); }
public static CsMethod WithBodyComment(this CsMethod m, string comment) { // uncomment for diagnostic code generation /* * m.Body = $"//{comment}\r\n{m.Body}".TrimEnd(); */ return(m); }
/// <summary> /// Helper method that will confirm the method is a controller action. /// </summary> /// <param name="source">The source method to check.</param> /// <returns>True if the method is a controller action.</returns> private static bool IsControllerAction(CsMethod source) { bool result = false; //Bounds and default value checking if (source == null) { return(false); } if (!source.IsLoaded) { return(false); } if (source.IsVoid) { return(false); } var returnType = source.ReturnType; //Checking to see if the return type is a task type if (returnType.IsClass & (returnType.Namespace == AspNetCoreConstants.SystemThreadingTasksNamespace & returnType.Name == AspNetCoreConstants.TaskClassName)) { //Getting the first parameter from the task var parameter = returnType.GenericParameters.FirstOrDefault(); //Setting the return type to check returnType = parameter?.Type; //If no type data was found then return false. if (returnType == null) { return(false); } } if (returnType.IsInterface) { result = (returnType.Namespace == AspNetCoreConstants.MvcNamespace & returnType.Name == AspNetCoreConstants.ActionResultInterfaceName); } else { result = (returnType.Namespace == AspNetCoreConstants.MvcNamespace & returnType.Name == AspNetCoreConstants.ActionResultClassName); if (!result) { result = returnType.InheritsBaseClass(AspNetCoreConstants.ActionResultClassName, AspNetCoreConstants.MvcNamespace); } } return(result); }
private static CsMethod ParseMethod(MethodDeclarationSyntax x) { var method = new CsMethod() { Name = x.Identifier.ToString(), Type = x.ReturnType.ToString() }; return(method); }
private (string PropertyName, PropertyMethod?PropertyMethod) GetPropertySpec(CsMethod csMethod) { var isIs = csMethod.Name.StartsWith("Is"); var isGet = csMethod.Name.StartsWith("Get") || isIs; var isSet = csMethod.Name.StartsWith("Set"); var propertyName = isIs ? csMethod.Name : csMethod.Name.Substring("Get".Length); return(propertyName, isGet ? PropertyMethod.Getter : isSet?PropertyMethod.Setter : (PropertyMethod?)null); }
private ExpressionSyntax GetMarshalFunctionPointerForDelegate(CsMethod method, PlatformDetectionType platform) => InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, GlobalNamespace.GetTypeNameSyntax(BuiltinType.Marshal), IdentifierName("GetFunctionPointerForDelegate") ), ArgumentList(SingletonSeparatedList(Argument(IdentifierName(GetMethodCacheName(method, platform))))) );
private async Task <CsSource> AddMethodMember(CsClass targetClass, CsMethod member, bool logging, bool cdf, bool cdfAspNet, string targetFilePath, NamespaceManager manager) { CsSource result = null; string sourceCode = null; if (cdfAspNet) { if (WebApiSupport.IsControllerAction(member)) { sourceCode = CSharpSourceGenerationCommonDeliveryFramework.GenerateControllerActionMethodSourceCode(member, manager, true, true, CsSecurity.Public, logging, "_logger"); } else { sourceCode = CSharpSourceGenerationCommonDeliveryFramework.GenerateStandardMethodSourceCode(member, manager, true, true, CsSecurity.Public, logging, "_logger"); } } else { if (cdf) { sourceCode = CSharpSourceGenerationCommonDeliveryFramework.GenerateStandardMethodSourceCode(member, manager, true, true, CsSecurity.Public, logging, "_logger"); } else { if (logging) { sourceCode = CSharpSourceGenerationNetCommon.GenerateStandardMethodSourceCode(member, manager, true, true, CsSecurity.Public, true, "_logger"); } else { sourceCode = CSharpSourceGenerationCommon.GenerateStandardMethodSourceCode(member, manager, true, true); } } } if (string.IsNullOrEmpty(sourceCode)) { throw new CodeFactoryException("Was not able to generate the source code for the member method."); } result = await targetClass.AddToEndAsync(targetFilePath, CsSourceFormatter.IndentCodeBlock(2, sourceCode)); if (result == null) { throw new CodeFactoryException("Could not load the source code after adding the member."); } return(result); }
public TheMethod GetMethod(CsMethod pMethod) { TheMethod c; if (_methods.TryGetValue(pMethod, out c)) { return(c); } return(_entityMethods.TryGetValue(pMethod.entity, out c) ? c : null); }
private string FormatMethodSignature(CsMethod methodData) { if (methodData == null) { return(null); } bool isAsyncReturn = false; bool isVoid = methodData.IsVoid; if (!isVoid) { isAsyncReturn = methodData.IsAsync; if (!isAsyncReturn) { isAsyncReturn = methodData.ReturnType.Name == "Task"; } if (!isAsyncReturn) { isAsyncReturn = methodData.ReturnType.Name.StartsWith("Task<"); } } StringBuilder methodSignature = new StringBuilder($"{methodData.Security.FormatCSharpSyntax()} "); if (isVoid) { methodSignature.Append($"{CodeFactory.DotNet.CSharp.FormattedSyntax.Keywords.Void} "); } else { if (isAsyncReturn) { methodSignature.Append($"{CodeFactory.DotNet.CSharp.FormattedSyntax.CommonContextualKeywords.Async} "); } methodSignature.Append($"{methodData.ReturnType.FormatCSharpFullTypeName()} "); } methodSignature.Append(methodData.Name); if (methodData.IsGeneric) { methodSignature.Append(methodData.GenericParameters.FormatCSharpGenericSignatureSyntax()); } if (methodData.HasParameters) { methodSignature.Append(methodData.Parameters.FormatCSharpParametersSignatureSyntax()); } else { methodSignature.Append("()"); } return(methodSignature.ToString()); }
internal TheMethod(CsMethod pCsMethod, TheClass pMyClass, FactoryExpressionCreator pCreator) { MyClass = pMyClass; Modifiers.AddRange(Helpers.GetModifiers(pCsMethod.modifiers)); Arguments = getArguments(pCsMethod.parameters.parameters, pCreator); Signature = getSignature(Arguments); CodeBlock = pCsMethod.definition; //_sig = Signature.Replace(',', '_').Replace("<", "").Replace(">", ""); //_name = Helpers.GetRealName(pCsMethod, pCsMethod.identifier.identifier); _realName = _name = pCsMethod.identifier.identifier; //FullRealName = MyClass.FullRealName + "." + RealName; ReturnType = Helpers.GetType(pCsMethod.return_type); IsExtensionMethod = pCsMethod.entity.isExtensionMethod(); }
public TheMethod GetMethod(CsMethod pMethod) { TheMethod c; if (_methods.TryGetValue(pMethod, out c)) { return c; } return _entityMethods.TryGetValue(pMethod.entity, out c) ? c : null; }