Skip to content

Instantly share code, notes, and snippets.

@ftomassetti
Created May 27, 2017 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftomassetti/1f0e2a5710ee4f7258a3a1bfa44c3375 to your computer and use it in GitHub Desktop.
Save ftomassetti/1f0e2a5710ee4f7258a3a1bfa44c3375 to your computer and use it in GitHub Desktop.
package me.tomassetti.examples;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.stmt.WhileStmt;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.model.declarations.ValueDeclaration;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import me.tomassetti.support.NodeIterator;
public class FindWhileStatements {
public static void main(String[] args) {
String code = "class A {\n" +
"\tstatic boolean f = true;\n" +
"\tvoid methodFoo() {\n" +
"\t\twhile (f) {\n" +
"\t\t\tmethodFoo();\n" +
"\t\t}\n" +
"\t}\n" +
"}";
TypeSolver ts = new ReflectionTypeSolver(true);
JavaParserFacade jpf = JavaParserFacade.get(ts);
new NodeIterator(node -> {
if (node instanceof WhileStmt) {
WhileStmt whileStmt = (WhileStmt)node;
if (whileStmt.getCondition() instanceof NameExpr) {
SymbolReference<? extends ValueDeclaration> symbolReference = jpf.solve((NameExpr) whileStmt.getCondition());
if (symbolReference.isSolved() && symbolReference.getCorrespondingDeclaration().isField() && symbolReference.getCorrespondingDeclaration().asField().isStatic()) {
System.out.println("FOUND!");
}
}
}
return true;
})
.explore(JavaParser.parse(code));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment