use of org.sonar.plugins.java.api.tree.ExpressionTree 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.ExpressionTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method visitVariable.
@Override
public void visitVariable(VariableTree tree) {
scan(tree.modifiers());
completeMetadata(((VariableTreeImpl) tree).getSymbol(), tree.modifiers().annotations());
// skip type, it has been resolved in second pass
ExpressionTree initializer = tree.initializer();
if (initializer != null) {
resolveAs(initializer, JavaSymbol.VAR);
if (((JavaType) initializer.symbolType()).isTagged(JavaType.DEFERRED)) {
setInferedType(tree.type().symbolType(), (DeferredType) initializer.symbolType());
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree 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.ExpressionTree in project sonar-java by SonarSource.
the class ParameterNullnessCheck method reportIssue.
private void reportIssue(Tree syntaxNode, ExpressionTree argument, JavaSymbol.MethodJavaSymbol methodSymbol) {
String declarationMessage = "constructor declaration";
if (!methodSymbol.isConstructor()) {
declarationMessage = "method '" + methodSymbol.getName() + "' declaration";
}
String message = String.format("Annotate the parameter with @javax.annotation.Nullable in %s, or make sure that null can not be passed as argument.", declarationMessage);
Tree reportTree;
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
reportTree = ExpressionUtils.methodName((MethodInvocationTree) syntaxNode);
} else {
reportTree = ((NewClassTree) syntaxNode).identifier();
}
Flow.Builder secondaryBuilder = Flow.builder();
MethodTree declarationTree = methodSymbol.declaration();
if (declarationTree != null) {
secondaryBuilder.add(new JavaFileScannerContext.Location(StringUtils.capitalize(declarationMessage) + ".", declarationTree.simpleName()));
}
secondaryBuilder.add(new JavaFileScannerContext.Location("Argument can be null.", argument));
reportIssue(reportTree, message, Collections.singleton(secondaryBuilder.build()));
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class FirstPass method visitCompilationUnit.
@Override
public void visitCompilationUnit(CompilationUnitTree tree) {
JavaSymbol.PackageJavaSymbol compilationUnitPackage = symbols.defaultPackage;
PackageDeclarationTree packageDeclaration = tree.packageDeclaration();
if (packageDeclaration != null) {
ExpressionTree packageName = packageDeclaration.packageName();
PackageResolverVisitor packageResolver = new PackageResolverVisitor();
packageName.accept(packageResolver);
compilationUnitPackage = (JavaSymbol.PackageJavaSymbol) resolve.findIdentInPackage(compilationUnitPackage, packageResolver.packageName, JavaSymbol.PCK);
semanticModel.associateSymbol(packageName, compilationUnitPackage);
}
compilationUnitPackage.members = new Scope(compilationUnitPackage);
env = new Resolve.Env();
env.packge = compilationUnitPackage;
env.scope = compilationUnitPackage.members;
env.namedImports = new Scope.ImportScope(compilationUnitPackage);
env.starImports = resolve.createStarImportScope(compilationUnitPackage);
env.staticStarImports = resolve.createStaticStarImportScope(compilationUnitPackage);
semanticModel.associateEnv(tree, env);
scan(tree.types());
restoreEnvironment(tree);
resolveImports(tree.imports());
completeSymbols();
}
Aggregations