From 06bb463b1d49dedfea4fc837e6ab4936ae87c76d Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Tue, 6 Feb 2024 12:13:53 -0600 Subject: [PATCH] Ensure wildcard imports work against JRE classes Also updates our classgraph build, in case we were affected by Java 17 issues. Fixes #4994 --- engine/table/build.gradle | 2 +- .../engine/util/GroovyDeephavenSession.java | 3 ++- .../util/scripts/TestGroovyDeephavenSession.java | 14 ++++++++------ .../engine/util/scripts/wontbeused/WontBeUsed.java | 11 +++++++++++ 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 engine/table/src/test/java/io/deephaven/engine/util/scripts/wontbeused/WontBeUsed.java diff --git a/engine/table/build.gradle b/engine/table/build.gradle index cef93c96f6c..8809652e223 100644 --- a/engine/table/build.gradle +++ b/engine/table/build.gradle @@ -33,7 +33,7 @@ dependencies { // TODO(deephaven-core#3204): t-digest 3.3 appears to have higher errors than 3.2 implementation 'com.tdunning:t-digest:3.2' implementation 'com.squareup:javapoet:1.13.0' - implementation 'io.github.classgraph:classgraph:4.8.154' + implementation 'io.github.classgraph:classgraph:4.8.165' implementation project(':plugin') implementation depCommonsLang3 diff --git a/engine/table/src/main/java/io/deephaven/engine/util/GroovyDeephavenSession.java b/engine/table/src/main/java/io/deephaven/engine/util/GroovyDeephavenSession.java index 9cb12275e73..88c8a51f3a8 100644 --- a/engine/table/src/main/java/io/deephaven/engine/util/GroovyDeephavenSession.java +++ b/engine/table/src/main/java/io/deephaven/engine/util/GroovyDeephavenSession.java @@ -557,7 +557,8 @@ private Optional createClassImport(boolean isWildcard, String body } private static boolean packageIsVisibleToClassGraph(String packageImport) { - try (ScanResult scanResult = new ClassGraph().enableClassInfo().acceptPackages(packageImport).scan()) { + try (ScanResult scanResult = + new ClassGraph().enableClassInfo().enableSystemJarsAndModules().acceptPackages(packageImport).scan()) { final Optional firstClassFound = scanResult.getAllClasses().stream().findFirst(); // force load the class so that the jvm is aware of the package firstClassFound.ifPresent(ClassInfo::loadClass); diff --git a/engine/table/src/test/java/io/deephaven/engine/util/scripts/TestGroovyDeephavenSession.java b/engine/table/src/test/java/io/deephaven/engine/util/scripts/TestGroovyDeephavenSession.java index ddcffbfe219..ed265465494 100644 --- a/engine/table/src/test/java/io/deephaven/engine/util/scripts/TestGroovyDeephavenSession.java +++ b/engine/table/src/test/java/io/deephaven/engine/util/scripts/TestGroovyDeephavenSession.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Optional; +import java.util.Set; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -130,13 +131,14 @@ public void testScriptResultOrder() { @Test public void testUnloadedWildcardPackageImport() { - // it's unlikely we have loaded anything from this groovy package - final String packageString = "groovy.time"; - - if (this.getClass().getClassLoader().getDefinedPackage(packageString) != null) { - Assert.fail("Package '" + packageString + "' is already loaded, test with a more obscure package."); + // Pick three packages we won't have loaded directly - a JRE package, a third party package (from a jar), and a + // package in the current source set + for (String packageString : Set.of("java.util", "groovy.time", "io.deephaven.engine.util.scripts.wontbeused")) { + if (this.getClass().getClassLoader().getDefinedPackage(packageString) != null) { + Assert.fail("Package '" + packageString + "' is already loaded, test with a more obscure package."); + } + session.evaluateScript("import " + packageString + ".*").throwIfError(); } - session.evaluateScript("import " + packageString + ".*").throwIfError(); } /** diff --git a/engine/table/src/test/java/io/deephaven/engine/util/scripts/wontbeused/WontBeUsed.java b/engine/table/src/test/java/io/deephaven/engine/util/scripts/wontbeused/WontBeUsed.java new file mode 100644 index 00000000000..2a20ead7aaa --- /dev/null +++ b/engine/table/src/test/java/io/deephaven/engine/util/scripts/wontbeused/WontBeUsed.java @@ -0,0 +1,11 @@ +/** + * Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending + */ +package io.deephaven.engine.util.scripts.wontbeused; + +/** + * Deliberately unused class so that we have a package that can be tested by + * {@link io.deephaven.engine.util.scripts.TestGroovyDeephavenSession}. + */ +public class WontBeUsed { +}