Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of from SpreadOperator performance warning that was given by Detekt while using Spring Boot?

Recently, I added Detekt analyzer to my application.

After I runned detekt (./gradlew detekt), I got SpreadOperator warning in my main class of application.

Warning on code: runApplication<MessCallsApplication>(*args)

You can read about SpreadOperator warning here: SpreadOperator Warning

my main class:

@SpringBootApplication(exclude = [RedisAutoConfiguration::class])
@EnableCircuitBreaker
@EnableScheduling
class MyApplication {

    companion object : KLogging()
}

fun main(args: Array<String>) {
    Thread.setDefaultUncaughtExceptionHandler { _, exception ->
        MessCallsApplication.logger.error(exception) { "Uncaught exception" }
    }

    runApplication<MessCallsApplication>(*args)
}

Question is, What is the best practice to get rid of from that SpreadOperator warning? Or is it impossible?

like image 483
Seydazimov Nurbol Avatar asked Oct 22 '19 11:10

Seydazimov Nurbol


3 Answers

You can add @Suppress("SpreadOperator") before your expression like this:

@SpringBootApplication(exclude = [RedisAutoConfiguration::class])
@EnableCircuitBreaker
@EnableScheduling
class MyApplication {

    companion object : KLogging()
}

fun main(args: Array<String>) {
    Thread.setDefaultUncaughtExceptionHandler { _, exception ->
        MessCallsApplication.logger.error(exception) { "Uncaught exception" }
    }
    @Suppress("SpreadOperator")
    runApplication<MessCallsApplication>(*args)
}
like image 113
akobor Avatar answered Oct 24 '22 21:10

akobor


In your particular case:

 runApplication<MessCallsApplication>(args = args)

Edit:

This detekt warning is not longer a case: https://github.com/detekt/detekt/pull/3157

However as Klitos Kyriacou mentioned in code - this array is copied (even twice!). Decompiled bytecode:

public final class MessCallsApplicationKt {
   public static final void main(@NotNull String[] args) {
      Intrinsics.checkNotNullParameter(args, "args");
      Schedulers.enableMetrics();
      String[] args$iv = (String[])Arrays.copyOf(args, args.length);
      int $i$f$runApplication = false;
      Intrinsics.checkExpressionValueIsNotNull(SpringApplication.run(MessCallsApplication.class, (String[])Arrays.copyOf(args$iv, args$iv.length)), "SpringApplication.run(T::class.java, *args)");
   }
}
like image 37
pixel Avatar answered Oct 24 '22 21:10

pixel


or simply change from:

fun main(args: Array<String>)

to

fun main(vararg args: String)
like image 37
Jeff Fang Avatar answered Oct 24 '22 21:10

Jeff Fang