Search in sources :

Example 1 with InternalJavaIssueBuilder

use of org.sonar.java.reporting.InternalJavaIssueBuilder in project sonar-java by SonarSource.

the class AssertJChainSimplificationCheck method createIssueBuilder.

private InternalJavaIssueBuilder createIssueBuilder(MethodInvocationTree predicate, AssertJChainSimplificationIndex.Simplification simplification) {
    InternalJavaIssueBuilder builder = QuickFixHelper.newIssue(context).forRule(this).onTree(ExpressionUtils.methodName(predicate)).withMessage(ISSUE_MESSAGE_FORMAT_STRING, simplification.getReplacement());
    simplification.getQuickFix().ifPresent(builder::withQuickFixes);
    return builder;
}
Also used : InternalJavaIssueBuilder(org.sonar.java.reporting.InternalJavaIssueBuilder)

Example 2 with InternalJavaIssueBuilder

use of org.sonar.java.reporting.InternalJavaIssueBuilder in project sonar-java by SonarSource.

the class BigDecimalDoubleConstructorCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    NewClassTree newClassTree = (NewClassTree) tree;
    if (BIG_DECIMAL_DOUBLE_FLOAT.matches(newClassTree)) {
        InternalJavaIssueBuilder builder = ((InternalJavaIssueBuilder) ((DefaultJavaFileScannerContext) context).newIssue()).forRule(this).onTree(tree);
        Arguments arguments = newClassTree.arguments();
        if (arguments.size() == 1) {
            builder.withMessage("Use \"BigDecimal.valueOf\" instead.");
            builder.withQuickFix(() -> valueOfQuickFix(newClassTree));
        } else {
            builder.withMessage("Use \"new BigDecimal(String, MathContext)\" instead.");
            ExpressionTree firstArgument = arguments.get(0);
            if (firstArgument instanceof LiteralTree) {
                builder.withQuickFix(() -> stringConstructorQuickFix((LiteralTree) firstArgument));
            }
        }
        builder.report();
    }
}
Also used : InternalJavaIssueBuilder(org.sonar.java.reporting.InternalJavaIssueBuilder) DefaultJavaFileScannerContext(org.sonar.java.model.DefaultJavaFileScannerContext) Arguments(org.sonar.plugins.java.api.tree.Arguments) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 3 with InternalJavaIssueBuilder

use of org.sonar.java.reporting.InternalJavaIssueBuilder in project sonar-java by SonarSource.

the class CompareToResultTestCheck method reportIssue.

private void reportIssue(BinaryExpressionTree binaryExpression, long operandValue, boolean compareToIsLeft) {
    InternalJavaIssueBuilder builder = QuickFixHelper.newIssue(context).forRule(this).onTree(binaryExpression.operatorToken()).withMessage("Only the sign of the result should be examined.");
    if (binaryExpression.is(Kind.EQUAL_TO)) {
        // For !=, even if we could in theory replace by <=/>= 0, we do not suggest quick fixes and let the user figure out what was his intent
        builder.withQuickFix(() -> getQuickFix(binaryExpression, operandValue, compareToIsLeft));
    }
    builder.report();
}
Also used : InternalJavaIssueBuilder(org.sonar.java.reporting.InternalJavaIssueBuilder)

Example 4 with InternalJavaIssueBuilder

use of org.sonar.java.reporting.InternalJavaIssueBuilder in project sonar-java by SonarSource.

the class DateFormatWeekYearCheck method inspectPattern.

private void inspectPattern(ExpressionTree argument) {
    Optional<String> literal = argument.asConstant(String.class);
    if (!literal.isPresent()) {
        return;
    }
    String datePattern = literal.get();
    if (StringUtils.contains(datePattern, 'w')) {
        return;
    }
    int start = datePattern.indexOf('Y');
    if (start > -1) {
        int end = getEndIndexOfYearSequence(datePattern, start);
        String firstYSeq = datePattern.substring(start, end);
        String replacement = firstYSeq.toLowerCase(Locale.ENGLISH);
        String message = String.format(RECOMMENDATION_YEAR_MESSAGE, firstYSeq, replacement);
        InternalJavaIssueBuilder issueBuilder = QuickFixHelper.newIssue(context).forRule(this).onTree(argument).withMessage(message);
        if (argument.is(Tree.Kind.STRING_LITERAL)) {
            issueBuilder.withQuickFix(() -> computeQuickFix(argument, start, end, replacement));
        }
        issueBuilder.report();
    }
}
Also used : InternalJavaIssueBuilder(org.sonar.java.reporting.InternalJavaIssueBuilder)

Aggregations

InternalJavaIssueBuilder (org.sonar.java.reporting.InternalJavaIssueBuilder)4 DefaultJavaFileScannerContext (org.sonar.java.model.DefaultJavaFileScannerContext)1 Arguments (org.sonar.plugins.java.api.tree.Arguments)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)1 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)1