[java-decompiler] utility classes reorganized

master
Roman Shevchenko 8 years ago
parent 857dc4d023
commit b96586cc2f
  1. 6
      src/org/jetbrains/java/decompiler/code/InstructionSequence.java
  2. 6
      src/org/jetbrains/java/decompiler/main/decompiler/PrintStreamLogger.java
  3. 5
      src/org/jetbrains/java/decompiler/main/rels/ClassWrapper.java
  4. 17
      src/org/jetbrains/java/decompiler/main/rels/MethodWrapper.java
  5. 9
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java
  6. 18
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java
  7. 7
      src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java
  8. 10
      src/org/jetbrains/java/decompiler/struct/StructMethod.java
  9. 25
      src/org/jetbrains/java/decompiler/util/InterpreterUtil.java
  10. 59
      src/org/jetbrains/java/decompiler/util/StructUtils.java
  11. 27
      src/org/jetbrains/java/decompiler/util/TextUtil.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.code.interpreter.Util;
import org.jetbrains.java.decompiler.main.DecompilerContext; import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.struct.StructContext; 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 org.jetbrains.java.decompiler.util.VBStyleCollection;
import java.io.DataOutputStream; import java.io.DataOutputStream;
@ -157,7 +157,7 @@ public int getOffset(int index) {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
for (int i = 0; i < collinstr.size(); i++) { 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(collinstr.getKey(i).intValue());
buf.append(": "); buf.append(": ");
buf.append(collinstr.get(i).toString()); buf.append(collinstr.get(i).toString());

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; package org.jetbrains.java.decompiler.main.decompiler;
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger; 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; import java.io.PrintStream;
@ -33,7 +33,7 @@ public class PrintStreamLogger extends IFernflowerLogger {
@Override @Override
public void writeMessage(String message, Severity severity) { public void writeMessage(String message, Severity severity) {
if (accepts(severity)) { if (accepts(severity)) {
stream.println(severity.prefix + InterpreterUtil.getIndentString(indent) + message); stream.println(severity.prefix + TextUtil.getIndentString(indent) + message);
} }
} }

@ -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.StructClass;
import org.jetbrains.java.decompiler.struct.StructField; import org.jetbrains.java.decompiler.struct.StructField;
import org.jetbrains.java.decompiler.struct.StructMethod; 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.attr.StructLocalVariableTableAttribute;
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor; import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.util.InterpreterUtil; import org.jetbrains.java.decompiler.util.InterpreterUtil;
@ -162,9 +161,7 @@ public class ClassWrapper {
// if debug information present and should be used // if debug information present and should be used
if (DecompilerContext.getOption(IFernflowerPreferences.USE_DEBUG_VAR_NAMES)) { if (DecompilerContext.getOption(IFernflowerPreferences.USE_DEBUG_VAR_NAMES)) {
StructLocalVariableTableAttribute attr = (StructLocalVariableTableAttribute)mt.getAttributes().getWithKey( StructLocalVariableTableAttribute attr = mt.getLocalVariableAttr();
StructGeneralAttribute.ATTRIBUTE_LOCAL_VARIABLE_TABLE);
if (attr != null) { if (attr != null) {
varProc.setDebugVarNames(attr.getMapVarNames()); varProc.setDebugVarNames(attr.getMapVarNames());
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.HashSet;
import java.util.List; import java.util.List;
public class MethodWrapper { public class MethodWrapper {
public final RootStatement root; public final RootStatement root;
public final VarProcessor varproc; public final VarProcessor varproc;
public final StructMethod methodStruct; public final StructMethod methodStruct;
public final CounterContainer counter; public final CounterContainer counter;
public final HashSet<String> setOuterVarNames = new HashSet<String>();
public DirectGraph graph; public DirectGraph graph;
public List<VarVersionPair> signatureFields; public List<VarVersionPair> signatureFields;
public boolean decompiledWithErrors; public boolean decompiledWithErrors;
public final HashSet<String> setOuterVarNames = new HashSet<String>();
public MethodWrapper(RootStatement root, VarProcessor varproc, StructMethod methodStruct, CounterContainer counter) { public MethodWrapper(RootStatement root, VarProcessor varproc, StructMethod methodStruct, CounterContainer counter) {
this.root = root; this.root = root;
this.varproc = varproc; this.varproc = varproc;
@ -54,9 +46,8 @@ public class MethodWrapper {
public DirectGraph getOrBuildGraph() { public DirectGraph getOrBuildGraph() {
if (graph == null && root != null) { if (graph == null && root != null) {
FlattenStatementsHelper flatthelper = new FlattenStatementsHelper(); graph = new FlattenStatementsHelper().buildDirectGraph(root);
graph = flatthelper.buildDirectGraph(root);
} }
return graph; return graph;
} }
} }

@ -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;
import org.jetbrains.java.decompiler.struct.match.MatchNode.RuleValue; import org.jetbrains.java.decompiler.struct.match.MatchNode.RuleValue;
import org.jetbrains.java.decompiler.util.InterpreterUtil; import org.jetbrains.java.decompiler.util.InterpreterUtil;
import org.jetbrains.java.decompiler.util.TextUtil;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -131,11 +132,11 @@ public class ConstExprent extends Exprent {
String ret = ESCAPES.get(val); String ret = ESCAPES.get(val);
if (ret == null) { if (ret == null) {
char c = (char)val.intValue(); 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); ret = String.valueOf(c);
} }
else { else {
ret = InterpreterUtil.charToUnicodeLiteral(c); ret = TextUtil.charToUnicodeLiteral(c);
} }
} }
return new TextBuffer(ret).enclose("\'", "\'"); return new TextBuffer(ret).enclose("\'", "\'");
@ -307,11 +308,11 @@ public class ConstExprent extends Exprent {
buffer.append("\\\'"); buffer.append("\\\'");
break; break;
default: default:
if (c >= 32 && c < 127 || !ascii && InterpreterUtil.isPrintableUnicode(c)) { if (c >= 32 && c < 127 || !ascii && TextUtil.isPrintableUnicode(c)) {
buffer.append(c); buffer.append(c);
} }
else { else {
buffer.append(InterpreterUtil.charToUnicodeLiteral(c)); buffer.append(TextUtil.charToUnicodeLiteral(c));
} }
} }
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.main.rels.MethodWrapper;
import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair; 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.consts.LinkConstant;
import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor; import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor;
import org.jetbrains.java.decompiler.struct.gen.VarType; 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;
import org.jetbrains.java.decompiler.struct.match.MatchNode.RuleValue; import org.jetbrains.java.decompiler.struct.match.MatchNode.RuleValue;
import org.jetbrains.java.decompiler.util.InterpreterUtil; import org.jetbrains.java.decompiler.util.InterpreterUtil;
import org.jetbrains.java.decompiler.util.StructUtils;
import org.jetbrains.java.decompiler.util.TextUtil; import org.jetbrains.java.decompiler.util.TextUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -38,7 +38,6 @@ import java.util.List;
import java.util.Set; import java.util.Set;
public class FieldExprent extends Exprent { public class FieldExprent extends Exprent {
private final String name; private final String name;
private final String classname; private final String classname;
private final boolean isStatic; private final boolean isStatic;
@ -85,7 +84,15 @@ public class FieldExprent extends Exprent {
} }
private boolean isAmbiguous() { 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 @Override
@ -215,5 +222,4 @@ public class FieldExprent extends Exprent {
return true; return true;
} }
}
}

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.IMatchable;
import org.jetbrains.java.decompiler.struct.match.MatchEngine; import org.jetbrains.java.decompiler.struct.match.MatchEngine;
import org.jetbrains.java.decompiler.struct.match.MatchNode; import org.jetbrains.java.decompiler.struct.match.MatchNode;
import org.jetbrains.java.decompiler.struct.match.IMatchable.MatchProperties; import org.jetbrains.java.decompiler.util.TextUtil;
import org.jetbrains.java.decompiler.util.InterpreterUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -205,7 +204,7 @@ public class IfStatement extends Statement {
} }
public TextBuffer toJava(int indent, BytecodeMappingTracer tracer) { public TextBuffer toJava(int indent, BytecodeMappingTracer tracer) {
String indstr = InterpreterUtil.getIndentString(indent); String indstr = TextUtil.getIndentString(indent);
TextBuffer buf = new TextBuffer(); TextBuffer buf = new TextBuffer();
buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer)); buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer));

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.code.*;
import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute; 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.struct.consts.ConstantPool;
import org.jetbrains.java.decompiler.util.DataInputFullStream; import org.jetbrains.java.decompiler.util.DataInputFullStream;
import org.jetbrains.java.decompiler.util.VBStyleCollection; import org.jetbrains.java.decompiler.util.VBStyleCollection;
@ -37,7 +38,6 @@ import static org.jetbrains.java.decompiler.code.CodeConstants.*;
} }
*/ */
public class StructMethod extends StructMember { public class StructMethod extends StructMember {
private static final int[] opr_iconst = {-1, 0, 1, 2, 3, 4, 5}; 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[] 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}; 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() { public InstructionSequence getInstructionSequence() {
return seq; return seq;
} }
}
public StructLocalVariableTableAttribute getLocalVariableAttr() {
return (StructLocalVariableTableAttribute)getAttributes().getWithKey(StructGeneralAttribute.ATTRIBUTE_LOCAL_VARIABLE_TABLE);
}
}

@ -15,9 +15,6 @@
*/ */
package org.jetbrains.java.decompiler.util; 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.io.*;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; 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) { public static boolean equalSets(Collection<?> c1, Collection<?> c2) {
if (c1 == null) { if (c1 == null) {
return c2 == null; return c2 == null;
@ -130,18 +117,6 @@ public class InterpreterUtil {
return false; 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) { public static String makeUniqueKey(String name, String descriptor) {
return name + ' ' + descriptor; return name + ' ' + descriptor;
} }

@ -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<String> 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<String> getLocalVariables(final StructMember structMember) {
final VBStyleCollection<StructGeneralAttribute, String> 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();
}
}

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.ClassesProcessor;
import org.jetbrains.java.decompiler.main.DecompilerContext; import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.TextBuffer; import org.jetbrains.java.decompiler.main.TextBuffer;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
public class TextUtil { public class TextUtil {
@ -28,4 +29,26 @@ public class TextUtil {
} }
buf.append("super"); 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;
}
}
Loading…
Cancel
Save