Hello
I am a recoder user and I have a question about branches detection. I would like to know if recoder can return the line branch number (if, while and for). If is possible, could you send me a simple code example? Tank You.
-- Alexandre
Hi,
Yes Recoder can do that. The following does the trick:
import java.util.List;
import recoder.CrossReferenceServiceConfiguration; import recoder.ParserException; import recoder.convenience.ForestWalker; import recoder.io.PropertyNames; import recoder.java.CompilationUnit; import recoder.java.ProgramElement; import recoder.java.statement.BranchStatement; import recoder.java.statement.LoopStatement; import recoder.kit.UnitKit;
public class Test { public static void main(String[] args) { CrossReferenceServiceConfiguration sc = new CrossReferenceServiceConfiguration(); sc.getProjectSettings().setProperty(PropertyNames.INPUT_PATH, / Set proper path here /); sc.getProjectSettings().ensureSystemClassesAreInPath(); try { List<CompilationUnit> cus = sc.getSourceFileRepository().getAllCompilationUnitsFromPath(); ForestWalker fw = new ForestWalker(cus); while (fw.next()) { ProgramElement pe = fw.getProgramElement(); if (pe instanceof LoopStatement || pe instanceof BranchStatement) { System.out.println(pe.getClass().getSimpleName() + " in " + UnitKit.getCompilationUnit(pe).getDataLocation() + " line " + pe.getStartPosition().getLine() + " column " + pe.getStartPosition().getColumn()); } } } catch (ParserException e) { e.printStackTrace(); System.exit(-1); } } }
Note that this also prints switch, try, and do-loops. Just switch the condition in the if-statement to change to the desired behavior.
Regards, Tobias
Thanks a lot. Regards, Alexandre
2014-09-07 11:46 GMT-03:00 Tobias Gutzmann tgutzmann@users.sf.net:
Hi, Yes Recoder can do that. The following does the trick: import java.util.List; import recoder.CrossReferenceServiceConfiguration; import recoder.ParserException; import recoder.convenience.ForestWalker; import recoder.io.PropertyNames; import recoder.java.CompilationUnit; import recoder.java.ProgramElement; import recoder.java.statement.BranchStatement; import recoder.java.statement.LoopStatement; import recoder.kit.UnitKit; public class Test { public static void main(String[] args) { CrossReferenceServiceConfiguration sc = new CrossReferenceServiceConfiguration(); sc.getProjectSettings().setProperty(PropertyNames.INPUT_PATH, / Set proper path here /); sc.getProjectSettings().ensureSystemClassesAreInPath(); try { List<CompilationUnit> cus = sc.getSourceFileRepository().getAllCompilationUnitsFromPath(); ForestWalker fw = new ForestWalker(cus); while (fw.next()) { ProgramElement pe = fw.getProgramElement(); if (pe instanceof LoopStatement || pe instanceof BranchStatement) { System.out.println(pe.getClass().getSimpleName() + " in " + UnitKit.getCompilationUnit(pe).getDataLocation() + " line " + pe.getStartPosition().getLine() + " column " + pe.getStartPosition().getColumn()); } } } catch (ParserException e) { e.printStackTrace(); System.exit(-1); } } } Note that this also prints switch, try, and do-loops. Just switch the condition in the if-statement to change to the desired behavior. Regards, Tobias Branch line number. https://sourceforge.net/p/recoder/discussion/88904/thread/b57e54bc/?limit=25#5e80 Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/recoder/discussion/88904/ To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/
Branch line number. https://sourceforge.net/p/recoder/discussion/88904/thread/b57e54bc/?limit=25#5e80
Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/recoder/discussion/88904/
To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/
Log in to post a comment.
Hello
I am a recoder user and I have a question about branches detection.
I would like to know if recoder can return the line branch number (if,
while and for). If is possible, could you send me a simple code example?
Tank You.
--
Alexandre
Hi,
Yes Recoder can do that. The following does the trick:
import java.util.List;
import recoder.CrossReferenceServiceConfiguration;
import recoder.ParserException;
import recoder.convenience.ForestWalker;
import recoder.io.PropertyNames;
import recoder.java.CompilationUnit;
import recoder.java.ProgramElement;
import recoder.java.statement.BranchStatement;
import recoder.java.statement.LoopStatement;
import recoder.kit.UnitKit;
public class Test {
public static void main(String[] args) {
CrossReferenceServiceConfiguration sc = new CrossReferenceServiceConfiguration();
sc.getProjectSettings().setProperty(PropertyNames.INPUT_PATH, / Set proper path here /);
sc.getProjectSettings().ensureSystemClassesAreInPath();
try {
List<CompilationUnit> cus = sc.getSourceFileRepository().getAllCompilationUnitsFromPath();
ForestWalker fw = new ForestWalker(cus);
while (fw.next()) {
ProgramElement pe = fw.getProgramElement();
if (pe instanceof LoopStatement || pe instanceof BranchStatement) {
System.out.println(pe.getClass().getSimpleName() + " in " + UnitKit.getCompilationUnit(pe).getDataLocation() + " line "
+ pe.getStartPosition().getLine() + " column " + pe.getStartPosition().getColumn());
}
}
} catch (ParserException e) {
e.printStackTrace();
System.exit(-1);
}
}
}
Note that this also prints switch, try, and do-loops. Just switch the condition in the if-statement to change to the desired behavior.
Regards,
Tobias
Hello
Thanks a lot.
Regards,
Alexandre
2014-09-07 11:46 GMT-03:00 Tobias Gutzmann tgutzmann@users.sf.net:
--
Alexandre