From 6bb4b4facfe3724516f75df0ceecba9e6ff5aa8a Mon Sep 17 00:00:00 2001 From: Alexander Guril Date: Fri, 14 Aug 2020 13:36:49 +0200 Subject: [PATCH 1/3] LANG-1598 Use JSR-305 for Null-Analysis --- .gitignore | 2 ++ pom.xml | 10 ++++++++++ .../apache/commons/lang3/arch/Processor.java | 18 ++++++++++-------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c30f980e360..f2822bca0de 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ site-content .project .externalToolBuilders .checkstyle +/.apt_generated/ +/.apt_generated_tests/ diff --git a/pom.xml b/pom.xml index e833726536e..6b98d2e7dd4 100644 --- a/pom.xml +++ b/pom.xml @@ -515,6 +515,16 @@ + + + + + + com.google.code.findbugs + jsr305 + 3.0.2 + + org.junit.jupiter diff --git a/src/main/java/org/apache/commons/lang3/arch/Processor.java b/src/main/java/org/apache/commons/lang3/arch/Processor.java index 563fefeb7f1..130c6da44e6 100644 --- a/src/main/java/org/apache/commons/lang3/arch/Processor.java +++ b/src/main/java/org/apache/commons/lang3/arch/Processor.java @@ -16,6 +16,8 @@ */ package org.apache.commons.lang3.arch; +import javax.annotation.Nonnull; + /** * The {@link Processor} represents a microprocessor and defines * some properties like architecture and type of the microprocessor. @@ -57,9 +59,9 @@ public enum Arch { * * @since 3.10 */ - private final String label; + private final @Nonnull String label; - Arch(final String label) { + Arch(final @Nonnull String label) { this.label = label; } @@ -68,7 +70,7 @@ public enum Arch { * * @return the label. */ - public String getLabel() { + public @Nonnull String getLabel() { return label; } } @@ -106,8 +108,8 @@ public enum Type { UNKNOWN } - private final Arch arch; - private final Type type; + private final @Nonnull Arch arch; + private final @Nonnull Type type; /** * Constructs a {@link Processor} object with the given @@ -116,7 +118,7 @@ public enum Type { * @param arch The processor architecture. * @param type The processor type. */ - public Processor(final Arch arch, final Type type) { + public Processor(final @Nonnull Arch arch, final @Nonnull Type type) { this.arch = arch; this.type = type; } @@ -128,7 +130,7 @@ public Processor(final Arch arch, final Type type) { * * @return A {@link Arch} enum. */ - public Arch getArch() { + public @Nonnull Arch getArch() { return arch; } @@ -139,7 +141,7 @@ public Arch getArch() { * * @return A {@link Type} enum. */ - public Type getType() { + public @Nonnull Type getType() { return type; } From f48d444557724a7bcb492f7b4e01f1be44f6ed10 Mon Sep 17 00:00:00 2001 From: Alexander Guril Date: Fri, 14 Aug 2020 14:06:00 +0200 Subject: [PATCH 2/3] Update ThreadUtils --- .../org/apache/commons/lang3/ThreadUtils.java | 65 +++++++++++-------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ThreadUtils.java b/src/main/java/org/apache/commons/lang3/ThreadUtils.java index fef3dc85025..1492016fee9 100644 --- a/src/main/java/org/apache/commons/lang3/ThreadUtils.java +++ b/src/main/java/org/apache/commons/lang3/ThreadUtils.java @@ -21,6 +21,10 @@ import java.util.Collections; import java.util.List; +import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + /** *

* Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}. @@ -34,7 +38,7 @@ * @since 3.5 */ public class ThreadUtils { - + /** * Finds the active thread with the specified id if it belongs to the specified thread group. * @@ -49,7 +53,7 @@ public class ThreadUtils { * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) { + public static @Nullable Thread findThreadById(final long threadId, final @Nonnull ThreadGroup threadGroup) { Validate.notNull(threadGroup, "The thread group must not be null"); final Thread thread = findThreadById(threadId); if (thread != null && threadGroup.equals(thread.getThreadGroup())) { @@ -72,7 +76,7 @@ public static Thread findThreadById(final long threadId, final ThreadGroup threa * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Thread findThreadById(final long threadId, final String threadGroupName) { + public static @Nullable Thread findThreadById(final long threadId, final @Nonnull String threadGroupName) { Validate.notNull(threadGroupName, "The thread group name must not be null"); final Thread thread = findThreadById(threadId); if (thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) { @@ -95,7 +99,7 @@ public static Thread findThreadById(final long threadId, final String threadGrou * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection findThreadsByName(final String threadName, final ThreadGroup threadGroup) { + public static @Nonnull Collection findThreadsByName(final @Nonnull String threadName, final @Nonnull ThreadGroup threadGroup) { return findThreads(threadGroup, false, new NamePredicate(threadName)); } @@ -113,7 +117,8 @@ public static Collection findThreadsByName(final String threadName, fina * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection findThreadsByName(final String threadName, final String threadGroupName) { + @SuppressWarnings("null") + public static @Nonnull Collection findThreadsByName(final @Nonnull String threadName, final @Nonnull String threadGroupName) { Validate.notNull(threadName, "The thread name must not be null"); Validate.notNull(threadGroupName, "The thread group name must not be null"); @@ -143,7 +148,7 @@ public static Collection findThreadsByName(final String threadName, fina * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection findThreadGroupsByName(final String threadGroupName) { + public static @Nonnull Collection findThreadGroupsByName(final @Nonnull String threadGroupName) { return findThreadGroups(new NamePredicate(threadGroupName)); } @@ -157,7 +162,7 @@ public static Collection findThreadGroupsByName(final String thread * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection getAllThreadGroups() { + public static @Nonnull Collection getAllThreadGroups() { return findThreadGroups(ALWAYS_TRUE_PREDICATE); } @@ -168,7 +173,7 @@ public static Collection getAllThreadGroups() { * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static ThreadGroup getSystemThreadGroup() { + public static @Nonnull ThreadGroup getSystemThreadGroup() { ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); while (threadGroup.getParent() != null) { threadGroup = threadGroup.getParent(); @@ -186,7 +191,7 @@ public static ThreadGroup getSystemThreadGroup() { * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection getAllThreads() { + public static @Nonnull Collection getAllThreads() { return findThreads(ALWAYS_TRUE_PREDICATE); } @@ -202,7 +207,7 @@ public static Collection getAllThreads() { * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection findThreadsByName(final String threadName) { + public static @Nonnull Collection findThreadsByName(final @Nonnull String threadName) { return findThreads(new NamePredicate(threadName)); } @@ -218,7 +223,7 @@ public static Collection findThreadsByName(final String threadName) { * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Thread findThreadById(final long threadId) { + public static @Nullable Thread findThreadById(final long threadId) { final Collection result = findThreads(new ThreadIdPredicate(threadId)); return result.isEmpty() ? null : result.iterator().next(); } @@ -269,7 +274,7 @@ public interface ThreadGroupPredicate { /** * Predicate which always returns true. */ - public static final AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate(); + public static final @Nonnull AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate(); /** * A predicate implementation which always returns true. @@ -280,12 +285,12 @@ private AlwaysTruePredicate() { } @Override - public boolean test(final ThreadGroup threadGroup) { + public boolean test(final @Nullable ThreadGroup threadGroup) { return true; } @Override - public boolean test(final Thread thread) { + public boolean test(final @Nullable Thread thread) { return true; } } @@ -295,7 +300,7 @@ public boolean test(final Thread thread) { */ public static class NamePredicate implements ThreadPredicate, ThreadGroupPredicate { - private final String name; + private final @Nonnull String name; /** * Predicate constructor @@ -303,19 +308,19 @@ public static class NamePredicate implements ThreadPredicate, ThreadGroupPredica * @param name thread or threadgroup name * @throws IllegalArgumentException if the name is {@code null} */ - public NamePredicate(final String name) { + public NamePredicate(final @Nonnull String name) { super(); Validate.notNull(name, "The name must not be null"); this.name = name; } @Override - public boolean test(final ThreadGroup threadGroup) { + public boolean test(final @Nullable ThreadGroup threadGroup) { return threadGroup != null && threadGroup.getName().equals(name); } @Override - public boolean test(final Thread thread) { + public boolean test(final @Nullable Thread thread) { return thread != null && thread.getName().equals(name); } } @@ -333,7 +338,7 @@ public static class ThreadIdPredicate implements ThreadPredicate { * @param threadId the threadId to match * @throws IllegalArgumentException if the threadId is zero or negative */ - public ThreadIdPredicate(final long threadId) { + public ThreadIdPredicate(final @Nonnegative long threadId) { super(); if (threadId <= 0) { throw new IllegalArgumentException("The thread id must be greater than zero"); @@ -342,7 +347,7 @@ public ThreadIdPredicate(final long threadId) { } @Override - public boolean test(final Thread thread) { + public boolean test(final @Nullable Thread thread) { return thread != null && thread.getId() == threadId; } } @@ -359,7 +364,7 @@ public boolean test(final Thread thread) { * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection findThreads(final ThreadPredicate predicate) { + public static @Nonnull Collection findThreads(final @Nonnull ThreadPredicate predicate) { return findThreads(getSystemThreadGroup(), true, predicate); } @@ -374,7 +379,7 @@ public static Collection findThreads(final ThreadPredicate predicate) { * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection findThreadGroups(final ThreadGroupPredicate predicate) { + public static @Nonnull Collection findThreadGroups(final @Nonnull ThreadGroupPredicate predicate) { return findThreadGroups(getSystemThreadGroup(), true, predicate); } @@ -389,14 +394,18 @@ public static Collection findThreadGroups(final ThreadGroupPredicat * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection findThreads(final ThreadGroup group, final boolean recurse, final ThreadPredicate predicate) { + @SuppressWarnings("null") + public static @Nonnull Collection findThreads( + final @Nonnull ThreadGroup group, + final boolean recurse, + final @Nonnull ThreadPredicate predicate) { Validate.notNull(group, "The group must not be null"); Validate.notNull(predicate, "The predicate must not be null"); int count = group.activeCount(); Thread[] threads; do { - threads = new Thread[count + (count / 2) + 1]; //slightly grow the array size + threads = new Thread[count + count / 2 + 1]; //slightly grow the array size count = group.enumerate(threads, recurse); //return value of enumerate() must be strictly less than the array size according to javadoc } while (count >= threads.length); @@ -421,14 +430,18 @@ public static Collection findThreads(final ThreadGroup group, final bool * @throws SecurityException if the current thread cannot modify * thread groups from this thread's thread group up to the system thread group */ - public static Collection findThreadGroups(final ThreadGroup group, final boolean recurse, final ThreadGroupPredicate predicate) { + @SuppressWarnings("null") + public static @Nonnull Collection findThreadGroups( + final @Nonnull ThreadGroup group, + final boolean recurse, + final @Nonnull ThreadGroupPredicate predicate) { Validate.notNull(group, "The group must not be null"); Validate.notNull(predicate, "The predicate must not be null"); int count = group.activeGroupCount(); ThreadGroup[] threadGroups; do { - threadGroups = new ThreadGroup[count + (count / 2) + 1]; //slightly grow the array size + threadGroups = new ThreadGroup[count + count / 2 + 1]; //slightly grow the array size count = group.enumerate(threadGroups, recurse); //return value of enumerate() must be strictly less than the array size according to javadoc } while (count >= threadGroups.length); From a6d3b80c11b1c290abec1eb0a3b5783150daacfe Mon Sep 17 00:00:00 2001 From: Alexander Guril Date: Fri, 14 Aug 2020 14:24:51 +0200 Subject: [PATCH 3/3] Update SystemUtils --- .../org/apache/commons/lang3/SystemUtils.java | 183 +++++++++++------- 1 file changed, 108 insertions(+), 75 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java b/src/main/java/org/apache/commons/lang3/SystemUtils.java index 2d6a97bf8a3..4a13dd48736 100644 --- a/src/main/java/org/apache/commons/lang3/SystemUtils.java +++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java @@ -18,6 +18,9 @@ import java.io.File; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + /** *

* Helpers for {@code java.lang.System}. @@ -37,7 +40,7 @@ public class SystemUtils { /** * The prefix String for all Windows OS. */ - private static final String OS_NAME_WINDOWS_PREFIX = "Windows"; + private static final @Nonnull String OS_NAME_WINDOWS_PREFIX = "Windows"; // System property constants // ----------------------------------------------------------------------- @@ -46,27 +49,27 @@ public class SystemUtils { /** * The System property key for the user home directory. */ - private static final String USER_HOME_KEY = "user.home"; + private static final @Nonnull String USER_HOME_KEY = "user.home"; /** * The System property key for the user name. */ - private static final String USER_NAME_KEY = "user.name"; + private static final @Nonnull String USER_NAME_KEY = "user.name"; /** * The System property key for the user directory. */ - private static final String USER_DIR_KEY = "user.dir"; + private static final @Nonnull String USER_DIR_KEY = "user.dir"; /** * The System property key for the Java IO temporary directory. */ - private static final String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir"; + private static final @Nonnull String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir"; /** * The System property key for the Java home directory. */ - private static final String JAVA_HOME_KEY = "java.home"; + private static final @Nonnull String JAVA_HOME_KEY = "java.home"; /** *

@@ -90,7 +93,7 @@ public class SystemUtils { * * @since 2.1 */ - public static final String AWT_TOOLKIT = getSystemProperty("awt.toolkit"); + public static final @Nullable String AWT_TOOLKIT = getSystemProperty("awt.toolkit"); /** *

@@ -112,7 +115,7 @@ public class SystemUtils { * @since 2.0 * @since Java 1.2 */ - public static final String FILE_ENCODING = getSystemProperty("file.encoding"); + public static final @Nullable String FILE_ENCODING = getSystemProperty("file.encoding"); /** *

@@ -139,7 +142,7 @@ public class SystemUtils { * @since Java 1.1 */ @Deprecated - public static final String FILE_SEPARATOR = getSystemProperty("file.separator"); + public static final @Nullable String FILE_SEPARATOR = getSystemProperty("file.separator"); /** *

@@ -157,7 +160,7 @@ public class SystemUtils { * * @since 2.1 */ - public static final String JAVA_AWT_FONTS = getSystemProperty("java.awt.fonts"); + public static final @Nullable String JAVA_AWT_FONTS = getSystemProperty("java.awt.fonts"); /** *

@@ -175,7 +178,7 @@ public class SystemUtils { * * @since 2.1 */ - public static final String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv"); + public static final @Nullable String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv"); /** *

@@ -196,7 +199,7 @@ public class SystemUtils { * @since 2.1 * @since Java 1.4 */ - public static final String JAVA_AWT_HEADLESS = getSystemProperty("java.awt.headless"); + public static final @Nullable String JAVA_AWT_HEADLESS = getSystemProperty("java.awt.headless"); /** *

@@ -214,7 +217,7 @@ public class SystemUtils { * * @since 2.1 */ - public static final String JAVA_AWT_PRINTERJOB = getSystemProperty("java.awt.printerjob"); + public static final @Nullable String JAVA_AWT_PRINTERJOB = getSystemProperty("java.awt.printerjob"); /** *

@@ -232,7 +235,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path"); + public static final @Nullable String JAVA_CLASS_PATH = getSystemProperty("java.class.path"); /** *

@@ -250,7 +253,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version"); + public static final @Nullable String JAVA_CLASS_VERSION = getSystemProperty("java.class.version"); /** *

@@ -269,7 +272,7 @@ public class SystemUtils { * * @since Java 1.2. Not used in Sun versions after 1.2. */ - public static final String JAVA_COMPILER = getSystemProperty("java.compiler"); + public static final @Nullable String JAVA_COMPILER = getSystemProperty("java.compiler"); /** *

@@ -287,7 +290,7 @@ public class SystemUtils { * * @since Java 1.4 */ - public static final String JAVA_ENDORSED_DIRS = getSystemProperty("java.endorsed.dirs"); + public static final @Nullable String JAVA_ENDORSED_DIRS = getSystemProperty("java.endorsed.dirs"); /** *

@@ -305,7 +308,7 @@ public class SystemUtils { * * @since Java 1.3 */ - public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs"); + public static final @Nullable String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs"); /** *

@@ -323,7 +326,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String JAVA_HOME = getSystemProperty(JAVA_HOME_KEY); + public static final @Nullable String JAVA_HOME = getSystemProperty(JAVA_HOME_KEY); /** *

@@ -341,7 +344,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_IO_TMPDIR = getSystemProperty(JAVA_IO_TMPDIR_KEY); + public static final @Nullable String JAVA_IO_TMPDIR = getSystemProperty(JAVA_IO_TMPDIR_KEY); /** *

@@ -359,7 +362,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path"); + public static final @Nullable String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path"); /** *

@@ -378,7 +381,7 @@ public class SystemUtils { * @since 2.0 * @since Java 1.3 */ - public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name"); + public static final @Nullable String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name"); /** *

@@ -397,7 +400,7 @@ public class SystemUtils { * @since 2.0 * @since Java 1.3 */ - public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version"); + public static final @Nullable String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version"); /** *

@@ -415,7 +418,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name"); + public static final @Nullable String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name"); /** *

@@ -433,7 +436,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor"); + public static final @Nullable String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor"); /** *

@@ -451,8 +454,9 @@ public class SystemUtils { * * @since Java 1.3 */ - public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version"); - private static final JavaVersion JAVA_SPECIFICATION_VERSION_AS_ENUM = JavaVersion.get(JAVA_SPECIFICATION_VERSION); + public static final @Nullable String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version"); + + private static final @Nullable JavaVersion JAVA_SPECIFICATION_VERSION_AS_ENUM = JavaVersion.get(JAVA_SPECIFICATION_VERSION); /** *

@@ -471,7 +475,7 @@ public class SystemUtils { * @since 2.1 * @since Java 1.4 */ - public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = + public static final @Nullable String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = getSystemProperty("java.util.prefs.PreferencesFactory"); /** @@ -490,7 +494,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String JAVA_VENDOR = getSystemProperty("java.vendor"); + public static final @Nullable String JAVA_VENDOR = getSystemProperty("java.vendor"); /** *

@@ -508,7 +512,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url"); + public static final @Nullable String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url"); /** *

@@ -526,7 +530,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String JAVA_VERSION = getSystemProperty("java.version"); + public static final @Nullable String JAVA_VERSION = getSystemProperty("java.version"); /** *

@@ -545,7 +549,7 @@ public class SystemUtils { * @since 2.0 * @since Java 1.2 */ - public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info"); + public static final @Nullable String JAVA_VM_INFO = getSystemProperty("java.vm.info"); /** *

@@ -563,7 +567,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name"); + public static final @Nullable String JAVA_VM_NAME = getSystemProperty("java.vm.name"); /** *

@@ -581,7 +585,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name"); + public static final @Nullable String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name"); /** *

@@ -599,7 +603,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor"); + public static final @Nullable String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor"); /** *

@@ -617,7 +621,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version"); + public static final @Nullable String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version"); /** *

@@ -635,7 +639,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor"); + public static final @Nullable String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor"); /** *

@@ -653,7 +657,7 @@ public class SystemUtils { * * @since Java 1.2 */ - public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version"); + public static final @Nullable String JAVA_VM_VERSION = getSystemProperty("java.vm.version"); /** *

@@ -673,7 +677,7 @@ public class SystemUtils { * @since Java 1.1 */ @Deprecated - public static final String LINE_SEPARATOR = getSystemProperty("line.separator"); + public static final @Nullable String LINE_SEPARATOR = getSystemProperty("line.separator"); /** *

@@ -691,7 +695,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String OS_ARCH = getSystemProperty("os.arch"); + public static final @Nullable String OS_ARCH = getSystemProperty("os.arch"); /** *

@@ -709,7 +713,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String OS_NAME = getSystemProperty("os.name"); + public static final @Nullable String OS_NAME = getSystemProperty("os.name"); /** *

@@ -727,7 +731,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String OS_VERSION = getSystemProperty("os.version"); + public static final @Nullable String OS_VERSION = getSystemProperty("os.version"); /** *

@@ -748,7 +752,7 @@ public class SystemUtils { * @since Java 1.1 */ @Deprecated - public static final String PATH_SEPARATOR = getSystemProperty("path.separator"); + public static final @Nullable String PATH_SEPARATOR = getSystemProperty("path.separator"); /** *

@@ -768,7 +772,7 @@ public class SystemUtils { * @since 2.0 * @since Java 1.2 */ - public static final String USER_COUNTRY = getSystemProperty("user.country") == null ? + public static final @Nullable String USER_COUNTRY = getSystemProperty("user.country") == null ? getSystemProperty("user.region") : getSystemProperty("user.country"); /** @@ -787,7 +791,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String USER_DIR = getSystemProperty(USER_DIR_KEY); + public static final @Nullable String USER_DIR = getSystemProperty(USER_DIR_KEY); /** *

@@ -805,7 +809,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String USER_HOME = getSystemProperty(USER_HOME_KEY); + public static final @Nullable String USER_HOME = getSystemProperty(USER_HOME_KEY); /** *

@@ -824,7 +828,7 @@ public class SystemUtils { * @since 2.0 * @since Java 1.2 */ - public static final String USER_LANGUAGE = getSystemProperty("user.language"); + public static final @Nullable String USER_LANGUAGE = getSystemProperty("user.language"); /** *

@@ -842,7 +846,7 @@ public class SystemUtils { * * @since Java 1.1 */ - public static final String USER_NAME = getSystemProperty(USER_NAME_KEY); + public static final @Nullable String USER_NAME = getSystemProperty(USER_NAME_KEY); /** *

@@ -860,7 +864,7 @@ public class SystemUtils { * * @since 2.1 */ - public static final String USER_TIMEZONE = getSystemProperty("user.timezone"); + public static final @Nullable String USER_TIMEZONE = getSystemProperty("user.timezone"); // Java version checks // ----------------------------------------------------------------------- @@ -1568,7 +1572,7 @@ public class SystemUtils { * @see System#getProperty(String) * @since 2.1 */ - public static File getJavaHome() { + public static @Nonnull File getJavaHome() { return new File(System.getProperty(JAVA_HOME_KEY)); } @@ -1583,7 +1587,7 @@ public static File getJavaHome() { * @return the host name. Will be {@code null} if the environment variable is not defined. * @since 3.6 */ - public static String getHostName() { + public static @Nullable String getHostName() { return IS_OS_WINDOWS ? System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME"); } @@ -1598,7 +1602,7 @@ public static String getHostName() { * @see System#getProperty(String) * @since 2.1 */ - public static File getJavaIoTmpDir() { + public static @Nonnull File getJavaIoTmpDir() { return new File(System.getProperty(JAVA_IO_TMPDIR_KEY)); } @@ -1610,8 +1614,13 @@ public static File getJavaIoTmpDir() { * @param versionPrefix the prefix for the java version * @return true if matches, or false if not or can't determine */ - private static boolean getJavaVersionMatches(final String versionPrefix) { - return isJavaVersionMatch(JAVA_SPECIFICATION_VERSION, versionPrefix); + private static boolean getJavaVersionMatches(final @Nonnull String versionPrefix) { + final String javaSpecVersion = JAVA_SPECIFICATION_VERSION; + if (javaSpecVersion != null) { + return isJavaVersionMatch(javaSpecVersion, versionPrefix); + } else { + return false; + } } /** @@ -1621,8 +1630,13 @@ private static boolean getJavaVersionMatches(final String versionPrefix) { * @param osVersionPrefix the prefix for the version * @return true if matches, or false if not or can't determine */ - private static boolean getOsMatches(final String osNamePrefix, final String osVersionPrefix) { - return isOSMatch(OS_NAME, OS_VERSION, osNamePrefix, osVersionPrefix); + private static boolean getOsMatches(final @Nonnull String osNamePrefix, final @Nonnull String osVersionPrefix) { + final String osName = OS_NAME; + final String osVersion = OS_VERSION; + if(osName == null || osVersion == null) { + return false; + } + return isOSMatch(osName, osVersion, osNamePrefix, osVersionPrefix); } /** @@ -1631,8 +1645,13 @@ private static boolean getOsMatches(final String osNamePrefix, final String osVe * @param osNamePrefix the prefix for the OS name * @return true if matches, or false if not or can't determine */ - private static boolean getOsMatchesName(final String osNamePrefix) { - return isOSNameMatch(OS_NAME, osNamePrefix); + private static boolean getOsMatchesName(final @Nonnull String osNamePrefix) { + final String osName = OS_NAME; + if (osName != null) { + return isOSNameMatch(osName, osNamePrefix); + } else { + return false; + } } // ----------------------------------------------------------------------- @@ -1648,7 +1667,7 @@ private static boolean getOsMatchesName(final String osNamePrefix) { * @param property the system property name * @return the system property value or {@code null} if a security problem occurs */ - private static String getSystemProperty(final String property) { + private static @Nullable String getSystemProperty(final @Nonnull String property) { try { return System.getProperty(property); } catch (final SecurityException ex) { @@ -1675,7 +1694,7 @@ private static String getSystemProperty(final String property) { * @return the environment variable value or {@code defaultValue} if a security problem occurs * @since 3.8 */ - public static String getEnvironmentVariable(final String name, final String defaultValue) { + public static @Nullable String getEnvironmentVariable(final @Nonnull String name, final @Nullable String defaultValue) { try { final String value = System.getenv(name); return value == null ? defaultValue : value; @@ -1697,7 +1716,7 @@ public static String getEnvironmentVariable(final String name, final String defa * @see System#getProperty(String) * @since 2.1 */ - public static File getUserDir() { + public static @Nonnull File getUserDir() { return new File(System.getProperty(USER_DIR_KEY)); } @@ -1712,7 +1731,7 @@ public static File getUserDir() { * @see System#getProperty(String) * @since 2.1 */ - public static File getUserHome() { + public static @Nonnull File getUserHome() { return new File(System.getProperty(USER_HOME_KEY)); } @@ -1727,7 +1746,7 @@ public static File getUserHome() { * @see System#getProperty(String) * @since 3.10 */ - public static String getUserName() { + public static @Nullable String getUserName() { return System.getProperty(USER_NAME_KEY); } @@ -1743,7 +1762,7 @@ public static String getUserName() { * @see System#getProperty(String) * @since 3.10 */ - public static String getUserName(final String defaultValue) { + public static @Nullable String getUserName(final @Nullable String defaultValue) { return System.getProperty(USER_NAME_KEY, defaultValue); } @@ -1768,8 +1787,13 @@ public static boolean isJavaAwtHeadless() { * @param requiredVersion the required version, for example 1.31f * @return {@code true} if the actual version is equal or greater than the required version */ - public static boolean isJavaVersionAtLeast(final JavaVersion requiredVersion) { - return JAVA_SPECIFICATION_VERSION_AS_ENUM.atLeast(requiredVersion); + public static boolean isJavaVersionAtLeast(final @Nonnull JavaVersion requiredVersion) { + final JavaVersion javaSpecVersion = JAVA_SPECIFICATION_VERSION_AS_ENUM; + if (javaSpecVersion != null) { + return javaSpecVersion.atLeast(requiredVersion); + } else { + return false; + } } /** @@ -1784,8 +1808,13 @@ public static boolean isJavaVersionAtLeast(final JavaVersion requiredVersion) { * @return {@code true} if the actual version is equal or greater than the required version * @since 3.9 */ - public static boolean isJavaVersionAtMost(final JavaVersion requiredVersion) { - return JAVA_SPECIFICATION_VERSION_AS_ENUM.atMost(requiredVersion); + public static boolean isJavaVersionAtMost(final @Nonnull JavaVersion requiredVersion) { + final JavaVersion javaSpecVersion = JAVA_SPECIFICATION_VERSION_AS_ENUM; + if (javaSpecVersion != null) { + return javaSpecVersion.atMost(requiredVersion); + } else { + return false; + } } /** @@ -1800,8 +1829,8 @@ public static boolean isJavaVersionAtMost(final JavaVersion requiredVersion) { * @param versionPrefix the prefix for the expected Java version * @return true if matches, or false if not or can't determine */ - static boolean isJavaVersionMatch(final String version, final String versionPrefix) { - if (version == null) { + static boolean isJavaVersionMatch(final @Nonnull String version, final @Nonnull String versionPrefix) { + if (StringUtils.isEmpty(version)) { return false; } return version.startsWith(versionPrefix); @@ -1819,8 +1848,12 @@ static boolean isJavaVersionMatch(final String version, final String versionPref * @param osVersionPrefix the prefix for the expected OS version * @return true if matches, or false if not or can't determine */ - static boolean isOSMatch(final String osName, final String osVersion, final String osNamePrefix, final String osVersionPrefix) { - if (osName == null || osVersion == null) { + static boolean isOSMatch( + final @Nonnull String osName, + final @Nonnull String osVersion, + final @Nonnull String osNamePrefix, + final @Nonnull String osVersionPrefix) { + if (StringUtils.isEmpty(osName) || StringUtils.isEmpty(osVersion)) { return false; } return isOSNameMatch(osName, osNamePrefix) && isOSVersionMatch(osVersion, osVersionPrefix); @@ -1836,8 +1869,8 @@ static boolean isOSMatch(final String osName, final String osVersion, final Stri * @param osNamePrefix the prefix for the expected OS name * @return true if matches, or false if not or can't determine */ - static boolean isOSNameMatch(final String osName, final String osNamePrefix) { - if (osName == null) { + static boolean isOSNameMatch(final @Nonnull String osName, final @Nonnull String osNamePrefix) { + if (StringUtils.isEmpty(osName)) { return false; } return osName.startsWith(osNamePrefix); @@ -1853,7 +1886,7 @@ static boolean isOSNameMatch(final String osName, final String osNamePrefix) { * @param osVersionPrefix the prefix for the expected OS version * @return true if matches, or false if not or can't determine */ - static boolean isOSVersionMatch(final String osVersion, final String osVersionPrefix) { + static boolean isOSVersionMatch(final @Nonnull String osVersion, final @Nonnull String osVersionPrefix) { if (StringUtils.isEmpty(osVersion)) { return false; }