其他分享
首页 > 其他分享> > # SonarLint 检查提示和解决方法

# SonarLint 检查提示和解决方法

作者:互联网

SonarLint 检查提示和解决方法


SonarLint: Use the built-in formatting to construct this argument.
String message = "Hellow World!";
logger.info("Test Info message:"+message);
String message = "Hellow World!";
String format = String.format("Test Info message:%s", message);
logger.info(format);

SonarLint: Replace this use of System.out or System.err by a logger.
System.out.printf("Hellow");
private static final Logger logger = Logger.getLogger(String.valueOf(Demo.class));
logger.info("Hellow");

SonarLint: Invoke method(s) only conditionally.
logger.info(String.valueOf((char)c));
String s2 = String.valueOf((char) c);
logger.info(s2);

SonarLint: Define and throw a dedicated exception instead of using a generic one.
throw new Exception("Test");
public class MyException extends RuntimeException{

    public MyException() {
    }

    public MyException(String message) {
        super(message);
    }
}

throw new MyException("Hellow");

SonarLint: String contains no format specifiers.
String message = "Hellow Test!";
logger.info(String.format("method error: {}",message));
String message = "Hellow Test!";
logger.info(String.format("method error: %s",message));

SonarLint: Either re-interrupt this method or rethrow the “InterruptedException”.
private  void dealAddNum() {
    try {
        num++;
        Thread.sleep(100);
    } catch (InterruptedException e) {
        log.info(e.getMessage());
    }
}
private  void dealAddNum() {
    try {
        num++;
        Thread.sleep(100);
    } catch (InterruptedException e) {
        log.info(e.getMessage());
        Thread.currentThread().interrupt();
    }
}

SonarLint: Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.
@Override
public void setProperties(Properties properties) {
    
}
@Override
public void setProperties(Properties properties) {
    throw new UnsupportedOperationException();
}

SonarLint: “ThreadLocal” variables should be cleaned up when no longer used
private static final ThreadLocal<SimpleDateFormat> DATETIME_FORMATTER = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

private static final ThreadLocal<SimpleDateFormat> DATETIME_FORMATTER = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

public void test1(){
    try{
        // do something
    }catch(Exception e){
        // do something
    }finally{
		DATETIME_FORMATTER.remove();
    }
}

SonarLint: Extract this nested try block into a separate method.
@Test
public void test1(){
    try {

        try{

        }catch (NullPointerException e){
            logger.info(e.getMessage());
        }
    }catch (Exception e){
        logger.info(e.getMessage());
    }
}
@Test
public void test1(){
    try {
        logger.info("test messages!");
    }catch (Exception e){
        logger.info(e.getMessage());
    }
}

SonarLint: Replace the usage of the “instanceof” operator by a catch block.
try {
    // do somethings
} catch (Exception e) {
    if (e instanceof InvocationTargetException) {
        logger.error(LogConstant.LOG_FAILURE_PREFIX + (((InvocationTargetException) e).getTargetException()).getMessage());
    } else {
        logger.error(LogConstant.LOG_FAILURE_PREFIX + e.getMessage());
    }
    return result;
} 
try{
    logger.info("test!");
}catch (NullPointerException nullPointerException){
    logger.info(nullPointerException.getMessage());
}catch (IllegalArgumentException illegalArgumentException){
    logger.info(illegalArgumentException.getMessage());
}

SonarLint: Use “java.util.Random.nextInt()” instead.
user.setGender(((int)(10 * Math.random())) % 2  );
Random random = new Random();
user.setGender(random.nextInt(10) % 2  );

标签:info,String,检查,提示,SonarLint,catch,logger,message
来源: https://blog.csdn.net/qq_37248504/article/details/115266913