/// <summary>
        /// Tries to find a disassembled entity, given a disassembled target.
        /// </summary>
        /// <param name="disassemblyTarget">The disassembly target.</param>
        /// <returns></returns>
        public DisassembledEntity FindDisassembledEntity(DisassemblyTarget disassemblyTarget)
        {
            //  If there's no target, we can't find anything.
            if (disassemblyTarget == null)
            {
                return(null);
            }

            switch (disassemblyTarget.TargetType)
            {
            case DisassemblyTargetType.Class:

                //  Find the class with the given name.
                return(AllClasses.FirstOrDefault(c => c.FullName == disassemblyTarget.FullName));

            case DisassemblyTargetType.Enumeration:

                //  Find the enumeration with the given name.
                return(AllEnumerations.FirstOrDefault(c => c.FullName == disassemblyTarget.FullName));

            case DisassemblyTargetType.Method:

                //  Find the entity with the given name.
                return(AllMethods.FirstOrDefault(c => c.FullName == disassemblyTarget.FullName));

            case DisassemblyTargetType.Property:

                //  Find the entity with the given name.
                return(AllProperties.FirstOrDefault(c => c.FullName == disassemblyTarget.FullName));

            case DisassemblyTargetType.Field:

                //  Find the entity with the given name.
                return(AllFields.FirstOrDefault(c => c.FullName == disassemblyTarget.FullName));

            case DisassemblyTargetType.Structure:

                //  Find the structure with the given name.
                return(AllStructures.FirstOrDefault(c => c.FullName == disassemblyTarget.FullName));

            case DisassemblyTargetType.Interface:

                //  Find the structure with the given name.
                return(AllInterfaces.FirstOrDefault(c => c.FullName == disassemblyTarget.FullName));

            case DisassemblyTargetType.Event:

                //  Find the structure with the given name.
                return(AllEvents.FirstOrDefault(c => c.FullName == disassemblyTarget.FullName));

            case DisassemblyTargetType.Delegate:

                //  Find the structure with the given name.
                return(AllDelegates.FirstOrDefault(c => c.FullName == disassemblyTarget.FullName));


            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Traverses the interface inheritance tree and collects all found interfaces.
        /// </summary>
        /// <param name="Interface">The starting innterface.</param>
        /// <param name="AllInterfaces">A list of all interfaces found.</param>
        public static IEnumerable <Type> GetRecursiveInterfaces(this Type Interface, List <Type> AllInterfaces = null)
        {
            if (AllInterfaces == null)
            {
                AllInterfaces = new List <Type>();
            }

            AllInterfaces.Add(Interface);

            var BaseInterfaces = Interface.GetInterfaces();

            if (BaseInterfaces != null && BaseInterfaces.Count() > 0)
            {
                foreach (var BaseInterface in BaseInterfaces)
                {
                    GetRecursiveInterfaces(BaseInterface, AllInterfaces);
                }
            }

            return(AllInterfaces);
        }
        public void Execute(GeneratorExecutionContext context)
        {
            var cancellationToken = context.CancellationToken;
            var compilation       = context.Compilation;
            Action <Diagnostic> reportDiagnostic = context.ReportDiagnostic;

            if (!WellKnownTypes.TryCreate(compilation, reportDiagnostic, out var wellKnownTypes))
            {
                return;
            }

            var registrationCalculator =
                new RegistrationCalculator(compilation, wellKnownTypes, reportDiagnostic, cancellationToken);

            foreach (var syntaxTree in context.Compilation.SyntaxTrees)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var semanticModel = context.Compilation.GetSemanticModel(syntaxTree);
                var modules       = syntaxTree.GetRoot(cancellationToken).DescendantNodesAndSelf()
                                    .OfType <ClassDeclarationSyntax>()
                                    .Select(x => semanticModel.GetDeclaredSymbol(x, cancellationToken))
                                    .Where(x => x != null)
                                    .Select(x =>
                {
                    var isContainer = x !.AllInterfaces.Any(x
                                                            => x.OriginalDefinition.Equals(wellKnownTypes.IContainer, SymbolEqualityComparer.Default) ||
                                                            x.OriginalDefinition.Equals(wellKnownTypes.IAsyncContainer,
                                                                                        SymbolEqualityComparer.Default));
                    return(type: x, isContainer);
                })
                                    .Where(x =>
                                           x.isContainer ||
                                           x.type.GetAttributes().Any(x =>
                                                                      x.AttributeClass is { } attribute&&
                                                                      (attribute.Equals(wellKnownTypes.RegisterAttribute, SymbolEqualityComparer.Default) ||
                                                                       attribute.Equals(wellKnownTypes.RegisterModuleAttribute, SymbolEqualityComparer.Default) ||
                                                                       attribute.Equals(wellKnownTypes.RegisterFactoryAttribute,
                                                                                        SymbolEqualityComparer.Default) ||
                                                                       attribute.Equals(wellKnownTypes.RegisterDecoratorAttribute,
                                                                                        SymbolEqualityComparer.Default))) ||
                                           x.type.GetMembers().Any(x => x.GetAttributes().Any(x =>
                                                                                              x.AttributeClass is { } attribute&&
                                                                                              (attribute.Equals(wellKnownTypes.FactoryAttribute, SymbolEqualityComparer.Default) ||
                                                                                               attribute.Equals(wellKnownTypes.InstanceAttribute, SymbolEqualityComparer.Default) ||
                                                                                               attribute.Equals(wellKnownTypes.DecoratorFactoryAttribute,
                                                                                                                SymbolEqualityComparer.Default) ||
                                                                                               attribute.Equals(wellKnownTypes.FactoryOfAttribute, SymbolEqualityComparer.Default)))));

                foreach (var module in modules)
                {
                    if (!module.type.IsInternal() && !module.type.IsPublic())
                    {
                        reportDiagnostic(ModuleNotPublicOrInternal(
                                             module.type,
                                             ((TypeDeclarationSyntax)module.type.DeclaringSyntaxReferences[0].GetSyntax()).Identifier
                                             .GetLocation()));
                    }

                    cancellationToken.ThrowIfCancellationRequested();
                    if (module.isContainer)
                    {
                        var file = ContainerGenerator.GenerateContainerImplementations(
                            module.type,
                            registrationCalculator.GetContainerRegistrations(module.type),
                            wellKnownTypes,
                            reportDiagnostic,
                            cancellationToken);

                        var source = CSharpSyntaxTree.ParseText(SourceText.From(file, Encoding.UTF8)).GetRoot()
                                     .NormalizeWhitespace().SyntaxTree.GetText();
                        context.AddSource(
                            GenerateNameHint(module.type),
                            source);
                    }
                    else
                    {
                        registrationCalculator.ValidateModuleRegistrations(module.type);
                    }
                }
            }
        }
Beispiel #4
0
        public void UpdateNames()
        {
            foreach (GType ifc in Interfaces)
            {
                foreach (GType inIfc in ifc.AllInterfaces)
                {
                    if (!AllInterfaces.Contains(inIfc))
                    {
                        AllInterfaces.Add(inIfc);
                    }
                }
            }
            if (IsCLRType)
            {
                CLRNamespace = CLRType.Namespace;
                Name         = CLRType.Name;
                if (!IsJVMType && JVMFullName == null)
                {
                    JVMNamespace = CLRNamespace.ToLowerInvariant();

                    if (CLRType.IsGenericType)
                    {
                        bool rref = typeof(Ref <>).IsAssignableFrom(CLRType.GetGenericTypeDefinition());
                        bool oout = typeof(Out <>).IsAssignableFrom(CLRType.GetGenericTypeDefinition());
                        if (rref || oout)
                        {
                            JVMFullName = JVMNamespace + (rref ? ".Ref<" : ".Out<");
                            Type[] genericArguments = CLRType.GetGenericArguments();
                            for (int i = 0; i < genericArguments.Length; i++)
                            {
                                Type argument = genericArguments[i];

                                if (argument.IsPrimitive)
                                {
                                    String objName = Repository.jvmPrimitives[argument].getName();
                                    JVMFullName += objName;
                                }
                                else
                                {
                                    GType real = Repository.RegisterType(argument);
                                    real.UpdateNames();
                                    JVMFullName += real.JVMResolved;
                                }
                                if (i + 1 < genericArguments.Length)
                                {
                                    JVMFullName += ",";
                                }
                            }
                            JVMFullName += ">";
                        }
                    }
                    else
                    {
                        JVMFullName = JVMNamespace + "." + CLRType.Name;
                    }
                }
            }
            if (IsJVMType)
            {
                JVMNamespace = JVMType.PackageName;
                Name         = JVMType.ShortName;
                if (!IsCLRType)
                {
                    CLRNamespace = JVMNamespace;

                    if (IsArray)
                    {
                        CLRFullName = JVMType.getComponentType().getName() + "[]";
                    }
                    else
                    {
                        CLRFullName = JVMType.FullName;
                    }
                }
            }
            JVMNamespaceExt = JVMNamespace;
            CLRNamespaceExt = CLRNamespace;
            if (JVMNamespace.StartsWith("java."))
            {
                JVMNamespaceExt = "java_." + JVMNamespace.Substring(5);
            }

            /* TODO
             * if (IsJVMGenerate)
             * {
             *  if (Base!=null && !Base.IsJVMType && !Base.IsJVMGenerate)
             *  {
             *      Console.WriteLine("you should add " + Base.Name);
             *  }
             *  foreach (GType ifc in Interfaces)
             *  {
             *      if (!ifc.IsJVMType && !ifc.IsJVMGenerate)
             *      {
             *          Console.WriteLine("you should add " + ifc.Name);
             *      }
             *  }
             * }
             *
             * if (IsCLRGenerate && CLRType!=typeof(IClrProxy))
             * {
             *  if (Base != null && !Base.IsCLRType && !Base.IsCLRGenerate)
             *  {
             *      Console.WriteLine("you should add " + Base.Name);
             *  }
             *  foreach (GType ifc in Interfaces)
             *  {
             *      if (!ifc.IsCLRType && !ifc.IsCLRGenerate)
             *      {
             *          Console.WriteLine("you should add " + ifc.Name);
             *      }
             *  }
             * }*/
        }