-
Notifications
You must be signed in to change notification settings - Fork 0
ASTProcessor samples
Carsten Hammer edited this page Nov 4, 2021
·
2 revisions
Here we search for all variable declarations with the type Iterator.
ReferenceHolder<String, Object> dataholder = new ReferenceHolder<>();
ASTProcessor<ReferenceHolder<String, Object>,String,Object> astp=new ASTProcessor<>(dataholder, null);
astp.callVariableDeclarationStatementVisitor(Iterator.class,(node,holder) -> {
holder.put("init", node);
List<String> computeVarName = computeVarName((VariableDeclarationStatement)node);
holder.put("initvarname", computeVarName.get(0));
System.out.println("init "+node.getNodeType() + " :" + node);
return true;
}).build(cunit2);Here we search for all variable declarations with the type Iterator.
The following points are important to understand the code
- the third parameter of the method
callVariableDeclarationStatementVisitoris providing aFunctionto navigate from the found node to another node. This way you can change the node the next fluent call is starting on. You can leave it out. In that case, it is just starting with an implicit visitor of the next fluent call on the found node itself. - the first parameter of the method
callMethodInvocationVisitorallows specifying the method name to consider in the method visit. Without it, you would have to determine the method in your handler lambda expression. - all fluent calls are creating a visitor that runs on all nodes below starting with the node from the fluent call before in the usual way.
ReferenceHolder<String, Object> dataholder = new ReferenceHolder<>();
ASTProcessor<ReferenceHolder<String, Object>, String, Object> astp=new ASTProcessor<>(dataholder, null);
astp.callVariableDeclarationStatementVisitor(Iterator.class,(node,holder) -> {
/**
* This lambda expression is called for all VariableDeclarationStatement of type Iterator
*/
holder.put("init", node);
List<String> computeVarName = computeVarName((VariableDeclarationStatement)node);
holder.put("initvarname", computeVarName.get(0));
return true;
},s -> s.getParent()).callWhileStatementVisitor((node,holder) -> {
/**
* This lambda expression is called for all WhileStatements below the parent of each VariableDeclarationStatement
*/
holder.put("while", node);
String name = computeNextVarname((WhileStatement)node);
holder.put("whilevarname", name);
return true;
}, s -> ((WhileStatement)s).getBody()).callMethodInvocationVisitor("next",(node,holder) -> {
/**
* This lambda expression is called for all MethodInvocations "next()" in each Body of WhileStatements found above
*/
String name=(String) holder.get("initvarname");
Expression element2 = ((MethodInvocation)node).getExpression();
SimpleName sn= ASTNodes.as(element2, SimpleName.class);
if (sn !=null) {
String identifier = sn.getIdentifier();
if(!name.equals(identifier))
return true;
if(name.equals(holder.get("whilevarname"))) {
System.out.println("=====================");
System.out.println("iterator: "+holder.get("init").toString().trim());
System.out.println("while: "+holder.get("while").toString().trim());
System.out.println("next: "+node.toString().trim());
}
}
return true;
}).build(cunit2);In this case, we find all
- "next" method calls
- following a "while"
- following a variable declaration statement of type
Iteratorin the given code. So with one call, you extract all while loops running onIterator.
Sample Plugins