use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class ExplodedGraphWalker method executeLogicalAssignment.
private void executeLogicalAssignment(AssignmentExpressionTree tree) {
ExpressionTree variable = tree.variable();
// FIXME handle also assignments with this SONARJAVA-2242
if (variable.is(Tree.Kind.IDENTIFIER)) {
ProgramState.Pop unstack = programState.unstackValue(2);
ProgramState.SymbolicValueSymbol assignedTo = unstack.valuesAndSymbols.get(1);
ProgramState.SymbolicValueSymbol value = unstack.valuesAndSymbols.get(0);
programState = unstack.state;
SymbolicValue symbolicValue = constraintManager.createBinarySymbolicValue(tree, ImmutableList.of(assignedTo, value));
Symbol symbol = ((IdentifierTree) variable).symbol();
programState = programState.stackValue(symbolicValue, symbol);
programState = programState.put(symbol, symbolicValue);
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method resolveAs.
public JavaSymbol resolveAs(Tree tree, int kind, Resolve.Env resolveEnv, boolean associateReference) {
if (tree.is(Tree.Kind.IDENTIFIER, Tree.Kind.MEMBER_SELECT)) {
JavaSymbol resolvedSymbol;
IdentifierTree identifierTree;
if (tree.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mse = (MemberSelectExpressionTree) tree;
if (JavaKeyword.CLASS.getValue().equals(mse.identifier().name())) {
// resolve type of expression xxx.class
return resolveClassType(tree, resolveEnv, mse);
}
identifierTree = mse.identifier();
List<AnnotationTree> identifierAnnotations = identifierTree.annotations();
scan(identifierAnnotations);
completeMetadata((JavaSymbol) identifierTree.symbol(), identifierAnnotations);
Resolve.Resolution res = getSymbolOfMemberSelectExpression(mse, kind, resolveEnv);
resolvedSymbol = res.symbol();
JavaType resolvedType = resolve.resolveTypeSubstitution(res.type(), getType(mse.expression()));
registerType(identifierTree, resolvedType);
registerType(tree, resolvedType);
} else {
identifierTree = (IdentifierTree) tree;
Resolve.Resolution resolution = resolve.findIdent(resolveEnv, identifierTree.name(), kind);
resolvedSymbol = resolution.symbol();
registerType(tree, resolution.type());
}
if (associateReference) {
associateReference(identifierTree, resolvedSymbol);
}
return resolvedSymbol;
}
tree.accept(this);
JavaType type = getType(tree);
if (tree.is(Tree.Kind.INFERED_TYPE)) {
type = symbols.deferedType((AbstractTypedTree) tree);
registerType(tree, type);
}
if (type == null) {
throw new IllegalStateException("Type not resolved " + tree);
}
return type.symbol;
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method completeMetadata.
private static void completeMetadata(JavaSymbol symbol, List<AnnotationTree> annotations) {
for (AnnotationTree tree : annotations) {
AnnotationInstanceResolve annotationInstance = new AnnotationInstanceResolve((JavaSymbol.TypeJavaSymbol) tree.symbolType().symbol());
symbol.metadata().addAnnotation(annotationInstance);
Arguments arguments = tree.arguments();
if (arguments.size() > 1 || (!arguments.isEmpty() && arguments.get(0).is(Tree.Kind.ASSIGNMENT))) {
for (ExpressionTree expressionTree : arguments) {
AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
// TODO: Store more precise value than the expression (real value in case of literal, symbol for enums, array of values, solve constants?)
annotationInstance.addValue(new AnnotationValueResolve(((IdentifierTree) aet.variable()).name(), aet.expression()));
}
} else {
// Constant
addConstantValue(tree, annotationInstance);
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method visitAnnotation.
@Override
public void visitAnnotation(AnnotationTree tree) {
if (((AbstractTypedTree) tree.annotationType()).isTypeSet()) {
// This handle the case where type and its annotation is shared between two variables.
return;
}
resolveAs(tree.annotationType(), JavaSymbol.TYP);
Arguments arguments = tree.arguments();
if (arguments.size() > 1 || (!arguments.isEmpty() && arguments.get(0).is(Tree.Kind.ASSIGNMENT))) {
// resolve by identifying correct identifier in assignment.
for (ExpressionTree expressionTree : arguments) {
AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
IdentifierTree variable = (IdentifierTree) aet.variable();
JavaSymbol identInType = resolve.findMethod(semanticModel.getEnv(tree), getType(tree.annotationType()), variable.name(), ImmutableList.<JavaType>of()).symbol();
associateReference(variable, identInType);
JavaType type = identInType.type;
if (type == null) {
type = Symbols.unknownType;
}
registerType(variable, type);
resolveAs(aet.expression(), JavaSymbol.VAR);
}
} else {
for (ExpressionTree expressionTree : arguments) {
resolveAs(expressionTree, JavaSymbol.VAR);
}
}
registerType(tree, getType(tree.annotationType()));
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class DebugMethodYieldsOnInvocationsCheck method reportYields.
private void reportYields(MethodInvocationTree mit, CheckerDispatcher checkerDispatcher) {
MethodBehavior mb = checkerDispatcher.peekMethodBehavior((Symbol.MethodSymbol) mit.symbol());
if (mb != null && mb.isComplete()) {
IdentifierTree methodName = getIdentifier(mit.methodSelect());
String message = String.format("Method '%s' has %d method yields.", methodName.name(), mb.yields().size());
Set<Flow> flow = flowFromYield(mb, methodName);
reportIssue(methodName, message, flow);
}
}
Aggregations