Created
May 27, 2017 09:26
-
-
Save ftomassetti/1f0e2a5710ee4f7258a3a1bfa44c3375 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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