use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class ExplodedGraphWalker method executeVariable.
/**
* @see JLS8 4.12.5 for details
*/
private void executeVariable(VariableTree variableTree, @Nullable Tree terminator) {
Symbol variableSymbol = variableTree.symbol();
if (variableTree.initializer() == null) {
SymbolicValue sv = null;
if (terminator != null && terminator.is(Tree.Kind.FOR_EACH_STATEMENT)) {
sv = constraintManager.createSymbolicValue(variableTree);
if (isAnnotatedNonNull(variableSymbol)) {
programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
}
} else if (variableTree.parent().is(Tree.Kind.CATCH)) {
sv = handleCatchVariable(variableSymbol.type());
// an exception have been thrown and caught, stack must be cleared
// see notes in JVMS 8 - ยง6.5. - instruction "athrow"
programState = programState.clearStack();
// exception variable is not null by definition
programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
}
if (sv != null) {
programState = programState.put(variableSymbol, sv);
}
} else {
ProgramState.Pop unstack = programState.unstackValue(1);
programState = unstack.state;
programState = programState.put(variableSymbol, unstack.values.get(0));
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class FlowComputation method run.
private Set<Flow> run(final ExplodedGraph.Node node, PSet<Symbol> trackedSymbols) {
Set<Flow> flows = new HashSet<>();
Deque<ExecutionPath> workList = new ArrayDeque<>();
SameConstraints sameConstraints = new SameConstraints(node, trackedSymbols, domains);
node.edges().stream().flatMap(e -> startPath(e, trackedSymbols, sameConstraints)).forEach(workList::push);
int flowSteps = 0;
Set<ExecutionPath> visited = new HashSet<>(workList);
while (!workList.isEmpty()) {
ExecutionPath path = workList.pop();
if (path.finished) {
flows.add(path.flow);
} else {
path.lastEdge.parent.edges().stream().filter(path::notVisited).flatMap(path::addEdge).forEach(ep -> {
if (visited.add(ep)) {
workList.push(ep);
}
});
}
flowSteps++;
if (flowSteps == MAX_FLOW_STEPS) {
LOG.debug("Flow was not able to complete");
break;
}
}
return flows;
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class FlowComputation method flow.
private static Set<Flow> flow(ExplodedGraph.Node currentNode, Set<SymbolicValue> symbolicValues, Predicate<Constraint> addToFlow, Predicate<Constraint> terminateTraversal, List<Class<? extends Constraint>> domains, Set<Symbol> symbols, boolean skipExceptionMessages) {
Set<SymbolicValue> allSymbolicValues = symbolicValues.stream().map(FlowComputation::computedFrom).flatMap(Set::stream).collect(Collectors.toSet());
PSet<Symbol> trackedSymbols = PCollections.emptySet();
for (Symbol symbol : symbols) {
trackedSymbols = trackedSymbols.add(symbol);
}
if (symbols.isEmpty()) {
for (SymbolicValue symbolicValue : symbolicValues) {
for (Symbol symbol : symbolicValue.computedFromSymbols()) {
trackedSymbols = trackedSymbols.add(symbol);
}
}
}
FlowComputation flowComputation = new FlowComputation(allSymbolicValues, addToFlow, terminateTraversal, domains, skipExceptionMessages);
return flowComputation.run(currentNode, trackedSymbols);
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method addConstantValue.
private static void addConstantValue(AnnotationTree tree, AnnotationInstanceResolve annotationInstance) {
Collection<Symbol> scopeSymbols = tree.annotationType().symbolType().symbol().memberSymbols();
for (ExpressionTree expressionTree : tree.arguments()) {
String name = "";
for (Symbol scopeSymbol : scopeSymbols) {
if (scopeSymbol.isMethodSymbol()) {
name = scopeSymbol.name();
break;
}
}
annotationInstance.addValue(new AnnotationValueResolve(name, expressionTree));
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class LeastUpperBound method supertypes.
@VisibleForTesting
Set<Type> supertypes(JavaType type) {
List<Type> result = new ArrayList<>();
result.add(type);
Symbol.TypeSymbol symbol = type.symbol();
TypeSubstitution substitution = getTypeSubstitution(type);
if (substitution.size() == 0 && !((JavaSymbol.TypeJavaSymbol) symbol).typeVariableTypes.isEmpty()) {
// raw type : let's create a substitution based on erasures
TypeSubstitution ts = new TypeSubstitution();
((JavaSymbol.TypeJavaSymbol) symbol).typeVariableTypes.forEach(t -> ts.add(t, t.erasure()));
substitution = ts;
}
result.addAll(interfacesWithSubstitution(symbol, substitution));
Type superClass = symbol.superClass();
while (superClass != null) {
JavaType substitutedSuperClass = applySubstitution(superClass, substitution);
result.add(substitutedSuperClass);
substitution = getTypeSubstitution(substitutedSuperClass);
JavaSymbol.TypeJavaSymbol superClassSymbol = substitutedSuperClass.getSymbol();
result.addAll(interfacesWithSubstitution(superClassSymbol, substitution));
superClass = superClassSymbol.superClass();
}
return new LinkedHashSet<>(result);
}
Aggregations