use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.
the class ExplodedGraphWalker method execute.
private void execute(MethodTree tree) {
CFG cfg = CFG.build(tree);
exitBlock = cfg.exitBlock();
checkerDispatcher.init(tree, cfg);
liveVariables = LiveVariables.analyze(cfg);
explodedGraph = new ExplodedGraph();
methodTree = tree;
constraintManager = new ConstraintManager();
workList = new LinkedList<>();
// Linked hashSet is required to guarantee order of yields to be generated
endOfExecutionPath = new LinkedHashSet<>();
if (DEBUG_MODE_ACTIVATED) {
LOG.debug("Exploring Exploded Graph for method " + tree.simpleName().name() + " at line " + ((JavaTree) tree).getLine());
}
programState = ProgramState.EMPTY_STATE;
steps = 0;
for (ProgramState startingState : startingStates(tree, programState)) {
enqueue(new ProgramPoint(cfg.entry()), startingState);
}
while (!workList.isEmpty()) {
steps++;
if (steps > maxSteps()) {
throwMaxSteps(tree);
}
// LIFO:
setNode(workList.removeFirst());
CFG.Block block = (CFG.Block) programPosition.block;
if (block.successors().isEmpty()) {
endOfExecutionPath.add(node);
continue;
}
try {
Tree terminator = block.terminator();
if (programPosition.i < block.elements().size()) {
// process block element
visit(block.elements().get(programPosition.i), terminator);
} else if (terminator == null) {
// process block exit, which is unconditional jump such as goto-statement or return-statement
handleBlockExit(programPosition);
} else if (programPosition.i == block.elements().size()) {
// process block exist, which is conditional jump such as if-statement
checkerDispatcher.executeCheckPostStatement(terminator);
} else {
// process branch
// process block exist, which is conditional jump such as if-statement
checkerDispatcher.executeCheckPreStatement(terminator);
handleBlockExit(programPosition);
}
} catch (TooManyNestedBooleanStatesException e) {
throwTooManyBooleanStates(tree, e);
} catch (RelationalSymbolicValue.TransitiveRelationExceededException e) {
throwTooManyTransitiveRelationsException(tree, e);
}
}
handleEndOfExecutionPath(false);
checkerDispatcher.executeCheckEndOfExecution();
// Cleanup:
workList = null;
node = null;
programState = null;
constraintManager = null;
}
use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.
the class ClassTreeImplTest method getLine.
@Test
public void getLine() {
CompilationUnitTree tree = createTree("class A {\n" + "A a = new A() {};" + "\n}");
ClassTree classTree = (ClassTree) tree.types().get(0);
assertThat(((JavaTree) classTree).getLine()).isEqualTo(1);
// get line of anonymous class
NewClassTree newClassTree = (NewClassTree) ((VariableTree) classTree.members().get(0)).initializer();
assertThat(((JavaTree) newClassTree.classBody()).getLine()).isEqualTo(2);
}
use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.
the class TreeFactory method annotationIdentifier.
public TypeTree annotationIdentifier(InternalSyntaxToken firstIdentifier, Optional<List<Tuple<InternalSyntaxToken, InternalSyntaxToken>>> rests) {
List<InternalSyntaxToken> children = Lists.newArrayList();
children.add(firstIdentifier);
if (rests.isPresent()) {
for (Tuple<InternalSyntaxToken, InternalSyntaxToken> rest : rests.get()) {
children.add(rest.first());
children.add(rest.second());
}
}
JavaTree result = null;
InternalSyntaxToken dotToken = null;
for (InternalSyntaxToken child : children) {
if (!child.getGrammarRuleKey().equals(JavaTokenType.IDENTIFIER)) {
dotToken = child;
} else {
InternalSyntaxToken identifierToken = child;
if (result == null) {
result = new IdentifierTreeImpl(identifierToken);
} else {
IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
result = new MemberSelectExpressionTreeImpl((ExpressionTree) result, dotToken, identifier);
}
}
}
return (TypeTree) result;
}
use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.
the class TreeFactory method newCompilationUnit.
// End of literals
// Compilation unit
public CompilationUnitTreeImpl newCompilationUnit(JavaTree spacing, Optional<PackageDeclarationTree> packageDeclaration, Optional<List<ImportClauseTree>> importDeclarations, Optional<ModuleDeclarationTree> moduleDeclaration, Optional<List<Tree>> typeDeclarations, InternalSyntaxToken eof) {
ImmutableList.Builder<ImportClauseTree> imports = ImmutableList.builder();
if (importDeclarations.isPresent()) {
for (ImportClauseTree child : importDeclarations.get()) {
imports.add(child);
}
}
ImmutableList.Builder<Tree> types = ImmutableList.builder();
if (typeDeclarations.isPresent()) {
for (Tree child : typeDeclarations.get()) {
types.add(child);
}
}
return new CompilationUnitTreeImpl(packageDeclaration.orNull(), imports.build(), types.build(), moduleDeclaration.orNull(), eof);
}
use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.
the class TreeFactory method newAnnotationType.
public ClassTreeImpl newAnnotationType(InternalSyntaxToken openBraceToken, Optional<List<JavaTree>> annotationTypeElementDeclarations, InternalSyntaxToken closeBraceToken) {
// TODO
ModifiersTreeImpl emptyModifiers = ModifiersTreeImpl.emptyModifiers();
ImmutableList.Builder<Tree> members = ImmutableList.builder();
if (annotationTypeElementDeclarations.isPresent()) {
for (JavaTree annotationTypeElementDeclaration : annotationTypeElementDeclarations.get()) {
if (annotationTypeElementDeclaration.getGrammarRuleKey().equals(JavaLexer.VARIABLE_DECLARATORS)) {
for (VariableTreeImpl variable : (VariableDeclaratorListTreeImpl) annotationTypeElementDeclaration) {
members.add(variable);
}
} else if (!annotationTypeElementDeclaration.is(Kind.TOKEN)) {
members.add(annotationTypeElementDeclaration);
}
}
}
return new ClassTreeImpl(emptyModifiers, openBraceToken, members.build(), closeBraceToken);
}
Aggregations