Esempio n. 1
0
 private void AppendDefinitionType(TextBuffer buffer)
 {
     if (DecompilerContext.GetOption(IFernflowerPreferences.Use_Debug_Var_Names))
     {
         MethodWrapper method = (MethodWrapper)DecompilerContext.GetProperty(DecompilerContext
                                                                             .Current_Method_Wrapper);
         if (method != null)
         {
             int?originalIndex = null;
             if (processor != null)
             {
                 originalIndex = processor.GetVarOriginalIndex(index);
             }
             if (originalIndex != null)
             {
                 // first try from signature
                 if (DecompilerContext.GetOption(IFernflowerPreferences.Decompile_Generic_Signatures
                                                 ))
                 {
                     StructLocalVariableTypeTableAttribute attr = method.methodStruct.GetAttribute(StructGeneralAttribute
                                                                                                   .Attribute_Local_Variable_Type_Table);
                     if (attr != null)
                     {
                         string signature = attr.GetSignature(originalIndex, visibleOffset);
                         if (signature != null)
                         {
                             GenericFieldDescriptor descriptor = GenericMain.ParseFieldSignature(signature);
                             if (descriptor != null)
                             {
                                 buffer.Append(GenericMain.GetGenericCastTypeName(descriptor.type));
                                 return;
                             }
                         }
                     }
                 }
                 // then try from descriptor
                 StructLocalVariableTableAttribute attr_1 = method.methodStruct.GetLocalVariableAttr
                                                                ();
                 if (attr_1 != null)
                 {
                     string descriptor = attr_1.GetDescriptor(originalIndex, visibleOffset);
                     if (descriptor != null)
                     {
                         buffer.Append(ExprProcessor.GetCastTypeName(new VarType(descriptor)));
                         return;
                     }
                 }
             }
         }
     }
     buffer.Append(ExprProcessor.GetCastTypeName(GetVarType()));
 }
Esempio n. 2
0
        private bool IsAmbiguous()
        {
            MethodWrapper method = (MethodWrapper)DecompilerContext.GetProperty(DecompilerContext
                                                                                .Current_Method_Wrapper);

            if (method != null)
            {
                StructLocalVariableTableAttribute attr = method.methodStruct.GetLocalVariableAttr
                                                             ();
                if (attr != null)
                {
                    return(attr.ContainsName(name));
                }
            }
            return(false);
        }
Esempio n. 3
0
        public virtual string GetDebugName(StructMethod method)
        {
            StructLocalVariableTableAttribute attr = method.GetLocalVariableAttr();

            if (attr != null && processor != null)
            {
                int?origIndex = processor.GetVarOriginalIndex(index);
                if (origIndex != null)
                {
                    string name = attr.GetName(origIndex.Value, visibleOffset);
                    if (name != null && TextUtil.IsValidIdentifier(name, method.GetClassStruct().GetBytecodeVersion
                                                                       ()))
                    {
                        return(name);
                    }
                }
            }
            return(null);
        }
Esempio n. 4
0
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual Dictionary <string, StructGeneralAttribute> ReadAttributes
            (DataInputFullStream @in, ConstantPool pool)
        {
            int length = @in.ReadUnsignedShort();
            Dictionary <string, StructGeneralAttribute> attributes = new Dictionary <string, StructGeneralAttribute
                                                                                     >(length);

            for (int i = 0; i < length; i++)
            {
                int    nameIndex = @in.ReadUnsignedShort();
                string name      = pool.GetPrimitiveConstant(nameIndex).GetString();
                StructGeneralAttribute attribute = ReadAttribute(@in, pool, name);
                if (attribute != null)
                {
                    if (StructGeneralAttribute.Attribute_Local_Variable_Table.GetName().Equals(name) &&
                        attributes.ContainsKey(name))
                    {
                        // merge all variable tables
                        StructLocalVariableTableAttribute table = (StructLocalVariableTableAttribute)attributes
                                                                  .GetOrNull(name);
                        table.Add((StructLocalVariableTableAttribute)attribute);
                    }
                    else if (StructGeneralAttribute.Attribute_Local_Variable_Type_Table.GetName().Equals
                                 (name) && attributes.ContainsKey(name))
                    {
                        // merge all variable tables
                        StructLocalVariableTypeTableAttribute table = (StructLocalVariableTypeTableAttribute
                                                                       )attributes.GetOrNull(name);
                        table.Add((StructLocalVariableTypeTableAttribute)attribute);
                    }
                    else
                    {
                        Sharpen.Collections.Put(attributes, attribute.GetName(), attribute);
                    }
                }
            }
            return(attributes);
        }
Esempio n. 5
0
        public virtual void Init()
        {
            DecompilerContext.SetProperty(DecompilerContext.Current_Class, classStruct);
            DecompilerContext.SetProperty(DecompilerContext.Current_Class_Wrapper, this);
            DecompilerContext.GetLogger().StartClass(classStruct.qualifiedName);
            int maxSec = System.Convert.ToInt32(DecompilerContext.GetProperty(IFernflowerPreferences
                                                                              .Max_Processing_Method).ToString());
            bool testMode = DecompilerContext.GetOption(IFernflowerPreferences.Unit_Test_Mode
                                                        );

            foreach (StructMethod mt in classStruct.GetMethods())
            {
                DecompilerContext.GetLogger().StartMethod(mt.GetName() + " " + mt.GetDescriptor()
                                                          );
                MethodDescriptor md      = MethodDescriptor.ParseDescriptor(mt.GetDescriptor());
                VarProcessor     varProc = new VarProcessor(mt, md);
                DecompilerContext.StartMethod(varProc);
                VarNamesCollector vc      = varProc.GetVarNamesCollector();
                CounterContainer  counter = DecompilerContext.GetCounterContainer();
                RootStatement     root    = null;
                bool isError = false;
                try
                {
                    if (mt.ContainsCode())
                    {
                        if (maxSec == 0 || testMode)
                        {
                            root = MethodProcessorRunnable.CodeToJava(mt, md, varProc);
                        }
                        else
                        {
                            MethodProcessorRunnable mtProc = new MethodProcessorRunnable(mt, md, varProc, DecompilerContext
                                                                                         .GetCurrentContext());
                            Thread mtThread = new Thread(o => mtProc.Run())
                            {
                                Name = "Java decompiler"
                            };
                            long stopAt = Runtime.CurrentTimeMillis() + maxSec * 1000L;
                            mtThread.Start();
                            while (!mtProc.IsFinished())
                            {
                                try
                                {
                                    lock (mtProc.Lock)
                                    {
                                        Thread.Sleep(200);
                                    }
                                }
                                catch (Exception e)
                                {
                                    KillThread(mtThread);
                                    throw;
                                }
                                if (Runtime.CurrentTimeMillis() >= stopAt)
                                {
                                    string message = "Processing time limit exceeded for method " + mt.GetName() + ", execution interrupted.";
                                    DecompilerContext.GetLogger().WriteMessage(message, IFernflowerLogger.Severity.Error
                                                                               );
                                    KillThread(mtThread);
                                    isError = true;
                                    break;
                                }
                            }
                            if (!isError)
                            {
                                root = mtProc.GetResult();
                            }
                        }
                    }
                    else
                    {
                        bool thisVar    = !mt.HasModifier(ICodeConstants.Acc_Static);
                        int  paramCount = 0;
                        if (thisVar)
                        {
                            Sharpen.Collections.Put(varProc.GetThisVars(), new VarVersionPair(0, 0), classStruct
                                                    .qualifiedName);
                            paramCount = 1;
                        }
                        paramCount += [email protected];
                        int varIndex = 0;
                        for (int i = 0; i < paramCount; i++)
                        {
                            varProc.SetVarName(new VarVersionPair(varIndex, 0), vc.GetFreeName(varIndex));
                            if (thisVar)
                            {
                                if (i == 0)
                                {
                                    varIndex++;
                                }
                                else
                                {
                                    varIndex += md.@params[i - 1].stackSize;
                                }
                            }
                            else
                            {
                                varIndex += md.@params[i].stackSize;
                            }
                        }
                    }
                }
                catch (Exception t)
                {
                    string message = "Method " + mt.GetName() + " " + mt.GetDescriptor() + " couldn't be decompiled.";
                    DecompilerContext.GetLogger().WriteMessage(message, IFernflowerLogger.Severity.Warn
                                                               , t);
                    isError = true;
                }
                MethodWrapper methodWrapper = new MethodWrapper(root, varProc, mt, counter);
                methodWrapper.decompiledWithErrors = isError;
                methods.AddWithKey(methodWrapper, InterpreterUtil.MakeUniqueKey(mt.GetName(), mt.
                                                                                GetDescriptor()));
                if (!isError)
                {
                    // rename vars so that no one has the same name as a field
                    VarNamesCollector namesCollector = new VarNamesCollector();
                    classStruct.GetFields().ForEach((StructField f) => namesCollector.AddName(f.GetName
                                                                                                  ()));
                    varProc.RefreshVarNames(namesCollector);
                    // if debug information present and should be used
                    if (DecompilerContext.GetOption(IFernflowerPreferences.Use_Debug_Var_Names))
                    {
                        StructLocalVariableTableAttribute attr = mt.GetLocalVariableAttr();
                        if (attr != null)
                        {
                            // only param names here
                            varProc.SetDebugVarNames(attr.GetMapParamNames());
                            // the rest is here
                            methodWrapper.GetOrBuildGraph().IterateExprents((Exprent exprent) => {
                                List <Exprent> lst = exprent.GetAllExprents(true);
                                lst.Add(exprent);
                                lst.Where(e => e.type == Exprent.Exprent_Var).ToList().ForEach((Exprent
                                                                                                e) => {
                                    VarExprent varExprent = (VarExprent)e;
                                    string name           = varExprent.GetDebugName(mt);
                                    if (name != null)
                                    {
                                        varProc.SetVarName(varExprent.GetVarVersionPair(), name);
                                    }
                                }
                                                                                               );
                                return(0);
                            }
                                                                            );
                        }
                    }
                }
                DecompilerContext.GetLogger().EndMethod();
            }
            DecompilerContext.GetLogger().EndClass();
        }