Beispiel #1
0
        private static InterfaceDeclarationSyntax VisitInterfaceDeclaration(ConversionContext context, ClassOrInterfaceDeclaration javai, bool isNested = false)
        {
            string name = "I" + javai.getName();

            if (!isNested)
            {
                context.RootTypeName = name;
            }

            context.LastTypeName = name;

            var classSyntax = SyntaxFactory.InterfaceDeclaration(name);

            var typeParams = javai.getTypeParameters().ToList <TypeParameter>();

            if (typeParams != null && typeParams.Count > 0)
            {
                classSyntax = classSyntax.AddTypeParameterListParameters(typeParams.Select(i => SyntaxFactory.TypeParameter(i.getName())).ToArray());
            }

            var mods = javai.getModifiers();

            if (mods.HasFlag(Modifier.PRIVATE))
            {
                classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
            }
            if (mods.HasFlag(Modifier.PROTECTED))
            {
                classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword));
            }
            if (mods.HasFlag(Modifier.PUBLIC))
            {
                classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
            }
            if (mods.HasFlag(Modifier.FINAL))
            {
                classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.SealedKeyword));
            }

            var implements = javai.getImplements().ToList <ClassOrInterfaceType>();

            if (implements != null)
            {
                foreach (var implement in implements)
                {
                    classSyntax = classSyntax.AddBaseListTypes(SyntaxFactory.SimpleBaseType(TypeHelper.GetSyntaxFromType(implement)));
                }
            }

            var members = javai.getMembers().ToList <BodyDeclaration>();

            foreach (var member in members)
            {
                var syntax = BodyDeclarationVisitor.VisitBodyDeclarationForInterface(context, classSyntax, member);

                classSyntax = classSyntax.AddMembers(syntax);
            }

            return(classSyntax);
        }
        public static string ConvertMethodDeclaration(string java)
        {
            var javaClassDeclaration = @"
            class A
            {
                " + java + @"
            }";
            var declaration          = JavaParser.parseBodyDeclaration(javaClassDeclaration);
            var options          = new JavaConversionOptions();
            var context          = new ConversionContext(options);
            var classDeclaration = SyntaxFactory.ClassDeclaration("A");
            var statementSyntax  = BodyDeclarationVisitor.VisitBodyDeclarationForClass(context,
                                                                                       classDeclaration, (BodyDeclaration)declaration.getChildrenNodes().get(0))
                                   .NormalizeWhitespace();

            var tree = CSharpSyntaxTree.Create(statementSyntax);

            return(tree.GetText().ToString());
        }
Beispiel #3
0
        private static ClassDeclarationSyntax VisitClassDeclaration(ConversionContext context, ClassOrInterfaceDeclaration javac, bool isNested = false)
        {
            string name = javac.getName();

            if (!isNested)
            {
                context.RootTypeName = name;
            }

            context.LastTypeName = name;

            var classSyntax = SyntaxFactory.ClassDeclaration(name);

            var typeParams = javac.getTypeParameters().ToList <TypeParameter>();

            if (typeParams != null && typeParams.Count > 0)
            {
                classSyntax = classSyntax.AddTypeParameterListParameters(typeParams.Select(i => SyntaxFactory.TypeParameter(i.getName())).ToArray());
            }

            var mods = javac.getModifiers();

            if (mods.HasFlag(Modifier.PRIVATE))
            {
                classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
            }
            if (mods.HasFlag(Modifier.PROTECTED))
            {
                classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword));
            }
            if (mods.HasFlag(Modifier.PUBLIC))
            {
                classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
            }
            if (mods.HasFlag(Modifier.ABSTRACT))
            {
                classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.AbstractKeyword));
            }
            if (mods.HasFlag(Modifier.FINAL))
            {
                classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.SealedKeyword));
            }

            var extends = javac.getExtends().ToList <ClassOrInterfaceType>();

            if (extends != null)
            {
                foreach (var extend in extends)
                {
                    classSyntax = classSyntax.AddBaseListTypes(SyntaxFactory.SimpleBaseType(TypeHelper.GetSyntaxFromType(extend)));
                }
            }

            var implements = javac.getImplements().ToList <ClassOrInterfaceType>();

            if (implements != null)
            {
                foreach (var implement in implements)
                {
                    classSyntax = classSyntax.AddBaseListTypes(SyntaxFactory.SimpleBaseType(TypeHelper.GetSyntaxFromType(implement, true)));
                }
            }

            var members = javac.getMembers().ToList <BodyDeclaration>();

            foreach (var member in members)
            {
                if (member is ClassOrInterfaceDeclaration)
                {
                    var childc = (ClassOrInterfaceDeclaration)member;

                    if (childc.isInterface())
                    {
                        var childInt = VisitInterfaceDeclaration(context, childc, true);

                        classSyntax = classSyntax.AddMembers(childInt);
                    }
                    else
                    {
                        var childClass = VisitClassDeclaration(context, childc, true);

                        classSyntax = classSyntax.AddMembers(childClass);
                    }
                }
                else
                {
                    var syntax = BodyDeclarationVisitor.VisitBodyDeclarationForClass(context, classSyntax, member);
                    classSyntax = classSyntax.AddMembers(syntax);
                }

                while (context.PendingAnonymousTypes.Count > 0)
                {
                    var anon = context.PendingAnonymousTypes.Dequeue();
                    classSyntax = classSyntax.AddMembers(anon);
                }
            }

            return(classSyntax);
        }
Beispiel #4
0
        private static ExpressionSyntax VisitAnonymousClassCreationExpression(ConversionContext context, ObjectCreationExpr newExpr, List <BodyDeclaration> anonBody)
        {
            string baseTypeName = TypeHelper.ConvertType(newExpr.getType().getName());
            string anonTypeName = string.Empty;

            for (int i = 0; i <= 100; i++)
            {
                if (i == 100)
                {
                    throw new InvalidOperationException("Too many anonymous types");
                }

                anonTypeName = string.Format("Anonymous{0}{1}", baseTypeName, i == 0 ? string.Empty : i.ToString());

                if (!context.UsedAnonymousTypeNames.Contains(anonTypeName))
                {
                    context.UsedAnonymousTypeNames.Add(anonTypeName);
                    break; // go with this one
                }
            }

            var classSyntax = SyntaxFactory.ClassDeclaration(anonTypeName)
                              .AddModifiers(
                SyntaxFactory.Token(SyntaxKind.PrivateKeyword),
                SyntaxFactory.Token(SyntaxKind.SealedKeyword))
                              .WithBaseList(SyntaxFactory.BaseList(SyntaxFactory.SeparatedList(new List <BaseTypeSyntax>
            {
                SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(baseTypeName))
            })));

            var parentField = SyntaxFactory.FieldDeclaration(
                SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName(context.LastTypeName)).AddVariables(SyntaxFactory.VariableDeclarator("parent")))
                              .AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword), SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword));

            var ctorSyntax = SyntaxFactory.ConstructorDeclaration(anonTypeName)
                             .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                             .AddParameterListParameters(SyntaxFactory.Parameter(SyntaxFactory.ParseToken("parent")).WithType(SyntaxFactory.ParseTypeName(context.LastTypeName)))
                             .AddBodyStatements(SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ThisExpression(), SyntaxFactory.IdentifierName("parent")), SyntaxFactory.IdentifierName("parent"))));

            classSyntax = classSyntax.AddMembers(ctorSyntax, parentField);

            foreach (var member in anonBody)
            {
                var memberSyntax = BodyDeclarationVisitor.VisitBodyDeclarationForClass(context, classSyntax, member);
                classSyntax = classSyntax.AddMembers(memberSyntax);
            }

            context.PendingAnonymousTypes.Enqueue(classSyntax);

            var args = newExpr.getArgs().ToList <Expression>();

            if (args == null || args.Count == 0)
            {
                return(SyntaxFactory.ObjectCreationExpression(SyntaxFactory.ParseTypeName(anonTypeName))
                       .AddArgumentListArguments(SyntaxFactory.Argument(SyntaxFactory.ThisExpression())));
            }

            var argSyntaxes = new List <ArgumentSyntax>();

            argSyntaxes.Add(SyntaxFactory.Argument(SyntaxFactory.ThisExpression()));

            foreach (var arg in args)
            {
                var argSyntax = ExpressionVisitor.VisitExpression(context, arg);
                argSyntaxes.Add(SyntaxFactory.Argument(argSyntax));
            }

            return(SyntaxFactory.ObjectCreationExpression(SyntaxFactory.ParseTypeName(anonTypeName), SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(argSyntaxes, Enumerable.Repeat(SyntaxFactory.Token(SyntaxKind.CommaToken), argSyntaxes.Count - 1))), null));
        }