Skip to content

Commit

Permalink
Handle Class.getPackage() returning null for default package on Java 8
Browse files Browse the repository at this point in the history
Fixes #4076.
  • Loading branch information
marcphilipp committed Oct 17, 2024
1 parent 380cb88 commit 464de7e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions junit-platform-commons/junit-platform-commons.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tasks.jar {

tasks.codeCoverageClassesJar {
exclude("org/junit/platform/commons/util/ModuleUtils.class")
exclude("org/junit/platform/commons/util/PackageNameUtils.class")
}

eclipse {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <h2>DISCLAIMER</h2>
*
* <p>These utilities are intended solely for usage within the JUnit framework
* itself. <strong>Any usage by external parties is not supported.</strong>
* 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <h2>DISCLAIMER</h2>
*
* <p>These utilities are intended solely for usage within the JUnit framework
* itself. <strong>Any usage by external parties is not supported.</strong>
* Use at your own risk!
*
* @since 1.11.3
*/
class PackageNameUtils {

static String getPackageName(Class<?> clazz) {
return clazz.getPackageName();
}

}

0 comments on commit 464de7e

Please sign in to comment.