Ejemplo n.º 1
0
 private static bool IsQualifiedNewGetClass(Exprent first, Exprent second)
 {
     if (first.type == Exprent.Exprent_Invocation)
     {
         InvocationExprent invocation = (InvocationExprent)first;
         if (!invocation.IsStatic() && invocation.GetInstance().type == Exprent.Exprent_Var &&
             invocation.GetName().Equals("getClass") && invocation.GetStringDescriptor().
             Equals("()Ljava/lang/Class;"))
         {
             List <Exprent> lstExprents = second.GetAllExprents();
             lstExprents.Add(second);
             foreach (Exprent expr in lstExprents)
             {
                 if (expr.type == Exprent.Exprent_New)
                 {
                     NewExprent newExpr = (NewExprent)expr;
                     if (newExpr.GetConstructor() != null && !(newExpr.GetConstructor().GetLstParameters
                                                                   ().Count == 0) && newExpr.GetConstructor().GetLstParameters()[0].Equals(invocation
                                                                                                                                           .GetInstance()))
                     {
                         string classname = newExpr.GetNewType().value;
                         ClassesProcessor.ClassNode node = DecompilerContext.GetClassProcessor().GetMapRootClasses
                                                               ().GetOrNull(classname);
                         if (node != null && node.type != ClassesProcessor.ClassNode.Class_Root)
                         {
                             return(true);
                         }
                     }
                 }
             }
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
 private static bool IsNewConcat(NewExprent expr, VarType cltype)
 {
     if (expr.GetNewType().Equals(cltype))
     {
         VarType[] @params = expr.GetConstructor().GetDescriptor().@params;
         return(@params.Length == 0 || @params.Length == 1 && @params[0].Equals(VarType.Vartype_String
                                                                                ));
     }
     return(false);
 }
Ejemplo n.º 3
0
        private static InvocationExprent IsAssertionError(Statement stat)
        {
            if (stat == null || stat.GetExprents() == null || stat.GetExprents().Count != 1)
            {
                return(null);
            }
            Exprent expr = stat.GetExprents()[0];

            if (expr.type == Exprent.Exprent_Exit)
            {
                ExitExprent exexpr = (ExitExprent)expr;
                if (exexpr.GetExitType() == ExitExprent.Exit_Throw && exexpr.GetValue().type == Exprent
                    .Exprent_New)
                {
                    NewExprent nexpr = (NewExprent)exexpr.GetValue();
                    if (Class_Assertion_Error.Equals(nexpr.GetNewType()) && nexpr.GetConstructor() !=
                        null)
                    {
                        return(nexpr.GetConstructor());
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 4
0
        // propagate (var = new X) forward to the <init> invocation
        private static bool IsConstructorInvocationRemote(List <Exprent> list, int index)
        {
            Exprent current = list[index];

            if (current.type == Exprent.Exprent_Assignment)
            {
                AssignmentExprent @as = (AssignmentExprent)current;
                if (@as.GetLeft().type == Exprent.Exprent_Var && @as.GetRight().type == Exprent.Exprent_New)
                {
                    NewExprent     newExpr  = (NewExprent)@as.GetRight();
                    VarType        newType  = newExpr.GetNewType();
                    VarVersionPair leftPair = new VarVersionPair((VarExprent)@as.GetLeft());
                    if (newType.type == ICodeConstants.Type_Object && newType.arrayDim == 0 && newExpr
                        .GetConstructor() == null)
                    {
                        for (int i = index + 1; i < list.Count; i++)
                        {
                            Exprent remote = list[i];
                            // <init> invocation
                            if (remote.type == Exprent.Exprent_Invocation)
                            {
                                InvocationExprent @in = (InvocationExprent)remote;
                                if (@in.GetFunctype() == InvocationExprent.Typ_Init && @in.GetInstance().type ==
                                    Exprent.Exprent_Var && @as.GetLeft().Equals(@in.GetInstance()))
                                {
                                    newExpr.SetConstructor(@in);
                                    @in.SetInstance(null);
                                    list[i] = @as.Copy();
                                    return(true);
                                }
                            }
                            // check for variable in use
                            HashSet <VarVersionPair> setVars = remote.GetAllVariables();
                            if (setVars.Contains(leftPair))
                            {
                                // variable used somewhere in between -> exit, need a better reduced code
                                return(false);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 5
0
        public static Exprent ContractStringConcat(Exprent expr)
        {
            Exprent exprTmp = null;
            VarType cltype  = null;

            // first quick test
            if (expr.type == Exprent.Exprent_Invocation)
            {
                InvocationExprent iex = (InvocationExprent)expr;
                if ("toString".Equals(iex.GetName()))
                {
                    if (builderClass.Equals(iex.GetClassname()))
                    {
                        cltype = builderType;
                    }
                    else if (bufferClass.Equals(iex.GetClassname()))
                    {
                        cltype = bufferType;
                    }
                    if (cltype != null)
                    {
                        exprTmp = iex.GetInstance();
                    }
                }
                else if ("makeConcatWithConstants".Equals(iex.GetName()))
                {
                    // java 9 style
                    List <Exprent> parameters = ExtractParameters(iex.GetBootstrapArguments(), iex);
                    if (parameters.Count >= 2)
                    {
                        return(CreateConcatExprent(parameters, expr.bytecode));
                    }
                }
            }
            if (exprTmp == null)
            {
                return(expr);
            }
            // iterate in depth, collecting possible operands
            List <Exprent> lstOperands = new List <Exprent>();

            while (true)
            {
                int found = 0;
                switch (exprTmp.type)
                {
                case Exprent.Exprent_Invocation:
                {
                    InvocationExprent iex = (InvocationExprent)exprTmp;
                    if (IsAppendConcat(iex, cltype))
                    {
                        lstOperands.Add(0, iex.GetLstParameters()[0]);
                        exprTmp = iex.GetInstance();
                        found   = 1;
                    }
                    break;
                }

                case Exprent.Exprent_New:
                {
                    NewExprent nex = (NewExprent)exprTmp;
                    if (IsNewConcat(nex, cltype))
                    {
                        VarType[] @params = nex.GetConstructor().GetDescriptor().@params;
                        if (@params.Length == 1)
                        {
                            lstOperands.Add(0, nex.GetConstructor().GetLstParameters()[0]);
                        }
                        found = 2;
                    }
                    break;
                }
                }
                if (found == 0)
                {
                    return(expr);
                }
                else if (found == 2)
                {
                    break;
                }
            }
            int first2str = 0;
            int index     = 0;

            while (index < lstOperands.Count && index < 2)
            {
                if (lstOperands[index].GetExprType().Equals(VarType.Vartype_String))
                {
                    first2str |= (index + 1);
                }
                index++;
            }
            if (first2str == 0)
            {
                lstOperands.Add(0, new ConstExprent(VarType.Vartype_String, string.Empty, expr.bytecode
                                                    ));
            }
            // remove redundant String.valueOf
            for (int i = 0; i < lstOperands.Count; i++)
            {
                Exprent rep = RemoveStringValueOf(lstOperands[i]);
                bool    ok  = (i > 1);
                if (!ok)
                {
                    bool isstr = rep.GetExprType().Equals(VarType.Vartype_String);
                    ok = isstr || first2str != i + 1;
                    if (i == 0)
                    {
                        first2str &= 2;
                    }
                }
                if (ok)
                {
                    lstOperands[i] = rep;
                }
            }
            return(CreateConcatExprent(lstOperands, expr.bytecode));
        }