From b96586cc2f8dd79be6d20df1379ee36e92316165 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 20 Apr 2016 14:06:28 +0200 Subject: [PATCH] [java-decompiler] utility classes reorganized --- .../decompiler/code/InstructionSequence.java | 6 +- .../main/decompiler/PrintStreamLogger.java | 6 +- .../decompiler/main/rels/ClassWrapper.java | 5 +- .../decompiler/main/rels/MethodWrapper.java | 17 ++---- .../modules/decompiler/exps/ConstExprent.java | 9 +-- .../modules/decompiler/exps/FieldExprent.java | 18 ++++-- .../modules/decompiler/stats/IfStatement.java | 7 +-- .../java/decompiler/struct/StructMethod.java | 10 +++- .../java/decompiler/util/InterpreterUtil.java | 25 -------- .../java/decompiler/util/StructUtils.java | 59 ------------------- .../java/decompiler/util/TextUtil.java | 27 ++++++++- 11 files changed, 63 insertions(+), 126 deletions(-) delete mode 100644 src/org/jetbrains/java/decompiler/util/StructUtils.java diff --git a/src/org/jetbrains/java/decompiler/code/InstructionSequence.java b/src/org/jetbrains/java/decompiler/code/InstructionSequence.java index 51951ab..bce3b60 100644 --- a/src/org/jetbrains/java/decompiler/code/InstructionSequence.java +++ b/src/org/jetbrains/java/decompiler/code/InstructionSequence.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ package org.jetbrains.java.decompiler.code; import org.jetbrains.java.decompiler.code.interpreter.Util; import org.jetbrains.java.decompiler.main.DecompilerContext; import org.jetbrains.java.decompiler.struct.StructContext; -import org.jetbrains.java.decompiler.util.InterpreterUtil; +import org.jetbrains.java.decompiler.util.TextUtil; import org.jetbrains.java.decompiler.util.VBStyleCollection; import java.io.DataOutputStream; @@ -157,7 +157,7 @@ public int getOffset(int index) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < collinstr.size(); i++) { - buf.append(InterpreterUtil.getIndentString(indent)); + buf.append(TextUtil.getIndentString(indent)); buf.append(collinstr.getKey(i).intValue()); buf.append(": "); buf.append(collinstr.get(i).toString()); diff --git a/src/org/jetbrains/java/decompiler/main/decompiler/PrintStreamLogger.java b/src/org/jetbrains/java/decompiler/main/decompiler/PrintStreamLogger.java index 0995613..a201000 100644 --- a/src/org/jetbrains/java/decompiler/main/decompiler/PrintStreamLogger.java +++ b/src/org/jetbrains/java/decompiler/main/decompiler/PrintStreamLogger.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ package org.jetbrains.java.decompiler.main.decompiler; import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger; -import org.jetbrains.java.decompiler.util.InterpreterUtil; +import org.jetbrains.java.decompiler.util.TextUtil; import java.io.PrintStream; @@ -33,7 +33,7 @@ public class PrintStreamLogger extends IFernflowerLogger { @Override public void writeMessage(String message, Severity severity) { if (accepts(severity)) { - stream.println(severity.prefix + InterpreterUtil.getIndentString(indent) + message); + stream.println(severity.prefix + TextUtil.getIndentString(indent) + message); } } diff --git a/src/org/jetbrains/java/decompiler/main/rels/ClassWrapper.java b/src/org/jetbrains/java/decompiler/main/rels/ClassWrapper.java index cc7eeab..ef09bd0 100644 --- a/src/org/jetbrains/java/decompiler/main/rels/ClassWrapper.java +++ b/src/org/jetbrains/java/decompiler/main/rels/ClassWrapper.java @@ -28,7 +28,6 @@ import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair; import org.jetbrains.java.decompiler.struct.StructClass; import org.jetbrains.java.decompiler.struct.StructField; import org.jetbrains.java.decompiler.struct.StructMethod; -import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute; import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTableAttribute; import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor; import org.jetbrains.java.decompiler.util.InterpreterUtil; @@ -162,9 +161,7 @@ public class ClassWrapper { // if debug information present and should be used if (DecompilerContext.getOption(IFernflowerPreferences.USE_DEBUG_VAR_NAMES)) { - StructLocalVariableTableAttribute attr = (StructLocalVariableTableAttribute)mt.getAttributes().getWithKey( - StructGeneralAttribute.ATTRIBUTE_LOCAL_VARIABLE_TABLE); - + StructLocalVariableTableAttribute attr = mt.getLocalVariableAttr(); if (attr != null) { varProc.setDebugVarNames(attr.getMapVarNames()); } diff --git a/src/org/jetbrains/java/decompiler/main/rels/MethodWrapper.java b/src/org/jetbrains/java/decompiler/main/rels/MethodWrapper.java index 0f44e42..eb397d1 100644 --- a/src/org/jetbrains/java/decompiler/main/rels/MethodWrapper.java +++ b/src/org/jetbrains/java/decompiler/main/rels/MethodWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,25 +26,17 @@ import org.jetbrains.java.decompiler.struct.StructMethod; import java.util.HashSet; import java.util.List; - public class MethodWrapper { - public final RootStatement root; - public final VarProcessor varproc; - public final StructMethod methodStruct; - public final CounterContainer counter; + public final HashSet setOuterVarNames = new HashSet(); public DirectGraph graph; - public List signatureFields; - public boolean decompiledWithErrors; - public final HashSet setOuterVarNames = new HashSet(); - public MethodWrapper(RootStatement root, VarProcessor varproc, StructMethod methodStruct, CounterContainer counter) { this.root = root; this.varproc = varproc; @@ -54,9 +46,8 @@ public class MethodWrapper { public DirectGraph getOrBuildGraph() { if (graph == null && root != null) { - FlattenStatementsHelper flatthelper = new FlattenStatementsHelper(); - graph = flatthelper.buildDirectGraph(root); + graph = new FlattenStatementsHelper().buildDirectGraph(root); } return graph; } -} +} \ No newline at end of file diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java index 8fcf176..908611a 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java @@ -27,6 +27,7 @@ import org.jetbrains.java.decompiler.struct.match.MatchEngine; import org.jetbrains.java.decompiler.struct.match.MatchNode; import org.jetbrains.java.decompiler.struct.match.MatchNode.RuleValue; import org.jetbrains.java.decompiler.util.InterpreterUtil; +import org.jetbrains.java.decompiler.util.TextUtil; import java.util.*; import java.util.Map.Entry; @@ -131,11 +132,11 @@ public class ConstExprent extends Exprent { String ret = ESCAPES.get(val); if (ret == null) { char c = (char)val.intValue(); - if (c >= 32 && c < 127 || !ascii && InterpreterUtil.isPrintableUnicode(c)) { + if (c >= 32 && c < 127 || !ascii && TextUtil.isPrintableUnicode(c)) { ret = String.valueOf(c); } else { - ret = InterpreterUtil.charToUnicodeLiteral(c); + ret = TextUtil.charToUnicodeLiteral(c); } } return new TextBuffer(ret).enclose("\'", "\'"); @@ -307,11 +308,11 @@ public class ConstExprent extends Exprent { buffer.append("\\\'"); break; default: - if (c >= 32 && c < 127 || !ascii && InterpreterUtil.isPrintableUnicode(c)) { + if (c >= 32 && c < 127 || !ascii && TextUtil.isPrintableUnicode(c)) { buffer.append(c); } else { - buffer.append(InterpreterUtil.charToUnicodeLiteral(c)); + buffer.append(TextUtil.charToUnicodeLiteral(c)); } } } diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java index 28002ff..5e6a6b5 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import org.jetbrains.java.decompiler.main.collectors.BytecodeMappingTracer; import org.jetbrains.java.decompiler.main.rels.MethodWrapper; import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair; +import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTableAttribute; import org.jetbrains.java.decompiler.struct.consts.LinkConstant; import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor; import org.jetbrains.java.decompiler.struct.gen.VarType; @@ -30,7 +31,6 @@ import org.jetbrains.java.decompiler.struct.match.MatchEngine; import org.jetbrains.java.decompiler.struct.match.MatchNode; import org.jetbrains.java.decompiler.struct.match.MatchNode.RuleValue; import org.jetbrains.java.decompiler.util.InterpreterUtil; -import org.jetbrains.java.decompiler.util.StructUtils; import org.jetbrains.java.decompiler.util.TextUtil; import java.util.ArrayList; @@ -38,7 +38,6 @@ import java.util.List; import java.util.Set; public class FieldExprent extends Exprent { - private final String name; private final String classname; private final boolean isStatic; @@ -85,7 +84,15 @@ public class FieldExprent extends Exprent { } private boolean isAmbiguous() { - return StructUtils.getCurrentMethodLocalVariableNames().contains(name); + MethodWrapper method = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER); + if (method != null) { + StructLocalVariableTableAttribute attr = method.methodStruct.getLocalVariableAttr(); + if (attr != null) { + return attr.getMapVarNames().containsValue(name); + } + } + + return false; } @Override @@ -215,5 +222,4 @@ public class FieldExprent extends Exprent { return true; } - -} +} \ No newline at end of file diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java index 8be0dcc..ab48ce1 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,8 +25,7 @@ import org.jetbrains.java.decompiler.modules.decompiler.exps.IfExprent; import org.jetbrains.java.decompiler.struct.match.IMatchable; import org.jetbrains.java.decompiler.struct.match.MatchEngine; import org.jetbrains.java.decompiler.struct.match.MatchNode; -import org.jetbrains.java.decompiler.struct.match.IMatchable.MatchProperties; -import org.jetbrains.java.decompiler.util.InterpreterUtil; +import org.jetbrains.java.decompiler.util.TextUtil; import java.util.ArrayList; import java.util.List; @@ -205,7 +204,7 @@ public class IfStatement extends Statement { } public TextBuffer toJava(int indent, BytecodeMappingTracer tracer) { - String indstr = InterpreterUtil.getIndentString(indent); + String indstr = TextUtil.getIndentString(indent); TextBuffer buf = new TextBuffer(); buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer)); diff --git a/src/org/jetbrains/java/decompiler/struct/StructMethod.java b/src/org/jetbrains/java/decompiler/struct/StructMethod.java index 2b5249f..4ed9be3 100644 --- a/src/org/jetbrains/java/decompiler/struct/StructMethod.java +++ b/src/org/jetbrains/java/decompiler/struct/StructMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.jetbrains.java.decompiler.struct; import org.jetbrains.java.decompiler.code.*; import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute; +import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTableAttribute; import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.util.DataInputFullStream; import org.jetbrains.java.decompiler.util.VBStyleCollection; @@ -37,7 +38,6 @@ import static org.jetbrains.java.decompiler.code.CodeConstants.*; } */ public class StructMethod extends StructMember { - private static final int[] opr_iconst = {-1, 0, 1, 2, 3, 4, 5}; private static final int[] opr_loadstore = {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}; private static final int[] opcs_load = {opc_iload, opc_lload, opc_fload, opc_dload, opc_aload}; @@ -389,4 +389,8 @@ public class StructMethod extends StructMember { public InstructionSequence getInstructionSequence() { return seq; } -} + + public StructLocalVariableTableAttribute getLocalVariableAttr() { + return (StructLocalVariableTableAttribute)getAttributes().getWithKey(StructGeneralAttribute.ATTRIBUTE_LOCAL_VARIABLE_TABLE); + } +} \ No newline at end of file diff --git a/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java b/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java index 8130145..5576aa1 100644 --- a/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java +++ b/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java @@ -15,9 +15,6 @@ */ package org.jetbrains.java.decompiler.util; -import org.jetbrains.java.decompiler.main.DecompilerContext; -import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; - import java.io.*; import java.util.Collection; import java.util.HashSet; @@ -79,16 +76,6 @@ public class InterpreterUtil { } } - public static String getIndentString(int length) { - if (length == 0) return ""; - StringBuilder buf = new StringBuilder(); - String indent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING); - while (length-- > 0) { - buf.append(indent); - } - return buf.toString(); - } - public static boolean equalSets(Collection c1, Collection c2) { if (c1 == null) { return c2 == null; @@ -130,18 +117,6 @@ public class InterpreterUtil { return false; } - public static boolean isPrintableUnicode(char c) { - int t = Character.getType(c); - return t != Character.UNASSIGNED && t != Character.LINE_SEPARATOR && t != Character.PARAGRAPH_SEPARATOR && - t != Character.CONTROL && t != Character.FORMAT && t != Character.PRIVATE_USE && t != Character.SURROGATE; - } - - public static String charToUnicodeLiteral(int value) { - String sTemp = Integer.toHexString(value); - sTemp = ("0000" + sTemp).substring(sTemp.length()); - return "\\u" + sTemp; - } - public static String makeUniqueKey(String name, String descriptor) { return name + ' ' + descriptor; } diff --git a/src/org/jetbrains/java/decompiler/util/StructUtils.java b/src/org/jetbrains/java/decompiler/util/StructUtils.java deleted file mode 100644 index 1d4042e..0000000 --- a/src/org/jetbrains/java/decompiler/util/StructUtils.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jetbrains.java.decompiler.util; - -import org.jetbrains.java.decompiler.main.DecompilerContext; -import org.jetbrains.java.decompiler.main.rels.MethodWrapper; -import org.jetbrains.java.decompiler.struct.StructMember; -import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute; -import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTableAttribute; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static org.jetbrains.java.decompiler.main.DecompilerContext.CURRENT_METHOD_WRAPPER; -import static org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute.ATTRIBUTE_LOCAL_VARIABLE_TABLE; - -/** - * @author Alexandru-Constantin Bledea - * @since March 07, 2016 - */ -public final class StructUtils { - private StructUtils() { } - - /** - * @return the local variables of the current method - */ - public static List getCurrentMethodLocalVariableNames() { - final MethodWrapper method = (MethodWrapper) DecompilerContext.getProperty(CURRENT_METHOD_WRAPPER); - return method == null ? Collections.emptyList() : getLocalVariables(method.methodStruct); - } - - /** - * @param structMember the struct member from which to extract the local variables - * @return the local variables of the struct member - */ - public static List getLocalVariables(final StructMember structMember) { - final VBStyleCollection methodStruct = structMember.getAttributes(); - final StructGeneralAttribute generalAttribute = methodStruct.getWithKey(ATTRIBUTE_LOCAL_VARIABLE_TABLE); - if (generalAttribute instanceof StructLocalVariableTableAttribute) { - final StructLocalVariableTableAttribute table = (StructLocalVariableTableAttribute)generalAttribute; - return Collections.unmodifiableList(new ArrayList<>(table.getMapVarNames().values())); - } - return Collections.emptyList(); - } -} \ No newline at end of file diff --git a/src/org/jetbrains/java/decompiler/util/TextUtil.java b/src/org/jetbrains/java/decompiler/util/TextUtil.java index 14e24f9..8957fe1 100644 --- a/src/org/jetbrains/java/decompiler/util/TextUtil.java +++ b/src/org/jetbrains/java/decompiler/util/TextUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.jetbrains.java.decompiler.util; import org.jetbrains.java.decompiler.main.ClassesProcessor; import org.jetbrains.java.decompiler.main.DecompilerContext; import org.jetbrains.java.decompiler.main.TextBuffer; +import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; public class TextUtil { @@ -28,4 +29,26 @@ public class TextUtil { } buf.append("super"); } -} + + public static String getIndentString(int length) { + if (length == 0) return ""; + StringBuilder buf = new StringBuilder(); + String indent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING); + while (length-- > 0) { + buf.append(indent); + } + return buf.toString(); + } + + public static boolean isPrintableUnicode(char c) { + int t = Character.getType(c); + return t != Character.UNASSIGNED && t != Character.LINE_SEPARATOR && t != Character.PARAGRAPH_SEPARATOR && + t != Character.CONTROL && t != Character.FORMAT && t != Character.PRIVATE_USE && t != Character.SURROGATE; + } + + public static String charToUnicodeLiteral(int value) { + String sTemp = Integer.toHexString(value); + sTemp = ("0000" + sTemp).substring(sTemp.length()); + return "\\u" + sTemp; + } +} \ No newline at end of file