public static List <VarVersionPair> GetSyntheticParametersMask(ClassesProcessor.ClassNode node, string descriptor, int parameters) { List <VarVersionPair> mask = null; ClassWrapper wrapper = node.GetWrapper(); if (wrapper != null) { // own class MethodWrapper methodWrapper = wrapper.GetMethodWrapper(ICodeConstants.Init_Name, descriptor); if (methodWrapper == null) { if (DecompilerContext.GetOption(IFernflowerPreferences.Ignore_Invalid_Bytecode)) { return(null); } throw new Exception("Constructor " + node.classStruct.qualifiedName + "." + ICodeConstants .Init_Name + descriptor + " not found"); } mask = methodWrapper.synthParameters; } else if (parameters > 0 && node.type == ClassesProcessor.ClassNode.Class_Member && (node.access & ICodeConstants.Acc_Static) == 0) { // non-static member class mask = new List <VarVersionPair>(Enumerable.Repeat <VarVersionPair>(null, parameters)); mask[0] = new VarVersionPair(-1, 0); } return(mask); }
private void EliminateStaticAccess(ClassesProcessor.ClassNode node) { if (node.type == ClassesProcessor.ClassNode.Class_Lambda) { return; } foreach (MethodWrapper meth in node.GetWrapper().GetMethods()) { if (meth.root != null) { bool replaced = false; DirectGraph graph = meth.GetOrBuildGraph(); HashSet <DirectNode> setVisited = new HashSet <DirectNode>(); LinkedList <DirectNode> stack = new LinkedList <DirectNode>(); stack.AddLast(graph.first); while (!(stack.Count == 0)) { // TODO: replace with interface iterator? DirectNode nd = Sharpen.Collections.RemoveFirst(stack); if (setVisited.Contains(nd)) { continue; } setVisited.Add(nd); for (int i = 0; i < nd.exprents.Count; i++) { Exprent exprent = nd.exprents[i]; replaced |= ReplaceInvocations(node, meth, exprent); if (exprent.type == Exprent.Exprent_Invocation) { Exprent ret = ReplaceAccessExprent(node, meth, (InvocationExprent)exprent); if (ret != null) { nd.exprents[i] = ret; replaced = true; } } } Sharpen.Collections.AddAll(stack, nd.succs); } if (replaced) { ComputeMethodType(node, meth); } } } foreach (ClassesProcessor.ClassNode child in node.nested) { EliminateStaticAccess(child); } }
private void ComputeMethodTypes(ClassesProcessor.ClassNode node) { if (node.type == ClassesProcessor.ClassNode.Class_Lambda) { return; } foreach (ClassesProcessor.ClassNode nd in node.nested) { ComputeMethodTypes(nd); } foreach (MethodWrapper method in node.GetWrapper().GetMethods()) { ComputeMethodType(node, method); } }
public static void Simplify(SwitchStatement switchStatement) { SwitchExprent switchExprent = (SwitchExprent)switchStatement.GetHeadexprent(); Exprent value = switchExprent.GetValue(); if (IsEnumArray(value)) { List <List <Exprent> > caseValues = switchStatement.GetCaseValues(); Dictionary <Exprent, Exprent> mapping = new Dictionary <Exprent, Exprent>(caseValues .Count); ArrayExprent array = (ArrayExprent)value; FieldExprent arrayField = (FieldExprent)array.GetArray(); ClassesProcessor.ClassNode classNode = DecompilerContext.GetClassProcessor().GetMapRootClasses ().GetOrNull(arrayField.GetClassname()); if (classNode != null) { MethodWrapper wrapper = classNode.GetWrapper().GetMethodWrapper(ICodeConstants.Clinit_Name , "()V"); if (wrapper != null && wrapper.root != null) { wrapper.GetOrBuildGraph().IterateExprents((Exprent exprent) => { if (exprent is AssignmentExprent) { AssignmentExprent assignment = (AssignmentExprent)exprent; Exprent left = assignment.GetLeft(); if (left.type == Exprent.Exprent_Array && ((ArrayExprent)left).GetArray().Equals( arrayField)) { Sharpen.Collections.Put(mapping, assignment.GetRight(), ((InvocationExprent)((ArrayExprent )left).GetIndex()).GetInstance()); } } return(0); } ); } } List <List <Exprent> > realCaseValues = new List <List <Exprent> >(caseValues.Count); foreach (List <Exprent> caseValue in caseValues) { List <Exprent> values = new List <Exprent>(caseValue.Count); realCaseValues.Add(values); foreach (Exprent exprent in caseValue) { if (exprent == null) { values.Add(null); } else { Exprent realConst = mapping.GetOrNull(exprent); if (realConst == null) { DecompilerContext.GetLogger().WriteMessage("Unable to simplify switch on enum: " + exprent + " not found, available: " + mapping, IFernflowerLogger.Severity.Error ); return; } values.Add(realConst.Copy()); } } } caseValues.Clear(); Sharpen.Collections.AddAll(caseValues, realCaseValues); switchExprent.ReplaceExprent(value, ((InvocationExprent)array.GetIndex()).GetInstance ().Copy()); } }
private Exprent ReplaceAccessExprent(ClassesProcessor.ClassNode caller, MethodWrapper methdest, InvocationExprent invexpr) { ClassesProcessor.ClassNode node = DecompilerContext.GetClassProcessor().GetMapRootClasses ().GetOrNull(invexpr.GetClassname()); MethodWrapper methsource = null; if (node != null && node.GetWrapper() != null) { methsource = node.GetWrapper().GetMethodWrapper(invexpr.GetName(), invexpr.GetStringDescriptor ()); } if (methsource == null || !mapMethodType.ContainsKey(methsource)) { return(null); } // if same method, return if (node.classStruct.qualifiedName.Equals(caller.classStruct.qualifiedName) && methsource .methodStruct.GetName().Equals(methdest.methodStruct.GetName()) && methsource.methodStruct .GetDescriptor().Equals(methdest.methodStruct.GetDescriptor())) { // no recursive invocations permitted! return(null); } NestedMemberAccess.MethodAccess type = mapMethodType.GetOrNull(methsource); // // FIXME: impossible case. MethodAccess.NORMAL is not saved in the map // if(type == MethodAccess.NORMAL) { // return null; // } if (!SameTree(caller, node)) { return(null); } DirectGraph graph = methsource.GetOrBuildGraph(); Exprent source = graph.first.exprents[0]; Exprent retexprent = null; switch (type.ordinal()) { case 1: { ExitExprent exsource = (ExitExprent)source; if (exsource.GetValue().type == Exprent.Exprent_Var) { // qualified this VarExprent var = (VarExprent)exsource.GetValue(); string varname = methsource.varproc.GetVarName(new VarVersionPair(var)); if (!methdest.setOuterVarNames.Contains(varname)) { VarNamesCollector vnc = new VarNamesCollector(); vnc.AddName(varname); methdest.varproc.RefreshVarNames(vnc); methdest.setOuterVarNames.Add(varname); } int index = methdest.counter.GetCounterAndIncrement(CounterContainer.Var_Counter); VarExprent ret = new VarExprent(index, var.GetVarType(), methdest.varproc); methdest.varproc.SetVarName(new VarVersionPair(index, 0), varname); retexprent = ret; } else { // field FieldExprent ret = (FieldExprent)exsource.GetValue().Copy(); if (!ret.IsStatic()) { ret.ReplaceExprent(ret.GetInstance(), invexpr.GetLstParameters()[0]); } retexprent = ret; } break; } case 2: { AssignmentExprent ret_1; if (source.type == Exprent.Exprent_Exit) { ExitExprent extex = (ExitExprent)source; ret_1 = (AssignmentExprent)extex.GetValue().Copy(); } else { ret_1 = (AssignmentExprent)source.Copy(); } FieldExprent fexpr = (FieldExprent)ret_1.GetLeft(); if (fexpr.IsStatic()) { ret_1.ReplaceExprent(ret_1.GetRight(), invexpr.GetLstParameters()[0]); } else { ret_1.ReplaceExprent(ret_1.GetRight(), invexpr.GetLstParameters()[1]); fexpr.ReplaceExprent(fexpr.GetInstance(), invexpr.GetLstParameters()[0]); } // do not use copied bytecodes ret_1.GetLeft().bytecode = null; ret_1.GetRight().bytecode = null; retexprent = ret_1; break; } case 4: { retexprent = ReplaceFunction(invexpr, source); break; } case 3: { if (source.type == Exprent.Exprent_Exit) { source = ((ExitExprent)source).GetValue(); } InvocationExprent invret = (InvocationExprent)source.Copy(); int index_1 = 0; if (!invret.IsStatic()) { invret.ReplaceExprent(invret.GetInstance(), invexpr.GetLstParameters()[0]); index_1 = 1; } for (int i = 0; i < invret.GetLstParameters().Count; i++) { invret.ReplaceExprent(invret.GetLstParameters()[i], invexpr.GetLstParameters()[i + index_1]); } retexprent = invret; break; } } if (retexprent != null) { // preserve original bytecodes retexprent.bytecode = null; retexprent.AddBytecodeOffsets(invexpr.bytecode); // hide synthetic access method bool hide = true; if (node.type == ClassesProcessor.ClassNode.Class_Root || (node.access & ICodeConstants .Acc_Static) != 0) { StructMethod mt = methsource.methodStruct; if (!mt.IsSynthetic()) { hide = false; } } if (hide) { node.GetWrapper().GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(invexpr.GetName (), invexpr.GetStringDescriptor())); } } return(retexprent); }
public override TextBuffer ToJava(int indent, BytecodeMappingTracer tracer) { VarType leftType = left.GetExprType(); VarType rightType = right.GetExprType(); bool fieldInClassInit = false; bool hiddenField = false; if (left.type == Exprent.Exprent_Field) { // first assignment to a final field. Field name without "this" in front of it FieldExprent field = (FieldExprent)left; ClassesProcessor.ClassNode node = ((ClassesProcessor.ClassNode)DecompilerContext. GetProperty(DecompilerContext.Current_Class_Node)); if (node != null) { StructField fd = node.classStruct.GetField(field.GetName(), field.GetDescriptor() .descriptorString); if (fd != null) { if (field.IsStatic() && fd.HasModifier(ICodeConstants.Acc_Final)) { fieldInClassInit = true; } if (node.GetWrapper() != null && node.GetWrapper().GetHiddenMembers().Contains(InterpreterUtil .MakeUniqueKey(fd.GetName(), fd.GetDescriptor()))) { hiddenField = true; } } } } if (hiddenField) { return(new TextBuffer()); } TextBuffer buffer = new TextBuffer(); if (fieldInClassInit) { buffer.Append(((FieldExprent)left).GetName()); } else { buffer.Append(left.ToJava(indent, tracer)); } if (right.type == Exprent_Const) { ((ConstExprent)right).AdjustConstType(leftType); } TextBuffer res = right.ToJava(indent, tracer); if (condType == Condition_None && !leftType.IsSuperset(rightType) && (rightType.Equals (VarType.Vartype_Object) || leftType.type != ICodeConstants.Type_Object)) { if (right.GetPrecedence() >= FunctionExprent.GetPrecedence(FunctionExprent.Function_Cast )) { res.Enclose("(", ")"); } res.Prepend("(" + ExprProcessor.GetCastTypeName(leftType) + ")"); } buffer.Append(condType == Condition_None ? " = " : Operators[condType]).Append(res ); tracer.AddMapping(bytecode); return(buffer); }
// precedence of new public override TextBuffer ToJava(int indent, BytecodeMappingTracer tracer) { TextBuffer buf = new TextBuffer(); if (anonymous) { ClassesProcessor.ClassNode child = DecompilerContext.GetClassProcessor().GetMapRootClasses ().GetOrNull(newType.value); // IDEA-204310 - avoid backtracking later on for lambdas (causes spurious imports) if (!enumConst && (!lambda || DecompilerContext.GetOption(IFernflowerPreferences .Lambda_To_Anonymous_Class))) { string enclosing = null; if (!lambda && constructor != null) { enclosing = GetQualifiedNewInstance(child.anonymousClassType.value, constructor.GetLstParameters (), indent, tracer); if (enclosing != null) { buf.Append(enclosing).Append('.'); } } buf.Append("new "); string typename = ExprProcessor.GetCastTypeName(child.anonymousClassType); if (enclosing != null) { ClassesProcessor.ClassNode anonymousNode = DecompilerContext.GetClassProcessor(). GetMapRootClasses().GetOrNull(child.anonymousClassType.value); if (anonymousNode != null) { typename = anonymousNode.simpleName; } else { typename = Sharpen.Runtime.Substring(typename, typename.LastIndexOf('.') + 1); } } GenericClassDescriptor descriptor = ClassWriter.GetGenericClassDescriptor(child.classStruct ); if (descriptor != null) { if ((descriptor.superinterfaces.Count == 0)) { buf.Append(GenericMain.GetGenericCastTypeName(descriptor.superclass)); } else { if (descriptor.superinterfaces.Count > 1 && !lambda) { DecompilerContext.GetLogger().WriteMessage("Inconsistent anonymous class signature: " + child.classStruct.qualifiedName, IFernflowerLogger.Severity.Warn); } buf.Append(GenericMain.GetGenericCastTypeName(descriptor.superinterfaces[0])); } } else { buf.Append(typename); } } buf.Append('('); if (!lambda && constructor != null) { List <Exprent> parameters = constructor.GetLstParameters(); List <VarVersionPair> mask = child.GetWrapper().GetMethodWrapper(ICodeConstants.Init_Name , constructor.GetStringDescriptor()).synthParameters; if (mask == null) { InvocationExprent superCall = child.superInvocation; mask = ExprUtil.GetSyntheticParametersMask(superCall.GetClassname(), superCall.GetStringDescriptor (), parameters.Count); } int start = enumConst ? 2 : 0; bool firstParam = true; for (int i = start; i < parameters.Count; i++) { if (mask == null || mask[i] == null) { if (!firstParam) { buf.Append(", "); } ExprProcessor.GetCastedExprent(parameters[i], constructor.GetDescriptor().@params [i], buf, indent, true, tracer); firstParam = false; } } } buf.Append(')'); if (enumConst && buf.Length() == 2) { buf.SetLength(0); } if (lambda) { if (!DecompilerContext.GetOption(IFernflowerPreferences.Lambda_To_Anonymous_Class )) { buf.SetLength(0); } // remove the usual 'new <class>()', it will be replaced with lambda style '() ->' Exprent methodObject = constructor == null ? null : constructor.GetInstance(); TextBuffer clsBuf = new TextBuffer(); new ClassWriter().ClassLambdaToJava(child, clsBuf, methodObject, indent, tracer); buf.Append(clsBuf); tracer.IncrementCurrentSourceLine(clsBuf.CountLines()); } else { TextBuffer clsBuf = new TextBuffer(); new ClassWriter().ClassToJava(child, clsBuf, indent, tracer); buf.Append(clsBuf); tracer.IncrementCurrentSourceLine(clsBuf.CountLines()); } } else if (directArrayInit) { VarType leftType = newType.DecreaseArrayDim(); buf.Append('{'); for (int i = 0; i < lstArrayElements.Count; i++) { if (i > 0) { buf.Append(", "); } ExprProcessor.GetCastedExprent(lstArrayElements[i], leftType, buf, indent, false, tracer); } buf.Append('}'); } else if (newType.arrayDim == 0) { if (!enumConst) { string enclosing = null; if (constructor != null) { enclosing = GetQualifiedNewInstance(newType.value, constructor.GetLstParameters() , indent, tracer); if (enclosing != null) { buf.Append(enclosing).Append('.'); } } buf.Append("new "); string typename = ExprProcessor.GetTypeName(newType); if (enclosing != null) { ClassesProcessor.ClassNode newNode = DecompilerContext.GetClassProcessor().GetMapRootClasses ().GetOrNull(newType.value); if (newNode != null) { typename = newNode.simpleName; } else { typename = Sharpen.Runtime.Substring(typename, typename.LastIndexOf('.') + 1); } } buf.Append(typename); } if (constructor != null) { List <Exprent> parameters = constructor.GetLstParameters(); List <VarVersionPair> mask = ExprUtil.GetSyntheticParametersMask(constructor.GetClassname (), constructor.GetStringDescriptor(), parameters.Count); int start = enumConst ? 2 : 0; if (!enumConst || start < parameters.Count) { buf.Append('('); bool firstParam = true; for (int i = start; i < parameters.Count; i++) { if (mask == null || mask[i] == null) { Exprent expr = parameters[i]; VarType leftType = constructor.GetDescriptor().@params[i]; if (i == parameters.Count - 1 && expr.GetExprType() == VarType.Vartype_Null && ProbablySyntheticParameter (leftType.value)) { break; } // skip last parameter of synthetic constructor call if (!firstParam) { buf.Append(", "); } ExprProcessor.GetCastedExprent(expr, leftType, buf, indent, true, false, true, true , tracer); firstParam = false; } } buf.Append(')'); } } } else if (isVarArgParam) { // just print the array elements VarType leftType = newType.DecreaseArrayDim(); for (int i = 0; i < lstArrayElements.Count; i++) { if (i > 0) { buf.Append(", "); } // new String[][]{{"abc"}, {"DEF"}} => new String[]{"abc"}, new String[]{"DEF"} Exprent element = lstArrayElements[i]; if (element.type == Exprent_New) { ((NewExprent)element).SetDirectArrayInit(false); } ExprProcessor.GetCastedExprent(element, leftType, buf, indent, false, tracer); } // if there is just one element of Object[] type it needs to be casted to resolve ambiguity if (lstArrayElements.Count == 1) { VarType elementType = lstArrayElements[0].GetExprType(); if (elementType.type == ICodeConstants.Type_Object && elementType.value.Equals("java/lang/Object" ) && elementType.arrayDim >= 1) { buf.Prepend("(Object)"); } } } else { buf.Append("new ").Append(ExprProcessor.GetTypeName(newType)); if ((lstArrayElements.Count == 0)) { for (int i = 0; i < newType.arrayDim; i++) { buf.Append('['); if (i < lstDims.Count) { buf.Append(lstDims[i].ToJava(indent, tracer)); } buf.Append(']'); } } else { for (int i = 0; i < newType.arrayDim; i++) { buf.Append("[]"); } VarType leftType = newType.DecreaseArrayDim(); buf.Append('{'); for (int i = 0; i < lstArrayElements.Count; i++) { if (i > 0) { buf.Append(", "); } ExprProcessor.GetCastedExprent(lstArrayElements[i], leftType, buf, indent, false, tracer); } buf.Append('}'); } } return(buf); }