From 226cb990b4216f2cd94893e67b388e0719d5fdf1 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Thu, 17 Oct 2024 08:33:26 +0200 Subject: [PATCH] Handle Class.getPackage() returning `null` for default package on Java 8 Fixes #4076. --- .../commons/util/PackageNameUtils.java | 38 +++++++++++++++++++ .../commons/util/ReflectionUtils.java | 3 +- .../commons/util/PackageNameUtils.java | 30 +++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 junit-platform-commons/src/main/java/org/junit/platform/commons/util/PackageNameUtils.java create mode 100644 junit-platform-commons/src/main/java9/org/junit/platform/commons/util/PackageNameUtils.java diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/PackageNameUtils.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/PackageNameUtils.java new file mode 100644 index 000000000000..9a018363a4a1 --- /dev/null +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/PackageNameUtils.java @@ -0,0 +1,38 @@ +/* + * Copyright 2015-2024 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * https://www.eclipse.org/legal/epl-v20.html + */ + +package org.junit.platform.commons.util; + +import static org.junit.platform.commons.util.PackageUtils.DEFAULT_PACKAGE_NAME; + +/** + * Collection of utilities for working with package names. + * + *

DISCLAIMER

+ * + *

These utilities are intended solely for usage within the JUnit framework + * itself. Any usage by external parties is not supported. + * Use at your own risk! + * + * @since 1.11.3 + */ +class PackageNameUtils { + + static String getPackageName(Class clazz) { + Package p = clazz.getPackage(); + if (p != null) { + return p.getName(); + } + String className = clazz.getName(); + int index = className.lastIndexOf('.'); + return index == -1 ? DEFAULT_PACKAGE_NAME : className.substring(0, index); + } + +} diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java index 26bd8e1698cf..86d5758d5640 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java @@ -1903,7 +1903,8 @@ private static boolean isPackagePrivate(Member member) { } private static boolean declaredInSamePackage(Method m1, Method m2) { - return m1.getDeclaringClass().getPackage().getName().equals(m2.getDeclaringClass().getPackage().getName()); + return PackageNameUtils.getPackageName(m1.getDeclaringClass()).equals( + PackageNameUtils.getPackageName(m2.getDeclaringClass())); } /** diff --git a/junit-platform-commons/src/main/java9/org/junit/platform/commons/util/PackageNameUtils.java b/junit-platform-commons/src/main/java9/org/junit/platform/commons/util/PackageNameUtils.java new file mode 100644 index 000000000000..84afaf736290 --- /dev/null +++ b/junit-platform-commons/src/main/java9/org/junit/platform/commons/util/PackageNameUtils.java @@ -0,0 +1,30 @@ +/* + * Copyright 2015-2024 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * https://www.eclipse.org/legal/epl-v20.html + */ + +package org.junit.platform.commons.util; + +/** + * Collection of utilities for working with package names. + * + *

DISCLAIMER

+ * + *

These utilities are intended solely for usage within the JUnit framework + * itself. Any usage by external parties is not supported. + * Use at your own risk! + * + * @since 1.11.3 + */ +class PackageNameUtils { + + static String getPackageName(Class clazz) { + return clazz.getPackageName(); + } + +}