From d0f7bad656eaa281c9cdc7a8ee6ff91e2507d3f9 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 24 Dec 2024 09:20:45 +0100 Subject: [PATCH 1/3] Add a `isClassAvailable` method to the ReflectionUtils --- .../openrewrite/internal/ReflectionUtils.java | 17 +++++++++--- .../internal/ReflectionUtilsTest.java | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java diff --git a/rewrite-core/src/main/java/org/openrewrite/internal/ReflectionUtils.java b/rewrite-core/src/main/java/org/openrewrite/internal/ReflectionUtils.java index 8cda946a88d..b207a7232e9 100644 --- a/rewrite-core/src/main/java/org/openrewrite/internal/ReflectionUtils.java +++ b/rewrite-core/src/main/java/org/openrewrite/internal/ReflectionUtils.java @@ -36,7 +36,18 @@ public class ReflectionUtils { * Cache for {@link Class#getDeclaredMethods()} plus equivalent default methods * from Java 8 based interfaces, allowing for fast iteration. */ - private static final Map, Method[]> declaredMethodsCache = new ConcurrentHashMap<>(256); + private static final Map, Method[]> DECLARED_METHODS_CACHE = new ConcurrentHashMap<>(256); + + public static boolean isClassAvailable(String fullyQualifiedClassName) { + try { + ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader classLoader = contextClassLoader == null ? ReflectionUtils.class.getClassLoader() : contextClassLoader; + Class.forName(fullyQualifiedClassName, false, classLoader); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } public static @Nullable Method findMethod(Class clazz, String name, Class... paramTypes) { Class searchType = clazz; @@ -54,7 +65,7 @@ public class ReflectionUtils { } private static Method[] getDeclaredMethods(Class clazz) { - Method[] result = declaredMethodsCache.get(clazz); + Method[] result = DECLARED_METHODS_CACHE.get(clazz); if (result == null) { try { Method[] declaredMethods = clazz.getDeclaredMethods(); @@ -70,7 +81,7 @@ private static Method[] getDeclaredMethods(Class clazz) { } else { result = declaredMethods; } - declaredMethodsCache.put(clazz, (result.length == 0 ? EMPTY_METHOD_ARRAY : result)); + DECLARED_METHODS_CACHE.put(clazz, (result.length == 0 ? EMPTY_METHOD_ARRAY : result)); } catch (Throwable ex) { throw new IllegalStateException("Failed to introspect Class [" + clazz.getName() + "] from ClassLoader [" + clazz.getClassLoader() + "]", ex); diff --git a/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java b/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java new file mode 100644 index 00000000000..8ba8df2190c --- /dev/null +++ b/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java @@ -0,0 +1,26 @@ +package org.openrewrite.internal; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ReflectionUtilsTest { + + @Test + void classAvailable() { + boolean result = ReflectionUtils.isClassAvailable("org.openrewrite.internal.ReflectionUtilsTest"); + assertTrue(result); + } + + @Test + void classNotAvailable() { + boolean result = ReflectionUtils.isClassAvailable("org.openrewrite.internal.ReflectionUtilsTest"); + assertTrue(result); + } + + @Test + void classNotAvailableWhenFQNOmitted() { + boolean result = ReflectionUtils.isClassAvailable("ReflectionUtilsTest"); + assertFalse(result); + } +} From 1da52e417f34e8c995c26e2b4b9a844077cd7724 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 24 Dec 2024 09:27:46 +0100 Subject: [PATCH 2/3] Add a `isClassAvailable` method to the ReflectionUtils --- .../openrewrite/internal/ReflectionUtilsTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java b/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java index 8ba8df2190c..9f5c0122d89 100644 --- a/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.openrewrite.internal; import org.junit.jupiter.api.Test; From eb618359c64af63556350295bf92d5a81c764b80 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 24 Dec 2024 10:02:12 +0100 Subject: [PATCH 3/3] Add a `isClassAvailable` method to the ReflectionUtils --- .../java/org/openrewrite/internal/ReflectionUtilsTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java b/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java index 9f5c0122d89..83eb1763c75 100644 --- a/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/internal/ReflectionUtilsTest.java @@ -29,8 +29,8 @@ void classAvailable() { @Test void classNotAvailable() { - boolean result = ReflectionUtils.isClassAvailable("org.openrewrite.internal.ReflectionUtilsTest"); - assertTrue(result); + boolean result = ReflectionUtils.isClassAvailable("org.openrewrite.internal.ReflectionUtilsTest2"); + assertFalse(result); } @Test