public void GetChildrenWithType() { string program = TestUtil.TypeMemberParse(@" private int x; private string y; public int X; public string Y; public void A(){} public void B(){} public void C(){}" ); CompilationUnit compilationUnit = TestUtil.ParseProgram(program); NamespaceDeclaration ns = (NamespaceDeclaration)compilationUnit.Children[0]; TypeDeclaration typeDeclaration = (TypeDeclaration)ns.Children[0]; List <INode> fieldList = AstUtil.GetChildrenWithType(typeDeclaration, typeof(FieldDeclaration)); List <INode> methodList = AstUtil.GetChildrenWithType(typeDeclaration, typeof(MethodDeclaration)); Assert.IsNotNull(fieldList); Assert.AreEqual(4, fieldList.Count); FieldDeclaration fieldDeclaration = fieldList[0] as FieldDeclaration; Assert.AreEqual("x", ((VariableDeclaration)fieldDeclaration.Fields[0]).Name); Assert.IsNotNull(methodList); Assert.AreEqual(3, methodList.Count); MethodDeclaration methodDeclaration = methodList[0] as MethodDeclaration; Assert.AreEqual("A", methodDeclaration.Name); }
protected MethodDeclaration FindMethod(TypeDeclaration typeDeclaration, InvocationExpression invocationExpression) { foreach (MethodDeclaration methodDeclaration in AstUtil.GetChildrenWithType(typeDeclaration, typeof(MethodDeclaration))) { if (IsInvocationForMethod(methodDeclaration, invocationExpression)) { return(methodDeclaration); } } return(null); }
private TypeDeclaration GetTypeDeclaration(NamespaceDeclaration namespaceDeclaration, string name) { IList types = AstUtil.GetChildrenWithType(namespaceDeclaration, typeof(TypeDeclaration)); foreach (TypeDeclaration typeDeclaration in types) { if (name.StartsWith(namespaceDeclaration.Name + "." + typeDeclaration.Name)) { return(typeDeclaration); } } return((TypeDeclaration)namespaceDeclaration.Children[namespaceDeclaration.Children.Count - 1]); }
protected TypeReference GetTypeInLocalVariables(BlockStatement block, string identifier) { foreach (LocalVariableDeclaration variableDeclaration in AstUtil.GetChildrenWithType(block, typeof(LocalVariableDeclaration))) { foreach (VariableDeclaration localVariable in variableDeclaration.Variables) { if (localVariable.Name == identifier) { return(variableDeclaration.TypeReference); } } } return(null); }
protected bool IsMethodInExternalTypes(TypeDeclaration typeDeclaration, MethodDeclaration methodDeclaration) { bool found = false; foreach (TypeReference baseType in typeDeclaration.BaseTypes) { string fullName = GetFullName(baseType); if (!found && CodeBase.Types.Contains(fullName)) { TypeDeclaration baseTypeDeclaration = (TypeDeclaration)CodeBase.Types[fullName]; if (IsInExternalLibraries(fullName) || fullName.StartsWith("Helpers.")) { IList methods = AstUtil.GetChildrenWithType(baseTypeDeclaration, typeof(MethodDeclaration)); if (ContainsMethod(methods, methodDeclaration)) { found = true; } else { found = IsMethodInExternalTypes(baseTypeDeclaration, methodDeclaration); } } else { found = IsMethodInExternalTypes(baseTypeDeclaration, methodDeclaration); } if (found) { break; } } } return(found); }
public void IndexOf() { string program = TestUtil.GetInput(); CompilationUnit cu = TestUtil.ParseProgram(program); NamespaceDeclaration ns = (NamespaceDeclaration)cu.Children[0]; TypeDeclaration ty = (TypeDeclaration)ns.Children[0]; IList methodDecList = AstUtil.GetChildrenWithType(ty, typeof(MethodDeclaration)); ParameterDeclarationExpression p1 = new ParameterDeclarationExpression(new TypeReference("Circle"), "circle"); p1.TypeReference.RankSpecifier = new int[] {}; List <ParameterDeclarationExpression> md1Param = new List <ParameterDeclarationExpression>(); md1Param.Add(p1); MethodDeclaration md1 = new MethodDeclaration("GetRadius", Modifiers.Private, new TypeReference("int"), md1Param, null); int md1Index = IndexOf(methodDecList, md1); Assert.AreEqual(1, md1Index); MethodDeclaration md2 = new MethodDeclaration("ToString", Modifiers.Protected, new TypeReference("string"), null, null); int md2Index = IndexOf(methodDecList, md2); Assert.AreEqual(-1, md2Index); }
private bool ContainsMethod(TypeDeclaration typeDeclaration, InvocationExpression invocationExpression) { string identifier = null; if (invocationExpression.TargetObject is IdentifierExpression) { identifier = ((IdentifierExpression)invocationExpression.TargetObject).Identifier; } else if (invocationExpression.TargetObject is FieldReferenceExpression) { identifier = ((FieldReferenceExpression)invocationExpression.TargetObject).FieldName; } IList methods = AstUtil.GetChildrenWithType(typeDeclaration, typeof(MethodDeclaration)); foreach (MethodDeclaration method in methods) { string pascalName = identifier[0].ToString().ToUpper() + identifier.Substring(1); if ((method.Name == identifier || method.Name == pascalName) && !IsMethodInExternalTypes(typeDeclaration, method)) { return(true); } } return(false); }
public override object TrackedVisitMethodDeclaration(MethodDeclaration methodDeclaration, object data) { TypeDeclaration typeDeclaration = (TypeDeclaration)methodDeclaration.Parent; if (methodDeclaration.Name.Length > 3) { if (IsAccessor(methodDeclaration, fields) || IsInterfaceOrAbstract(typeDeclaration) && ImplementInheritors(typeDeclaration, methodDeclaration) || ImplementSiblings(typeDeclaration, methodDeclaration)) { string name = GetPropertyName(methodDeclaration); TypeReference typeReference = GetAccessorTypeReference(methodDeclaration); IList properties = AstUtil.GetChildrenWithType(typeDeclaration, typeof(PropertyDeclaration)); PropertyDeclaration propertyDeclaration = GetProperty(properties, name, methodDeclaration.Modifier, typeReference); if (methodDeclaration.Name.StartsWith("set")) { AddSetSection(propertyDeclaration, methodDeclaration, fields); } else if (methodDeclaration.Name.StartsWith("get")) { AddGetSection(propertyDeclaration, methodDeclaration, fields); } AddToReferences(typeDeclaration, methodDeclaration.Name, propertyDeclaration.Name); if (!HasProperty(properties, name)) { ReplaceCurrentNode(propertyDeclaration); } else { RemoveCurrentNode(); } } } return(base.TrackedVisitMethodDeclaration(methodDeclaration, data)); }
public override object TrackedVisitTypeDeclaration(TypeDeclaration typeDeclaration, object data) { fields = AstUtil.GetChildrenWithType(typeDeclaration, typeof(FieldDeclaration)); base.TrackedVisitTypeDeclaration(typeDeclaration, data); AddNotDeclaredAccessor(typeDeclaration); return(null); }
public override object TrackedVisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration, object data) { IList usings = AstUtil.GetChildrenWithType(namespaceDeclaration, typeof(UsingDeclaration)); foreach (UsingDeclaration usi in usings) { AddUsing(namespaceDeclaration, usings, usi); } return(base.TrackedVisitNamespaceDeclaration(namespaceDeclaration, data)); }
private IList GetFieldsName(TypeDeclaration typeDeclaration) { IList fields = new ArrayList(); List <INode> fieldDeclarations = AstUtil.GetChildrenWithType(typeDeclaration, typeof(FieldDeclaration)); foreach (FieldDeclaration fieldDeclaration in fieldDeclarations) { fields.Add(((VariableDeclaration)fieldDeclaration.Fields[0]).Name); } return(fields); }
private bool ContainsProperty(TypeDeclaration typeDeclaration, string propertyName) { IList properties = AstUtil.GetChildrenWithType(typeDeclaration, typeof(PropertyDeclaration)); foreach (PropertyDeclaration property in properties) { if (property.Name == propertyName) { return(true); } } return(false); }
private bool DefinedInFieldsClass(TypeDeclaration tyedeDeclaration, string identifier) { IList fields = AstUtil.GetChildrenWithType(tyedeDeclaration, typeof(FieldDeclaration)); foreach (FieldDeclaration field in fields) { if (identifier == ((VariableDeclaration)field.Fields[0]).Name) { return(true); } } return(false); }
protected override bool VerifyMethodCondition(TypeDeclaration typeDeclaration, MethodDeclaration methodDeclaration) { IList properties = AstUtil.GetChildrenWithType(typeDeclaration, typeof(PropertyDeclaration)); if (HasSectionFor(properties, methodDeclaration)) { return(true); } else { IList methods = AstUtil.GetChildrenWithType(typeDeclaration, typeof(MethodDeclaration)); return(Contains(methods, methodDeclaration)); } }
public override object TrackedVisitTypeDeclaration(TypeDeclaration typeDeclaration, object data) { List <INode> fields = AstUtil.GetChildrenWithType(typeDeclaration, typeof(FieldDeclaration)); if (fields.Count > 0) { IList methods = AstUtil.GetChildrenWithType(typeDeclaration, typeof(MethodDeclaration)); foreach (FieldDeclaration fieldDeclaration in fields) { RenameFeildNameSimilarToMethods(fieldDeclaration, methods); } } return(base.TrackedVisitTypeDeclaration(typeDeclaration, data)); }
public override object TrackedVisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration, object data) { Removeables.Clear(); UsedTypes.Clear(); NamespaceDeclaration replaced = namespaceDeclaration; IList types = AstUtil.GetChildrenWithType(replaced, typeof(TypeDeclaration)); foreach (TypeDeclaration typeDeclaration in types) { VisitTypeDeclaration(typeDeclaration, data); } ReplaceCurrentNode(replaced); return(base.TrackedVisitNamespaceDeclaration(namespaceDeclaration, data)); }
private void AddUsing(INode currentNode, object data, string name) { string ns = name.Substring(name.LastIndexOf('.') + 1); NamespaceDeclaration namespaceDeclaration = (NamespaceDeclaration)AstUtil.GetParentOfType(currentNode, typeof(NamespaceDeclaration)); UsingDeclaration usingDeclaration = new UsingDeclaration(ns); usingDeclaration.Parent = namespaceDeclaration; ((Using)usingDeclaration.Usings[0]).Alias = AstUtil.GetTypeReference(name, usingDeclaration); IList usings = AstUtil.GetChildrenWithType(namespaceDeclaration, typeof(UsingDeclaration)); if (!ContainsUsing(usings, usingDeclaration) && !ContainsUsing((IList)data, usingDeclaration)) { ((IList)data).Add(usingDeclaration); } }
private bool IsAccessor(TypeDeclaration typeDeclaration, MethodDeclaration methodDeclaration) { IList methods = AstUtil.GetChildrenWithType(typeDeclaration, typeof(MethodDeclaration)); if (Contains(methods, methodDeclaration)) { IList fields = AstUtil.GetChildrenWithType(typeDeclaration, typeof(FieldDeclaration)); return(IsAccessor(methodDeclaration, fields)); } else { string propertyName = methodDeclaration.Name.Substring(3); IList properties = AstUtil.GetChildrenWithType(typeDeclaration, typeof(PropertyDeclaration)); return(HasProperty(properties, propertyName)); } }
private void AddNotDeclaredAccessor(TypeDeclaration typeDeclaration) { IList properties = AstUtil.GetChildrenWithType(typeDeclaration, typeof(PropertyDeclaration)); foreach (PropertyDeclaration property in properties) { if (!property.HasGetRegion || !property.HasSetRegion) { string accessorType; if (!property.HasSetRegion) { accessorType = "set"; } else { accessorType = "get"; } string propertyName = property.Name; ImplementPropertyRegionTransformer implementPropertyRegionTransformer = new ImplementPropertyRegionTransformer(); implementPropertyRegionTransformer.CodeBase = CodeBase; if (implementPropertyRegionTransformer.ShouldAddAccessor(typeDeclaration, accessorType + propertyName, property.TypeReference)) { BlockStatement block; if (IsInterface(typeDeclaration) || (IsAbstractClass(typeDeclaration) && !HasField(fields, propertyName))) { block = BlockStatement.Null; } else { block = new BlockStatement(); ObjectCreateExpression notSupported = new ObjectCreateExpression(new TypeReference("System.NotSupportedException"), null); ThrowStatement throwStatement = new ThrowStatement(notSupported); block.Children.Add(throwStatement); } if (accessorType == "get") { property.GetRegion = new PropertyGetRegion(block, null); } else { property.SetRegion = new PropertySetRegion(block, null); } } } } }
public void Contains() { string program = TestUtil.GetInput(); CompilationUnit cu = TestUtil.ParseProgram(program); NamespaceDeclaration ns = (NamespaceDeclaration)cu.Children[0]; TypeDeclaration ty = (TypeDeclaration)ns.Children[0]; IList methods = AstUtil.GetChildrenWithType(ty, typeof(MethodDeclaration)); ParameterDeclarationExpression pm = new ParameterDeclarationExpression( new TypeReference("int"), "from"); pm.TypeReference.RankSpecifier = new int[] {}; List <ParameterDeclarationExpression> al = new List <ParameterDeclarationExpression>(); al.Add(pm); MethodDeclaration myMethod = new MethodDeclaration("Distance", Modifiers.Protected, new TypeReference("int"), al, null); Assert.IsTrue(Contains(methods, myMethod)); }
protected override bool VerifyMethodCondition(TypeDeclaration typeDeclaration, MethodDeclaration methodDeclaration) { IList methods = AstUtil.GetChildrenWithType(typeDeclaration, typeof(MethodDeclaration)); string name = methodDeclaration.Name.Substring(3); if (Contains(methods, methodDeclaration)) { IList innerTypes = AstUtil.GetChildrenWithType(typeDeclaration, typeof(TypeDeclaration)); foreach (TypeDeclaration innerType in innerTypes) { if (innerType.Name == name) { return(true); } } IList constructors = AstUtil.GetChildrenWithType(typeDeclaration, typeof(ConstructorDeclaration)); foreach (ConstructorDeclaration constructorDeclaration in constructors) { if (constructorDeclaration.Name == name) { return(true); } } foreach (MethodDeclaration method in methods) { if (method.Name == name || char.ToUpper(method.Name[0]) + method.Name.Substring(1) == name) { return(true); } } } else { IList properties = AstUtil.GetChildrenWithType(typeDeclaration, typeof(PropertyDeclaration)); return(Contains(properties, name)); } return(false); }
private TypeReference GetDeclaringType(TypeDeclaration typeDeclaration, string name) { TypeReference declaringType = null; IList methods = AstUtil.GetChildrenWithType(typeDeclaration, typeof(MethodDeclaration)); foreach (MethodDeclaration method in methods) { if (name == method.Name) { return(AstUtil.GetTypeReference(typeDeclaration.Name, method.Parent)); } } IList fields = AstUtil.GetChildrenWithType(typeDeclaration, typeof(FieldDeclaration)); foreach (FieldDeclaration fieldDeclaration in fields) { if (name == ((VariableDeclaration)fieldDeclaration.Fields[0]).Name) { return(AstUtil.GetTypeReference(typeDeclaration.Name, fieldDeclaration.Parent)); } } return(declaringType); }
private string ContainsIdentifier(IdentifierExpression identifier) { NamespaceDeclaration ns = (NamespaceDeclaration)AstUtil.GetParentOfType(identifier, typeof(NamespaceDeclaration)); string key = ns.Name + "." + identifier.Identifier; INode idParent = identifier.Parent; if (CodeBase.References.Contains(key)) { if (identifier.Parent is FieldReferenceExpression) { FieldReferenceExpression fieldReference = (FieldReferenceExpression)identifier.Parent; string st = GetInterfaceFieldsClass(key, fieldReference.FieldName); if (st != null) { return(st); } } } else if (IsPascalStyle(identifier.Identifier) && CodeBase.Types.Contains(key)) { return(GetProperIdentifier(idParent, key)); } else { TypeDeclaration typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(identifier, typeof(TypeDeclaration)); key = ns.Name + "." + typeDeclaration.Name; if (key.EndsWith("_Fields")) { key = key.Replace("_Fields", ""); } if (CodeBase.References.Contains(key)) { TypeDeclaration baseInterface = (TypeDeclaration)CodeBase.Types[key]; if (baseInterface.BaseTypes.Count > 0) { foreach (TypeReference baseType in baseInterface.BaseTypes) { string fullName = GetFullName(baseType); string interfaceFieldsClass = GetInterfaceFieldsClass(fullName, identifier.Identifier); if (interfaceFieldsClass != null) { return(interfaceFieldsClass + "." + identifier.Identifier); } } } } IList usings = AstUtil.GetChildrenWithType(ns, typeof(UsingDeclaration)); foreach (UsingDeclaration usingDeclaration in usings) { Using usi = (Using)usingDeclaration.Usings[0]; key = usi.Name + "." + identifier.Identifier; if (usi.IsAlias && usi.Alias.Type.EndsWith("." + identifier.Identifier)) { key = usi.Alias.Type; } if (CodeBase.References.Contains(key)) { return((string)CodeBase.References[key]); } else if (IsPascalStyle(identifier.Identifier) && CodeBase.Types.Contains(key)) { return(GetProperIdentifier(idParent, key)); } } } return(null); }
public string GetFullName(TypeReference typeReference) { NamespaceDeclaration nsd = (NamespaceDeclaration)AstUtil.GetParentOfType(typeReference, typeof(NamespaceDeclaration)); if (nsd == null) { return(typeReference.Type); } if (CodeBase.Types.Contains(typeReference.Type)) { return(typeReference.Type); } if (typeReference.Type.IndexOf(".") != -1) { int firstDot = typeReference.Type.IndexOf('.'); if (firstDot == typeReference.Type.LastIndexOf('.')) { string enclosing = typeReference.Type.Substring(0, firstDot); string subType = typeReference.Type.Substring(firstDot + 1); if (CodeBase.Types.Contains(nsd.Name + "." + enclosing)) { return(nsd.Name + "." + enclosing + "$" + subType); } IList usingDeclarations = AstUtil.GetChildrenWithType(nsd, typeof(UsingDeclaration)); foreach (UsingDeclaration usingDeclaration in usingDeclarations) { foreach (Using us in usingDeclaration.Usings) { if (us.IsAlias) { if (us.Alias.Type.EndsWith('.' + enclosing)) { return(us.Alias + "$" + subType); } } else { if (us.Name.EndsWith('.' + enclosing)) { return(us.Name + "$" + subType); } else { string nsName = us.Name; if (nsName.EndsWith(".*")) { nsName = nsName.Substring(0, nsName.Length - 2); } if (CodeBase.Types.Contains(nsName + "." + enclosing)) { return(nsName + "." + enclosing + "$" + subType); } } } } } return(typeReference.Type); } else { return(typeReference.Type); } } TypeDeclaration typeDec = (TypeDeclaration)AstUtil.GetParentOfType(typeReference, typeof(TypeDeclaration)); if (typeDec != null) { if (typeDec.BaseTypes.Count > 0) { foreach (TypeReference baseTypeReference in typeDec.BaseTypes) { string fullBaseName = GetFullName(baseTypeReference, nsd); string typeName = fullBaseName + "$" + typeReference.Type; if (CodeBase.Types.Contains(typeName)) { return(typeName); } } } if (typeDec.Name == typeReference.Type) { typeDec = (TypeDeclaration)AstUtil.GetParentOfType(typeDec, typeof(TypeDeclaration)); } if (typeDec != null) { while (typeDec.Parent is TypeDeclaration) { typeDec = (TypeDeclaration)typeDec.Parent; } string typeName = nsd.Name + "." + typeDec.Name + "$" + typeReference.Type; if (CodeBase.Types.Contains(typeName)) { return(typeName); } } } if (TypeReference.PrimitiveTypesJava.ContainsKey(typeReference.Type)) { return((string)TypeReference.PrimitiveTypesJava[typeReference.Type]); } IList nsUsings = AstUtil.GetChildrenWithType(nsd, typeof(UsingDeclaration)); if (nsUsings.Count == 0) { CompilationUnit compilationUnit = (CompilationUnit)nsd.Parent; if (compilationUnit != null) { nsUsings = AstUtil.GetChildrenWithType(compilationUnit, typeof(UsingDeclaration)); } } string typeR = GetAliasUsing(nsUsings, typeReference); if (typeR != null) { return(typeR); } foreach (UsingDeclaration us in nsUsings) { Using uss = (Using)us.Usings[0]; string usingName = uss.Name; if (usingName.EndsWith(".*")) { usingName = usingName.Substring(0, usingName.Length - 2); } if (uss.IsAlias) { if (uss.Alias.Type.EndsWith("." + typeReference.Type)) { return(uss.Alias.Type); } if (usingName == typeReference.Type) { return(uss.Alias.Type); } } else if (usingName.EndsWith("." + typeReference.Type)) { return(usingName); } else if (CodeBase.Types.Contains(usingName + "." + typeReference.Type)) { return(usingName + "." + typeReference.Type); } } if (CodeBase.Types.Contains(nsd.Name + "." + typeReference.Type)) { return(nsd.Name + "." + typeReference.Type); } return("java.lang." + typeReference.Type); }