Package io.microsphere.reflect
Class MemberUtils
- java.lang.Object
-
- io.microsphere.reflect.MemberUtils
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.function.Predicate<? super java.lang.reflect.Member>
FINAL_MEMBER_PREDICATE
ThePredicate
reference toisFinal(Member)
static java.util.function.Predicate<? super java.lang.reflect.Member>
NON_PRIVATE_MEMBER_PREDICATE
ThePredicate
reference toisNonPrivate(Member)
static java.util.function.Predicate<? super java.lang.reflect.Member>
NON_STATIC_MEMBER_PREDICATE
ThePredicate
reference toisNonStatic(Member)
static java.util.function.Predicate<? super java.lang.reflect.Member>
PUBLIC_MEMBER_PREDICATE
ThePredicate
reference toisPublic(Member)
static java.util.function.Predicate<? super java.lang.reflect.Member>
STATIC_MEMBER_PREDICATE
ThePredicate
reference toisStatic(Member)
-
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 ofMember
.static boolean
isAbstract(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared asabstract
.static boolean
isFinal(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared asfinal
.static boolean
isNonPrivate(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared as non-private.static boolean
isNonStatic(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared as non-static.static boolean
isPrivate(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared asprivate
.static boolean
isPublic(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared aspublic
.static boolean
isStatic(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared asstatic
.
-
-
-
Field Detail
-
STATIC_MEMBER_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Member> STATIC_MEMBER_PREDICATE
ThePredicate
reference toisStatic(Member)
-
NON_STATIC_MEMBER_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Member> NON_STATIC_MEMBER_PREDICATE
ThePredicate
reference toisNonStatic(Member)
-
FINAL_MEMBER_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Member> FINAL_MEMBER_PREDICATE
ThePredicate
reference toisFinal(Member)
-
PUBLIC_MEMBER_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Member> PUBLIC_MEMBER_PREDICATE
ThePredicate
reference toisPublic(Member)
-
NON_PRIVATE_MEMBER_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Member> NON_PRIVATE_MEMBER_PREDICATE
ThePredicate
reference toisNonPrivate(Member)
-
-
Method Detail
-
isStatic
public static boolean isStatic(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared asstatic
.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
- theMember
instance to check, such as aConstructor
,Method
, orField
- Returns:
true
if the member is static;false
otherwise
-
isAbstract
public static boolean isAbstract(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared asabstract
.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
- theMember
instance to check, such as aMethod
orClass
- Returns:
true
if the member is abstract;false
otherwise
-
isNonStatic
public static boolean isNonStatic(java.lang.reflect.Member member)
Checks whether the specifiedMember
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
- theMember
instance to check, such as aConstructor
,Method
, orField
- Returns:
true
if the member is non-static;false
otherwise
-
isFinal
public static boolean isFinal(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared asfinal
.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
- theMember
instance to check, such as aConstructor
,Method
, orField
- Returns:
true
if the member is final;false
otherwise
-
isPrivate
public static boolean isPrivate(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared asprivate
.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
- theMember
instance to check, such as aConstructor
,Method
, orField
- Returns:
true
if the member is private;false
otherwise
-
isPublic
public static boolean isPublic(java.lang.reflect.Member member)
Checks whether the specifiedMember
is declared aspublic
.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
- theMember
instance to check, such as aConstructor
,Method
, orField
- Returns:
true
if the member is public;false
otherwise
-
isNonPrivate
public static boolean isNonPrivate(java.lang.reflect.Member member)
Checks whether the specifiedMember
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
- theMember
instance to check, such as aConstructor
,Method
, orField
- 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 ofMember
.This method checks whether the given object is an instance of the
Member
interface. If it is, the object is casted and returned as aMember
. Otherwise, this method returnsnull
.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 aMember
, may benull
- Returns:
- the casted
Member
instance if the object is a validMember
; otherwise, returnsnull
-
-