public void GetParentOfType() { string program = TestUtil.TypeMemberParse(@" public void A() { if(a) { a = b; } }" ); CompilationUnit compilationUnit = TestUtil.ParseProgram(program); NamespaceDeclaration ns = (NamespaceDeclaration)compilationUnit.Children[0]; TypeDeclaration typeDeclaration = (TypeDeclaration)ns.Children[0]; MethodDeclaration method = (MethodDeclaration)typeDeclaration.Children[0]; IfElseStatement ifElse = (IfElseStatement)method.Body.Children[0]; ExpressionStatement statement = (ExpressionStatement)((BlockStatement)ifElse.TrueStatement[0]).Children[0]; INode parent = AstUtil.GetParentOfType(statement, typeof(IfElseStatement)); Assert.IsTrue(parent is IfElseStatement); parent = AstUtil.GetParentOfType(statement, typeof(MethodDeclaration)); Assert.IsTrue(parent is MethodDeclaration); parent = AstUtil.GetParentOfType(statement, typeof(TypeDeclaration)); Assert.IsTrue(parent is TypeDeclaration); }
public override object TrackedVisitInvocationExpression(InvocationExpression invocationExpression, object data) { Expression invocationTarget = invocationExpression.TargetObject; if (invocationTarget is FieldReferenceExpression) { FieldReferenceExpression methodTargetObject = (FieldReferenceExpression)invocationTarget; Expression invoker = methodTargetObject.TargetObject; TypeReference invokerType = GetExpressionType(invoker); if (invokerType != null) { ReplaceMember(invocationExpression, data, invokerType); } } else if (invocationTarget is IdentifierExpression) { TypeDeclaration typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(invocationExpression, typeof(TypeDeclaration)); VerifyDerivedMethod(typeDeclaration, invocationExpression, data); } if (invocationExpression.TypeArguments.Count == 0) { return(base.TrackedVisitInvocationExpression(invocationExpression, data)); } else { invocationExpression.TypeArguments.Clear(); return(null); } }
protected TypeDeclaration GetEnclosingTypeDeclaration(InvocationExpression invocationExpression) { TypeDeclaration typeDeclaration; if (invocationExpression.TargetObject is IdentifierExpression) { typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(invocationExpression, typeof(TypeDeclaration)); return(GetTypeDeclarationOf(typeDeclaration, invocationExpression)); } else if (invocationExpression.TargetObject is FieldReferenceExpression) { FieldReferenceExpression fieldReference = (FieldReferenceExpression)invocationExpression.TargetObject; TypeReference targetType = GetExpressionType(fieldReference.TargetObject); if (targetType != null) { string fullName = GetFullName(targetType); if (CodeBase.Types.Contains(fullName)) { TypeDeclaration targetTypeDeclaration = (TypeDeclaration)CodeBase.Types[fullName]; return(GetTypeDeclarationOf(targetTypeDeclaration, invocationExpression)); } } } return(null); }
private int GetScopeHashCode(string key) { if (localVariables.Contains(key)) { VariableDeclaration value = (VariableDeclaration)localVariables[key]; BlockStatement block = (BlockStatement)AstUtil.GetParentOfType(value, typeof(BlockStatement)); return(block.GetHashCode()); } return(-1); }
private bool HasConflict(VariableDeclaration variableDeclaration) { if (localVariables.Contains(variableDeclaration.Name)) { VariableDeclaration value = (VariableDeclaration)localVariables[variableDeclaration.Name]; BlockStatement block = (BlockStatement)AstUtil.GetParentOfType(value, typeof(BlockStatement)); return(HasConflictingVariable(block, variableDeclaration)); } return(false); }
public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data) { if (!IsMethodInvocation(identifierExpression)) { TypeDeclaration typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(identifierExpression, typeof(TypeDeclaration)); string fullName = GetFullName(typeDeclaration); string key = fullName + "." + identifierExpression.Identifier; if (CodeBase.References.Contains(key)) { identifierExpression.Identifier = (string)CodeBase.References[key]; } } return(base.TrackedVisitIdentifierExpression(identifierExpression, 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); } }
public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data) { if (localVariables.Contains(identifierExpression.Identifier)) { BlockStatement blockStatement = (BlockStatement)AstUtil.GetParentOfType(identifierExpression, typeof(BlockStatement)); int hashCode = blockStatement.GetHashCode(); if (hashCode != GetScopeHashCode(identifierExpression.Identifier)) { string renamedVariableName = GetModifiedName(blockStatement, identifierExpression.Identifier); if (renamedVariableName != null) { identifierExpression.Identifier = renamedVariableName; } } } return(base.TrackedVisitIdentifierExpression(identifierExpression, data)); }
private string GetModifiedName(BlockStatement blockStatement, string identifier) { int hashCode; while (blockStatement != null) { hashCode = blockStatement.GetHashCode(); string identifierHash = identifier + "_" + hashCode; if (renamedVariables.Contains(identifierHash)) { return((string)renamedVariables[identifierHash]); } else { blockStatement = (BlockStatement)AstUtil.GetParentOfType(blockStatement, typeof(BlockStatement)); } } return(null); }
public override object TrackedVisitVariableDeclaration(VariableDeclaration variableDeclaration, object data) { BlockStatement blockStatement = (BlockStatement)AstUtil.GetParentOfType(variableDeclaration, typeof(BlockStatement)); if (blockStatement != null) { int hashCode = blockStatement.GetHashCode(); if (!(blockStatement.Parent is MethodDeclaration) && !localVariables.Contains(variableDeclaration.Name)) { localVariables.Add(variableDeclaration.Name, variableDeclaration); } else if (HasConflict(variableDeclaration)) { string newName = renamer.GetNewName(variableDeclaration.Name); renamedVariables.Add(variableDeclaration.Name + "_" + hashCode, newName); variableDeclaration.Name = newName; } } return(base.TrackedVisitVariableDeclaration(variableDeclaration, data)); }
public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data) { string identifier = ContainsIdentifier(identifierExpression); if (identifier != null) { TypeReferenceExpression replacedIdentifier = new TypeReferenceExpression(identifier); replacedIdentifier.Parent = identifierExpression.Parent; ReplaceCurrentNode(replacedIdentifier); } else { TypeDeclaration typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(identifierExpression, typeof(TypeDeclaration)); if (typeDeclaration != null) { CheckThroughParents(typeDeclaration, identifierExpression); } } return(null); }
private bool HasConflictingVariable(BlockStatement blockStatement, VariableDeclaration variableDeclaration) { INode parentScope = AstUtil.GetParentOfType(blockStatement, typeof(BlockStatement)); if (parentScope != null) { int enclosing = parentScope.GetHashCode(); INode varScope = GetParentScope(variableDeclaration); int varCode = varScope.GetHashCode(); if (varCode == enclosing) { return(true); } else { return(HasConflictingVariable((BlockStatement)parentScope, variableDeclaration)); } } else { return(false); } }
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); }
public override object TrackedVisitInvocationExpression(InvocationExpression invocationExpression, object data) { Expression targetObject = invocationExpression.TargetObject; string methodName = null; if (targetObject is IdentifierExpression) { methodName = ((IdentifierExpression)targetObject).Identifier; } else if (targetObject is FieldReferenceExpression) { methodName = ((FieldReferenceExpression)targetObject).FieldName; } if (methodName.StartsWith("set") || methodName.StartsWith("get")) { if (targetObject is IdentifierExpression) { TypeDeclaration typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(invocationExpression, typeof(TypeDeclaration)); string key; if (ContainsReferences(typeDeclaration, methodName, out key)) { IdentifierExpression identifierExpression = new IdentifierExpression((string)CodeBase.References[key]); if (methodName.StartsWith("get")) { identifierExpression.Parent = invocationExpression.Parent; ReplaceCurrentNode(identifierExpression); } else if (methodName.StartsWith("set")) { Expression setValue = (Expression)invocationExpression.Arguments[0]; AssignmentExpression assignment = new AssignmentExpression(identifierExpression, AssignmentOperatorType.Assign, setValue); assignment.Parent = invocationExpression.Parent; ReplaceCurrentNode(assignment); assignment.AcceptVisitor(this, data); } } } else if (targetObject is FieldReferenceExpression) { Expression target = ((FieldReferenceExpression)targetObject).TargetObject; TypeReference invokerType = GetExpressionType(target); if (invokerType != null) { string fullName = GetFullName(invokerType); string key = fullName + "." + methodName; if (CodeBase.References.Contains(key)) { string property = (string)CodeBase.References[key]; FieldReferenceExpression replaced = new FieldReferenceExpression(target, property); if (methodName.StartsWith("get")) { replaced.Parent = invocationExpression.Parent; ReplaceCurrentNode(replaced); replaced.AcceptVisitor(this, data); } else { Expression setValue = (Expression)invocationExpression.Arguments[0]; AssignmentExpression assignment = new AssignmentExpression(replaced, AssignmentOperatorType.Assign, setValue); assignment.Parent = invocationExpression.Parent; ReplaceCurrentNode(assignment); assignment.AcceptVisitor(this, data); } } } } } return(base.TrackedVisitInvocationExpression(invocationExpression, data)); }
public override object TrackedVisitInvocationExpression(InvocationExpression invocationExpression, object data) { if (invocationExpression.TargetObject is IdentifierExpression) { IdentifierExpression identifierExpression = (IdentifierExpression)invocationExpression.TargetObject; TypeDeclaration typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(invocationExpression, typeof(TypeDeclaration)); if (ExistMethodIn(typeDeclaration, invocationExpression)) { identifierExpression.Identifier = Renamer.GetNewName(identifierExpression.Identifier); } } else if (invocationExpression.TargetObject is FieldReferenceExpression) { FieldReferenceExpression fieldReferenceExpression = (FieldReferenceExpression)invocationExpression.TargetObject; Expression invoker = fieldReferenceExpression.TargetObject; if (fieldReferenceExpression.FieldName == "CallInternalMethod") { PrimitiveExpression methodName = (PrimitiveExpression)invocationExpression.Arguments[0]; if (methodName.Value.ToString().StartsWith("set") || methodName.Value.ToString().StartsWith("get")) { Expression obj = (Expression)invocationExpression.Arguments[1]; TypeReference objType = GetExpressionType(obj); if (objType != null) { string fullName = GetFullName(objType); if (CodeBase.Types.Contains(fullName)) { TypeDeclaration typeDeclaration = (TypeDeclaration)CodeBase.Types[fullName]; string propertyName = methodName.Value.ToString().Substring(3); if (ContainsProperty(typeDeclaration, propertyName)) { methodName.Value = methodName.Value.ToString().Insert(3, "_"); } else { methodName.Value = Renamer.GetNewName(methodName.Value.ToString()); } } } } else { methodName.Value = Renamer.GetNewName(methodName.Value.ToString()); } } TypeReference invokerType = GetExpressionType(invoker); if (invokerType != null) { string fullName = GetFullName(invokerType); if (CodeBase.Types.Contains(fullName) && !IsInExternalLibraries(fullName)) { TypeDeclaration typeDeclaration = (TypeDeclaration)CodeBase.Types[fullName]; if (ExistMethodIn(typeDeclaration, invocationExpression)) { fieldReferenceExpression.FieldName = Renamer.GetNewName(fieldReferenceExpression.FieldName); } } else { TypeMapping mapping = CodeBase.Mappings.GetCounterpart(fullName); string mapkey; if (ContainsMapping(mapping, invocationExpression, out mapkey)) { fieldReferenceExpression.FieldName = Renamer.GetNewName(fieldReferenceExpression.FieldName); } } } } return(base.TrackedVisitInvocationExpression(invocationExpression, data)); }
private string GetFile(TypeDeclaration type) { CompilationUnit compilationUnit = (CompilationUnit)AstUtil.GetParentOfType(type, typeof(CompilationUnit)); return(((TypeReference)compilationUnit.Parent).Type); }
public TypeReference GetType(Expression ex, INode parent) { if (ex is IdentifierExpression) { return(GetIdentifierType(ex, parent)); } else if (ex is FieldReferenceExpression) { FieldReferenceExpression fieldReference = (FieldReferenceExpression)ex; Expression targetObject = fieldReference.TargetObject; TypeReference targetType = GetType(targetObject); if (targetType != null) { string fullName = TypeResolver.GetFullName(targetType); if (targetType.RankSpecifier != null && targetType.RankSpecifier.Length > 0 && !(ex.Parent is IndexerExpression)) { fullName = "JavaArray"; } if (CodeBase.Types.Contains(fullName)) { TypeDeclaration typeDeclaration = (TypeDeclaration)CodeBase.Types[fullName]; if (typeDeclaration.Type == ClassType.Enum) { return(AstUtil.GetTypeReference(typeDeclaration.Name, ex.Parent)); } else { return(GetTypeInMembers(typeDeclaration, fieldReference.FieldName)); } } } return(null); } else if (ex is PrimitiveExpression) { return(GetConstantType((PrimitiveExpression)ex)); } else if (ex is InvocationExpression) { return(GetType(((InvocationExpression)ex).TargetObject)); } else if (ex is IndexerExpression) { return(GetType(((IndexerExpression)ex).TargetObject)); } else if (ex is BinaryOperatorExpression) { return(GetType(((BinaryOperatorExpression)ex).Left)); } else if (ex is ObjectCreateExpression) { return(((ObjectCreateExpression)ex).CreateType); } else if (ex is ThisReferenceExpression) { TypeDeclaration ty = (TypeDeclaration)AstUtil.GetParentOfType(ex, typeof(TypeDeclaration)); return(AstUtil.GetTypeReference(ty.Name, ex.Parent)); } else if (ex is CastExpression) { CastExpression cast = (CastExpression)ex; return(cast.CastTo); } else if (ex is ArrayCreateExpression) { return(((ArrayCreateExpression)ex).CreateType); } else if (ex is BaseReferenceExpression) { TypeDeclaration typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(ex, typeof(TypeDeclaration)); if (typeDeclaration.BaseTypes.Count > 0) { return((TypeReference)typeDeclaration.BaseTypes[0]); } else { return(AstUtil.GetTypeReference("System.Object", parent)); } } else if (ex is ParenthesizedExpression) { return(GetType(((ParenthesizedExpression)ex).Expression)); } else if (ex is TypeReferenceExpression) { return(((TypeReferenceExpression)ex).TypeReference); } else if (ex is AssignmentExpression) { return(GetType(((AssignmentExpression)ex).Left)); } else if (ex is UnaryOperatorExpression) { return(GetType(((UnaryOperatorExpression)ex).Expression)); } else if (ex is TypeOfExpression) { TypeReference typeReference = new TypeReference("java.lang.Class"); typeReference.Parent = parent; return(typeReference); } else if (ex is TypeOfIsExpression) { return(GetType(((TypeOfIsExpression)ex).Expression)); } else if (ex is ConditionalExpression) { return(GetType(((ConditionalExpression)ex).TrueExpression)); } else if (ex is ParameterDeclarationExpression) { return(((ParameterDeclarationExpression)ex).TypeReference); } else if (ex == Expression.Null) { return(null); } else { throw new NotSupportedException(ex.GetType().Name); } }
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); }