Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix https://github.com/pxb1988/dex2jar/issues/165 #519

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions dex-ir/src/main/java/com/googlecode/dex2jar/ir/ts/CleanLabel.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* dex2jar - Tools to work with android .dex and java .class files
* Copyright (c) 2009-2012 Panxiaobo
*
*
* 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.
Expand All @@ -16,21 +16,20 @@
*/
package com.googlecode.dex2jar.ir.ts;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.googlecode.dex2jar.ir.IrMethod;
import com.googlecode.dex2jar.ir.LocalVar;
import com.googlecode.dex2jar.ir.Trap;
import com.googlecode.dex2jar.ir.stmt.*;
import com.googlecode.dex2jar.ir.stmt.Stmt.ST;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Clean unused {@link LabelStmt}
*
*
* @author <a href="mailto:[email protected]">Panxiaobo</a>
*
*/
public class CleanLabel implements Transformer {

Expand All @@ -43,6 +42,7 @@ public void transform(IrMethod irMethod) {
if (irMethod.phiLabels != null) {
uselabels.addAll(irMethod.phiLabels);
}
addLineNumber(irMethod.stmts, uselabels);
rmUnused(irMethod.stmts, uselabels);
}

Expand All @@ -57,7 +57,7 @@ private void addVars(List<LocalVar> vars, Set<LabelStmt> uselabels) {
}

private void rmUnused(StmtList stmts, Set<LabelStmt> uselabels) {
for (Stmt p = stmts.getFirst(); p != null;) {
for (Stmt p = stmts.getFirst(); p != null; ) {
if (p.st == ST.LABEL) {
if (!uselabels.contains(p)) {
Stmt q = p.getNext();
Expand Down Expand Up @@ -96,4 +96,12 @@ private void addTrap(List<Trap> traps, Set<LabelStmt> labels) {
}
}

// fix https://github.com/pxb1988/dex2jar/issues/165
private void addLineNumber(StmtList stmts, Set<LabelStmt> uselabels) {
for (Stmt p = stmts.getFirst(); p != null; p = p.getNext()) {
if (p instanceof LabelStmt && ((LabelStmt) p).lineNumber != -1) {
uselabels.add((LabelStmt) p);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.googlecode.d2j.Field;
import com.googlecode.d2j.Method;
import com.googlecode.d2j.node.DexCodeNode;
import com.googlecode.d2j.node.DexDebugNode;
import com.googlecode.d2j.node.TryCatchNode;
import com.googlecode.d2j.node.analysis.DvmFrame;
import com.googlecode.d2j.node.analysis.DvmInterpreter;
Expand Down Expand Up @@ -97,7 +98,6 @@ public IrMethod convert(boolean isStatic, Method method, DexCodeNode dexCodeNode

dfs(exBranch, handlers, access, interpreter);


StmtList stmts = target.stmts;
stmts.addAll(preEmit);
for (int i = 0; i < insnList.size(); i++) {
Expand Down Expand Up @@ -169,9 +169,30 @@ public IrMethod convert(boolean isStatic, Method method, DexCodeNode dexCodeNode
target.phiLabels = phiLabels;
}

supplementLineNumber(dexCodeNode);

return target;
}

// fix https://github.com/pxb1988/dex2jar/issues/165
private void supplementLineNumber(DexCodeNode dexCodeNode) {
if (dexCodeNode == null || dexCodeNode.debugNode == null || dexCodeNode.debugNode.debugNodes == null) {
return;
}
Map<DexLabel, Integer> lineNumber = new HashMap<>();
for (DexDebugNode.DexDebugOpNode debugNode : dexCodeNode.debugNode.debugNodes) {
if (debugNode instanceof DexDebugNode.DexDebugOpNode.LineNumber) {
lineNumber.put(debugNode.label, ((DexDebugNode.DexDebugOpNode.LineNumber) debugNode).line);
}
}
for (Map.Entry<DexLabel, LabelStmt> entry : map.entrySet()) {
Integer line = lineNumber.get(entry.getKey());
if (line != null) {
entry.getValue().lineNumber = line;
}
}
}

/**
* issue 63
* <pre>
Expand Down