Add and use ClassUtils.isPublic(Class).
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index fcfc03e..86286b0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -93,6 +93,7 @@
<action issue="LANG-1659" type="add" dev="ggregory" due-to="Arturo Bernal, Gary Gregory">Add null-safe ObjectUtils.isArray() #754.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ComparableUtils.max(A, A) and ComparableUtils.min(A, A).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add UncheckedReflectiveOperationException.</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add and use ClassUtils.isPublic(Class).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.4.2.2 #735, #808, #822.</action>
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump Bump actions/cache from v2.1.4 to v2.1.6 #742, #752, #764.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ClassUtils.java b/src/main/java/org/apache/commons/lang3/ClassUtils.java
index cd4fd69..d28fb79 100644
--- a/src/main/java/org/apache/commons/lang3/ClassUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ClassUtils.java
@@ -824,7 +824,7 @@ public static String getPackageName(String className) {
public static Method getPublicMethod(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) throws NoSuchMethodException {
final Method declaredMethod = cls.getMethod(methodName, parameterTypes);
- if (Modifier.isPublic(declaredMethod.getDeclaringClass().getModifiers())) {
+ if (isPublic(declaredMethod.getDeclaringClass())) {
return declaredMethod;
}
@@ -832,7 +832,7 @@ public static Method getPublicMethod(final Class<?> cls, final String methodName
candidateClasses.addAll(getAllSuperclasses(cls));
for (final Class<?> candidateClass : candidateClasses) {
- if (!Modifier.isPublic(candidateClass.getModifiers())) {
+ if (!isPublic(candidateClass)) {
continue;
}
final Method candidateMethod;
@@ -1494,6 +1494,15 @@ public static boolean isInnerClass(final Class<?> cls) {
}
/**
+ * Tests whether a {@link Class} is public.
+ * @param cls Class to test.
+ * @return {@code true} if {@code cls} is public.
+ * @since 3.13.0
+ */
+ public static boolean isPublic(final Class<?> cls) {
+ return Modifier.isPublic(cls.getModifiers());
+ }
+ /**
* Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte},
* {@link Character}, {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
*
diff --git a/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java b/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java
index 7fa51ba..b860bc6 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java
@@ -18,7 +18,6 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Modifier;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
@@ -286,7 +285,7 @@ public static <T> Constructor<T> getMatchingAccessibleConstructor(final Class<T>
private static boolean isAccessible(final Class<?> type) {
Class<?> cls = type;
while (cls != null) {
- if (!Modifier.isPublic(cls.getModifiers())) {
+ if (!ClassUtils.isPublic(cls)) {
return false;
}
cls = cls.getEnclosingClass();
diff --git a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
index c305c84..90d1d6a 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -22,7 +22,6 @@
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
@@ -556,7 +555,7 @@ public static Method getAccessibleMethod(Method method) {
}
// If the declaring class is public, we are done
final Class<?> cls = method.getDeclaringClass();
- if (Modifier.isPublic(cls.getModifiers())) {
+ if (ClassUtils.isPublic(cls)) {
return method;
}
final String methodName = method.getName();
@@ -588,7 +587,7 @@ private static Method getAccessibleMethodFromSuperclass(final Class<?> cls,
final String methodName, final Class<?>... parameterTypes) {
Class<?> parentClass = cls.getSuperclass();
while (parentClass != null) {
- if (Modifier.isPublic(parentClass.getModifiers())) {
+ if (ClassUtils.isPublic(parentClass)) {
try {
return parentClass.getMethod(methodName, parameterTypes);
} catch (final NoSuchMethodException e) {
@@ -624,7 +623,7 @@ private static Method getAccessibleMethodFromInterfaceNest(Class<?> cls,
final Class<?>[] interfaces = cls.getInterfaces();
for (final Class<?> anInterface : interfaces) {
// Is this interface public?
- if (!Modifier.isPublic(anInterface.getModifiers())) {
+ if (!ClassUtils.isPublic(anInterface)) {
continue;
}
// Does the method exist on this interface?