From ccc197d9503267243cc4427d60dd360e1953d4c1 Mon Sep 17 00:00:00 2001 From: Mikolaj Izdebski Date: Fri, 24 May 2013 17:15:36 +0200 Subject: [PATCH] Be careful when unboxing Boolean that can be null Add explicit check for null value when unboxing Booleans that can be null. Also extract duplicated code to a utility class. --- .../maven/resolver/DefaultArtifactBlacklist.java | 4 +-- .../maven/resolver/DefaultResolver.java | 4 +-- .../fedoraproject/maven/utils/LoggingUtils.java | 30 ++++++++++++++++++++++ .../maven/rpminstall/plugin/InstallMojo.java | 4 +-- 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 xmvn-core/src/main/java/org/fedoraproject/maven/utils/LoggingUtils.java diff --git a/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultArtifactBlacklist.java b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultArtifactBlacklist.java index 451f71e..a6502be 100644 --- a/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultArtifactBlacklist.java +++ b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultArtifactBlacklist.java @@ -26,6 +26,7 @@ import org.codehaus.plexus.logging.Logger; import org.fedoraproject.maven.config.Configurator; import org.fedoraproject.maven.config.ResolverSettings; import org.fedoraproject.maven.model.Artifact; +import org.fedoraproject.maven.utils.LoggingUtils; /** * @author Mikolaj Izdebski @@ -113,8 +114,7 @@ public class DefaultArtifactBlacklist { Set aliasBlacklist = new TreeSet<>(); ResolverSettings settings = configurator.getConfiguration().getResolverSettings(); - int loggerThreshold = settings.isDebug() ? Logger.LEVEL_DEBUG : Logger.LEVEL_WARN; - logger.setThreshold( Math.min( logger.getThreshold(), loggerThreshold ) ); + LoggingUtils.setLoggerThreshold( logger, settings.isDebug() ); for ( String prefix : settings.getPrefixes() ) { diff --git a/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultResolver.java b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultResolver.java index 7fb51c5..c6a82ad 100644 --- a/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultResolver.java +++ b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultResolver.java @@ -31,6 +31,7 @@ import org.fedoraproject.maven.repository.Layout; import org.fedoraproject.maven.repository.Repository; import org.fedoraproject.maven.repository.SingletonRepository; import org.fedoraproject.maven.utils.AtomicFileCounter; +import org.fedoraproject.maven.utils.LoggingUtils; /** * @author Mikolaj Izdebski @@ -90,8 +91,7 @@ public class DefaultResolver initializeBisect(); ResolverSettings settings = configurator.getConfiguration().getResolverSettings(); - int loggerThreshold = settings.isDebug() ? Logger.LEVEL_DEBUG : Logger.LEVEL_WARN; - logger.setThreshold( Math.min( logger.getThreshold(), loggerThreshold ) ); + LoggingUtils.setLoggerThreshold( logger, settings.isDebug() ); resolvers.add( new LocalResolver( settings ) ); diff --git a/xmvn-core/src/main/java/org/fedoraproject/maven/utils/LoggingUtils.java b/xmvn-core/src/main/java/org/fedoraproject/maven/utils/LoggingUtils.java new file mode 100644 index 0000000..7dcd265 --- /dev/null +++ b/xmvn-core/src/main/java/org/fedoraproject/maven/utils/LoggingUtils.java @@ -0,0 +1,30 @@ +/*- + * Copyright (c) 2012-2013 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.fedoraproject.maven.utils; + +import org.codehaus.plexus.logging.Logger; + +/** + * @author Mikolaj Izdebski + */ +public class LoggingUtils +{ + public static void setLoggerThreshold( Logger logger, Boolean debugSetting ) + { + int threshold = debugSetting != null && debugSetting ? Logger.LEVEL_DEBUG : Logger.LEVEL_WARN; + logger.setThreshold( Math.min( logger.getThreshold(), threshold ) ); + } +} diff --git a/xmvn-mojo/src/main/java/org/fedoraproject/maven/rpminstall/plugin/InstallMojo.java b/xmvn-mojo/src/main/java/org/fedoraproject/maven/rpminstall/plugin/InstallMojo.java index a8bf55d..1f53a49 100644 --- a/xmvn-mojo/src/main/java/org/fedoraproject/maven/rpminstall/plugin/InstallMojo.java +++ b/xmvn-mojo/src/main/java/org/fedoraproject/maven/rpminstall/plugin/InstallMojo.java @@ -44,6 +44,7 @@ import org.fedoraproject.maven.installer.DefaultPackage; import org.fedoraproject.maven.installer.Installer; import org.fedoraproject.maven.installer.ProjectInstallationException; import org.fedoraproject.maven.installer.ProjectInstaller; +import org.fedoraproject.maven.utils.LoggingUtils; /** * @author Mikolaj Izdebski @@ -124,8 +125,7 @@ public class InstallMojo { Configuration configuration = configurator.getConfiguration(); settings = configuration.getInstallerSettings(); - int loggerThreshold = settings.isDebug() ? Logger.LEVEL_DEBUG : Logger.LEVEL_WARN; - logger.setThreshold( Math.min( logger.getThreshold(), loggerThreshold ) ); + LoggingUtils.setLoggerThreshold( logger, settings.isDebug() ); Map packages = new TreeMap<>(); -- 1.8.1.4