예제 #1
0
        public void AddUsing(NamespaceDeclaration namespaceDeclaration, IList usings, UsingDeclaration usingDeclaration)
        {
            Using  usin  = (Using)usingDeclaration.Usings[0];
            string alias = null;
            string name  = usin.Name;

            if (!usin.IsAlias)
            {
                name = name.Substring(name.LastIndexOf('.') + 1);
            }
            else
            {
                alias = usin.Alias.Type;
            }

            if (CodeBase.References.Contains(name))
            {
                string reference = (string)CodeBase.References[name];
                alias = alias.Replace("." + name, "." + reference);
                TypeReference    typeReference          = AstUtil.GetTypeReference(alias, namespaceDeclaration);
                UsingDeclaration addingUsingDeclaration = new UsingDeclaration(reference, typeReference);
                if (!Contains(usings, addingUsingDeclaration))
                {
                    namespaceDeclaration.Children.Insert(0, addingUsingDeclaration);
                }
            }
        }
예제 #2
0
        public void RemoveCurrentNamespaceUsings()
        {
            string program = @"package Janett.Translator;
								public class Translation
								{
								}"                                ;

            string expected = @"namespace Janett.Translator
								{
									public class Translation
									{
									}
								}"                                ;

            CompilationUnit      cu = TestUtil.ParseProgram(program);
            NamespaceDeclaration ns = (NamespaceDeclaration)cu.Children[0];

            UsingDeclaration us1 = new UsingDeclaration("Refactoring", AstUtil.GetTypeReference("Janett.Translator.Refactoring", ns));
            UsingDeclaration us2 = new UsingDeclaration("Transformation", AstUtil.GetTypeReference("Janett.Translator.Transformation", ns));

            ns.Children.Insert(0, us2);
            ns.Children.Insert(0, us1);
            us1.Parent = ns;
            us2.Parent = ns;

            VisitCompilationUnit(cu, null);
            TestUtil.CodeEqual(expected, TestUtil.GenerateCode(cu));
        }
예제 #3
0
 public string GetStaticFullName(string identifier, INode parent)
 {
     if (Char.IsUpper(identifier[0]))
     {
         TypeReference typeRef = AstUtil.GetTypeReference(identifier, parent);
         return(GetFullName(typeRef));
     }
     else
     {
         return(null);
     }
 }
예제 #4
0
        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);
            }
        }
예제 #5
0
        public void Used()
        {
            string program  = TestUtil.PackageMemberParse("public class A {object obj = new ToStringBuilder();}");
            string expected = TestUtil.NamespaceMemberParse("using ToStringBuilder = NClassifier.Util.ToStringBuilder; public class A {object obj = new ToStringBuilder();}");

            CompilationUnit      cu = TestUtil.ParseProgram(program);
            NamespaceDeclaration ns = (NamespaceDeclaration)cu.Children[0];

            UsingDeclaration us = new UsingDeclaration("ToStringBuilder", AstUtil.GetTypeReference("NClassifier.Util.ToStringBuilder", ns));

            ns.Children.Insert(0, us);
            us.Parent = ns;

            Mode = "DotNet";
            VisitCompilationUnit(cu, null);
            TestUtil.CodeEqual(expected, TestUtil.GenerateCode(cu));
        }
예제 #6
0
 private void CheckThroughParents(TypeDeclaration typeDeclaration, IdentifierExpression identifierExpression)
 {
     if (typeDeclaration.Parent is TypeDeclaration)
     {
         TypeDeclaration parent = (TypeDeclaration)typeDeclaration.Parent;
         CheckThroughParents(parent, identifierExpression);
     }
     if (typeDeclaration.BaseTypes.Count > 0)
     {
         foreach (TypeReference baseType in typeDeclaration.BaseTypes)
         {
             string fullName = GetFullName(baseType);
             if (CodeBase.References.Contains(fullName))
             {
                 string          referedBaseType     = (string)CodeBase.References[fullName];
                 TypeReference   newBaseType         = AstUtil.GetTypeReference(referedBaseType, baseType.Parent);
                 string          referenceBaseType   = GetFullName(newBaseType);
                 TypeDeclaration baseTypeDeclaration = (TypeDeclaration)CodeBase.Types[referenceBaseType];
                 if (baseTypeDeclaration != null)
                 {
                     if (DefinedInFieldsClass(baseTypeDeclaration, identifierExpression.Identifier))
                     {
                         TypeReferenceExpression  id       = new TypeReferenceExpression(referedBaseType);
                         FieldReferenceExpression replaced = new FieldReferenceExpression(id, identifierExpression.Identifier);
                         replaced.Parent = identifierExpression.Parent;
                         ReplaceCurrentNode(replaced);
                     }
                     else
                     {
                         TypeDeclaration type = (TypeDeclaration)CodeBase.Types[fullName];
                         CheckThroughParents(type, identifierExpression);
                     }
                 }
             }
             else if (CodeBase.Types.Contains(fullName))
             {
                 TypeDeclaration baseTypeDeclaration = (TypeDeclaration)CodeBase.Types[fullName];
                 CheckThroughParents(baseTypeDeclaration, identifierExpression);
             }
         }
     }
 }
예제 #7
0
        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);
        }
예제 #8
0
 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);
     }
 }
예제 #9
0
        private void ReplaceMember(InvocationExpression invocationExpression, object data, TypeReference invokerType)
        {
            Expression[] replacedExpression;
            string       returnType = GetFullName(invokerType);

            if (invokerType.RankSpecifier != null && invokerType.RankSpecifier.Length > 0)
            {
                returnType = "JavaArray";
            }

            TypeMapping mapping = CodeBase.Mappings[returnType];
            string      methodKey;

            if (ContainsMapping(mapping, invocationExpression, out methodKey))
            {
                replacedExpression = GetReplacedExpression(invocationExpression, methodKey, mapping.Members);
                if (replacedExpression[0] == Expression.Null)
                {
                    RemoveCurrentNode();
                    ((IList)data).Add(invocationExpression);
                }
                else
                {
                    invocationExpression.TypeArguments.Add(null);
                    ReplaceCurrentNode(invocationExpression, replacedExpression, data);
                }
            }
            else if (invocationExpression.TargetObject is FieldReferenceExpression || invocationExpression.TargetObject is IdentifierExpression)
            {
                Expression invocationTarget = invocationExpression.TargetObject;
                string     methodName       = null;

                if (invocationTarget is FieldReferenceExpression)
                {
                    methodName = ((FieldReferenceExpression)invocationTarget).FieldName;
                }
                else if (invocationTarget is IdentifierExpression)
                {
                    methodName = ((IdentifierExpression)invocationTarget).Identifier;
                }

                mapping = GetProperMapping(invokerType, invocationExpression, methodName, out methodKey);

                if (Mode == "DotNet" && methodName == "clone" && CodeBase.Types.Contains(returnType))
                {
                    TypeDeclaration typeDeclaration = (TypeDeclaration)CodeBase.Types[returnType];
                    TypeReference   cloneableType   = AstUtil.GetTypeReference("java.lang.Cloneable", invocationTarget);
                    if (Extends(typeDeclaration, cloneableType))
                    {
                        mapping   = CodeBase.Mappings[cloneableType.Type];
                        methodKey = "clone()";
                    }
                }

                if (mapping != null)
                {
                    replacedExpression = GetReplacedExpression(invocationExpression, methodKey, mapping.Members);
                    ReplaceCurrentNode(invocationExpression, replacedExpression, data);
                }
            }
        }