Esempio n. 1
0
        private void GenerateOrdinaryClass(ClassDeclarationSyntax classDeclaration, SemanticModel semanticModel, string outputPath)
        {
            INamedTypeSymbol typeInfo   = semanticModel.GetDeclaredSymbol(classDeclaration);
            var fullyQualifiedNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(typeInfo);
            var genericTypeParameters   = typeInfo.TypeParameters;
            var typeReferences          = new HashSet <string>();

            var propertyDeclarations = classDeclaration.DescendantNodes().OfType <PropertyDeclarationSyntax>();
            var generatedProperties  = GetProperties(semanticModel, typeReferences, propertyDeclarations, false);

            var fieldsDeclarations = classDeclaration.DescendantNodes().OfType <FieldDeclarationSyntax>();
            var generatedFields    = GetFields(semanticModel, typeReferences, fieldsDeclarations);

            var constructorDeclarations = classDeclaration.DescendantNodes().OfType <ConstructorDeclarationSyntax>();
            var generatedConstructors   = GetConstructors(semanticModel, typeReferences, constructorDeclarations);

            var methodDeclarations = classDeclaration.DescendantNodes().OfType <MethodDeclarationSyntax>();
            var generatedMethods   = GetMethods(semanticModel, typeReferences, methodDeclarations);

            var classSourceCode = TypeBuilder.BuildType(semanticModel,
                                                        fullyQualifiedNameParts, genericTypeParameters,
                                                        typeInfo.BaseType,
                                                        typeInfo.AllInterfaces,
                                                        generatedConstructors,
                                                        generatedFields,
                                                        generatedProperties,
                                                        generatedMethods);

            WriteToOutputIfPathPresent(outputPath, fullyQualifiedNameParts, classSourceCode.MainPart);
        }
Esempio n. 2
0
        private string GenerateExceptionCreationExpression(ObjectCreationExpressionSyntax exceptionCreationExpressionSyntax, SemanticModel semanticModel)
        {
            var    exceptionType          = (INamedTypeSymbol)ModelExtensions.GetTypeInfo(semanticModel, exceptionCreationExpressionSyntax).Type;
            string exceptionTypeFullName  = SyntaxTreeHelper.GetFullyQualifiedName(exceptionType);
            string generatedExceptionName = "";

            if (exceptionTypeFullName == "System.ArgumentNullException")
            {
                generatedExceptionName = "IllegalArgumentException";
            }
            else if (exceptionTypeFullName == "System.ArgumentException")
            {
                generatedExceptionName = "IllegalArgumentException";
            }
            else if (exceptionTypeFullName == "System.NullReferenceException")
            {
                generatedExceptionName = "NullPointerException";
            }
            else if (exceptionTypeFullName == "System.InvalidOperationException")
            {
                generatedExceptionName = "IllegalStateException";
            }
            else
            {
                throw new NotSupportedException();
            }
            string argumentList = ArgumentListGenerator.Generate(exceptionCreationExpressionSyntax.ArgumentList, semanticModel);

            return("new " + generatedExceptionName + argumentList);
        }
Esempio n. 3
0
        private void Generate(EnumDeclarationSyntax enumDeclaration, SemanticModel semanticModel, string outputPath)
        {
            INamedTypeSymbol typeInfo   = semanticModel.GetDeclaredSymbol(enumDeclaration);
            var fullyQualifiedNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(typeInfo);

            var sourceCode = TypeBuilder.BuildEnum(semanticModel, fullyQualifiedNameParts, enumDeclaration.Members);

            WriteToOutputIfPathPresent(outputPath, fullyQualifiedNameParts, sourceCode.MainPart);
        }
Esempio n. 4
0
        public void NameOfConstEvaluatorTest(string textExpression, string expectedValue)
        {
            var walker = new ConstantExpressionSyntaxEvaluator <string>();
            var exp    = SyntaxTreeHelper.GetExpressionSyntax($"nameof({textExpression})");

            var value = walker.Visit(exp);

            Assert.Equal(expectedValue, value);
        }
        public static bool IsCustom(INamedTypeSymbol accessingType)
        {
            string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(accessingType);

            if (accessingTypeFullName == "System.String")
            {
                return(true);
            }
            return(false);
        }
Esempio n. 6
0
        public void SimpleStringConstEvaluatorTest()
        {
            var textValue = "MyValue";
            var walker    = new ConstantExpressionSyntaxEvaluator <string>();
            var exp       = SyntaxTreeHelper.GetExpressionSyntax($@"""{textValue}""");

            var value = walker.Visit(exp);

            Assert.Equal(textValue, value);
        }
        public void PropertyTypeWriterTest(string patternImplFieldType, string expectedGeneratedFieldName)
        {
            var pw = SetupPropertyWriter(PatternPropType, PatternPropName, (DeclPropType1, DeclPropName1));

            var implPatternFieldNode = SyntaxTreeHelper.GetFieldSyntax(patternImplFieldType, PatternFieldName);

            var generatedField = NodeWriterHelper.WriteAndAssertSingleMemberOfType <FieldDeclarationSyntax>(pw, implPatternFieldNode);

            Assert.Equal(expectedGeneratedFieldName, generatedField.Declaration.Type.ToString());
        }
        public static string Generate(INamedTypeSymbol containingType,
                                      string ownerExpression,
                                      string propertyName)
        {
            string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(containingType);

            return(Generate(accessingTypeFullName,
                            ownerExpression,
                            propertyName));
        }
        public void SimpleFieldWriterTest(string patternFieldName, string expectedImplPropName)
        {
            var pw = SetupPropertyWriter(PatternPropType, PatternPropName, (DeclPropType1, DeclPropName1));

            var implPatternPropNode = SyntaxTreeHelper.GetFieldSyntax(PatternPropType, patternFieldName);

            var fieldProperty = NodeWriterHelper.WriteAndAssertSingleMemberOfType <FieldDeclarationSyntax>(pw, implPatternPropNode);

            Assert.Equal(expectedImplPropName, fieldProperty.Declaration.Variables.Single().Identifier.Text);
            Assert.Equal(DeclPropType1, fieldProperty.Declaration.Type.ToString());
        }
        public void SimplePropertyWriterTest(string type, string name, string genType, string genName)
        {
            var srw = SetupStringReplaceWriter("PatternClassName", "NewClassString");

            var implPatternPropNode = SyntaxTreeHelper.GetPropertyImplSyntax(type, name);

            var generatedProperty = NodeWriterHelper.WriteAndAssertSingleMemberOfType <PropertyDeclarationSyntax>(srw, implPatternPropNode);

            Assert.Equal(genName, generatedProperty.Identifier.Text);
            Assert.Equal(genType, generatedProperty.Type.ToString());
        }
        public void SimplePropertyWriterTest()
        {
            var pw = SetupPropertyWriter(PatternPropType, PatternPropName, (DeclPropType1, DeclPropName1));

            var implPatternPropNode = SyntaxTreeHelper.GetPropertyImplSyntax(PatternPropType, PatternPropName);

            var generatedProperty = NodeWriterHelper.WriteAndAssertSingleMemberOfType <PropertyDeclarationSyntax>(pw, implPatternPropNode);

            Assert.Equal(DeclPropName1, generatedProperty.Identifier.Text);
            Assert.Equal(DeclPropType1, generatedProperty.Type.ToString());
        }
Esempio n. 12
0
        public static void ProcessClass(ClassDeclarationSyntax classDeclaration, NamespaceDeclarationSyntax namespaceDeclaration)
        {
            var nativeImplementationAttribute =
                SyntaxTreeHelper.GetNativeImplementationAttribute(classDeclaration);

            if (nativeImplementationAttribute.HasValue)
            {
                ProcessNativeImplementationClass(classDeclaration, namespaceDeclaration);
                return;
            }
            ProcessOrdinaryClass(classDeclaration, namespaceDeclaration);
        }
        public void MultiPropertyWriterTest()
        {
            var srw = SetupStringReplaceWriter("PatternClassName", new[] { "NewClassString1", "NewClassString2" });

            var implPatternPropNode = SyntaxTreeHelper.GetPropertyImplSyntax("object", "PatternClassName");

            var generatedProperties = NodeWriterHelper.WriteAndAssertMultiMemberOfType <PropertyDeclarationSyntax>(srw, implPatternPropNode);

            Assert.Equal(2, generatedProperties.Count);
            Assert.Equal("NewClassString1", generatedProperties[0].Identifier.Text);
            Assert.Equal("NewClassString2", generatedProperties[1].Identifier.Text);
        }
Esempio n. 14
0
        public static string Generate(INamedTypeSymbol containingType,
                                      string ownerExpression,
                                      string methodName,
                                      List <string> parameterExpressions)
        {
            string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(containingType);

            return(Generate(accessingTypeFullName,
                            ownerExpression,
                            methodName,
                            parameterExpressions));
        }
        public void PropertyWithAccessorsWriterTest(string patternFieldName, string implPropName)
        {
            var pw = SetupPropertyWriter(PatternPropType, PatternPropName, (DeclPropType1, DeclPropName1));

            var implPatternPropNode = SyntaxTreeHelper.GetPropertyImplSyntax(PatternPropType, PatternPropName, patternFieldName);

            var generatedProperty = NodeWriterHelper.WriteAndAssertSingleMemberOfType <PropertyDeclarationSyntax>(pw, implPatternPropNode);

            Assert.Equal(DeclPropName1, generatedProperty.Identifier.Text);
            Assert.Equal(DeclPropType1, generatedProperty.Type.ToString());
            Assert.Contains(implPropName, generatedProperty.AccessorList.ToFullString(), StringComparison.InvariantCulture);
            Assert.DoesNotContain(patternFieldName, generatedProperty.AccessorList.ToFullString(), StringComparison.InvariantCulture);
        }
Esempio n. 16
0
        public static bool IsCustom(INamedTypeSymbol leftOperandType, string operatorString)
        {
            string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(leftOperandType);

            if (accessingTypeFullName == "System.String")
            {
                if (operatorString == "==")
                {
                    return(true);
                }
            }
            return(false);
        }
        public void UnknownDeclarationUseLoadingTest()
        {
            var walker = SetupDeclarationUseWalker();

            var node = SyntaxTreeHelper.GetTypeSyntax("Unknown");

            var declarationUse = walker.Visit(node);

            Assert.NotNull(declarationUse);
            Assert.IsType <UnknownDeclarationUse>(declarationUse);
            Assert.NotNull(declarationUse.Declaration);
            Assert.IsType <UnknownDeclaration>(declarationUse.Declaration);
        }
        public void SimpleVariableStatementWriterTest(string patternVariableName, string implPropName)
        {
            var pw = SetupPropertyWriter(PatternPropType, PatternPropName, (DeclPropType1, DeclPropName1));

            var implPatternVariableNode = SyntaxTreeHelper.GetLocalDeclarationStatementSyntax(
                PatternPropType, $"{patternVariableName}Version", $"this.{patternVariableName}.Version");

            var variableProperty = NodeWriterHelper.WriteAndAssertSingleMemberOfType <FieldDeclarationSyntax>(
                pw, implPatternVariableNode);

            Assert.Equal($"{implPropName}Version", variableProperty.Declaration.Variables.Single().Identifier.Text);
            Assert.Equal($"this.{implPropName}.Version", variableProperty.Declaration.Variables.Single().Initializer.Value.ToString());
        }
Esempio n. 19
0
        private void Generate(ClassDeclarationSyntax classDeclaration, SemanticModel semanticModel, string outputPath)
        {
            var nativeImplementationAttribute =
                SyntaxTreeHelper.GetNativeImplementationAttribute(classDeclaration);

            if (!nativeImplementationAttribute.HasValue)
            {
                GenerateOrdinaryClass(classDeclaration, semanticModel, outputPath);
            }
            else
            {
                ProcessNativeImplementationClass(classDeclaration, semanticModel, outputPath);
            }
        }
Esempio n. 20
0
        public static string Generate(INamedTypeSymbol accessingType,
                                      string accessingExpression,
                                      string generatedArgument,
                                      ElementAccessExpressionSyntax elementAccessExpressionSyntax,
                                      SemanticModel semanticModel)
        {
            string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(accessingType);

            if (accessingTypeFullName == "System.String")
            {
                return(accessingExpression + ".charAt(" + generatedArgument + ")");
            }
            throw new ArgumentException();
        }
Esempio n. 21
0
        private bool IsExceptionType(INamedTypeSymbol typeSymbol)
        {
            string baseExceptionType = "System.Exception";

            while (typeSymbol != null)
            {
                string typeFullName = SyntaxTreeHelper.GetFullyQualifiedName(typeSymbol);
                if (typeFullName == baseExceptionType)
                {
                    return(true);
                }
                typeSymbol = typeSymbol.BaseType;
            }
            return(false);
        }
        public void SimpleMethodWriterTest(
            string patternMethodName,
            string patternMethodArgumentName,
            string expectedImplMethodName,
            string expectedArgName)
        {
            var pw = SetupPropertyWriter(PatternPropType, PatternPropName, (DeclPropType1, DeclPropName1));

            var implPatternMethodNode = SyntaxTreeHelper.GetMethodSyntax(PatternPropType, patternMethodName, patternMethodArgumentName, ", string otherArg");

            var methodProperty = NodeWriterHelper.WriteAndAssertSingleMemberOfType <MethodDeclarationSyntax>(pw, implPatternMethodNode);

            Assert.Equal(expectedImplMethodName, methodProperty.Identifier.Text);
            Assert.Equal(expectedArgName, methodProperty.ParameterList.Parameters.First().Identifier.Text);
            Assert.Equal(DeclPropType1, methodProperty.ReturnType.ToString());
        }
        public void ExpressionBodyPropertyWriterTest()
        {
            var pw = SetupPropertyWriter(PatternPropType, PatternPropName, (DeclPropType1, DeclPropName1));

            var implPatternPropNode = SyntaxTreeHelper.GetExpressionBodyPropertyImplSyntax(
                PatternPropType,
                PatternPropName,
                $"this.{PatternFieldName}");

            var generatedProperty = NodeWriterHelper
                                    .WriteAndAssertSingleMemberOfType <PropertyDeclarationSyntax>(pw, implPatternPropNode);

            Assert.Equal(DeclPropName1, generatedProperty.Identifier.Text);
            Assert.Equal(DeclPropType1, generatedProperty.Type.ToString());
            Assert.NotNull(generatedProperty.ExpressionBody);
            Assert.Equal($"=> this.{DeclFieldName1}", generatedProperty.ExpressionBody.ToString());
        }
Esempio n. 24
0
        public static string Generate(INamedTypeSymbol leftOperandType,
                                      string leftExpression,
                                      string operatorString,
                                      string rightExpression,
                                      SemanticModel semanticModel)
        {
            string accessingTypeFullName = SyntaxTreeHelper.GetFullyQualifiedName(leftOperandType);

            if (accessingTypeFullName == "System.String")
            {
                if (operatorString == "==")
                {
                    return("by.besmart.cross.SystemStringUtils.equals(" + leftExpression + ", " + rightExpression + ")");
                }
            }
            throw new ArgumentException();
        }
        public void GenericParameterDeclarationUseLoadingTest()
        {
            var declarationUseStatement = "TName";
            IGenericParameterDeclaration genericParameterDeclaration = null;

            var walker = SetupDeclarationUseWalker(ctx =>
            {
                genericParameterDeclaration = SetupGenericParameterDeclaration(ctx, declarationUseStatement);
            });

            var node = SyntaxTreeHelper.GetTypeSyntax(declarationUseStatement);

            var declarationUse = walker.Visit(node);

            Assert.NotNull(declarationUse);
            Assert.IsType <GenericParameterDeclarationUse>(declarationUse);
            Assert.Same(genericParameterDeclaration, declarationUse.Declaration);
        }
        public void MultiPropertyDeclarationWriterTest()
        {
            var pw = SetupPropertyWriter(PatternPropType, PatternPropName, (DeclPropType1, DeclPropName1), (DeclPropType2, DeclPropName2));

            var implPatternFieldNode = SyntaxTreeHelper.GetFieldSyntax(PatternPropType, PatternFieldName);

            var generatedFields = NodeWriterHelper.WriteAndAssertMultiMemberOfType <FieldDeclarationSyntax>(pw, implPatternFieldNode);

            Assert.Equal(2, generatedFields.Count);

            Assert.Equal(DeclPropType1, generatedFields[0].Declaration.Type.ToString());
            var v1 = Assert.Single(generatedFields[0].Declaration.Variables);

            Assert.Equal(DeclFieldName1, v1.Identifier.ValueText);

            Assert.Equal(DeclPropType2, generatedFields[1].Declaration.Type.ToString());
            var v2 = Assert.Single(generatedFields[1].Declaration.Variables);

            Assert.Equal(DeclFieldName2, v2.Identifier.ValueText);
        }
Esempio n. 27
0
        private void Generate(InterfaceDeclarationSyntax interfaceDeclaration, SemanticModel semanticModel, string outputPath)
        {
            INamedTypeSymbol typeInfo   = semanticModel.GetDeclaredSymbol(interfaceDeclaration);
            var fullyQualifiedNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(typeInfo);
            var genericTypeParameters   = typeInfo.TypeParameters;
            var typeReferences          = new HashSet <string>();

            var propertyDeclarations = interfaceDeclaration.DescendantNodes().OfType <PropertyDeclarationSyntax>();
            var generatedProperties  = GetProperties(semanticModel, typeReferences, propertyDeclarations, true);

            var methodDeclarations = interfaceDeclaration.DescendantNodes().OfType <MethodDeclarationSyntax>();
            var generatedMethods   = GetMethods(semanticModel, typeReferences, methodDeclarations);

            var interfaceSourceCode = TypeBuilder.BuildInterface(semanticModel,
                                                                 fullyQualifiedNameParts, genericTypeParameters,
                                                                 generatedProperties,
                                                                 generatedMethods);

            WriteToOutputIfPathPresent(outputPath, fullyQualifiedNameParts, interfaceSourceCode.MainPart);
        }
        public void GenericTypeParameterFieldPropertyWriterTest()
        {
            var itfType           = "IList<IPatternType>";
            var declType          = "IList<IDeclType>";
            var patternFieldType  = "AnyImplType<IPatternType>";
            var expectedFieldType = "AnyImplType<IDeclType>";

            var pw = SetupPropertyWriter(
                itfType,
                PatternPropName,
                TypeParamExtract,
                (declType, DeclPropName1));

            var implPatternFieldNode = SyntaxTreeHelper.GetFieldSyntax(patternFieldType, PatternFieldName);

            var generatedFields = NodeWriterHelper.WriteAndAssertMultiMemberOfType <FieldDeclarationSyntax>(pw, implPatternFieldNode);

            var field = Assert.Single(generatedFields);

            Assert.Equal(expectedFieldType, field.Declaration.Type.ToString());
        }
        public void GenericDeclarationUseLoadingTest(string declarationName, string genericParametersText, int genericParams)
        {
            var declarationMock = new Mock <IClassDeclaration>();

            var walker = SetupDeclarationUseWalker(resolverSetup: resolver =>
            {
                if (genericParams == 0)
                {
                    resolver
                    .Setup(r => r.Resolve(declarationName, It.IsAny <IDeclaration <SyntaxNode> >()))
                    .Returns(declarationMock.Object);
                    resolver
                    .Setup(r => r.Resolve(declarationName, Array.Empty <IDeclarationUse <SyntaxNode> >(), It.IsAny <IDeclaration <SyntaxNode> >()))
                    .Returns(declarationMock.Object);
                }
                else
                {
                    resolver
                    .Setup(r => r.Resolve(declarationName, It.IsAny <IReadOnlyList <IDeclarationUse <SyntaxNode> > >(), It.IsAny <IDeclaration <SyntaxNode> >()))
                    .Returns(declarationMock.Object);
                }
            });

            var node = SyntaxTreeHelper.GetTypeSyntax($"{declarationName}{genericParametersText}");

            var declarationUse = walker.Visit(node);

            Assert.NotNull(declarationUse);
            Assert.Same(declarationMock.Object, declarationUse.Declaration);

            var genericDeclarationUse = Assert.IsType <GenericDeclarationUse>(declarationUse);

            Assert.NotNull(genericDeclarationUse.GenericParameters);
            Assert.Equal(genericParams, genericDeclarationUse.GenericParameters.Count);

            foreach (var genericParameter in genericDeclarationUse.GenericParameters)
            {
                Assert.IsType <PredefinedDeclarationUse>(genericParameter);
            }
        }
        public TypeReference BuildDelegateReference(INamedTypeSymbol namedTypeSymbol, SemanticModel semanticModel)
        {
            var    fullyQualifiedPropertyTypeNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(namedTypeSymbol);
            string name     = namedTypeSymbol.Name;
            bool   isAction = name == "Action";
            bool   isFunc   = name == "Func";

            if (namedTypeSymbol.IsGenericType)
            {
                var typeParameters = new List <TypeReference>();
                var args           = namedTypeSymbol.TypeArguments.ToList();
                for (int i = 0; i < args.Count; i++)
                {
                    var typeArgument  = args[i];
                    var typeReference = GenerateTypeReference(typeArgument, semanticModel, isInGenericContext: true);
                    typeParameters.Add(typeReference);
                    if (!(isFunc && i == 0))
                    {
                        name += "T";
                    }
                }
                string typeReferenceText = TypeReferenceBuilder
                                           .BuildTypeReference(new[] { "by.besmart.cross.delegates", name }, typeParameters);
                return(new TypeReference
                {
                    Text = typeReferenceText,
                    IsReferenceType = namedTypeSymbol.IsReferenceType,
                    IsGeneric = true
                });
            }
            string typeReferenceTextNonGeneric = TypeReferenceBuilder
                                                 .BuildTypeReference(new[] { "by.besmart.cross.delegates", name }, new List <TypeReference>());

            return(new TypeReference
            {
                Text = typeReferenceTextNonGeneric,
                IsReferenceType = namedTypeSymbol.IsReferenceType
            });
        }