Skip to content

Commit 226cb99

Browse files
committed
Handle Class.getPackage() returning null for default package on Java 8
Fixes #4076.
1 parent 380cb88 commit 226cb99

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.commons.util;
12+
13+
import static org.junit.platform.commons.util.PackageUtils.DEFAULT_PACKAGE_NAME;
14+
15+
/**
16+
* Collection of utilities for working with package names.
17+
*
18+
* <h2>DISCLAIMER</h2>
19+
*
20+
* <p>These utilities are intended solely for usage within the JUnit framework
21+
* itself. <strong>Any usage by external parties is not supported.</strong>
22+
* Use at your own risk!
23+
*
24+
* @since 1.11.3
25+
*/
26+
class PackageNameUtils {
27+
28+
static String getPackageName(Class<?> clazz) {
29+
Package p = clazz.getPackage();
30+
if (p != null) {
31+
return p.getName();
32+
}
33+
String className = clazz.getName();
34+
int index = className.lastIndexOf('.');
35+
return index == -1 ? DEFAULT_PACKAGE_NAME : className.substring(0, index);
36+
}
37+
38+
}

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,8 @@ private static boolean isPackagePrivate(Member member) {
19031903
}
19041904

19051905
private static boolean declaredInSamePackage(Method m1, Method m2) {
1906-
return m1.getDeclaringClass().getPackage().getName().equals(m2.getDeclaringClass().getPackage().getName());
1906+
return PackageNameUtils.getPackageName(m1.getDeclaringClass()).equals(
1907+
PackageNameUtils.getPackageName(m2.getDeclaringClass()));
19071908
}
19081909

19091910
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.commons.util;
12+
13+
/**
14+
* Collection of utilities for working with package names.
15+
*
16+
* <h2>DISCLAIMER</h2>
17+
*
18+
* <p>These utilities are intended solely for usage within the JUnit framework
19+
* itself. <strong>Any usage by external parties is not supported.</strong>
20+
* Use at your own risk!
21+
*
22+
* @since 1.11.3
23+
*/
24+
class PackageNameUtils {
25+
26+
static String getPackageName(Class<?> clazz) {
27+
return clazz.getPackageName();
28+
}
29+
30+
}

0 commit comments

Comments
 (0)