Skip to content

Commit

Permalink
Add a isClassAvailable method to the ReflectionUtils (#4810)
Browse files Browse the repository at this point in the history
* Add a `isClassAvailable` method to the ReflectionUtils

* Add a `isClassAvailable` method to the ReflectionUtils

* Add a `isClassAvailable` method to the ReflectionUtils
  • Loading branch information
jevanlingen authored Dec 24, 2024
1 parent e59e48b commit 804dea0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>, Method[]> declaredMethodsCache = new ConcurrentHashMap<>(256);
private static final Map<Class<?>, 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;
Expand All @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

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.ReflectionUtilsTest2");
assertFalse(result);
}

@Test
void classNotAvailableWhenFQNOmitted() {
boolean result = ReflectionUtils.isClassAvailable("ReflectionUtilsTest");
assertFalse(result);
}
}

0 comments on commit 804dea0

Please sign in to comment.