static bool AllowsHelper(string baseClass, string methodName, IReturnType resolvedType, IProjectContent content) { IClass extensions = content.GetClass("System.Xml.Linq.Extensions", 0); if (extensions == null) { return(false); } IMethod descendents = extensions.Methods.FirstOrDefault(m => m.Name == methodName); if (descendents == null) { return(false); } IParameter param = descendents.Parameters.FirstOrDefault(); if (param == null) { return(false); } IClass resolvedTypeClass = resolvedType.GetUnderlyingClass(); if (resolvedTypeClass == null) { return(false); } return(MemberLookupHelper.IsApplicable(resolvedType, param, descendents) || resolvedTypeClass.IsTypeInInheritanceTree(content.GetClass(baseClass, 0))); }
public void GetCommonType() { IReturnType res = MemberLookupHelper.GetCommonType(msc, swf.GetClass("System.Windows.Forms.ToolStripButton", 0).DefaultReturnType, swf.GetClass("System.Windows.Forms.ToolStripSeparator", 0).DefaultReturnType); Assert.AreEqual("System.Windows.Forms.ToolStripItem", res.FullyQualifiedName); }
public void ExpectedMethodsFromProjectContent() { ProjectContentRegistry registry = new ProjectContentRegistry(); IProjectContent mscorlibProjectContent = registry.Mscorlib; IClass c = mscorlibProjectContent.GetClass("System.Collections.ObjectModel.Collection", 1); List <string> methodNames = new List <string>(); foreach (IMethod m in c.Methods) { if (m.IsVirtual && !m.IsSealed) { methodNames.Add(m.Name); } } List <string> expectedMethodNames = new List <string>(); expectedMethodNames.Add("ClearItems"); expectedMethodNames.Add("InsertItem"); expectedMethodNames.Add("RemoveItem"); expectedMethodNames.Add("SetItem"); methodNames.Sort(); expectedMethodNames.Sort(); Assert.AreEqual(expectedMethodNames.ToArray(), methodNames.ToArray()); }
/// <summary> /// Looks for the specified type in all the projects in the open solution /// excluding the current project. /// </summary> static IProject FindProjectContainingType(string type) { IProject currentProject = ProjectService.CurrentProject; if (currentProject == null) { return(null); } foreach (IProject project in ProjectService.OpenSolution.Projects) { if (project != currentProject) { IProjectContent projectContent = ParserService.GetProjectContent(project); if (projectContent != null) { if (projectContent.GetClass(type, 0) != null) { LoggingService.Debug("Found project containing type: " + project.FileName); return(project); } } } } return(null); }
public void ExpectedPropertiesFromProjectContent() { ProjectContentRegistry registry = new ProjectContentRegistry(); IProjectContent mscorlibProjectContent = registry.Mscorlib; IClass c = mscorlibProjectContent.GetClass("System.Exception", 0); List <string> propertyNames = new List <string>(); foreach (IProperty p in c.Properties) { if (p.IsVirtual && !p.IsSealed) { propertyNames.Add(p.Name); } } propertyNames.Sort(); List <string> expectedPropertyNames = new List <string>(); expectedPropertyNames.Add("Data"); expectedPropertyNames.Add("HelpLink"); expectedPropertyNames.Add("Message"); expectedPropertyNames.Add("Source"); expectedPropertyNames.Add("StackTrace"); Assert.AreEqual(expectedPropertyNames.ToArray(), propertyNames.ToArray()); }
IClass GetClassIfTypeNameIsNotEmpty(IProjectContent projectContent, string modelTypeName) { if (!String.IsNullOrEmpty(modelTypeName)) { return projectContent.GetClass(modelTypeName, 0); } return null; }
public static void AddUsing(ArrayList result, IUsing u, IProjectContent projectContent) { if (u == null) { return; } bool importNamespaces = projectContent.Language.ImportNamespaces; bool importClasses = projectContent.Language.CanImportClasses; foreach (string name in u.Usings) { if (importClasses) { IClass c = projectContent.GetClass(name, 0); if (c != null) { ArrayList members = new ArrayList(); IReturnType t = c.DefaultReturnType; members.AddRange(t.GetMethods()); members.AddRange(t.GetFields()); members.AddRange(t.GetEvents()); members.AddRange(t.GetProperties()); foreach (IMember m in members) { if (m.IsStatic && m.IsPublic) { result.Add(m); } } continue; } } if (importNamespaces) { string newName = null; if (projectContent.DefaultImports != null) { newName = projectContent.DefaultImports.SearchNamespace(name); } projectContent.AddNamespaceContents(result, newName ?? name, projectContent.Language, true); } else { foreach (object o in projectContent.GetNamespaceContents(name)) { if (!(o is string)) { result.Add(o); } } } } if (u.HasAliases) { foreach (string alias in u.Aliases.Keys) { result.Add(alias); } } }
public bool HasField(string reflectionTypeName, int typeParameterCount, string fieldName) { IClass c; if (typeParameterCount > 0) { c = _projectContent.GetClass(reflectionTypeName, typeParameterCount); } else { c = _projectContent.GetClassByReflectionName(reflectionTypeName, true); } if (c == null) { return(false); } foreach (IField field in c.DefaultReturnType.GetFields()) { if (field.Name == fieldName) { return(true); } } return(false); }
public void LoadFromXml(IXPathNavigable doc, IProjectContent pc) { if (pc == null) { return; } if (doc == null) { return; } ClearCanvas(); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator ni = nav.Select(@"/ClassDiagram/Class | /ClassDiagram/Struct | /ClassDiagram/Enum | /ClassDiagram/Interface | /ClassDiagram/Delegate"); while (ni.MoveNext()) { string typeName = ni.Current.GetAttribute("Name", ""); IClass ct = pc.GetClass(typeName, 0); ClassCanvasItem canvasitem = ClassCanvas.CreateItemFromType(ct); if (canvasitem != null) { canvasitem.LoadFromXml(ni.Current); AddCanvasItem(canvasitem); } } ni = nav.Select(@"/ClassDiagram/Comment"); while (ni.MoveNext()) { NoteCanvasItem note = new NoteCanvasItem(); note.LoadFromXml(ni.Current); AddCanvasItem(note); } }
internal static IClass FindMyFormsClass(IProjectContent pc, string myNamespace) { if (pc != null) { return pc.GetClass(myNamespace + ".MyForms", 0); } return null; }
internal static IClass FindMyFormsClass(IProjectContent pc, string myNamespace) { if (pc != null) { return(pc.GetClass(myNamespace + ".MyForms", 0)); } return(null); }
IClass GetClassIfTypeNameIsNotEmpty(IProjectContent projectContent, string modelTypeName) { if (!String.IsNullOrEmpty(modelTypeName)) { return(projectContent.GetClass(modelTypeName, 0)); } return(null); }
public void InheritanceTest() { IClass c = pc.GetClass("System.SystemException"); IClass c2 = pc.GetClass("System.Exception"); Assert.IsNotNull(c, "c is null"); Assert.IsNotNull(c2, "c2 is null"); //Assert.AreEqual(3, c.BaseTypes.Count); // Inherited interfaces are not reported by Cecil // which matches the behaviour of our C#/VB parsers Assert.AreEqual("System.Exception", c.BaseTypes[0].FullyQualifiedName); Assert.AreSame(c2, c.BaseClass); List <IClass> subClasses = new List <IClass>(); foreach (IClass subClass in c.ClassInheritanceTree) { subClasses.Add(subClass); } Assert.AreEqual(5, subClasses.Count, "ClassInheritanceTree length"); Assert.AreEqual("System.SystemException", subClasses[0].FullyQualifiedName); Assert.AreEqual("System.Exception", subClasses[1].FullyQualifiedName); if (subClasses[2].FullyQualifiedName == "System.Object") { Assert.AreEqual("System.Object", subClasses[2].FullyQualifiedName); Assert.AreEqual("System.Runtime.Serialization.ISerializable", subClasses[3].FullyQualifiedName); Assert.AreEqual("System.Runtime.InteropServices._Exception", subClasses[4].FullyQualifiedName); } else { Assert.AreEqual("System.Runtime.Serialization.ISerializable", subClasses[2].FullyQualifiedName); Assert.AreEqual("System.Runtime.InteropServices._Exception", subClasses[3].FullyQualifiedName); Assert.AreEqual("System.Object", subClasses[4].FullyQualifiedName); } }
static IReturnType Create(IProjectContent pc, IEntity member, Type type, bool createLazyReturnType, bool forceGenericType) { if (type.IsByRef) { // TODO: Use ByRefRefReturnType return Create(pc, member, type.GetElementType(), createLazyReturnType); } else if (type.IsPointer) { return new PointerReturnType(Create(pc, member, type.GetElementType(), createLazyReturnType)); } else if (type.IsArray) { return new ArrayReturnType(pc, Create(pc, member, type.GetElementType(), createLazyReturnType), type.GetArrayRank()); } else if (type.IsGenericType && (forceGenericType || !type.IsGenericTypeDefinition)) { Type[] args = type.GetGenericArguments(); List<IReturnType> para = new List<IReturnType>(args.Length); for (int i = 0; i < args.Length; ++i) { para.Add(Create(pc, member, args[i], createLazyReturnType)); } return new ConstructedReturnType(Create(pc, member, type.GetGenericTypeDefinition(), createLazyReturnType, false), para); } else if (type.IsGenericParameter) { IClass c = (member is IClass) ? (IClass)member : (member is IMember) ? ((IMember)member).DeclaringType : null; if (c != null && type.GenericParameterPosition < c.TypeParameters.Count) { if (c.TypeParameters[type.GenericParameterPosition].Name == type.Name) { return new GenericReturnType(c.TypeParameters[type.GenericParameterPosition]); } } if (type.DeclaringMethod != null) { IMethod method = member as IMethod; if (method != null) { if (type.GenericParameterPosition < method.TypeParameters.Count) { return new GenericReturnType(method.TypeParameters[type.GenericParameterPosition]); } return new GenericReturnType(new DefaultTypeParameter(method, type)); } } return new GenericReturnType(new DefaultTypeParameter(c, type)); } else { string name = type.FullName; if (name == null) throw new ApplicationException("type.FullName returned null. Type: " + type.ToString()); int typeParameterCount = 0; if (name.Length > 2) { if (name[name.Length - 2] == '`') { typeParameterCount = int.Parse(name[name.Length - 1].ToString()); name = name.Substring(0, name.Length - 2); } } if (name.IndexOf('+') > 0) { name = name.Replace('+', '.'); } if (!createLazyReturnType) { IClass c = pc.GetClass(name, typeParameterCount); if (c != null) return c.DefaultReturnType; // example where name is not found: pointers like System.Char* // or when the class is in a assembly that is not referenced } return new GetClassReturnType(pc, name, typeParameterCount); } }
public void ClassDerivingFromTwoInstanciationsOfIEnumerable() { // class C : IEnumerable<int>, IEnumerable<uint> {} DefaultTypeDefinition c = new DefaultTypeDefinition(mscorlib, string.Empty, "C"); c.BaseTypes.Add(typeof(IEnumerable <int>).ToTypeReference()); c.BaseTypes.Add(typeof(IEnumerable <uint>).ToTypeReference()); IType[] expected = { c, c.BaseTypes[0].Resolve(context), c.BaseTypes[1].Resolve(context), mscorlib.GetClass(typeof(IEnumerable)), mscorlib.GetClass(typeof(object)) }; Assert.AreEqual(expected, c.GetAllBaseTypes(context).OrderBy(t => t.ReflectionName).ToArray()); }
public CodeType CodeTypeFromFullName(string name) { IClass matchedClass = projectContent.GetClass(name, 0); if (matchedClass != null) { return(CreateCodeTypeForClass(matchedClass)); } return(null); }
public override void OnReferenceExpression(ReferenceExpression node) { if (pc.GetClass(node.Name, 0) != null) { _expression = new CodeTypeReferenceExpression(node.Name); } else if (pc.NamespaceExists(node.Name)) { _expression = new CodeTypeReferenceExpression(node.Name); } else if (GetLocalVariable(node.Name) != null) { _expression = new CodeVariableReferenceExpression(node.Name); } else { _expression = CreateMemberExpression(new CodeThisReferenceExpression(), node.Name); } }
private void UpdateClassesFromProjectContent(IEnumerable <IClass> classes) { foreach (var c in classes) { var classInProjectContent = projectContent.GetClass(c.FullyQualifiedName, c.TypeParameters.Count); if (classInProjectContent != null) { UpdateTestClass(classInProjectContent); } } }
void SearchAllClassesWithName(List <IClass> searchResults, IProjectContent pc, string name, LanguageProperties language) { foreach (string ns in pc.NamespaceNames) { IClass c = pc.GetClass(ns + "." + name, 0, language, GetClassOptions.None); if (c != null) { searchResults.Add(c); } } }
static void SelectedToolUsedHandler(object sender, EventArgs e) { LoggingService.Debug("SelectedToolUsedHandler"); SideTab tab = sideBar.ActiveTab; // try to add project reference if (sender != null && sender is ICSharpCode.FormsDesigner.Services.ToolboxService) { ToolboxItem selectedItem = (sender as IToolboxService).GetSelectedToolboxItem(); if (tab is CustomComponentsSideTab) { if (selectedItem != null && selectedItem.TypeName != null) { LoggingService.Debug("Checking for reference to CustomComponent: " + selectedItem.TypeName); // Check current project has the custom component first. IProjectContent currentProjectContent = ParserService.CurrentProjectContent; if (currentProjectContent != null) { if (currentProjectContent.GetClass(selectedItem.TypeName, 0) == null) { // Check other projects in the solution. LoggingService.Debug("Checking other projects in the solution."); IProject projectContainingType = FindProjectContainingType(selectedItem.TypeName); if (projectContainingType != null) { AddProjectReferenceToProject(ProjectService.CurrentProject, projectContainingType); } } } } } else { if (selectedItem != null && selectedItem.AssemblyName != null) { IProject currentProject = ProjectService.CurrentProject; if (currentProject != null) { if (!ProjectContainsReference(currentProject, selectedItem.AssemblyName)) { AddReferenceToProject(currentProject, selectedItem.AssemblyName); } } } } } if (tab.Items.Count > 0) { tab.ChoosedItem = tab.Items[0]; } sideBar.Refresh(); }
IReturnType CreateFromName(string name) { IClass c = pc.GetClass(name, 0); if (c != null) { return(c.DefaultReturnType); } else { LoggingService.Warn("SystemTypes.CreateFromName could not find " + name); return(Void); } }
void SearchAttributesWithName(List <IClass> results, IProjectContent pc, string name) { if (baseClass == null) { baseClass = pc.GetClass("System.Attribute", 0); } foreach (IClass c in pc.Classes) { if ((c.Name == name || c.Name == name + "Attribute") && c.IsTypeInInheritanceTree(baseClass)) { results.Add(c); } } }
public bool HasField(string fullTypeName, string fieldName) { IClass c = pc.GetClass(fullTypeName); if (c == null) { return(false); } foreach (IField field in c.DefaultReturnType.GetFields()) { if (field.Name == fieldName) { return(true); } } return(false); }
public bool HasField(string fullTypeName, int typeParameterCount, string fieldName) { IClass c = _projectContent.GetClass(fullTypeName, typeParameterCount); if (c == null) { return(false); } foreach (IField field in c.DefaultReturnType.GetFields()) { if (field.Name == fieldName) { return(true); } } return(false); }
static IReturnType FindTypeInAssembly(IProjectContent projectContent, string xmlNamespace, string className) { foreach (IAttribute att in projectContent.GetAssemblyAttributes()) { if (att.PositionalArguments.Count == 2 && att.AttributeType.FullyQualifiedName == "System.Windows.Markup.XmlnsDefinitionAttribute") { string namespaceName = att.PositionalArguments[1] as string; if (xmlNamespace.Equals(att.PositionalArguments[0]) && namespaceName != null) { IClass c = projectContent.GetClass(namespaceName + "." + className, 0); if (c != null) { return(c.DefaultReturnType); } } } } return(null); }
static IClass GetPrimitiveClass(IProjectContent pc, string systemType, string newName) { IClass c = pc.GetClass(systemType, 0); if (c == null) { LoggingService.Warn("Could not find " + systemType); return(null); } DefaultClass c2 = new DefaultClass(c.CompilationUnit, newName); c2.ClassType = c.ClassType; c2.Modifiers = c.Modifiers; c2.Documentation = c.Documentation; c2.BaseTypes.AddRange(c.BaseTypes); c2.Methods.AddRange(c.Methods); c2.Fields.AddRange(c.Fields); c2.Properties.AddRange(c.Properties); c2.Events.AddRange(c.Events); return(c2); }
public void CodeGeneratorMethods() { ProjectContentRegistry registry = new ProjectContentRegistry(); IProjectContent mscorlibProjectContent = registry.Mscorlib; IClass collectionClass = mscorlibProjectContent.GetClass("System.Collections.ObjectModel.Collection", 1); DefaultProjectContent projectContent = new DefaultProjectContent(); DefaultCompilationUnit unit = new DefaultCompilationUnit(projectContent); DefaultClass c = new DefaultClass(unit, "MyCollection"); c.BaseTypes.Add(new DefaultReturnType(collectionClass)); MockProject project = new MockProject(); ProjectService.CurrentProject = project; OverrideMethodsCodeGenerator codeGenerator = new OverrideMethodsCodeGenerator(); codeGenerator.Initialize(c); List <string> methods = new List <string>(); foreach (object o in codeGenerator.Content) { methods.Add(o.ToString()); } List <string> expectedMethods = new List <string>(); expectedMethods.Add("ClearItems"); expectedMethods.Add("Equals"); expectedMethods.Add("Finalize"); expectedMethods.Add("GetHashCode"); expectedMethods.Add("InsertItem"); expectedMethods.Add("RemoveItem"); expectedMethods.Add("SetItem"); expectedMethods.Add("ToString"); Assert.AreEqual(expectedMethods.ToArray(), methods.ToArray()); }
public void CodeGeneratorProperties() { ProjectContentRegistry registry = new ProjectContentRegistry(); IProjectContent mscorlibProjectContent = registry.Mscorlib; IClass exceptionClass = mscorlibProjectContent.GetClass("System.Exception", 0); DefaultProjectContent projectContent = new DefaultProjectContent(); DefaultCompilationUnit unit = new DefaultCompilationUnit(projectContent); DefaultClass c = new DefaultClass(unit, "MyException"); c.BaseTypes.Add(new DefaultReturnType(exceptionClass)); MockProject project = new MockProject(); ProjectService.CurrentProject = project; OverridePropertiesCodeGenerator codeGenerator = new OverridePropertiesCodeGenerator(); codeGenerator.Initialize(c); List <string> properties = new List <string>(); foreach (object o in codeGenerator.Content) { properties.Add(o.ToString()); } List <string> expectedProperties = new List <string>(); expectedProperties.Add("Data"); expectedProperties.Add("HelpLink"); expectedProperties.Add("Message"); expectedProperties.Add("Source"); expectedProperties.Add("StackTrace"); Assert.AreEqual(expectedProperties.ToArray(), properties.ToArray()); }
public static IReturnType CreateReturnType(TypeReference reference, IClass callingClass, IMember callingMember, int caretLine, int caretColumn, IProjectContent projectContent, bool useLazyReturnType) { if (reference == null) { return(null); } if (reference.IsNull) { return(null); } if (reference is InnerClassTypeReference) { reference = ((InnerClassTypeReference)reference).CombineToNormalTypeReference(); } LanguageProperties languageProperties = projectContent.Language; IReturnType t = null; if (callingClass != null && !reference.IsGlobal) { foreach (ITypeParameter tp in callingClass.TypeParameters) { if (languageProperties.NameComparer.Equals(tp.Name, reference.SystemType)) { t = new GenericReturnType(tp); break; } } if (t == null && callingMember is IMethod && (callingMember as IMethod).TypeParameters != null) { foreach (ITypeParameter tp in (callingMember as IMethod).TypeParameters) { if (languageProperties.NameComparer.Equals(tp.Name, reference.SystemType)) { t = new GenericReturnType(tp); break; } } } } if (t == null) { if (reference.Type != reference.SystemType) { // keyword-type like void, int, string etc. IClass c = projectContent.GetClass(reference.SystemType); if (c != null) { t = c.DefaultReturnType; } else { t = new GetClassReturnType(projectContent, reference.SystemType, 0); } } else { int typeParameterCount = reference.GenericTypes.Count; if (useLazyReturnType) { if (reference.IsGlobal) { t = new GetClassReturnType(projectContent, reference.SystemType, typeParameterCount); } else if (callingClass != null) { t = new SearchClassReturnType(projectContent, callingClass, caretLine, caretColumn, reference.SystemType, typeParameterCount); } } else { IClass c; if (reference.IsGlobal) { c = projectContent.GetClass(reference.SystemType, typeParameterCount); t = (c != null) ? c.DefaultReturnType : null; } else if (callingClass != null) { t = projectContent.SearchType(new SearchTypeRequest(reference.SystemType, typeParameterCount, callingClass, caretLine, caretColumn)).Result; } if (t == null) { if (reference.GenericTypes.Count == 0 && !reference.IsArrayType) { // reference to namespace is possible if (reference.IsGlobal) { if (projectContent.NamespaceExists(reference.Type)) { return(new NamespaceReturnType(reference.Type)); } } else { string name = projectContent.SearchNamespace(reference.Type, callingClass, (callingClass == null) ? null : callingClass.CompilationUnit, caretLine, caretColumn); if (name != null) { return(new NamespaceReturnType(name)); } } } return(null); } } } } if (reference.GenericTypes.Count > 0) { List <IReturnType> para = new List <IReturnType>(reference.GenericTypes.Count); for (int i = 0; i < reference.GenericTypes.Count; ++i) { para.Add(CreateReturnType(reference.GenericTypes[i], callingClass, callingMember, caretLine, caretColumn, projectContent, useLazyReturnType)); } t = new ConstructedReturnType(t, para); } return(WrapArray(projectContent, t, reference)); }
static bool AllowsHelper(string baseClass, string methodName, IReturnType resolvedType, IProjectContent content) { IClass extensions = content.GetClass("System.Xml.Linq.Extensions", 0); if (extensions == null) return false; IMethod descendents = extensions.Methods.FirstOrDefault(m => m.Name == methodName); if (descendents == null) return false; IParameter param = descendents.Parameters.FirstOrDefault(); if (param == null) return false; IClass resolvedTypeClass = resolvedType.GetUnderlyingClass(); if (resolvedTypeClass == null) return false; return MemberLookupHelper.IsApplicable(resolvedType, param, descendents) || resolvedTypeClass.IsTypeInInheritanceTree(content.GetClass(baseClass, 0)); }
static void SearchAllClassesWithName(List<IClass> searchResults, IProjectContent pc, string name, LanguageProperties language) { foreach (string ns in pc.NamespaceNames) { IClass c = pc.GetClass(ns + "." + name, 0, language, GetClassOptions.None); if (c != null) searchResults.Add(c); } }
/// <summary> /// Create a SharpDevelop return type from a Cecil type reference. /// </summary> internal static IReturnType CreateType(IProjectContent pc, IEntity member, TypeReference type) { while (type is TypeSpecification) { type = (type as TypeSpecification).ElementType; } if (type == null) { LoggingService.Warn("CecilReader: Null type for: " + member); return new VoidReturnType(pc); } if (type is ByReferenceType) { // TODO: Use ByRefRefReturnType return CreateType(pc, member, (type as ByReferenceType).ElementType); } else if (type is PointerType) { return new PointerReturnType(CreateType(pc, member, (type as PointerType).ElementType)); } else if (type is ArrayType) { return new ArrayReturnType(pc, CreateType(pc, member, (type as ArrayType).ElementType), (type as ArrayType).Rank); } else if (type is GenericInstanceType) { GenericInstanceType gType = (GenericInstanceType)type; IReturnType[] para = new IReturnType[gType.GenericArguments.Count]; for (int i = 0; i < para.Length; ++i) { para[i] = CreateType(pc, member, gType.GenericArguments[i]); } return new ConstructedReturnType(CreateType(pc, member, gType.ElementType), para); } else if (type is GenericParameter) { GenericParameter typeGP = type as GenericParameter; if (typeGP.Owner is MethodDefinition) { IMethod method = member as IMethod; if (method != null) { if (typeGP.Position < method.TypeParameters.Count) { return new GenericReturnType(method.TypeParameters[typeGP.Position]); } } return new GenericReturnType(new DefaultTypeParameter(method, typeGP.Name, typeGP.Position)); } else { IClass c = (member is IClass) ? (IClass)member : (member is IMember) ? ((IMember)member).DeclaringType : null; if (c != null && typeGP.Position < c.TypeParameters.Count) { if (c.TypeParameters[typeGP.Position].Name == type.Name) { return new GenericReturnType(c.TypeParameters[typeGP.Position]); } } return new GenericReturnType(new DefaultTypeParameter(c, typeGP.Name, typeGP.Position)); } } else { string name = type.FullName; if (name == null) throw new ApplicationException("type.FullName returned null. Type: " + type.ToString()); int typeParameterCount; if (name.IndexOf('/') > 0) { typeParameterCount = 0; StringBuilder newName = new StringBuilder(); foreach (string namepart in name.Split('/')) { if (newName.Length > 0) newName.Append('.'); int partTypeParameterCount; newName.Append(ReflectionClass.SplitTypeParameterCountFromReflectionName(namepart, out partTypeParameterCount)); typeParameterCount += partTypeParameterCount; } name = newName.ToString(); } else { name = ReflectionClass.SplitTypeParameterCountFromReflectionName(name, out typeParameterCount); } IClass c = pc.GetClass(name, typeParameterCount); if (c != null) { return c.DefaultReturnType; } else { // example where name is not found: pointers like System.Char* // or when the class is in a assembly that is not referenced return new GetClassReturnType(pc, name, typeParameterCount); } } }
/// <summary> /// Create a SharpDevelop return type from a Cecil type reference. /// </summary> internal static IReturnType CreateType(IProjectContent pc, IDecoration member, TypeReference type) { while (type is ModType) { type = (type as ModType).ElementType; } if (type == null) { LoggingService.Warn("CecilReader: Null type for: " + member); return(VoidReturnType.Instance); } if (type is ReferenceType) { // TODO: Use ByRefRefReturnType return(CreateType(pc, member, (type as ReferenceType).ElementType)); } else if (type is ArrayType) { return(new ArrayReturnType(pc, CreateType(pc, member, (type as ArrayType).ElementType), (type as ArrayType).Rank)); } else if (type is GenericInstanceType) { GenericInstanceType gType = (GenericInstanceType)type; IReturnType[] para = new IReturnType[gType.GenericArguments.Count]; for (int i = 0; i < para.Length; ++i) { para[i] = CreateType(pc, member, gType.GenericArguments[i]); } return(new ConstructedReturnType(CreateType(pc, member, gType.ElementType), para)); } else if (type is GenericParameter) { GenericParameter typeGP = type as GenericParameter; if (typeGP.Owner is MethodDefinition) { IMethod method = member as IMethod; if (method != null) { if (typeGP.Position < method.TypeParameters.Count) { return(new GenericReturnType(method.TypeParameters[typeGP.Position])); } } return(new GenericReturnType(new DefaultTypeParameter(method, typeGP.Name, typeGP.Position))); } else { IClass c = (member is IClass) ? (IClass)member : (member is IMember) ? ((IMember)member).DeclaringType : null; if (c != null && typeGP.Position < c.TypeParameters.Count) { if (c.TypeParameters[typeGP.Position].Name == type.Name) { return(new GenericReturnType(c.TypeParameters[typeGP.Position])); } } return(new GenericReturnType(new DefaultTypeParameter(c, typeGP.Name, typeGP.Position))); } } else { string name = type.FullName; if (name == null) { throw new ApplicationException("type.FullName returned null. Type: " + type.ToString()); } if (name.IndexOf('/') > 0) { name = name.Replace('/', '.'); } int typeParameterCount = 0; if (name.Length > 2 && name[name.Length - 2] == '`') { typeParameterCount = name[name.Length - 1] - '0'; name = name.Substring(0, name.Length - 2); } IClass c = pc.GetClass(name, typeParameterCount); if (c != null) { return(c.DefaultReturnType); } else { // example where name is not found: pointers like System.Char* // or when the class is in a assembly that is not referenced return(new GetClassReturnType(pc, name, typeParameterCount)); } } }
/// <summary>Context expects a type deriving from System.Attribute.</summary> /// <example>[*expr*()]</example> /// <remarks>When using this context, a resolver should try resolving typenames with an /// appended "Attribute" suffix and treat "invocations" of the attribute type as /// object creation.</remarks> public static ExpressionContext GetAttribute(IProjectContent projectContent) { return new TypeExpressionContext(projectContent.GetClass("System.Attribute"), false, true); }
/// <summary> /// Returns a collection of possible types that could be meant when using this Import /// to search the type. /// Types with the incorrect type parameter count might be returned, but for each /// same using entry or alias entry at most one (the best matching) type should be returned. /// </summary> public IEnumerable <IReturnType> SearchType(string partialTypeName, int typeParameterCount) { if (HasAliases) { foreach (KeyValuePair <string, IReturnType> entry in aliases) { string aliasString = entry.Key; if (projectContent.Language.NameComparer.Equals(partialTypeName, aliasString)) { if (entry.Value.GetUnderlyingClass() == null) { continue; // type not found, maybe entry was a namespace } yield return(entry.Value); } if (partialTypeName.Length > aliasString.Length) { if (projectContent.Language.NameComparer.Equals(partialTypeName.Substring(0, aliasString.Length + 1), aliasString + ".")) { string className = entry.Value.FullyQualifiedName + partialTypeName.Remove(0, aliasString.Length); IClass c = projectContent.GetClass(className, typeParameterCount); if (c != null) { yield return(c.DefaultReturnType); } } } } } if (projectContent.Language.ImportNamespaces) { foreach (string str in usings) { IClass c = projectContent.GetClass(str + "." + partialTypeName, typeParameterCount); if (c != null) { yield return(c.DefaultReturnType); } } } else { int pos = partialTypeName.IndexOf('.'); string className, subClassName; if (pos < 0) { className = partialTypeName; subClassName = null; } else { className = partialTypeName.Substring(0, pos); subClassName = partialTypeName.Substring(pos + 1); } foreach (string str in usings) { IClass c = projectContent.GetClass(str + "." + className, typeParameterCount); if (c != null) { c = projectContent.GetClass(str + "." + partialTypeName, typeParameterCount); if (c != null) { yield return(c.DefaultReturnType); } } } } }
public IClass GetClass(string fullyQualifiedName) { return(projectContent.GetClass(fullyQualifiedName, 0)); }
void SearchAttributesWithName(List<IClass> searchResults, IProjectContent pc, string name) { if (baseClass == null) baseClass = pc.GetClass("System.Attribute", 0); foreach (IClass c in pc.Classes) { if (c.IsTypeInInheritanceTree(baseClass) && (c.Name == name || c.Name == name + "Attribute")) searchResults.Add(c); } }
public static IReturnType CreateReturnType(TypeReference reference, IClass callingClass, IMember callingMember, int caretLine, int caretColumn, IProjectContent projectContent, ReturnTypeOptions options) { if (reference == null) return null; if (reference.IsNull) return null; if (reference is InnerClassTypeReference) { reference = ((InnerClassTypeReference)reference).CombineToNormalTypeReference(); } bool useLazyReturnType = (options & ReturnTypeOptions.Lazy) == ReturnTypeOptions.Lazy; bool isBaseTypeReference = (options & ReturnTypeOptions.BaseTypeReference) == ReturnTypeOptions.BaseTypeReference; LanguageProperties languageProperties = projectContent.Language; IReturnType t = null; if (callingClass != null && !reference.IsGlobal) { foreach (ITypeParameter tp in callingClass.TypeParameters) { if (languageProperties.NameComparer.Equals(tp.Name, reference.Type)) { t = new GenericReturnType(tp); break; } } IMethod callingMethod = callingMember as IMethod; if (t == null && callingMethod != null) { foreach (ITypeParameter tp in callingMethod.TypeParameters) { if (languageProperties.NameComparer.Equals(tp.Name, reference.Type)) { t = new GenericReturnType(tp); break; } } } } if (t == null) { int typeParameterCount = reference.GenericTypes.Count; if (reference.IsKeyword) { // keyword-type like void, int, string etc. IClass c = projectContent.GetClass(reference.Type, typeParameterCount); if (c != null) t = c.DefaultReturnType; else t = new GetClassReturnType(projectContent, reference.Type, typeParameterCount); } else { if (useLazyReturnType || isBaseTypeReference) { if (reference.IsGlobal) { t = new GetClassReturnType(projectContent, reference.Type, typeParameterCount); } else if (callingClass != null) { SearchClassReturnType scrt = new SearchClassReturnType(projectContent, callingClass, caretLine, caretColumn, reference.Type, typeParameterCount); if (isBaseTypeReference) scrt.LookForInnerClassesInDeclaringClass = false; t = scrt; } } else { IClass c; if (reference.IsGlobal) { c = projectContent.GetClass(reference.Type, typeParameterCount); t = (c != null) ? c.DefaultReturnType : null; } else if (callingClass != null) { t = projectContent.SearchType(new SearchTypeRequest(reference.Type, typeParameterCount, callingClass, caretLine, caretColumn)).Result; } if (t == null) { return null; } } } } if (reference.GenericTypes.Count > 0) { IReturnType[] para = new IReturnType[reference.GenericTypes.Count]; for (int i = 0; i < reference.GenericTypes.Count; ++i) { para[i] = CreateReturnType(reference.GenericTypes[i], callingClass, callingMember, caretLine, caretColumn, projectContent, options); } t = new ConstructedReturnType(t, para); } for (int i = 0; i < reference.PointerNestingLevel; i++) { t = new PointerReturnType(t); } return WrapArray(projectContent, t, reference); }
public static IReturnType CreateReturnType(TypeReference reference, IClass callingClass, IMember callingMember, int caretLine, int caretColumn, IProjectContent projectContent, bool useLazyReturnType) { if (reference == null) return null; if (reference.IsNull) return null; if (reference is InnerClassTypeReference) { reference = ((InnerClassTypeReference)reference).CombineToNormalTypeReference(); } LanguageProperties languageProperties = projectContent.Language; IReturnType t = null; if (callingClass != null && !reference.IsGlobal) { foreach (ITypeParameter tp in callingClass.TypeParameters) { if (languageProperties.NameComparer.Equals(tp.Name, reference.SystemType)) { t = new GenericReturnType(tp); break; } } if (t == null && callingMember is IMethod && (callingMember as IMethod).TypeParameters != null) { foreach (ITypeParameter tp in (callingMember as IMethod).TypeParameters) { if (languageProperties.NameComparer.Equals(tp.Name, reference.SystemType)) { t = new GenericReturnType(tp); break; } } } } if (t == null) { if (reference.Type != reference.SystemType) { // keyword-type like void, int, string etc. IClass c = projectContent.GetClass(reference.SystemType); if (c != null) t = c.DefaultReturnType; else t = new GetClassReturnType(projectContent, reference.SystemType, 0); } else { int typeParameterCount = reference.GenericTypes.Count; if (useLazyReturnType) { if (reference.IsGlobal) t = new GetClassReturnType(projectContent, reference.SystemType, typeParameterCount); else if (callingClass != null) t = new SearchClassReturnType(projectContent, callingClass, caretLine, caretColumn, reference.SystemType, typeParameterCount); } else { IClass c; if (reference.IsGlobal) { c = projectContent.GetClass(reference.SystemType, typeParameterCount); t = (c != null) ? c.DefaultReturnType : null; } else if (callingClass != null) { t = projectContent.SearchType(new SearchTypeRequest(reference.SystemType, typeParameterCount, callingClass, caretLine, caretColumn)).Result; } if (t == null) { if (reference.GenericTypes.Count == 0 && !reference.IsArrayType) { // reference to namespace is possible if (reference.IsGlobal) { if (projectContent.NamespaceExists(reference.Type)) return new NamespaceReturnType(reference.Type); } else { string name = projectContent.SearchNamespace(reference.Type, callingClass, (callingClass == null) ? null : callingClass.CompilationUnit, caretLine, caretColumn); if (name != null) return new NamespaceReturnType(name); } } return null; } } } } if (reference.GenericTypes.Count > 0) { List<IReturnType> para = new List<IReturnType>(reference.GenericTypes.Count); for (int i = 0; i < reference.GenericTypes.Count; ++i) { para.Add(CreateReturnType(reference.GenericTypes[i], callingClass, callingMember, caretLine, caretColumn, projectContent, useLazyReturnType)); } t = new ConstructedReturnType(t, para); } return WrapArray(projectContent, t, reference); }
public void LoadFromXml (IXPathNavigable doc, IProjectContent pc) { if (pc == null) return; if (doc == null) return; ClearCanvas(); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator ni = nav.Select(@"/ClassDiagram/Class | /ClassDiagram/Struct | /ClassDiagram/Enum | /ClassDiagram/Interface | /ClassDiagram/Delegate"); while (ni.MoveNext()) { string typeName = ni.Current.GetAttribute("Name", ""); IClass ct = pc.GetClass(typeName, 0); ClassCanvasItem canvasitem = ClassCanvas.CreateItemFromType(ct); if (canvasitem != null) { canvasitem.LoadFromXml (ni.Current); AddCanvasItem(canvasitem); } } ni = nav.Select(@"/ClassDiagram/Comment"); while (ni.MoveNext()) { NoteCanvasItem note = new NoteCanvasItem(); note.LoadFromXml(ni.Current); AddCanvasItem(note); } }
/// <summary> /// Create a SharpDevelop return type from a Cecil type reference. /// </summary> internal static IReturnType CreateType(IProjectContent pc, IDecoration member, TypeReference type) { while (type is ModType) { type = (type as ModType).ElementType; } if (type == null) { LoggingService.Warn("CecilReader: Null type for: " + member); return VoidReturnType.Instance; } if (type is ReferenceType) { // TODO: Use ByRefRefReturnType return CreateType(pc, member, (type as ReferenceType).ElementType); } else if (type is ArrayType) { return new ArrayReturnType(pc, CreateType(pc, member, (type as ArrayType).ElementType), (type as ArrayType).Rank); } else if (type is GenericInstanceType) { GenericInstanceType gType = (GenericInstanceType)type; IReturnType[] para = new IReturnType[gType.GenericArguments.Count]; for (int i = 0; i < para.Length; ++i) { para[i] = CreateType(pc, member, gType.GenericArguments[i]); } return new ConstructedReturnType(CreateType(pc, member, gType.ElementType), para); } else if (type is GenericParameter) { GenericParameter typeGP = type as GenericParameter; if (typeGP.Owner is MethodDefinition) { IMethod method = member as IMethod; if (method != null) { if (typeGP.Position < method.TypeParameters.Count) { return new GenericReturnType(method.TypeParameters[typeGP.Position]); } } return new GenericReturnType(new DefaultTypeParameter(method, typeGP.Name, typeGP.Position)); } else { IClass c = (member is IClass) ? (IClass)member : (member is IMember) ? ((IMember)member).DeclaringType : null; if (c != null && typeGP.Position < c.TypeParameters.Count) { if (c.TypeParameters[typeGP.Position].Name == type.Name) { return new GenericReturnType(c.TypeParameters[typeGP.Position]); } } return new GenericReturnType(new DefaultTypeParameter(c, typeGP.Name, typeGP.Position)); } } else { string name = type.FullName; if (name == null) throw new ApplicationException("type.FullName returned null. Type: " + type.ToString()); if (name.IndexOf('/') > 0) { name = name.Replace('/', '.'); } int typeParameterCount = 0; if (name.Length > 2 && name[name.Length - 2] == '`') { typeParameterCount = name[name.Length - 1] - '0'; name = name.Substring(0, name.Length - 2); } IClass c = pc.GetClass(name, typeParameterCount); if (c != null) { return c.DefaultReturnType; } else { // example where name is not found: pointers like System.Char* // or when the class is in a assembly that is not referenced return new GetClassReturnType(pc, name, typeParameterCount); } } }
static IReturnType FindTypeInAssembly(IProjectContent projectContent, string xmlNamespace, string className) { foreach (IAttribute att in projectContent.GetAssemblyAttributes()) { if (att.PositionalArguments.Count == 2 && att.AttributeType.FullyQualifiedName == "System.Windows.Markup.XmlnsDefinitionAttribute") { string namespaceName = att.PositionalArguments[1] as string; if (xmlNamespace.Equals(att.PositionalArguments[0]) && namespaceName != null) { IClass c = projectContent.GetClass(namespaceName + "." + className, 0); if (c != null) return c.DefaultReturnType; } } } return null; }
static IClass GetPrimitiveClass(IProjectContent pc, string systemType, string newName) { IClass c = pc.GetClass(systemType, 0); if (c == null) { LoggingService.Warn("Could not find " + systemType); return null; } DefaultClass c2 = new DefaultClass(c.CompilationUnit, newName); c2.ClassType = c.ClassType; c2.Modifiers = c.Modifiers; c2.Documentation = c.Documentation; c2.BaseTypes.AddRange(c.BaseTypes); c2.Methods.AddRange(c.Methods); c2.Fields.AddRange(c.Fields); c2.Properties.AddRange(c.Properties); c2.Events.AddRange(c.Events); return c2; }
static IReturnType Create(IProjectContent pc, Type type, IEntity member, bool createLazyReturnType, ICustomAttributeProvider attributeProvider, ref int typeIndex, bool forceGenericType = true) { if (type.IsByRef) { // TODO: Use ByRefRefReturnType return Create(pc, type.GetElementType(), member, createLazyReturnType, attributeProvider, ref typeIndex); } else if (type.IsPointer) { typeIndex++; return new PointerReturnType(Create(pc, type.GetElementType(), member, createLazyReturnType, attributeProvider, ref typeIndex)); } else if (type.IsArray) { typeIndex++; return new ArrayReturnType(pc, Create(pc, type.GetElementType(), member, createLazyReturnType, attributeProvider, ref typeIndex), type.GetArrayRank()); } else if (type.IsGenericType && (forceGenericType || !type.IsGenericTypeDefinition)) { IReturnType baseType = Create(pc, type.GetGenericTypeDefinition(), member, createLazyReturnType, attributeProvider, ref typeIndex, forceGenericType: false); Type[] args = type.GetGenericArguments(); List<IReturnType> para = new List<IReturnType>(args.Length); for (int i = 0; i < args.Length; ++i) { typeIndex++; para.Add(Create(pc, args[i], member, createLazyReturnType, attributeProvider, ref typeIndex)); } return new ConstructedReturnType(baseType, para); } else if (type.IsGenericParameter) { IClass c = (member is IClass) ? (IClass)member : (member is IMember) ? ((IMember)member).DeclaringType : null; if (c != null && type.GenericParameterPosition < c.TypeParameters.Count) { if (c.TypeParameters[type.GenericParameterPosition].Name == type.Name) { return new GenericReturnType(c.TypeParameters[type.GenericParameterPosition]); } } if (type.DeclaringMethod != null) { IMethod method = member as IMethod; if (method != null) { if (type.GenericParameterPosition < method.TypeParameters.Count) { return new GenericReturnType(method.TypeParameters[type.GenericParameterPosition]); } return new GenericReturnType(new DefaultTypeParameter(method, type)); } } return new GenericReturnType(new DefaultTypeParameter(c, type)); } else { string name = type.FullName; if (name == null) throw new ApplicationException("type.FullName returned null. Type: " + type.ToString()); int typeParameterCount; name = ReflectionClass.ConvertReflectionNameToFullName(name, out typeParameterCount); if (typeParameterCount == 0 && name == "System.Object" && HasDynamicAttribute(attributeProvider, typeIndex)) return new DynamicReturnType(pc); if (!createLazyReturnType) { IClass c = pc.GetClass(name, typeParameterCount); if (c != null) return c.DefaultReturnType; // example where name is not found: pointers like System.Char* // or when the class is in a assembly that is not referenced } return new GetClassReturnType(pc, name, typeParameterCount); } }
public AttributesDataProvider(IProjectContent pc) : this(ExpressionContext.TypeDerivingFrom(pc.GetClass("System.Attribute"), true)) { }
public IClass GetClass(string fullName) { return(projectContent.GetClass(fullName)); }
public static void AddUsing(List<ICompletionEntry> result, IUsing u, IProjectContent projectContent) { if (u == null) { return; } bool importNamespaces = projectContent.Language.ImportNamespaces; bool importClasses = projectContent.Language.CanImportClasses; foreach (string name in u.Usings) { if (importClasses) { IClass c = projectContent.GetClass(name, 0); if (c != null) { ArrayList members = new ArrayList(); IReturnType t = c.DefaultReturnType; members.AddRange(t.GetMethods()); members.AddRange(t.GetFields()); members.AddRange(t.GetEvents()); members.AddRange(t.GetProperties()); foreach (IMember m in members) { if (m.IsStatic && m.IsPublic) { result.Add(m); } } continue; } } if (importNamespaces) { string newName = null; if (projectContent.DefaultImports != null) { newName = projectContent.DefaultImports.SearchNamespace(name); } projectContent.AddNamespaceContents(result, newName ?? name, projectContent.Language, true); } else { foreach (ICompletionEntry o in projectContent.GetNamespaceContents(name)) { if (!(o is NamespaceEntry)) result.Add(o); } } } if (u.HasAliases) { foreach (string alias in u.Aliases.Keys) { result.Add(new AliasEntry(alias)); } } }