Exemplo n.º 1
0
        public BasedOnRegistration(ITreeNode registrationRootElement, ITypeElement basedOnElement, IEnumerable<WithServiceRegistration> withServices)
            : base(registrationRootElement, withServices)
        {
            this.basedOnElement = basedOnElement;

            name = basedOnElement.GetClrName().FullName;
        }
        public ContextElement CreateContext(ITypeElement type)
        {
            if (_cache.Classes.ContainsKey(type))
            {
                return(_cache.Classes[type]);
            }

            var context = GetOrCreateContextElement(_provider,
#if RESHARPER_61
                                                    _manager,
                                                    _psiModuleManager,
                                                    _cacheManager,
#endif
                                                    _project,
                                                    _projectEnvoy,
#if RESHARPER_6
                                                    type.GetClrName().FullName,
#else
                                                    type.CLRName,
#endif
                                                    _assemblyPath,
                                                    type.GetSubjectString(),
                                                    type.GetTags(),
                                                    type.IsIgnored());

#if RESHARPER_6
            foreach (var child in context.Children)
            {
                child.State = UnitTestElementState.Pending;
            }
#endif

            _cache.Classes.Add(type, context);
            return(context);
        }
        public override string ToString()
        {
            string displayName = Implementation != null?Implementation.GetClrName().FullName
                                 : serviceType.GetClrName().FullName;

            return(string.Format("Implemented by: {0}", displayName));
        }
Exemplo n.º 4
0
        private static void FixQualifierExpression([NotNull] ITextControl textControl, [NotNull] ICSharpExpression expression, [NotNull] ITypeElement ownerType)
        {
            var qualifierRange = expression.GetDocumentRange().TextRange;

            var comparer = DeclaredElementEqualityComparer.TypeElementComparer;

            // do not produce type qualifier when static method from containing type completed
            var typeDeclaration = expression.GetContainingTypeDeclaration();

            if (typeDeclaration != null && comparer.Equals(typeDeclaration.DeclaredElement, ownerType))
            {
                var reference = ReferenceExpressionNavigator.GetByQualifierExpression(expression).NotNull("reference != null");

                var delimiter = reference.Delimiter;
                if (delimiter != null)
                {
                    qualifierRange = qualifierRange.JoinRight(delimiter.GetDocumentRange().TextRange);
                }

                textControl.Document.ReplaceText(qualifierRange, string.Empty);
            }
            else
            {
                var keyword = CSharpTypeFactory.GetTypeKeyword(ownerType.GetClrName());
                textControl.Document.ReplaceText(qualifierRange, keyword ?? "T");
            }
        }
Exemplo n.º 5
0
        public BasedOnRegistration(ITreeNode registrationRootElement, ITypeElement basedOnElement)
            : base(registrationRootElement)
        {
            this.basedOnElement = basedOnElement;

            name = basedOnElement.GetClrName().FullName;
        }
    public ContextElement CreateContext(ITypeElement type)
    {
      if (_cache.Classes.ContainsKey(type))
      {
        return _cache.Classes[type];
      }

      var context = GetOrCreateContextElement(_provider,
#if RESHARPER_61
                                              _manager,
                                              _psiModuleManager,
                                              _cacheManager,
#endif
                                              _project,
                                              _projectEnvoy,
                                              type.GetClrName().FullName,
                                              _assemblyPath,
                                              type.GetSubjectString(),
                                              type.GetTags(),
                                              type.IsIgnored());

      foreach (var child in context.Children)
      {
        child.State = UnitTestElementState.Pending;
      }

      _cache.Classes.Add(type, context);
      return context;
    }
Exemplo n.º 7
0
        private MethodSignature[] GetExpectedMethodSignatures(ITypeElement attributeTypeElement,
                                                              PredefinedType predefinedType)
        {
            var attributeClrName = attributeTypeElement.GetClrName();

            if (myMethodSignatures.TryGetValue(attributeClrName, out var signatures))
            {
                if (signatures.All(s => s.IsValid()))
                {
                    return(signatures);
                }

                // If any of the signatures are no longer valid, clear the cache and try again
                myMethodSignatures.Clear();
            }

            // Try to get the expected signature from a method marked with RequiredSignatureAttribute (introduced in
            // Unity 2017.3). If the attribute does not exist, either because it's not applied, or we're on an earlier
            // version, check our known attributes for a signature
            // Remember that we're run on pooled threads, so try to add to the concurrent dictionary. It doesn't matter
            // if another thread beats us.
            signatures = GetSignaturesFromRequiredSignatureAttribute(attributeTypeElement)
                         ?? GetSignaturesFromKnownAttributes(attributeClrName, predefinedType);
            if (signatures != null)
            {
                myMethodSignatures.TryAdd(attributeClrName.GetPersistent(), signatures);
            }
            return(signatures);
        }
Exemplo n.º 8
0
 public static string GetFullClrName([NotNull] this ITypeElement typeElement)
 {
     if (typeElement == null)
     {
         throw new ArgumentNullException("typeElement");
     }
     return(typeElement.GetClrName().FullName);
 }
Exemplo n.º 9
0
        public static bool IsEnumerableClass([CanBeNull] this ITypeElement type)
        {
            if (type == null)
            {
                return(false);
            }
            var typeName = type.GetClrName();

            return(typeName.Equals(PredefinedType.ENUMERABLE_CLASS));
        }
Exemplo n.º 10
0
        public override bool IsSatisfiedBy(ITypeElement typeElement)
        {
            var attributesOwner = typeElement as IAttributesOwner;

            if (attributesOwner == null)
            {
                return(false);
            }

            return(attributesOwner.HasAttributeInstance(attributeType.GetClrName(), true));
        }
Exemplo n.º 11
0
        private static string GetDisplayName(ITypeElement element)
        {
            IClrTypeName clrName = element.GetClrName();

            string fullName = string.Format("{0}.{1}", clrName.GetNamespaceName(), clrName.ShortName);

            if (element.HasTypeParameters())
            {
                fullName = string.Format("{0}<>", fullName);
            }

            return fullName;
        }
Exemplo n.º 12
0
        private static void GetPossibleSourceNames(ITypeElement type, ISet <string> names)
        {
            names.Add(type.ShortName);

            var typeShortName = type.ShortName;

            names.Add(typeShortName.SubstringBeforeLast(AttributeInstanceExtensions.ATTRIBUTE_SUFFIX));
            names.Add(typeShortName.SubstringBeforeLast(FSharpImplUtil.ModuleSuffix));

            if (type.GetClrName().TryGetPredefinedAbbreviations(out var abbreviations))
            {
                names.AddRange(abbreviations);
            }
        }
Exemplo n.º 13
0
        // NOTE: This method assumes that the type is not a descendant of UnityEngine.Object!
        private bool IsSerializableType([CanBeNull] ITypeElement type, [NotNull] IProject project, bool isTypeUsage,
                                        bool hasSerializeReference = false)
        {
            if (!(type is IStruct || type is IClass))
            {
                return(false);
            }

            if (isTypeUsage)
            {
                // Type usage (e.g. field declaration) is stricter. Means it must be a concrete type with no type
                // parameters, unless the type usage is for [SerializeReference], which allows abstract types
                if (type is IModifiersOwner modifiersOwner && modifiersOwner.IsAbstract && !hasSerializeReference)
                {
                    return(false);
                }

                // Unity 2020.1 allows fields to have generic types. It's currently undocumented, but there are no
                // limitations on the number of type parameters, or even nested type parameters. The base type needs to
                // be serializable, but type parameters don't (if a non-serializable type parameter is used as a field,
                // it just isn't serialised).
                // https://blogs.unity3d.com/2020/03/17/unity-2020-1-beta-is-now-available-for-feedback/
                var unityVersion = myUnityVersion.GetActualVersion(project);
                if (unityVersion < new Version(2020, 1) && type is ITypeParametersOwner typeParametersOwner &&
                    typeParametersOwner.TypeParameters.Count > 0)
                {
                    return(false);
                }
            }

            if (type is IClass @class && @class.IsStaticClass())
            {
                return(false);
            }

            // System.Dictionary is special cased and excluded. We can see this in UnitySerializationLogic.cs in the
            // reference source repo. It also excludes anything with a full name beginning "System.", which includes
            // "System.Version" (which is marked [Serializable]). However, it doesn't exclude string, int, etc.
            // TODO: Rewrite this whole section to properly mimic UnitySerializationLogic.cs
            var name = type.GetClrName();

            if (Equals(name, KnownTypes.SystemVersion) || Equals(name, PredefinedType.GENERIC_DICTIONARY_FQN))
            {
                return(false);
            }

            using (CompilationContextCookie.GetExplicitUniversalContextIfNotSet())
                return(type.HasAttributeInstance(PredefinedType.SERIALIZABLE_ATTRIBUTE_CLASS, true));
        }
        private static void GetPossibleSourceNames(ITypeElement type, ISet <string> names)
        {
            names.Add(type.ShortName);

            var typeShortName = type.ShortName;

            if (typeShortName.EndsWith(FSharpImplUtil.AttributeSuffix))
            {
                names.Add(typeShortName.SubstringBeforeLast(FSharpImplUtil.AttributeSuffix, StringComparison.Ordinal));
            }

            if (type.GetClrName().TryGetAbbreviations(out var abbreviations))
            {
                names.AddRange(abbreviations);
            }
        }
Exemplo n.º 15
0
        // NOTE: This method assumes that the type is not a descendant of UnityEngine.Object!
        private bool IsSerializableType([CanBeNull] ITypeElement type, [NotNull] IProject project, bool isTypeUsage)
        {
            if (!(type is IStruct || type is IClass))
            {
                return(false);
            }

            if (isTypeUsage)
            {
                // Type usage (e.g. field declaration) is stricter. Means it must be a concrete type with no type
                // parameters
                if (type is IModifiersOwner modifiersOwner && modifiersOwner.IsAbstract)
                {
                    return(false);
                }

                // Unity 2020.1 allows fields to have generic types. It's currently undocumented, but there are no
                // limitations on the number of type parameters, or even nested type parameters. The base type needs to
                // be serializable, but type parameters don't (if a non-serializable type parameter is used as a field,
                // it just isn't serialised).
                // https://blogs.unity3d.com/2020/03/17/unity-2020-1-beta-is-now-available-for-feedback/
                var unityVersion = myUnityVersion.GetActualVersion(project);
                if (unityVersion < new Version(2020, 1) && type is ITypeParametersOwner typeParametersOwner &&
                    typeParametersOwner.TypeParameters.Count > 0)
                {
                    return(false);
                }
            }

            if (type is IClass @class && @class.IsStaticClass())
            {
                return(false);
            }

            // System.Version seems to be special cased. In Mono, it's marked as [Serializable], but in netstandard,
            // it's not. Which means that depending on what runtime you're using, you could potentially get different
            // fields serialised. However, it never shows up in Inspector, so it's a good indication that it's handled
            // specially.
            if (Equals(type.GetClrName(), KnownTypes.SystemVersion))
            {
                return(false);
            }

            return(type.HasAttributeInstance(PredefinedType.SERIALIZABLE_ATTRIBUTE_CLASS, true));
        }
            private void CheckCommonCostlyMethods([NotNull] IInvocationExpression invocationExpressionParam, [NotNull] HotMethodAnalyzerContext context,
                                                  [NotNull] IClrDeclaredElement declaredElement, [NotNull] ITypeElement containingType, [NotNull] IReference reference)
            {
                ISet <string> knownCostlyMethods = null;
                var           clrTypeName        = containingType.GetClrName();

                if (clrTypeName.Equals(KnownTypes.Component))
                {
                    knownCostlyMethods = ourKnownComponentCostlyMethods;
                }

                if (clrTypeName.Equals(KnownTypes.GameObject))
                {
                    knownCostlyMethods = ourKnownGameObjectCostlyMethods;
                }

                if (clrTypeName.Equals(KnownTypes.Resources))
                {
                    knownCostlyMethods = ourKnownResourcesCostlyMethods;
                }

                if (clrTypeName.Equals(KnownTypes.Object))
                {
                    knownCostlyMethods = ourKnownObjectCostlyMethods;
                }

                if (clrTypeName.Equals(KnownTypes.Transform))
                {
                    knownCostlyMethods = ourKnownTransformCostlyMethods;
                }

                if (knownCostlyMethods == null)
                {
                    return;
                }

                var shortName = declaredElement.ShortName;

                if (knownCostlyMethods.Contains(shortName))
                {
                    context.MarkCurrentAsCostlyReachable();
                    myConsumer.AddHighlighting(new PerformanceCriticalCodeInvocationHighlighting(reference));
                }
            }
        private static string GetAssemblyQualifiedName(this ITypeElement type,
                                                       ISubstitution substitution,
                                                       IDictionary <DeclaredElementInstance, IName> seenElements)
        {
            if (type == null)
            {
                return(Names.UnknownType.Identifier);
            }

            var clrTypeName      = type.GetClrName();
            var containingModule = type.Module.ContainingProjectModule;

            Asserts.NotNull(containingModule, "module is null");
            var moduleName = containingModule.GetQualifiedName();

            var typeParameters = type.GetTypeParametersList(substitution, seenElements);

            string myName;
            var    parent     = type.GetContainingType();
            var    myFullName = clrTypeName.FullName;

            if (parent != null)
            {
                var parentName = parent.GetName <ITypeName>(substitution);
                // including the generic `N ticks
                var parentFullName = parentName.FullName;
                // shortName does not include the generic `N ticks, so we have to find it in the fullname...
                var startOfShortName = myFullName.LastIndexOf("+", StringComparison.Ordinal) + 1;
                //  ... and ignore the leading part
                var fullShortName = myFullName.Substring(startOfShortName);
                myName = string.Format("{0}+{1}", parentFullName, fullShortName);
            }
            else
            {
                myName = myFullName;
            }

            return(string.Format(
                       "{0}{1}, {2}",
                       myName,
                       typeParameters,
                       moduleName));
        }
    public ContextElement CreateContext(ITypeElement type)
    {
      if (_cache.Classes.ContainsKey(type))
      {
        return _cache.Classes[type];
      }

      ContextElement context = new ContextElement(_provider,
                                                  _projectEnvoy,
#if RESHARPER_6
                                                  type.GetClrName().FullName,
#else
                                                  type.CLRName,
#endif
                                                  _assemblyPath,
                                                  type.GetSubjectString(),
                                                  type.GetTags(),
                                                  type.IsIgnored());
      _cache.Classes.Add(type, context);
      return context;
    }
Exemplo n.º 19
0
        protected override bool IsReferenceExpressionNotRelated([NotNull] IReferenceExpression currentReference,
                                                                IClrDeclaredElement currentElement, ITypeElement currentContainingType)
        {
            if (base.IsReferenceExpressionNotRelated(currentReference, currentElement, currentContainingType))
            {
                return(true);
            }

            if (!currentContainingType.GetClrName().Equals(KnownTypes.Transform))
            {
                return(true);
            }

            if (ourTransformConflicts.ContainsKey(DeclaredElement.ShortName))
            {
                var conflicts = ourTransformConflicts[DeclaredElement.ShortName];
                return(!conflicts.Contains(currentElement.ShortName));
            }

            return(true);
        }
Exemplo n.º 20
0
        public IModule GetTargetModule <T>(T element)
        {
            var referenceExpression = element as IReferenceExpression;

            if (referenceExpression == null)
            {
                return(null);
            }

            if (referenceExpression.Reference.GetName() == "ThisAssembly")
            {
                IResolveResult   resolveResult   = referenceExpression.Reference.Resolve().Result;
                IDeclaredElement declaredElement = resolveResult.DeclaredElement;
                if (declaredElement == null)
                {
                    return(null);
                }

                var property = declaredElement as IProperty;
                if (property == null)
                {
                    return(null);
                }

                ITypeElement propertyType = property.GetContainingType();
                if (propertyType == null)
                {
                    return(null);
                }

                if (!propertyType.GetClrName().Equals(autofacModuleName))
                {
                    return(null);
                }

                return(referenceExpression.GetPsiModule().ContainingProjectModule);
            }

            return(null);
        }
Exemplo n.º 21
0
        public IModule GetTargetModule <T>(T element)
        {
            var referenceExpression = element as IReferenceExpression;

            if (referenceExpression == null)
            {
                return(null);
            }

            if (referenceExpression.Reference.GetName() == methodName)
            {
                IResolveResult   resolveResult   = referenceExpression.Reference.Resolve().Result;
                IDeclaredElement declaredElement = resolveResult.DeclaredElement;
                if (declaredElement == null)
                {
                    return(null);
                }

                var clrDeclaredElement = declaredElement as IClrDeclaredElement;
                if (clrDeclaredElement == null)
                {
                    return(null);
                }

                ITypeElement clrType = clrDeclaredElement.GetContainingType();
                if (clrType == null)
                {
                    return(null);
                }

                if (!clrType.GetClrName().Equals(clrTypeName))
                {
                    return(null);
                }

                return(referenceExpression.GetPsiModule().ContainingProjectModule);
            }

            return(null);
        }
    public ContextElement CreateContext(ITypeElement type)
    {
      var context = GetOrCreateContext(_provider,
                                       _manager,
                                       _psiModuleManager,
                                       _cacheManager,
                                       _project,
                                       _projectEnvoy,
                                       type.GetClrName().GetPersistent(),
                                       _assemblyPath,
                                       type.GetSubjectString(),
                                       type.GetTags(),
                                       type.IsIgnored());

      foreach (var child in context.Children)
      {
        child.State = UnitTestElementState.Pending;
      }

      _cache.Contexts.Add(type, context);
      return context;
    }
        public ContextElement CreateContext(ITypeElement type)
        {
            var context = GetOrCreateContext(_provider,
                                             _manager,
                                             _psiModuleManager,
                                             _cacheManager,
                                             _project,
                                             _projectEnvoy,
                                             type.GetClrName().GetPersistent(),
                                             _assemblyPath,
                                             type.GetSubjectString(),
                                             type.GetTags(),
                                             type.IsIgnored());

            foreach (var child in context.Children)
            {
                child.State = UnitTestElementState.Pending;
            }

            _cache.Contexts.Add(type, context);
            return(context);
        }
Exemplo n.º 24
0
        public static IClrTypeName GetClassNameAppropriateToLocation(ISolution solution, ITextControl textControl)
        {
            IDeclaredElement documentElement = TextControlToPsi.GetContainingTypeOrTypeMember(solution, textControl);

            if (documentElement == null)
            {
                return(FindFirstTypeInFile(solution, textControl.Document));
            }

            //AppendLineToOutputWindow("Element at cursor is of type " + documentElement.GetType().Name);

            IClrTypeName clrTypeName = null;

            if (documentElement is IClass @class)
            {
                clrTypeName = @class.GetClrName();
            }

            if (documentElement is ITypeElement element && clrTypeName == null)
            {
                ITypeElement containingType = element.GetContainingType();

                if (containingType != null)
                {
                    clrTypeName = containingType.GetClrName();
                }
            }

            if (clrTypeName == null)
            {
                AppendLineToOutputWindow(solution.Locks, "Unable to identify the class from current cursor position.");
                return(FindFirstTypeInFile(solution, textControl.Document));
            }

            return(clrTypeName);
        }
Exemplo n.º 25
0
 // I always have to think about what this means...
 // Based on Type.IsAssignableFrom(c)
 // "Determines whether an instance of the current Type can be assigned from an instance of the specified Type"
 // True if c and the current type are the same type, or if the current type is in the inheritance hierarchy of
 // the type c
 // In other words, type.IsAssignableFrom(c) is true if I can assign an instance of c to a variable of type "type"
 internal static bool IsAssignableFrom(this Type type, ITypeElement c)
 {
     return(type.FullName == c.GetClrName().FullName || c.GetAllSuperTypes().Any(superType => type.FullName == c.GetClrName().FullName));
 }
Exemplo n.º 26
0
        private static bool IsRootType(ITypeElement type)
        {
            var fn = type.GetClrName().FullName;

            return("System.Object".Equals(fn) || "System.ValueType".Equals(fn) || "System.Enum".Equals(fn));
        }
Exemplo n.º 27
0
        public CommonType GetCommonType(ITypeElement type)
        {
            var clrTypeName = type.GetClrName();

            return(new CommonType(clrTypeName.ShortName, clrTypeName.FullName, type.GetImplementedTypes().Select(x => x.GetClrName().FullName)));
        }
 public static bool IsInSystemLikeNamespace([NotNull] this ITypeElement typeElement)
 => typeElement.GetClrName().NamespaceNames.FirstOrDefault() == "System";
Exemplo n.º 29
0
 // I always have to think about what this means...
 // Based on Type.IsAssignableFrom(c)
 // "Determines whether an instance of the current Type can be assigned from an instance of the specified Type"
 // True if c and the current type are the same type, or if the current type is in the inheritance hierarchy of
 // the type c
 // In other words, type.IsAssignableFrom(c) is true if I can assign an instance of c to a variable of type "type"
 internal static bool IsAssignableFrom(this Type type, ITypeElement c)
 {
     return type.FullName == c.GetClrName().FullName || c.GetAllSuperTypes().Any(superType => type.FullName == c.GetClrName().FullName);
 }
Exemplo n.º 30
0
 public static ClassReference GetClassReference(this ITypeElement typeElement)
 {
     return(new ClassReference(new ClassId(typeElement.GetClrName().FullName)));
 }
        private XunitTestClassElement GetOrCreateClassElement(ITypeElement testClass, IProject projectElement, ProjectModelElementEnvoy projectEnvoy)
        {
            ITypeElement containingType = testClass.GetContainingType();
            XunitTestClassElement parent = null;
            if (containingType != null)
                parent = GetParent(containingType);

            return factory.GetOrCreateClassElement(testClass.GetClrName(), projectElement, projectEnvoy, parent);
        }
 private XunitTestClassElement GetParent(ITypeElement containingType)
 {
     var typeName = containingType.GetClrName().GetPersistent();
     var id = typeName.FullName;
     return factory.GetElementById(project, id) as XunitTestClassElement;
 }
Exemplo n.º 33
0
 static bool IsPredefinedTypeElement([CanBeNull] ITypeElement typeElement, [NotNull] IClrTypeName clrName)
 => typeElement != null && typeElement.GetClrName().Equals(clrName);
Exemplo n.º 34
0
 public CommonType GetCommonType(ITypeElement type)
 {
     var clrTypeName = type.GetClrName();
       return new CommonType(clrTypeName.ShortName, clrTypeName.FullName, type.GetImplementedTypes().Select(x => x.GetClrName().FullName));
 }
Exemplo n.º 35
0
        private static bool IsEnumerable(ITypeElement type)
        {
            var typeName = type.GetClrName();

            return(typeName.Equals(PredefinedType.ENUMERABLE_CLASS));
        }