Commit ab593e3c authored by Skylot's avatar Skylot

refactor some classes

parent 4a0aacf1
......@@ -10,6 +10,7 @@ import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -30,7 +31,7 @@ public final class AttributesList {
public AttributesList() {
flags = EnumSet.noneOf(AttributeFlag.class);
uniqAttr = new EnumMap<AttributeType, IAttribute>(AttributeType.class);
attributes = new ArrayList<IAttribute>(0);
attributes = new LinkedList<IAttribute>();
attrCount = new int[AttributeType.getNotUniqCount()];
}
......
......@@ -22,7 +22,7 @@ import jadx.core.utils.exceptions.DecodeException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
......@@ -39,49 +39,56 @@ public class ClassNode extends LineAttrNode implements ILoadable {
private final DexNode dex;
private final ClassInfo clsInfo;
private final AccessInfo accessFlags;
private ClassInfo superClass;
private List<ClassInfo> interfaces;
private Map<ArgType, List<ArgType>> genericMap;
private final List<MethodNode> methods = new ArrayList<MethodNode>();
private final List<FieldNode> fields = new ArrayList<FieldNode>();
private final AccessInfo accessFlags;
private final List<MethodNode> methods;
private final List<FieldNode> fields;
private Map<Object, FieldNode> constFields = Collections.emptyMap();
private List<ClassNode> innerClasses = Collections.emptyList();
private final Map<Object, FieldNode> constFields = new HashMap<Object, FieldNode>();
private CodeWriter code; // generated code
public ClassNode(DexNode dex, ClassDef cls) throws DecodeException {
this.dex = dex;
this.clsInfo = ClassInfo.fromDex(dex, cls.getTypeIndex());
try {
this.superClass = cls.getSupertypeIndex() == DexNode.NO_INDEX
? null
: ClassInfo.fromDex(dex, cls.getSupertypeIndex());
if (cls.getSupertypeIndex() == DexNode.NO_INDEX) {
this.superClass = null;
} else {
this.superClass = ClassInfo.fromDex(dex, cls.getSupertypeIndex());
}
this.interfaces = new ArrayList<ClassInfo>(cls.getInterfaces().length);
for (short interfaceIdx : cls.getInterfaces()) {
this.interfaces.add(ClassInfo.fromDex(dex, interfaceIdx));
}
if (cls.getClassDataOffset() != 0) {
ClassData clsData = dex.readClassData(cls);
int mthsCount = clsData.getDirectMethods().length + clsData.getVirtualMethods().length;
int fieldsCount = clsData.getStaticFields().length + clsData.getInstanceFields().length;
for (Method mth : clsData.getDirectMethods())
methods.add(new MethodNode(this, mth));
methods = new ArrayList<MethodNode>(mthsCount);
fields = new ArrayList<FieldNode>(fieldsCount);
for (Method mth : clsData.getVirtualMethods())
for (Method mth : clsData.getDirectMethods()) {
methods.add(new MethodNode(this, mth));
}
for (Method mth : clsData.getVirtualMethods()) {
methods.add(new MethodNode(this, mth));
}
for (Field f : clsData.getStaticFields())
for (Field f : clsData.getStaticFields()) {
fields.add(new FieldNode(this, f));
}
loadStaticValues(cls, fields);
for (Field f : clsData.getInstanceFields())
for (Field f : clsData.getInstanceFields()) {
fields.add(new FieldNode(this, f));
}
} else {
methods = Collections.emptyList();
fields = Collections.emptyList();
}
loadAnnotations(cls);
......@@ -98,13 +105,14 @@ public class ClassNode extends LineAttrNode implements ILoadable {
}
}
// restore original access flags from dalvik annotation if present
int accFlagsValue;
Annotation a = getAttributes().getAnnotation(Consts.DALVIK_INNER_CLASS);
if (a != null)
if (a != null) {
accFlagsValue = (Integer) a.getValues().get("accessFlags");
else
} else {
accFlagsValue = cls.getAccessFlags();
}
this.accessFlags = new AccessInfo(accFlagsValue, AFType.CLASS);
} catch (Exception e) {
......@@ -134,8 +142,8 @@ public class ClassNode extends LineAttrNode implements ILoadable {
int offset = cls.getStaticValuesOffset();
if (offset != 0) {
StaticValuesParser parser = new StaticValuesParser(dex, dex.openSection(offset));
parser.processFields(staticFields);
int count = parser.processFields(staticFields);
constFields = new LinkedHashMap<Object, FieldNode>(count);
for (FieldNode f : staticFields) {
AccessInfo accFlags = f.getAccessFlags();
if (accFlags.isStatic() && accFlags.isFinal()) {
......@@ -154,9 +162,9 @@ public class ClassNode extends LineAttrNode implements ILoadable {
@SuppressWarnings("unchecked")
private void parseClassSignature() {
Annotation a = this.getAttributes().getAnnotation(Consts.DALVIK_SIGNATURE);
if (a == null)
if (a == null) {
return;
}
String sign = Utils.mergeSignature((List<String>) a.getDefaultValue());
// parse generic map
int end = Utils.getGenericEnd(sign);
......@@ -188,13 +196,13 @@ public class ClassNode extends LineAttrNode implements ILoadable {
private void setFieldsTypesFromSignature() {
for (FieldNode field : fields) {
Annotation a = field.getAttributes().getAnnotation(Consts.DALVIK_SIGNATURE);
if (a == null)
continue;
String sign = Utils.mergeSignature((List<String>) a.getDefaultValue());
ArgType gType = ArgType.parseSignature(sign);
if (gType != null)
field.setType(gType);
if (a != null) {
String sign = Utils.mergeSignature((List<String>) a.getDefaultValue());
ArgType gType = ArgType.parseSignature(sign);
if (gType != null) {
field.setType(gType);
}
}
}
}
......@@ -288,8 +296,9 @@ public class ClassNode extends LineAttrNode implements ILoadable {
public FieldNode searchFieldById(int id) {
String name = FieldInfo.getNameById(dex, id);
for (FieldNode f : fields) {
if (f.getName().equals(name))
if (f.getName().equals(name)) {
return f;
}
}
return null;
}
......@@ -300,24 +309,27 @@ public class ClassNode extends LineAttrNode implements ILoadable {
public FieldNode searchFieldByName(String name) {
for (FieldNode f : fields) {
if (f.getName().equals(name))
if (f.getName().equals(name)) {
return f;
}
}
return null;
}
public MethodNode searchMethod(MethodInfo mth) {
for (MethodNode m : methods) {
if (m.getMethodInfo().equals(mth))
if (m.getMethodInfo().equals(mth)) {
return m;
}
}
return null;
}
public MethodNode searchMethodByName(String shortId) {
for (MethodNode m : methods) {
if (m.getMethodInfo().getShortId().equals(shortId))
if (m.getMethodInfo().getShortId().equals(shortId)) {
return m;
}
}
return null;
}
......@@ -331,8 +343,9 @@ public class ClassNode extends LineAttrNode implements ILoadable {
}
public void addInnerClass(ClassNode cls) {
if (innerClasses.isEmpty())
if (innerClasses.isEmpty()) {
innerClasses = new ArrayList<ClassNode>(3);
}
innerClasses.add(cls);
}
......@@ -351,7 +364,7 @@ public class ClassNode extends LineAttrNode implements ILoadable {
if (mth.getAccessFlags().isConstructor()
&& mth.getMethodInfo().isConstructor()
&& (mth.getMethodInfo().getArgsCount() == 0
|| (mth.getArguments(false) != null && mth.getArguments(false).isEmpty()))) {
|| (mth.getArguments(false) != null && mth.getArguments(false).isEmpty()))) {
return mth;
}
}
......
......@@ -6,7 +6,7 @@ import java.util.List;
public class InsnContainer extends AttrNode implements IBlock {
private List<InsnNode> insns;
private final List<InsnNode> insns;
public InsnContainer(List<InsnNode> insns) {
this.insns = insns;
......
......@@ -15,13 +15,12 @@ public class StaticValuesParser extends EncValueParser {
super(dex, in);
}
public void processFields(List<FieldNode> fields) throws DecodeException {
int size = Leb128Utils.readUnsignedLeb128(in);
visitArray(size);
for (int i = 0; i < size; i++) {
public int processFields(List<FieldNode> fields) throws DecodeException {
int count = Leb128Utils.readUnsignedLeb128(in);
for (int i = 0; i < count; i++) {
Object value = parseValue();
fields.get(i).getAttributes().add(new FieldValueAttr(value));
}
return count;
}
}
......@@ -24,7 +24,6 @@ public class BlockProcessingHelper {
if (mth.isNoCode()) {
return;
}
for (BlockNode block : mth.getBasicBlocks()) {
markExceptionHandlers(block);
}
......@@ -78,7 +77,6 @@ public class BlockProcessingHelper {
for (BlockNode node : BlockUtils.collectBlocksDominatedBy(block, block)) {
excHandler.addBlock(node);
}
for (BlockNode excBlock : excHandler.getBlocks()) {
// remove 'monitor-exit' from exception handler blocks
InstructionRemover remover = new InstructionRemover(excBlock.getInstructions());
......@@ -86,7 +84,6 @@ public class BlockProcessingHelper {
if (insn.getType() == InsnType.MONITOR_ENTER) {
break;
}
if (insn.getType() == InsnType.MONITOR_EXIT) {
remover.add(insn);
}
......@@ -119,7 +116,6 @@ public class BlockProcessingHelper {
if (catchAttr == null) {
continue;
}
if (commonCatchAttr == null) {
commonCatchAttr = catchAttr;
} else if (commonCatchAttr != catchAttr) {
......
......@@ -17,10 +17,11 @@ public class BlockUtils {
public static BlockNode getBlockByOffset(int offset, Iterable<BlockNode> casesBlocks) {
for (BlockNode block : casesBlocks) {
if (block.getStartOffset() == offset)
if (block.getStartOffset() == offset) {
return block;
}
}
throw new JadxRuntimeException("Can'r find block by offset: "
throw new JadxRuntimeException("Can't find block by offset: "
+ InsnUtils.formatOffset(offset)
+ " in list " + casesBlocks);
}
......@@ -30,7 +31,6 @@ public class BlockUtils {
if (list.size() > 2) {
list = cleanBlockList(list);
}
assert list.size() == 2 : "too many nodes for selectOther: " + node + " in " + list;
BlockNode first = list.get(0);
if (first != node)
......
......@@ -81,10 +81,12 @@ public class MainWindow extends JFrame {
private final JadxWrapper wrapper;
private JPanel mainPanel;
private JTree tree;
private final JTabbedPane tabbedPane = new JTabbedPane();
private DefaultTreeModel treeModel;
private Map<JClass, Component> openTabs = new HashMap<JClass, Component>();
private final JTabbedPane tabbedPane = new JTabbedPane();
private final Map<JClass, Component> openTabs = new HashMap<JClass, Component>();
public MainWindow(JadxWrapper wrapper) {
this.wrapper = wrapper;
......
......@@ -50,7 +50,7 @@ public class JClass extends JNode {
}
@Override
public void updateChilds() {
public synchronized void updateChilds() {
removeAllChildren();
if (!loaded) {
add(new TextNode(NLS.str("tree.loading")));
......
......@@ -15,8 +15,8 @@ public class JPackage extends JNode implements Comparable<JPackage> {
private static final ImageIcon PACKAGE_ICON = Utils.openIcon("package_obj");
private String name;
private List<JClass> classes;
private List<JPackage> innerPackages = new ArrayList<JPackage>(1);
private final List<JClass> classes;
private final List<JPackage> innerPackages = new ArrayList<JPackage>(1);
public JPackage(JavaPackage pkg) {
this.name = pkg.getName();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment