protected override Expression VisitTypeDefinitionExpression(TypeDefinitionExpression expression)
        {
            var includeExpressions = new List<Expression>
            {
                FickleExpression.Include(expression.Type.Name + ".h")
            };

            var comment = new CommentExpression("This file is AUTO GENERATED");

            var referencedTypes = ReferencedTypesCollector.CollectReferencedTypes(expression);
            referencedTypes.Sort((x, y) => String.Compare(x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase));

            foreach (var referencedType in referencedTypes.Where(ObjectiveBinderHelpers.TypeIsServiceClass))
            {
                includeExpressions.Add(FickleExpression.Include(referencedType.Name + ".h"));
            }

            var headerGroup = includeExpressions.ToStatementisedGroupedExpression();
            var header = new Expression[] { comment, headerGroup }.ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide);

            var methods = new List<Expression>
            {
                this.CreateInitMethod(expression),
                this.CreateAllPropertiesAsDictionaryMethod(expression),
                this.CreateScalarPropertiesAsFormEncodedStringMethod(expression),
                this.CreateCopyWithZoneMethod(expression),
            };

            var body = methods.ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide);

            return new TypeDefinitionExpression(expression.Type, header, body, false, expression.Attributes, expression.InterfaceTypes);
        }
Example #2
0
        protected override Expression VisitTypeDefinitionExpression(TypeDefinitionExpression expression)
        {
            try
            {
                currentTypeDefinition = expression;
                currentType = expression.Type;

                enumValueField = new FieldDefinitionExpression("value", typeof(int), AccessModifiers.Private | AccessModifiers.Constant);

                var includeExpressions = new List<Expression>()
                {
                    FickleExpression.Include("android.util.JsonReader"),
                    FickleExpression.Include("com.jaigo.androiddevkit.DefaultJsonBuilder"),
                    FickleExpression.Include("java.util.ArrayList")
                };

                var referencedTypes = ReferencedTypesCollector.CollectReferencedTypes(expression);
                referencedTypes.Sort((x, y) => String.Compare(x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase));

                if (!codeGenerationContext.Options.SerializeEnumsAsStrings)
                {
                    includeExpressions.Add(FickleExpression.Include("com.jaigo.androiddevkit.utils.ConvertUtils"));
                }

                var includeStatements = includeExpressions.ToStatementisedGroupedExpression();

                var comment = new CommentExpression("This file is AUTO GENERATED");
                var namespaceExpression = new NamespaceExpression(codeGenerationContext.Options.Namespace);
                var header = new Expression[] { comment, namespaceExpression, includeStatements }.ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide);

                var bodyExpressions = new List<Expression>()
                {
                    expression.Body,
                    enumValueField,
                    CreateConstructor(),
                    CreateDeserializeMethod(),
                    CreateDeserializeArrayMethod(),
                    CreateSerializeMethod(),
                    JavaBinderHelpers.CreateSerializeArrayMethod(currentType)
                };

                var body = new GroupedExpressionsExpression(bodyExpressions);

                return new TypeDefinitionExpression(expression.Type, header, body, false, expression.Attributes, expression.InterfaceTypes);
            }
            finally
            {
                currentTypeDefinition = null;
            }
        }
        protected override Expression VisitTypeDefinitionExpression(TypeDefinitionExpression expression)
        {
            var comment = new CommentExpression("This file is AUTO GENERATED");

            var includeExpressions = new List<IncludeExpression>
            {
                FickleExpression.Include("System"),
                FickleExpression.Include("System.Collections.Generic")
            };

            foreach (var include in this.codeGenerationContext.Options.Includes)
            {
                includeExpressions.Add(FickleExpression.Include(include));
            }

            var headerGroup = includeExpressions.ToStatementisedGroupedExpression();
            var header = new Expression[] { comment, headerGroup }.ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide);

            var typeDefinitionExpression = new TypeDefinitionExpression(expression.Type, null, expression.Body, false, expression.Attributes, expression.InterfaceTypes);

            return new NamespaceExpression(this.codeGenerationContext.Options.Namespace, header, typeDefinitionExpression);
        }
        protected override Expression VisitTypeDefinitionExpression(TypeDefinitionExpression expression)
        {
            var includeExpressions = new List<IncludeExpression>();
            var importExpressions = new List<Expression>();
            var referencedTypes = ReferencedTypesCollector.CollectReferencedTypes(expression);
            referencedTypes.Sort((x, y) => String.Compare(x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase));

            var lookup = new HashSet<Type>(referencedTypes.Where(TypeSystem.IsPrimitiveType));

            if (!this.codeGenerationContext.Options.ImportDependenciesAsFramework)
            {
                if (lookup.Contains(typeof(Guid)) || lookup.Contains(typeof(Guid?)))
                {
                    includeExpressions.Add(FickleExpression.Include("PKUUID.h"));
                }

                if (lookup.Contains(typeof(TimeSpan)) || lookup.Contains(typeof(TimeSpan?)))
                {
                    includeExpressions.Add(FickleExpression.Include("PKTimeSpan.h"));
                }

                includeExpressions.Add(FickleExpression.Include("PKDictionarySerializable.h"));
                includeExpressions.Add(FickleExpression.Include("PKFormEncodingSerializable.h"));
            }
            else
            {
                importExpressions.Add(new CodeLiteralExpression(c => c.WriteLine("@import PlatformKit;")));
            }

            if (ObjectiveBinderHelpers.TypeIsServiceClass(expression.Type.BaseType))
            {
                includeExpressions.Add(FickleExpression.Include(expression.Type.BaseType.Name + ".h"));
            }

            includeExpressions.AddRange(referencedTypes.Where(c => c.IsEnum).Select(c => FickleExpression.Include(c.Name + ".h")));

            includeExpressions.Sort(IncludeExpression.Compare);

            var comment = new CommentExpression("This file is AUTO GENERATED");

            var headerExpressions = new List<Expression>()
            {
                new[] { comment }.ToStatementisedGroupedExpression(),
                importExpressions.Count == 0 ? null : importExpressions.ToStatementisedGroupedExpression(),
                includeExpressions.ToStatementisedGroupedExpression()
            };

            var referencedTypeExpressions = referencedTypes
                .Where(ObjectiveBinderHelpers.TypeIsServiceClass)
                .Where(c => c != expression.Type.BaseType)
                .OrderBy(x => x.Name.Length)
                .ThenBy(x => x.Name)
                .Select(c => (Expression)new ReferencedTypeExpression(c)).ToList();

            if (referencedTypeExpressions.Count > 0)
            {
                headerExpressions.Add(referencedTypeExpressions.ToStatementisedGroupedExpression());
            }

            var header = headerExpressions.ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide);

            var propertyBody = this.Visit(expression.Body);

            var interfaceTypes = new List<Type>
            {
                FickleType.Define("NSCopying"),
                FickleType.Define("PKDictionarySerializable"),
                FickleType.Define("PKFormEncodingSerializable")
            };

            return new TypeDefinitionExpression(expression.Type, header, propertyBody, true, expression.Attributes, interfaceTypes.ToReadOnlyCollection());
        }
        private MethodDefinitionExpression CreateInitMethod(TypeDefinitionExpression expression)
        {
            var type = expression.Type;
            Expression superInitExpression;

            var parameters = new List<Expression>
            {
                Expression.Parameter(new FickleType("NSDictionary"), "properties")
            };

            var methodBodyExpressions = new List<Expression>();

            if (type.BaseType.IsServiceType())
            {
                superInitExpression = Expression.Call(Expression.Parameter(type.BaseType, "super"), new FickleMethodInfo(type.BaseType, type, "initWithPropertyDictionary", new [] { new FickleParameterInfo(parameters[0].Type, "dictionary")}), parameters[0]);
            }
            else
            {
                superInitExpression = Expression.Call(Expression.Parameter(type.BaseType, "super"), new FickleMethodInfo(type.BaseType, type, "init", new ParameterInfo[0]));
            }

            var assignExpression = Expression.Assign(Expression.Parameter(type, "self"), superInitExpression);
            var compareToNullExpression = Expression.ReferenceEqual(assignExpression, Expression.Constant(null, type));
            int count;

            methodBodyExpressions.Add(Expression.IfThen(compareToNullExpression, Expression.Block(Expression.Return(Expression.Label(), Expression.Constant(null)).ToStatement())));
            methodBodyExpressions.Add(PropertiesFromDictionaryExpressonBinder.Bind(expression, out count));
            methodBodyExpressions.Add(Expression.Return(Expression.Label(), Expression.Parameter(type, "self")).ToStatement());

            IEnumerable<ParameterExpression> variables;

            if (count > 0)
            {
                variables = new[] { Expression.Parameter(FickleType.Define("id"), "currentValueFromDictionary") };
            }
            else
            {
                variables = new ParameterExpression[0];
            }

            var methodBody = Expression.Block(variables, (Expression)methodBodyExpressions.ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide));

            return new MethodDefinitionExpression("initWithPropertyDictionary", parameters.ToReadOnlyCollection(), typeof(object), methodBody, false, null);
        }
Example #6
0
        protected override Expression VisitTypeDefinitionExpression(TypeDefinitionExpression expression)
        {
            currentTypeDefinition = expression;
            currentType = expression.Type;
            var referencedTypes = ReferencedTypesCollector.CollectReferencedTypes(expression);
            referencedTypes.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase));

            var includeExpressions = new List<IncludeExpression>();

            var lookup = new HashSet<Type>(referencedTypes.Where(TypeSystem.IsPrimitiveType));

            if (lookup.Contains(typeof(Guid)) || lookup.Contains(typeof(Guid?)))
            {
                includeExpressions.Add(FickleExpression.Include("java.util.UUID"));
            }

            if (lookup.Contains(typeof(TimeSpan)) || lookup.Contains(typeof(TimeSpan?)))
            {
                includeExpressions.Add(FickleExpression.Include("java.util.Date"));
            }

            var comment = new CommentExpression("This file is AUTO GENERATED");
            var namespaceExpression = new NamespaceExpression(codeGenerationContext.Options.Namespace);

            var members = new List<Expression>() { this.Visit(expression.Body) };

            if (((FickleType) currentTypeDefinition.Type).ServiceClass.Properties.Count > 0)
            {
                includeExpressions.Add(FickleExpression.Include("android.util.JsonReader"));
                includeExpressions.Add(FickleExpression.Include("android.util.JsonToken"));
                includeExpressions.Add(FickleExpression.Include("com.jaigo.androiddevkit.*"));
                includeExpressions.Add(FickleExpression.Include("com.jaigo.androiddevkit.utils.*"));
                includeExpressions.Add(FickleExpression.Include("java.lang.Exception"));
                includeExpressions.Add(FickleExpression.Include("java.io.InputStream"));
                includeExpressions.Add(FickleExpression.Include("java.io.InputStreamReader"));
                includeExpressions.Add(FickleExpression.Include("java.util.ArrayList"));

                members.Add(CreateDeserializeStreamMethod());
                members.Add(CreateDeserializeReaderMethod());
                members.Add(CreateDeserializeElementMethod());
                members.Add(CreateSerializeMethod());
            }

            if (CurrentTypeIsResponseType())
            {
                members.Add(CreateCreateErrorResponseMethod());
            }

            var headerGroup = includeExpressions.ToStatementisedGroupedExpression();
            var header = new Expression[] { comment, namespaceExpression, headerGroup }.ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide);

            var body = fieldDefinitionsForProperties.Concat(members).ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide);

            return new TypeDefinitionExpression(expression.Type, header, body, false, expression.Attributes, expression.InterfaceTypes);
        }
        protected override Expression VisitTypeDefinitionExpression(TypeDefinitionExpression expression)
        {
            var includeExpressions = new List<IncludeExpression>();
            var importExpressions = new List<Expression>();

            var optionsProperty = new PropertyDefinitionExpression("options", FickleType.Define("NSDictionary"), true);
            var responseFilterProperty = new PropertyDefinitionExpression("responseFilter", FickleType.Define("FKGatewayResponseFilter", isInterface:true), true, new[] { "weak" });

            var body = FickleExpression.GroupedWide
            (
                optionsProperty,
                responseFilterProperty,
                new GroupedExpressionsExpression(methods.Select(c => c.ChangePredeclaration(true)))
            );

            var referencedTypes = ReferencedTypesCollector.CollectReferencedTypes(body);
            referencedTypes.Sort((x, y) => String.Compare(x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase));

            var lookup = new HashSet<Type>(referencedTypes.Where(TypeSystem.IsPrimitiveType));

            if (!this.CodeGenerationContext.Options.ImportDependenciesAsFramework)
            {
                if (lookup.Contains(typeof(Guid)) || lookup.Contains(typeof(Guid?)))
                {
                    includeExpressions.Add(FickleExpression.Include("PKUUID.h"));
                }

                if (lookup.Contains(typeof(TimeSpan)) || lookup.Contains(typeof(TimeSpan?)))
                {
                    includeExpressions.Add(FickleExpression.Include("PKTimeSpan.h"));
                }

                includeExpressions.Add(FickleExpression.Include("PKWebServiceClient.h"));
                includeExpressions.Add(FickleExpression.Include("PKDictionarySerializable.h"));
            }
            else
            {
                importExpressions.Add(new CodeLiteralExpression(c => c.WriteLine("@import PlatformKit;")));
            }

            includeExpressions.Add(FickleExpression.Include("FKGatewayResponseFilter.h"));

            var referencedUserTypes = referencedTypes
                .Where(c => (c is FickleType && ((FickleType)c).ServiceClass != null) || c is FickleType && ((FickleType)c).ServiceEnum != null)
                .Sorted((x, y) => x.Name.Length == y.Name.Length ? String.CompareOrdinal(x.Name, y.Name) : x.Name.Length - y.Name.Length);

            includeExpressions.AddRange(referencedUserTypes.Select(c => FickleExpression.Include(c.Name + ".h")));

            var comment = new CommentExpression("This file is AUTO GENERATED");

            var header = new Expression[]
            {
                comment,
                importExpressions.Count == 0 ? null : importExpressions.ToStatementisedGroupedExpression(),
                includeExpressions.Sorted(IncludeExpression.Compare).ToStatementisedGroupedExpression()
            }.ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide);

            var interfaceTypes = new List<Type>();

            if (expression.InterfaceTypes != null)
            {
                interfaceTypes.AddRange(expression.InterfaceTypes);
            }

            interfaceTypes.Add(FickleType.Define("PKWebServiceClientDelegate"));

            return new TypeDefinitionExpression(expression.Type, header, body, true, expression.Attributes, interfaceTypes.ToReadOnlyCollection());
        }
        protected override Expression VisitPropertyDefinitionExpression(PropertyDefinitionExpression property)
        {
            var dictionaryType = new FickleType("NSDictionary");
            var currentValueFromDictionary = Expression.Parameter(typeof(object), "currentValueFromDictionary");
            var objectForKeyCall = Expression.Call(Expression.Parameter(dictionaryType, "properties"), new FickleMethodInfo(dictionaryType, typeof(object), "objectForKey", new ParameterInfo[] { new FickleParameterInfo(typeof(string), "key") }), Expression.Constant(property.PropertyName));
            var propertyExpression = Expression.Property(Expression.Parameter(this.type, "self"), new FicklePropertyInfo(this.type, property.PropertyType, property.PropertyName));

            var expressions = new List<Expression>
            {
                FickleExpression.Comment(property.PropertyName),
                Expression.Assign(currentValueFromDictionary, objectForKeyCall).ToStatement(),
                GetDeserializeExpressionProcessValueDeserializer(property.PropertyType, currentValueFromDictionary, c => Expression.Assign(propertyExpression, c).ToStatement())
            };

            this.propertyGetterExpressions.Add(expressions.ToStatementisedGroupedExpression(GroupedExpressionsExpressionStyle.Wide));

            return property;
        }