Skip to content

io microsphere reflect MemberUtils

github-actions[bot] edited this page Apr 11, 2026 · 5 revisions

MemberUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.reflect | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/reflect/MemberUtils.java

Overview

Java Reflection Member Utilities class

Declaration

public abstract class MemberUtils implements Utils

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.8-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

isStatic

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

isAbstract

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

isNonStatic

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

isFinal

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

isPrivate

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

isPublic

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

isNonPrivate

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

asMember

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

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.reflect.MemberUtils;

API Reference

Public Methods

Method Description
isStatic The Predicate reference to #isStatic(Member)
isAbstract Checks whether the specified Member is declared as abstract.
isNonStatic Checks whether the specified Member is declared as non-static.
isFinal Checks whether the specified Member is declared as final.
isPrivate Checks whether the specified Member is declared as private.
isPublic Checks whether the specified Member is declared as public.
isNonPrivate check the specified Member member is non-private or not ?
asMember Attempts to cast the provided object to an instance of Member.

Method Details

isStatic

public static boolean isStatic(Member member)

The Predicate reference to #isStatic(Member) / public final static Predicate STATIC_MEMBER_PREDICATE = MemberUtils::isStatic;

/** The Predicate reference to #isNonStatic(Member) / public final static Predicate NON_STATIC_MEMBER_PREDICATE = MemberUtils::isNonStatic;

/** The Predicate reference to #isFinal(Member) / public final static Predicate FINAL_MEMBER_PREDICATE = MemberUtils::isFinal;

/** The Predicate reference to #isPublic(Member) / public final static Predicate PUBLIC_MEMBER_PREDICATE = MemberUtils::isPublic;

/** The Predicate reference to #isNonPrivate(Member) / public final static Predicate NON_PRIVATE_MEMBER_PREDICATE = MemberUtils::isNonPrivate;

/** 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
}

isAbstract

public static boolean isAbstract(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 abstractClass = AbstractExample.class;
boolean result3 = MemberUtils.isAbstract(abstractClass); // true

Class concreteClass = ConcreteExample.class;
boolean result4 = MemberUtils.isAbstract(concreteClass); // false
}

isNonStatic

public static boolean isNonStatic(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
}

isFinal

public static boolean isFinal(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
}

isPrivate

public static boolean isPrivate(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
}

isPublic

public static boolean isPublic(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
}

isNonPrivate

public static boolean isNonPrivate(Member member)

check the specified Member member is non-private or not ?

asMember

public static Member asMember(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
`

This documentation was auto-generated from the source code of microsphere-java.

Home

annotation-processor

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally