Class MemberUtils

  • All Implemented Interfaces:
    Utils

    public abstract class MemberUtils
    extends java.lang.Object
    implements Utils
    Java Reflection Member Utilities class
    Since:
    1.0.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Member asMember​(java.lang.Object object)
      Attempts to cast the provided object to an instance of Member.
      static boolean isAbstract​(java.lang.reflect.Member member)
      Checks whether the specified Member is declared as abstract.
      static boolean isFinal​(java.lang.reflect.Member member)
      Checks whether the specified Member is declared as final.
      static boolean isNonPrivate​(java.lang.reflect.Member member)
      Checks whether the specified Member is declared as non-private.
      static boolean isNonStatic​(java.lang.reflect.Member member)
      Checks whether the specified Member is declared as non-static.
      static boolean isPrivate​(java.lang.reflect.Member member)
      Checks whether the specified Member is declared as private.
      static boolean isPublic​(java.lang.reflect.Member member)
      Checks whether the specified Member is declared as public.
      static boolean isStatic​(java.lang.reflect.Member member)
      Checks whether the specified Member is declared as static.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • STATIC_MEMBER_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Member> STATIC_MEMBER_PREDICATE
        The Predicate reference to isStatic(Member)
      • NON_STATIC_MEMBER_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Member> NON_STATIC_MEMBER_PREDICATE
        The Predicate reference to isNonStatic(Member)
      • FINAL_MEMBER_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Member> FINAL_MEMBER_PREDICATE
        The Predicate reference to isFinal(Member)
      • PUBLIC_MEMBER_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Member> PUBLIC_MEMBER_PREDICATE
        The Predicate reference to isPublic(Member)
      • NON_PRIVATE_MEMBER_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Member> NON_PRIVATE_MEMBER_PREDICATE
        The Predicate reference to isNonPrivate(Member)
    • Method Detail

      • isStatic

        public static boolean isStatic​(java.lang.reflect.Member member)
        Checks whether the specified Member is declared as static.

        Example Usage

        
         public class Example {
             static int staticField;
             int instanceField;
        
             static void staticMethod() {}
             void instanceMethod() {}
         }
        
         Field staticField = Example.class.getField("staticField");
         boolean result1 = MemberUtils.isStatic(staticField); // true
        
         Field instanceField = Example.class.getField("instanceField");
         boolean result2 = MemberUtils.isStatic(instanceField); // false
        
         Method staticMethod = Example.class.getMethod("staticMethod");
         boolean result3 = MemberUtils.isStatic(staticMethod); // true
        
         Method instanceMethod = Example.class.getMethod("instanceMethod");
         boolean result4 = MemberUtils.isStatic(instanceMethod); // false
         
        Parameters:
        member - the Member instance to check, such as a Constructor, Method, or Field
        Returns:
        true if the member is static; false otherwise
      • isAbstract

        public static boolean isAbstract​(java.lang.reflect.Member member)
        Checks whether the specified Member is declared as abstract.

        Example Usage

        
         public abstract class AbstractExample {
             abstract void abstractMethod();
         }
        
         public class ConcreteExample extends AbstractExample {
             void abstractMethod() {}
         }
        
         Method abstractMethod = AbstractExample.class.getMethod("abstractMethod");
         boolean result1 = MemberUtils.isAbstract(abstractMethod); // true
        
         Method concreteMethod = ConcreteExample.class.getMethod("abstractMethod");
         boolean result2 = MemberUtils.isAbstract(concreteMethod); // false
        
         Class<AbstractExample> abstractClass = AbstractExample.class;
         boolean result3 = MemberUtils.isAbstract(abstractClass); // true
        
         Class<ConcreteExample> concreteClass = ConcreteExample.class;
         boolean result4 = MemberUtils.isAbstract(concreteClass); // false
         
        Parameters:
        member - the Member instance to check, such as a Method or Class
        Returns:
        true if the member is abstract; false otherwise
      • isNonStatic

        public static boolean isNonStatic​(java.lang.reflect.Member member)
        Checks whether the specified Member is declared as non-static.

        Example Usage

        
         public class Example {
             static int staticField;
             int instanceField;
        
             static void staticMethod() {}
             void instanceMethod() {}
         }
        
         Field staticField = Example.class.getField("staticField");
         boolean result1 = MemberUtils.isNonStatic(staticField); // false
        
         Field instanceField = Example.class.getField("instanceField");
         boolean result2 = MemberUtils.isNonStatic(instanceField); // true
        
         Method staticMethod = Example.class.getMethod("staticMethod");
         boolean result3 = MemberUtils.isNonStatic(staticMethod); // false
        
         Method instanceMethod = Example.class.getMethod("instanceMethod");
         boolean result4 = MemberUtils.isNonStatic(instanceMethod); // true
         
        Parameters:
        member - the Member instance to check, such as a Constructor, Method, or Field
        Returns:
        true if the member is non-static; false otherwise
      • isFinal

        public static boolean isFinal​(java.lang.reflect.Member member)
        Checks whether the specified Member is declared as final.

        Example Usage

        
         public class Example {
             final int finalField = 0;
             int nonFinalField;
        
             final void finalMethod() {}
             void nonFinalMethod() {}
         }
        
         Field finalField = Example.class.getField("finalField");
         boolean result1 = MemberUtils.isFinal(finalField); // true
        
         Field nonFinalField = Example.class.getField("nonFinalField");
         boolean result2 = MemberUtils.isFinal(nonFinalField); // false
        
         Method finalMethod = Example.class.getMethod("finalMethod");
         boolean result3 = MemberUtils.isFinal(finalMethod); // true
        
         Method nonFinalMethod = Example.class.getMethod("nonFinalMethod");
         boolean result4 = MemberUtils.isFinal(nonFinalMethod); // false
         
        Parameters:
        member - the Member instance to check, such as a Constructor, Method, or Field
        Returns:
        true if the member is final; false otherwise
      • isPrivate

        public static boolean isPrivate​(java.lang.reflect.Member member)
        Checks whether the specified Member is declared as private.

        Example Usage

        
         public class Example {
             private int privateField;
             int defaultField;
        
             private void privateMethod() {}
             void defaultMethod() {}
         }
        
         Field privateField = Example.class.getDeclaredField("privateField");
         boolean result1 = MemberUtils.isPrivate(privateField); // true
        
         Field defaultField = Example.class.getField("defaultField");
         boolean result2 = MemberUtils.isPrivate(defaultField); // false
        
         Method privateMethod = Example.class.getDeclaredMethod("privateMethod");
         boolean result3 = MemberUtils.isPrivate(privateMethod); // true
        
         Method defaultMethod = Example.class.getMethod("defaultMethod");
         boolean result4 = MemberUtils.isPrivate(defaultMethod); // false
         
        Parameters:
        member - the Member instance to check, such as a Constructor, Method, or Field
        Returns:
        true if the member is private; false otherwise
      • isPublic

        public static boolean isPublic​(java.lang.reflect.Member member)
        Checks whether the specified Member is declared as public.

        Example Usage

        
         public class Example {
             public int publicField;
             private void privateMethod() {}
        
             public void publicMethod() {}
         }
        
         Field publicField = Example.class.getField("publicField");
         boolean result1 = MemberUtils.isPublic(publicField); // true
        
         Method privateMethod = Example.class.getDeclaredMethod("privateMethod");
         boolean result2 = MemberUtils.isPublic(privateMethod); // false
        
         Method publicMethod = Example.class.getMethod("publicMethod");
         boolean result3 = MemberUtils.isPublic(publicMethod); // true
         
        Parameters:
        member - the Member instance to check, such as a Constructor, Method, or Field
        Returns:
        true if the member is public; false otherwise
      • isNonPrivate

        public static boolean isNonPrivate​(java.lang.reflect.Member member)
        Checks whether the specified Member is declared as non-private.

        Example Usage

        
         public class Example {
             private int privateField;
             int defaultField;
             protected int protectedField;
             public int publicField;
        
             private void privateMethod() {}
             void defaultMethod() {}
             protected void protectedMethod() {}
             public void publicMethod() {}
         }
        
         Field privateField = Example.class.getDeclaredField("privateField");
         boolean result1 = MemberUtils.isNonPrivate(privateField); // false
        
         Field defaultField = Example.class.getField("defaultField");
         boolean result2 = MemberUtils.isNonPrivate(defaultField); // true
        
         Field protectedField = Example.class.getField("protectedField");
         boolean result3 = MemberUtils.isNonPrivate(protectedField); // true
        
         Field publicField = Example.class.getField("publicField");
         boolean result4 = MemberUtils.isNonPrivate(publicField); // true
        
         Method privateMethod = Example.class.getDeclaredMethod("privateMethod");
         boolean result5 = MemberUtils.isNonPrivate(privateMethod); // false
        
         Method defaultMethod = Example.class.getMethod("defaultMethod");
         boolean result6 = MemberUtils.isNonPrivate(defaultMethod); // true
        
         Method protectedMethod = Example.class.getMethod("protectedMethod");
         boolean result7 = MemberUtils.isNonPrivate(protectedMethod); // true
        
         Method publicMethod = Example.class.getMethod("publicMethod");
         boolean result8 = MemberUtils.isNonPrivate(publicMethod); // true
         
        Parameters:
        member - the Member instance to check, such as a Constructor, Method, or Field
        Returns:
        true if the member is non-private; false otherwise
      • asMember

        public static java.lang.reflect.Member asMember​(java.lang.Object object)
        Attempts to cast the provided object to an instance of Member.

        This method checks whether the given object is an instance of the Member interface. If it is, the object is casted and returned as a Member. Otherwise, this method returns null.

        Example Usage

        
         Field field = Example.class.getField("publicField");
         Member member1 = MemberUtils.asMember(field);
         System.out.println(member1 == field); // true
        
         Method method = Example.class.getMethod("publicMethod");
         Member member2 = MemberUtils.asMember(method);
         System.out.println(member2 == method); // true
        
         String notAMember = "This is not a Member";
         Member member3 = MemberUtils.asMember(notAMember);
         System.out.println(member3 == null); // true
         
        Parameters:
        object - the object to be casted to a Member, may be null
        Returns:
        the casted Member instance if the object is a valid Member; otherwise, returns null