All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.java.checks.AbstractPrintfChecker Maven / Gradle / Ivy

/*
 * SonarQube Java
 * Copyright (C) 2012-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the Sonar Source-Available License for more details.
 *
 * You should have received a copy of the Sonar Source-Available License
 * along with this program; if not, see https://sonarsource.com/license/ssal/
 */
package org.sonar.java.checks;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import org.sonar.java.checks.methods.AbstractMethodDetection;
import org.sonar.java.model.LiteralUtils;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.Arguments;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.LiteralTree;
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.NewArrayTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.collections.SetUtils;

public abstract class AbstractPrintfChecker extends AbstractMethodDetection {

  private static final Set TIME_CONVERSIONS = SetUtils.immutableSetOf(
    "H", "I", "k", "l", "M", "S", "L", "N", "p", "z", "Z", "s", "Q",
    "B", "b", "h", "A", "a", "C", "Y", "y", "j", "m", "d", "e",
    "R", "T", "r", "D", "F", "c"
  );

  protected static final String JAVA_LANG_STRING = "java.lang.String";
  protected static final String JAVA_LANG_THROWABLE = "java.lang.Throwable";
  protected static final String ORG_APACHE_LOGGING_LOG4J_LOGGER = "org.apache.logging.log4j.Logger";

  private static final String ARGUMENT_INDEX = "(\\d++\\$)?";
  private static final String FLAGS = "([-#+ 0,(\\<]++)?";
  private static final String WIDTH = "([1-9]\\d*+)?";
  private static final String PRECISION = "(\\.\\d++)?";
  private static final String CONVERSION = "([tT])?([a-zA-Z%])";
  private static final Pattern PRINTF_PARAM_PATTERN = Pattern.compile("%" + ARGUMENT_INDEX + FLAGS + WIDTH + PRECISION + CONVERSION);

  protected static final String PRINTF_METHOD_NAME = "printf";
  private static final String FORMAT_METHOD_NAME = "format";
  protected static final Set LEVELS = SetUtils.immutableSetOf("debug", "error", "info", "trace", "warn", "fatal");

  protected static final MethodMatchers MESSAGE_FORMAT = MethodMatchers.create()
    .ofTypes("java.text.MessageFormat")
    .names(FORMAT_METHOD_NAME)
    .withAnyParameters()
    .build();

  protected static final MethodMatchers STRING_FORMATTED = MethodMatchers.create()
    .ofTypes(JAVA_LANG_STRING)
    .names("formatted")
    .withAnyParameters()
    .build();

  protected static final Pattern MESSAGE_FORMAT_PATTERN = Pattern.compile("\\{(?\\d+)(?,\\w+)?(?