From 42b4ef1b7ce5b0ab83b6eaf65cf15c0b0ea73375 Mon Sep 17 00:00:00 2001 From: Eric Bodden Date: Thu, 31 Oct 2013 20:35:20 +0100 Subject: [PATCH] finally (!) added some methods to easily access line and column numbers (only took 12 years) --- src/soot/SootMethod.java | 19 ++++++++++++++++++ src/soot/tagkit/AbstractHost.java | 32 +++++++++++++++++++++++++++++++ src/soot/tagkit/Host.java | 12 ++++++++++++ 3 files changed, 63 insertions(+) diff --git a/src/soot/SootMethod.java b/src/soot/SootMethod.java index 675592a9f91..f39e10d68e8 100644 --- a/src/soot/SootMethod.java +++ b/src/soot/SootMethod.java @@ -824,4 +824,23 @@ public final void setNumber(int number) { public SootMethodRef makeRef() { return Scene.v().makeMethodRef( declaringClass, name, parameterTypes, returnType, isStatic() ); } + + @Override + public int getJavaSourceStartLineNumber() { + super.getJavaSourceStartLineNumber(); + //search statements for first line number + if(line==-1 && hasActiveBody()) { + PatchingChain unit = getActiveBody().getUnits(); + for (Unit u : unit) { + int l = u.getJavaSourceStartLineNumber(); + if(l>-1) { + //store l-1, as method header is usually one line before 1st statement + line = l-1; + break; + } + } + } + return line; + } + } diff --git a/src/soot/tagkit/AbstractHost.java b/src/soot/tagkit/AbstractHost.java index f57e8427d50..1468feae06b 100644 --- a/src/soot/tagkit/AbstractHost.java +++ b/src/soot/tagkit/AbstractHost.java @@ -39,6 +39,9 @@ */ public class AbstractHost implements Host { + + protected int line, col; + // avoid creating an empty list for each element, when it is not used // use lazy instantiation (in addTag) instead private final static List emptyList = Collections.emptyList(); @@ -110,6 +113,35 @@ public void addAllTagsOf( Host h ) { addTag( t ); } } + public int getJavaSourceStartLineNumber() { + if(line==0) { + //get line from source + SourceLnPosTag tag = (SourceLnPosTag) getTag("SourceLnPosTag"); + if(tag!=null) { + line = tag.startLn(); + } else { + //get line from bytecode + LineNumberTag tag2 = (LineNumberTag) getTag("LineNumberTag"); + if(tag2!=null) { + line = tag2.getLineNumber(); + } + else line = -1; + } + } + return line; + } + + public int getJavaSourceStartColumnNumber() { + if(col==0) { + //get line from source + SourceLnPosTag tag = (SourceLnPosTag) getTag("SourceLnPosTag"); + if(tag!=null) { + col = tag.startPos(); + } + else col = -1; + } + return col; + } } diff --git a/src/soot/tagkit/Host.java b/src/soot/tagkit/Host.java index 95ddebdf7fa..5faeaabcf63 100644 --- a/src/soot/tagkit/Host.java +++ b/src/soot/tagkit/Host.java @@ -61,6 +61,18 @@ public interface Host /** Adds all the tags from h to this host. */ public void addAllTagsOf( Host h ); + + /** + * Returns the Java source line number if available. + * Returns -1 if not. + */ + public int getJavaSourceStartLineNumber(); + + /** + * Returns the Java source line column if available. + * Returns -1 if not. + */ + public int getJavaSourceStartColumnNumber(); }