Eclipse – Sonar S2629 possible false positive with new String

I’m using latest Eclipse and Sonar plugin

In answer for logging there’s the following line:

log.debug("Request body: {}", new String(body, "UTF-8"));

Which should create String only if in DEBUG level:

/**
 * Log a message at the DEBUG level according to the specified format
 * and argument.
 * <p/>
 * <p>This form avoids superfluous object creation when the logger
 * is disabled for the DEBUG level. </p>
 *
 * @param format the format string
 * @param arg    the argument
 */
public void debug(String format, Object arg);

But Sonar is marking it as squid:S2629:

“Preconditions” and logging arguments should not require evaluation (squid:S2629)

And give examples with concatenation

logger.log(Level.DEBUG, “Something went wrong: ” + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages

Is it a false positive sonar warning or am I missing something?

This isn’t a duplicate of this question which is generally asking about the rule concept, which is concatenation, but not formatting with creating object as new String

Also link of an answer says creating new Date() isn’t creating an issue with built in format:

public static void main(String[] args) {
    LOGGER.info("The program started at {}", new Date());
}

}

Logging this way, you avoid performance overhead for strings concatenation when nothing should be actually logged.

Solution:

In non-DEBUG mode the line

log.debug("Request body: {}", new String(body, "UTF-8"));

instead of

log.debug(MessageFormatter.format("Request body: {}", new String(body, "UTF-8")));

avoids the creation of the string that is created via MessageFormatter.format(String messagePattern, Object arg), but not the creation of the other string that is created by new String(body, "UTF-8").

This means it is not a false positive, because the arguments are calculated first before the logging method is called.

As long as SLF4J does not support lambda expression to lazy evaluate arguments (see comment by ZhekaKozlov), the following utility method can be used as a workaround:

private static Object lazyToString(final Supplier<String> stringSupplier) {
    return new Object() {
        @Override
        public String toString() {
            return stringSupplier.get();
        }
    };
}

This can be used to limit the converting of the byte array into a string to the DEBUG mode only:

log.debug("Request body: {}", lazyToString(() -> new String(body, StandardCharsets.UTF_8)));

Java 11 and E(fx)clipse JavaFX plugin on Eclipse 4.9: An error has occurred – see the log file

I can’t get my Eclipse 2018-09 (4.9) running with Java 11 and E(fx)clipse JavaFX plugin.

I run the typical Help -> Install new software procedure for E(fx)clipse, but after restart, Eclipse fully crashes: I can’t even open Eclipse anymore for a new workspace!

When I try to run Eclipse, I get a dialog:

“An error has occurred. See the log file C:\workspace\.metadata\.log”

I’ve put a copy of that log file on my Github.

Well, I could run a hello world JavaFX application using this manual library setup, but I’d really like to get E(fx)clipse running in order to get the Eclipse menu options and functionality. So how to solve that? Thanks.

Solution:

In Eclipse e(fx)clipse the support for Java 11 has just been implemented and is just available since version 3.4.1, but according to your log you have version 3.3.0.201809010600.

See Eclipse bug 539739

Eclipse resetting compiler settings to java 1.5

I have just cloned a git repository for a maven project and then imported the project into STS as a maven project.

I set up the project and sub-modules as java 1.8 projects and then ran a maven update and then noticed that all the java 1.5 compiler settings seem to have been reapplied.

I cant figure out why eclipse is resetting this, even if I uncheck ‘Enable project specific settings’ it still reverts back to having this checked and for java 5 to be the default.

I read a post about setting the version in the maven-compiler-plugin configuration but this project does not currently have any configuration for that plugin in any of the pom files.

Solution:

In your pom.xml put this:

<project>
  ...
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  ...
</project>

The reason you’ll have to do this is that “the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with”. See Maven Compiler Plugin for further details.

When you run Maven update command from Eclipse without setting the explicit java version, the project will take the default Maven settings, that’s why you end up with 1.5 Java version.