Esempio n. 1
0
        public override bool VisitClassDecl(Class @class)
        {
            var @object   = IsByRefParameter ? $"{ArgName}.get()" : ArgName;
            var objectRef = @class.IsInterface ? "__getObject()" : "__object";

            if (IsByRefParameter)
            {
                CheckRefOutParameter(Parameter.IsInOut);

                TypePrinter.PushContext(TypePrinterContextKind.Native);
                var typeName = Parameter.Visit(TypePrinter);
                TypePrinter.PopContext();

                var varName = JavaGenerator.GeneratedIdentifier(ArgName);
                var marshal = Parameter.IsInOut ?
                              $"new {typeName}({@object}.{objectRef})" : $"new {typeName}()";
                Before.WriteLine($"{typeName} {varName} = {marshal};");

                var marshaler = new JavaMarshalNativeToManaged(Context)
                {
                    Parameter     = Parameter,
                    ReturnVarName = $"{varName}.getValue()"
                };

                @class.Visit(marshaler);

                After.WriteLine($"{ArgName}.set({marshaler.Return});");

                Return.Write(varName);
                return(true);
            }

            Return.Write($"{ArgName} == null ? null : {ArgName}.{objectRef}");
            return(true);
        }
        public static IEnumerable <string> GetPackageNames(Declaration decl)
        {
            var namespaces = Declaration.GatherNamespaces(decl.Namespace)
                             .Where(ns => !(ns is TranslationUnit));

            var names = namespaces.Select(n => n.Name.ToLowerInvariant()).ToList();

            names.Insert(0, JavaGenerator.GetNativeLibPackageName(decl.TranslationUnit));

            return(names);
        }
Esempio n. 3
0
        public override bool VisitEnumDecl(Enumeration @enum)
        {
            if (IsByRefParameter)
            {
                HandleRefOutPrimitiveType(@enum.BuiltinType.Type, @enum);
                return(true);
            }

            var typeName = @enum.BuiltinType.Visit(TypePrinter);
            var varName  = JavaGenerator.GeneratedIdentifier(Context.ArgName);

            Context.SupportBefore.WriteLine($"{typeName} {varName} = {Context.ArgName}.getValue();");

            Context.Return.Write(varName);
            return(true);
        }
        public override bool VisitClassDecl(Class @class)
        {
            if (!VisitDeclaration(@class))
            {
                return(false);
            }

            GenerateClassSpecifier(@class);

            Write(" ");
            WriteStartBraceIndent();

            var hasNonInterfaceBase = @class.HasBaseClass && @class.BaseClass.IsGenerated &&
                                      [email protected];

            var objectIdent = JavaGenerator.GeneratedIdentifier("object");

            if ([email protected] && [email protected])
            {
                if (!hasNonInterfaceBase)
                {
                    WriteLine($"public {JavaGenerator.IntPtrType} {objectIdent};");
                    NewLine();
                }

                Write($"public {@class.Name}({JavaGenerator.IntPtrType} object) {{ ");
                WriteLine(hasNonInterfaceBase ? "super(object); }" : $"this.{objectIdent} = object; }}");
                NewLine();

                var implementsInterfaces = @class.Bases.Any(b => b.Class.IsGenerated && b.Class.IsInterface);
                if (implementsInterfaces)
                {
                    WriteLine("@Override");
                    WriteLine($"public com.sun.jna.Pointer __getObject() {{ return this.{objectIdent}; }}");
                    NewLine();
                }
            }

            VisitDeclContext(@class);
            WriteCloseBraceIndent();

            return(true);
        }
Esempio n. 5
0
        public void HandleRefOutPrimitiveType(PrimitiveType type, Enumeration @enum = null)
        {
            TypePrinter.PushContext(TypePrinterContextKind.Native);
            var typeName = Context.Parameter.Visit(TypePrinter);

            TypePrinter.PopContext();

            CheckRefOutParameter(type != PrimitiveType.String && Context.Parameter.IsInOut);

            string marshal = $"{Context.ArgName}.get()";

            var isEnum = @enum != null;

            if (isEnum)
            {
                marshal = $"{marshal}.getValue()";
            }

            if (type == PrimitiveType.Bool)
            {
                marshal = $"((byte) ({marshal} ? 1 : 0))";
            }

            if (IsReferenceIntegerType(type))
            {
                var integerTypeName = TypePrinter.VisitPrimitiveType(GetSignedIntegerType(type));
                marshal = $"({integerTypeName}){marshal}.intValue()";
            }

            var varName = JavaGenerator.GeneratedIdentifier(Context.ArgName);

            Context.SupportBefore.Write($"{typeName} {varName} = ");

            if (isEnum || type == PrimitiveType.Bool || IsReferenceIntegerType(type))
            {
                Context.SupportBefore.WriteLine($"new {typeName}({marshal});");
            }
            else
            {
                Context.SupportBefore.WriteLine($"({marshal}) != null ? new {typeName}({marshal}) : new {typeName}();");
            }

            Context.Return.Write(varName);

            var value = $"{varName}.getValue()";

            marshal = value;

            if (isEnum)
            {
                marshal = $"{@enum.Visit(TypePrinter)}.fromOrdinal({value})";
            }

            if (type == PrimitiveType.Bool)
            {
                marshal = $"{value} != 0";
            }

            if (IsReferenceIntegerType(type))
            {
                var integerTypeName = Context.Parameter.Type.Visit(TypePrinter);
                marshal = $"new {integerTypeName}({value})";
            }

            Context.SupportAfter.WriteLine($"{Context.ArgName}.set({marshal});");
        }
 public static string GetNativeLibClassName(string fileName) =>
 $"Native_{JavaGenerator.FileNameAsIdentifier(fileName)}";