Exemplo n.º 1
0
 private static void HideEmptySuper(ClassWrapper wrapper)
 {
     foreach (MethodWrapper method in wrapper.GetMethods())
     {
         if (ICodeConstants.Init_Name.Equals(method.methodStruct.GetName()) && method.root
             != null)
         {
             Statement firstData = Statements.FindFirstData(method.root);
             if (firstData == null || (firstData.GetExprents().Count == 0))
             {
                 return;
             }
             Exprent exprent = firstData.GetExprents()[0];
             if (exprent.type == Exprent.Exprent_Invocation)
             {
                 InvocationExprent invExpr = (InvocationExprent)exprent;
                 if (Statements.IsInvocationInitConstructor(invExpr, method, wrapper, false) && (invExpr
                                                                                                 .GetLstParameters().Count == 0))
                 {
                     firstData.GetExprents().RemoveAtReturningValue(0);
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        public static void ClearEnum(ClassWrapper wrapper)
        {
            StructClass cl = wrapper.GetClassStruct();

            // hide values/valueOf methods and super() invocations
            foreach (MethodWrapper method in wrapper.GetMethods())
            {
                StructMethod mt         = method.methodStruct;
                string       name       = mt.GetName();
                string       descriptor = mt.GetDescriptor();
                if ("values".Equals(name))
                {
                    if (descriptor.Equals("()[L" + cl.qualifiedName + ";"))
                    {
                        wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(name, descriptor));
                    }
                }
                else if ("valueOf".Equals(name))
                {
                    if (descriptor.Equals("(Ljava/lang/String;)L" + cl.qualifiedName + ";"))
                    {
                        wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(name, descriptor));
                    }
                }
                else if (ICodeConstants.Init_Name.Equals(name))
                {
                    Statement firstData = Statements.FindFirstData(method.root);
                    if (firstData != null && !(firstData.GetExprents().Count == 0))
                    {
                        Exprent exprent = firstData.GetExprents()[0];
                        if (exprent.type == Exprent.Exprent_Invocation)
                        {
                            InvocationExprent invExpr = (InvocationExprent)exprent;
                            if (Statements.IsInvocationInitConstructor(invExpr, method, wrapper, false))
                            {
                                firstData.GetExprents().RemoveAtReturningValue(0);
                            }
                        }
                    }
                }
            }
            // hide synthetic fields of enum and it's constants
            foreach (StructField fd in cl.GetFields())
            {
                string descriptor = fd.GetDescriptor();
                if (fd.IsSynthetic() && descriptor.Equals("[L" + cl.qualifiedName + ";"))
                {
                    wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(fd.GetName(), descriptor
                                                                                 ));
                }
            }
        }
Exemplo n.º 3
0
 private static void LiftConstructor(ClassWrapper wrapper)
 {
     foreach (MethodWrapper method in wrapper.GetMethods())
     {
         if (ICodeConstants.Init_Name.Equals(method.methodStruct.GetName()) && method.root
             != null)
         {
             Statement firstData = Statements.FindFirstData(method.root);
             if (firstData == null)
             {
                 return;
             }
             int            index       = 0;
             List <Exprent> lstExprents = firstData.GetExprents();
             foreach (Exprent exprent in lstExprents)
             {
                 int action = 0;
                 if (exprent.type == Exprent.Exprent_Assignment)
                 {
                     AssignmentExprent assignExpr = (AssignmentExprent)exprent;
                     if (assignExpr.GetLeft().type == Exprent.Exprent_Field && assignExpr.GetRight().type
                         == Exprent.Exprent_Var)
                     {
                         FieldExprent fExpr = (FieldExprent)assignExpr.GetLeft();
                         if (fExpr.GetClassname().Equals(wrapper.GetClassStruct().qualifiedName))
                         {
                             StructField structField = wrapper.GetClassStruct().GetField(fExpr.GetName(), fExpr
                                                                                         .GetDescriptor().descriptorString);
                             if (structField != null && structField.HasModifier(ICodeConstants.Acc_Final))
                             {
                                 action = 1;
                             }
                         }
                     }
                 }
                 else if (index > 0 && exprent.type == Exprent.Exprent_Invocation && Statements.IsInvocationInitConstructor
                              ((InvocationExprent)exprent, method, wrapper, true))
                 {
                     // this() or super()
                     lstExprents.Add(0, lstExprents.RemoveAtReturningValue(index));
                     action = 2;
                 }
                 if (action != 1)
                 {
                     break;
                 }
                 index++;
             }
         }
     }
 }
Exemplo n.º 4
0
        private static void ExtractStaticInitializers(ClassWrapper wrapper, MethodWrapper
                                                      method)
        {
            RootStatement root      = method.root;
            StructClass   cl        = wrapper.GetClassStruct();
            Statement     firstData = Statements.FindFirstData(root);

            if (firstData != null)
            {
                bool inlineInitializers = cl.HasModifier(ICodeConstants.Acc_Interface) || cl.HasModifier
                                              (ICodeConstants.Acc_Enum);
                while (!(firstData.GetExprents().Count == 0))
                {
                    Exprent exprent = firstData.GetExprents()[0];
                    bool    found   = false;
                    if (exprent.type == Exprent.Exprent_Assignment)
                    {
                        AssignmentExprent assignExpr = (AssignmentExprent)exprent;
                        if (assignExpr.GetLeft().type == Exprent.Exprent_Field)
                        {
                            FieldExprent fExpr = (FieldExprent)assignExpr.GetLeft();
                            if (fExpr.IsStatic() && fExpr.GetClassname().Equals(cl.qualifiedName) && cl.HasField
                                    (fExpr.GetName(), fExpr.GetDescriptor().descriptorString))
                            {
                                // interfaces fields should always be initialized inline
                                if (inlineInitializers || IsExprentIndependent(assignExpr.GetRight(), method))
                                {
                                    string keyField = InterpreterUtil.MakeUniqueKey(fExpr.GetName(), fExpr.GetDescriptor
                                                                                        ().descriptorString);
                                    if (!wrapper.GetStaticFieldInitializers().ContainsKey(keyField))
                                    {
                                        wrapper.GetStaticFieldInitializers().AddWithKey(assignExpr.GetRight(), keyField);
                                        firstData.GetExprents().RemoveAtReturningValue(0);
                                        found = true;
                                    }
                                }
                            }
                        }
                    }
                    if (!found)
                    {
                        break;
                    }
                }
            }
        }
Exemplo n.º 5
0
        private static void ExtractDynamicInitializers(ClassWrapper wrapper)
        {
            StructClass cl          = wrapper.GetClassStruct();
            bool        isAnonymous = DecompilerContext.GetClassProcessor().GetMapRootClasses().GetOrNull
                                          (cl.qualifiedName).type == ClassesProcessor.ClassNode.Class_Anonymous;
            List <List <Exprent> > lstFirst          = new List <List <Exprent> >();
            List <MethodWrapper>   lstMethodWrappers = new List <MethodWrapper>();

            foreach (MethodWrapper method in wrapper.GetMethods())
            {
                if (ICodeConstants.Init_Name.Equals(method.methodStruct.GetName()) && method.root
                    != null)
                {
                    // successfully decompiled constructor
                    Statement firstData = Statements.FindFirstData(method.root);
                    if (firstData == null || (firstData.GetExprents().Count == 0))
                    {
                        return;
                    }
                    lstFirst.Add(firstData.GetExprents());
                    lstMethodWrappers.Add(method);
                    Exprent exprent = firstData.GetExprents()[0];
                    if (!isAnonymous)
                    {
                        // FIXME: doesn't make sense
                        if (exprent.type != Exprent.Exprent_Invocation || !Statements.IsInvocationInitConstructor
                                ((InvocationExprent)exprent, method, wrapper, false))
                        {
                            return;
                        }
                    }
                }
            }
            if ((lstFirst.Count == 0))
            {
                return;
            }
            while (true)
            {
                string  fieldWithDescr = null;
                Exprent value          = null;
                for (int i = 0; i < lstFirst.Count; i++)
                {
                    List <Exprent> lst = lstFirst[i];
                    if (lst.Count < (isAnonymous ? 1 : 2))
                    {
                        return;
                    }
                    Exprent exprent = lst[isAnonymous ? 0 : 1];
                    bool    found   = false;
                    if (exprent.type == Exprent.Exprent_Assignment)
                    {
                        AssignmentExprent assignExpr = (AssignmentExprent)exprent;
                        if (assignExpr.GetLeft().type == Exprent.Exprent_Field)
                        {
                            FieldExprent fExpr = (FieldExprent)assignExpr.GetLeft();
                            if (!fExpr.IsStatic() && fExpr.GetClassname().Equals(cl.qualifiedName) && cl.HasField
                                    (fExpr.GetName(), fExpr.GetDescriptor().descriptorString))
                            {
                                // check for the physical existence of the field. Could be defined in a superclass.
                                if (IsExprentIndependent(assignExpr.GetRight(), lstMethodWrappers[i]))
                                {
                                    string fieldKey = InterpreterUtil.MakeUniqueKey(fExpr.GetName(), fExpr.GetDescriptor
                                                                                        ().descriptorString);
                                    if (fieldWithDescr == null)
                                    {
                                        fieldWithDescr = fieldKey;
                                        value          = assignExpr.GetRight();
                                    }
                                    else if (!fieldWithDescr.Equals(fieldKey) || !value.Equals(assignExpr.GetRight()))
                                    {
                                        return;
                                    }
                                    found = true;
                                }
                            }
                        }
                    }
                    if (!found)
                    {
                        return;
                    }
                }
                if (!wrapper.GetDynamicFieldInitializers().ContainsKey(fieldWithDescr))
                {
                    wrapper.GetDynamicFieldInitializers().AddWithKey(value, fieldWithDescr);
                    foreach (List <Exprent> lst in lstFirst)
                    {
                        lst.RemoveAtReturningValue(isAnonymous ? 0 : 1);
                    }
                }
                else
                {
                    return;
                }
            }
        }