Implemented method to retrieve line numbers.
This commit is contained in:
parent
130ac94991
commit
8227286b38
|
@ -15,16 +15,22 @@ import java.io.InputStreamReader;
|
|||
import java.io.Reader;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import com.ibm.wala.cast.tree.CAstSourcePositionMap.Position;
|
||||
import com.ibm.wala.cast.tree.impl.AbstractSourcePosition;
|
||||
|
||||
public class RangePosition extends AbstractSourcePosition implements Position {
|
||||
private final static int TABSIZE = 4;
|
||||
private final URL url;
|
||||
private final int line;
|
||||
private final int startOffset;
|
||||
private final int endOffset;
|
||||
|
||||
|
||||
|
||||
private int col = -1;
|
||||
private int lastLine = -1;
|
||||
private int lastCol = -1;
|
||||
|
||||
public RangePosition(URL url, int line, int startOffset, int endOffset) {
|
||||
super();
|
||||
this.url = url;
|
||||
|
@ -50,17 +56,74 @@ public class RangePosition extends AbstractSourcePosition implements Position {
|
|||
|
||||
@Override
|
||||
public int getLastLine() {
|
||||
return -1;
|
||||
if (lastLine == -1) {
|
||||
String content;
|
||||
Reader reader = null;
|
||||
try {
|
||||
reader = getReader();
|
||||
content = IOUtils.toString(reader).substring(startOffset, endOffset);
|
||||
int nrOfNewlines = content.length() - content.replace("\n", "").length();
|
||||
lastLine = line + nrOfNewlines;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(reader);
|
||||
}
|
||||
}
|
||||
|
||||
return lastLine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFirstCol() {
|
||||
return -1;
|
||||
if (col == -1) {
|
||||
col = getCol(getFirstLine(), startOffset);
|
||||
}
|
||||
|
||||
return col;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLastCol() {
|
||||
return -1;
|
||||
if (lastCol == -1) {
|
||||
lastCol = getCol(getLastLine(), endOffset);
|
||||
}
|
||||
|
||||
return lastCol;
|
||||
}
|
||||
|
||||
private int getCol(int line, int offset) {
|
||||
int col = -1;
|
||||
Reader reader = null;
|
||||
try {
|
||||
reader = getReader();
|
||||
col = getCol(IOUtils.toString(reader), line, offset);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(reader);
|
||||
}
|
||||
|
||||
return col;
|
||||
}
|
||||
|
||||
public static int getCol(String content, int line, int offset) {
|
||||
if (line == 1) {
|
||||
return offset + 1;
|
||||
}
|
||||
int pos = -1;
|
||||
for (int i = 0; i < line - 1; i++) {
|
||||
pos = content.indexOf('\n', pos + 1);
|
||||
}
|
||||
|
||||
try {
|
||||
String lineBeginning = content.substring(pos, offset);
|
||||
int nrOfTabs = lineBeginning.length() - lineBeginning.replace("\t", "").length();
|
||||
return offset - pos + (TABSIZE - 1) * nrOfTabs;
|
||||
} catch (StringIndexOutOfBoundsException e) {
|
||||
e.printStackTrace();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -61,6 +61,7 @@ import static org.jf.dexlib.Util.AccessFlags.VOLATILE;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -183,7 +184,7 @@ public class DexIMethod implements IBytecodeMethod {
|
|||
|
||||
protected InstructionArray instructions;
|
||||
|
||||
|
||||
private Map<Integer, Integer> lineNumbers = new HashMap<Integer, Integer>();
|
||||
|
||||
private static int totalInsts = 0;
|
||||
|
||||
|
@ -537,7 +538,11 @@ public class DexIMethod implements IBytecodeMethod {
|
|||
}
|
||||
|
||||
public int getLineNumber(int bcIndex) {
|
||||
return getInstructionIndex(bcIndex);
|
||||
return lineNumbers.getOrDefault(bcIndex, -1);
|
||||
}
|
||||
|
||||
public void setLineNumber(int bcIndex, int lineNumber) {
|
||||
lineNumbers.put(bcIndex, lineNumber);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue