Example #1
0
        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));
        }
Example #2
0
        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 void TestFixtureSetup()
        {
            CodeBase = new CodeBase(SupportedLanguage.Java);
            CodeBase.Types.LibrariesFolder = @"../../../Translator/Libraries";

            AstUtil = new AstUtil();

            TypeResolver = new TypeResolver();
            TypeResolver.CodeBase = CodeBase;
            TypeResolver.AstUtil = AstUtil;
        }
        public void TestFixtureSetUp()
        {
            CodeBase = new CodeBase(SupportedLanguage.Java);
            AstUtil = new AstUtil();

            CodeBase.Types.LibrariesFolder = @"../../../Translator/Libraries";
            CodeBase.Mappings = new Mappings();
            CodeBase.Mappings.Add("String", new TypeMapping("string"));
        }
        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));
        }
Example #6
0
        private string GetFile(TypeDeclaration type)
        {
            CompilationUnit compilationUnit = (CompilationUnit)AstUtil.GetParentOfType(type, typeof(CompilationUnit));

            return(((TypeReference)compilationUnit.Parent).Type);
        }