Skip to content

Commit

Permalink
Ryan's feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauernfeind committed Feb 3, 2023
1 parent 5c3e39d commit a437b47
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,9 @@ private Exception maybeRewriteStackTrace(String scriptName, String currentScript
return e;
}

private static String classForNameString(String className) throws ClassNotFoundException {
private static Class<?> loadClass(String className) throws ClassNotFoundException {
try {
Class.forName(className);
return className;
return Class.forName(className, false, GroovyDeephavenSession.class.getClassLoader());
} catch (ClassNotFoundException e) {
if (className.contains(".")) {
// handle inner class cases
Expand All @@ -285,7 +284,7 @@ private static String classForNameString(String className) throws ClassNotFoundE
String tail = className.substring(index + 1);
String newClassName = head + "$" + tail;

return classForNameString(newClassName);
return loadClass(newClassName);
} else {
throw e;
}
Expand All @@ -294,7 +293,7 @@ private static String classForNameString(String className) throws ClassNotFoundE

private static boolean classExists(String className) {
try {
classForNameString(className);
loadClass(className);
return true;
} catch (ClassNotFoundException e) {
return false;
Expand All @@ -303,7 +302,7 @@ private static boolean classExists(String className) {

private static boolean functionExists(String className, String functionName) {
try {
Method[] ms = Class.forName(classForNameString(className)).getMethods();
Method[] ms = loadClass(className).getMethods();

for (Method m : ms) {
if (m.getName().equals(functionName)) {
Expand All @@ -319,7 +318,7 @@ private static boolean functionExists(String className, String functionName) {

private static boolean fieldExists(String className, String fieldName) {
try {
Field[] fs = Class.forName(classForNameString(className)).getFields();
Field[] fs = loadClass(className).getFields();

for (Field f : fs) {
if (f.getName().equals(fieldName)) {
Expand Down Expand Up @@ -396,7 +395,8 @@ public static String isValidImportString(Logger log, String importString) {
}
} else {
if (isWildcard) {
okToImport = classExists(body) || (Package.getPackage(body) != null) || visibleToClassGraph(body);
okToImport = classExists(body) || (Package.getPackage(body) != null)
|| packageIsVisibleToClassGraph(body);

if (!okToImport) {
if (ALLOW_UNKNOWN_GROOVY_PACKAGE_IMPORTS) {
Expand Down Expand Up @@ -437,7 +437,7 @@ public static String isValidImportString(Logger log, String importString) {
}
}

private static boolean visibleToClassGraph(String packageImport) {
private static boolean packageIsVisibleToClassGraph(String packageImport) {
try (ScanResult scanResult = new ClassGraph().enableClassInfo().acceptPackages(packageImport).scan()) {
final Optional<ClassInfo> firstClassFound = scanResult.getAllClasses().stream().findFirst();
// force load the class so that the jvm is aware of the package
Expand Down

0 comments on commit a437b47

Please sign in to comment.