You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/11/15 19:28:44 UTC

[commons-lang] branch master updated: Add and use ClassUtils.isPublic(Class).

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 88500be  Add and use ClassUtils.isPublic(Class).
88500be is described below

commit 88500bece2c1f506874179ad7c3899104d628365
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Nov 15 14:28:41 2021 -0500

    Add and use ClassUtils.isPublic(Class).
---
 src/changes/changes.xml                                     |  1 +
 src/main/java/org/apache/commons/lang3/ClassUtils.java      | 13 +++++++++++--
 .../org/apache/commons/lang3/reflect/ConstructorUtils.java  |  3 +--
 .../java/org/apache/commons/lang3/reflect/MethodUtils.java  |  7 +++----
 4 files changed, 16 insertions(+), 8 deletions(-)

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 @@ The <action> type attribute can be add,update,fix,remove.
     <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 class ClassUtils {
     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 class ClassUtils {
         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 class ClassUtils {
     }
 
     /**
+     * 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 @@ package org.apache.commons.lang3.reflect;
 
 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 class ConstructorUtils {
     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.annotation.Annotation;
 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 class MethodUtils {
         }
         // 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 @@ public class MethodUtils {
             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 @@ public class MethodUtils {
             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?