Thanks guys. Both of your answers led me towards solving this problem using the AST implementation in JAVAPARSER. Here is a code snippet to help others
class CatchNameExpr extends VoidVisitorAdapter { HashMap<String, ArrayList<Integer>> variableLineNumMap;`` ArrayList<String> variableList; boolean functionParsing = false; public CatchNameExpr(ArrayList<String> classVariables) { variableList=classVariables; } public void visit(MethodDeclaration method, Object arg) { System.out.println("---------------"); System.out.println(method.getName()); System.out.println("---------------"); variableLineNumMap = new HashMap<String, ArrayList<Integer>>(); System.out.println(); functionParsing = true; visit(method.getBody(),arg); // Analyze lines for variable usage. Add to list of vars after checking if its read or written or unknown. functionParsing = false; } public void visit(NameExpr n, Object arg) { if(!functionParsing) return; //TODO: check if this var was declared above it, as a local var to the func. if yes, return ArrayList<Integer> setOfLineNum; System.out.println(n.getBeginLine()+" NameExpr " + n.getName()); if(!variableList.contains(n.getName()) || n.getName().length()==0) return; if (!variableLineNumMap.containsKey(n.getName())) { setOfLineNum = new ArrayList<Integer>(); setOfLineNum.add(n.getBeginLine()); variableLineNumMap.put(n.getName(), setOfLineNum); } else { setOfLineNum = variableLineNumMap.get(n.getName()); setOfLineNum.add(n.getBeginLine()); variableLineNumMap.put(n.getName(), setOfLineNum); } } }
Create an instance of the class --->
CatchNameExpr nameExp = new CatchNameExpr(classVariables); nameExp.visit(classCompilationUnit, null);
Similarly, you can visit the AST for the following expressions, operators, conditions, etc.
http://www.jarvana.com/jarvana/view/com/google/code/javaparser/javaparser/1.0.8/javaparser-1.0.8-javadoc.jar!/japa/parser/ast/visitor/VoidVisitorAdapter.html
I am well aware that a processor with byte code will be more efficient and do a better job than I can hope for. But, given the time constraints, this option set me better.
Thanks guys Jasmeet
source share