-
Notifications
You must be signed in to change notification settings - Fork 53
io microsphere reflect 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
Java Reflection Member Utilities class
public abstract class MemberUtils implements Utils-
Introduced in:
1.0.0 -
Current Project Version:
0.2.8-SNAPSHOT
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 |
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); // falsepublic 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); // falsepublic 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); // truepublic 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); // falsepublic 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); // falsepublic 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); // truepublic 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); // trueField 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); // trueAdd 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 io.microsphere.reflect.MemberUtils;| 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. |
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.
`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
}
public static boolean isAbstract(Member member)Checks whether the specified Member is declared as abstract.
`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
}
public static boolean isNonStatic(Member member)Checks whether the specified Member is declared as non-static.
`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
}
public static boolean isFinal(Member member)Checks whether the specified Member is declared as final.
`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
}
public static boolean isPrivate(Member member)Checks whether the specified Member is declared as private.
`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
}
public static boolean isPublic(Member member)Checks whether the specified Member is declared as public.
`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
}
public static boolean isNonPrivate(Member member)check the specified Member member is non-private or not ?
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.
`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.
annotation-processor
- ConfigurationPropertyAnnotationProcessor
- ConfigurationPropertyJSONElementVisitor
- FilerProcessor
- ResourceProcessor
java-annotations
java-core
- ACLLoggerFactory
- AbstractArtifactResourceResolver
- AbstractConverter
- AbstractDeque
- AbstractEventDispatcher
- AbstractLogger
- AbstractURLClassPathHandle
- AccessibleObjectUtils
- AdditionalMetadataResourceConfigurationPropertyLoader
- AnnotationUtils
- ArchiveFileArtifactResourceResolver
- ArrayEnumeration
- ArrayStack
- ArrayUtils
- Artifact
- ArtifactDetector
- ArtifactResourceResolver
- Assert
- BannedArtifactClassLoadingExecutor
- BaseUtils
- BeanMetadata
- BeanProperty
- BeanUtils
- ByteArrayToObjectConverter
- CharSequenceComparator
- CharSequenceUtils
- CharsetUtils
- ClassDataRepository
- ClassDefinition
- ClassFileJarEntryFilter
- ClassFilter
- ClassLoaderUtils
- ClassPathResourceConfigurationPropertyLoader
- ClassPathUtils
- ClassUtils
- ClassicProcessIdResolver
- ClassicURLClassPathHandle
- CollectionUtils
- Compatible
- CompositeSubProtocolURLConnectionFactory
- CompositeURLStreamHandlerFactory
- ConditionalEventListener
- ConfigurationProperty
- ConfigurationPropertyGenerator
- ConfigurationPropertyLoader
- ConfigurationPropertyReader
- Configurer
- ConsoleURLConnection
- Constants
- ConstructorDefinition
- ConstructorUtils
- Converter
- Converters
- CustomizedThreadFactory
- DefaultConfigurationPropertyGenerator
- DefaultConfigurationPropertyReader
- DefaultDeserializer
- DefaultEntry
- DefaultSerializer
- DelegatingBlockingQueue
- DelegatingDeque
- DelegatingIterator
- DelegatingQueue
- DelegatingScheduledExecutorService
- DelegatingURLConnection
- DelegatingURLStreamHandlerFactory
- DelegatingWrapper
- Deprecation
- Deserializer
- Deserializers
- DirectEventDispatcher
- DirectoryFileFilter
- EmptyDeque
- EmptyIterable
- EmptyIterator
- EnumerationIteratorAdapter
- EnumerationUtils
- Event
- EventDispatcher
- EventListener
- ExceptionUtils
- ExecutableDefinition
- ExecutableUtils
- ExecutorUtils
- ExtendableProtocolURLStreamHandler
- FastByteArrayInputStream
- FastByteArrayOutputStream
- FieldDefinition
- FieldUtils
- FileChangedEvent
- FileChangedListener
- FileConstants
- FileExtensionFilter
- FileUtils
- FileWatchService
- Filter
- FilterOperator
- FilterUtils
- FormatUtils
- Functional
- GenericEvent
- GenericEventListener
- Handler
- Handler
- HierarchicalClassComparator
- IOFileFilter
- IOUtils
- ImmutableEntry
- IterableAdapter
- IterableUtils
- Iterators
- JDKLoggerFactory
- JSON
- JSONArray
- JSONException
- JSONObject
- JSONStringer
- JSONTokener
- JSONUtils
- JarEntryFilter
- JarUtils
- JavaType
- JmxUtils
- ListUtils
- Listenable
- Lists
- Logger
- LoggerFactory
- LoggingFileChangedListener
- MBeanAttribute
- MBeanAttributeInfoBuilder
- MBeanConstructorInfoBuilder
- MBeanDescribableBuilder
- MBeanExecutableInfoBuilder
- MBeanFeatureInfoBuilder
- MBeanInfoBuilder
- MBeanNotificationInfoBuilder
- MBeanOperationInfoBuilder
- MBeanParameterInfoBuilder
- ManagementUtils
- ManifestArtifactResourceResolver
- MapToPropertiesConverter
- MapUtils
- Maps
- MavenArtifact
- MavenArtifactResourceResolver
- MemberDefinition
- MemberUtils
- MetadataResourceConfigurationPropertyLoader
- MethodDefinition
- MethodHandleUtils
- MethodHandlesLookupUtils
- MethodUtils
- ModernProcessIdResolver
- ModernURLClassPathHandle
- Modifier
- MultiValueConverter
- MultipleType
- MutableInteger
- MutableURLStreamHandlerFactory
- NameFileFilter
- NoOpLogger
- NoOpLoggerFactory
- NoOpURLClassPathHandle
- NumberToByteConverter
- NumberToCharacterConverter
- NumberToDoubleConverter
- NumberToFloatConverter
- NumberToIntegerConverter
- NumberToLongConverter
- NumberToShortConverter
- NumberUtils
- ObjectToBooleanConverter
- ObjectToByteArrayConverter
- ObjectToByteConverter
- ObjectToCharacterConverter
- ObjectToDoubleConverter
- ObjectToFloatConverter
- ObjectToIntegerConverter
- ObjectToLongConverter
- ObjectToOptionalConverter
- ObjectToShortConverter
- ObjectToStringConverter
- PackageNameClassFilter
- PackageNameClassNameFilter
- ParallelEventDispatcher
- ParameterizedTypeImpl
- PathConstants
- Predicates
- Prioritized
- PriorityComparator
- ProcessExecutor
- ProcessIdResolver
- ProcessManager
- PropertiesToStringConverter
- PropertiesUtils
- PropertyConstants
- PropertyResourceBundleControl
- PropertyResourceBundleUtils
- ProtocolConstants
- ProxyUtils
- QueueUtils
- ReadOnlyIterator
- ReflectionUtils
- ReflectiveConfigurationPropertyGenerator
- ReflectiveDefinition
- ResourceConstants
- ReversedDeque
- Scanner
- SecurityUtils
- SeparatorConstants
- Serializer
- Serializers
- ServiceLoaderURLStreamHandlerFactory
- ServiceLoaderUtils
- ServiceLoadingURLClassPathHandle
- SetUtils
- Sets
- Sfl4jLoggerFactory
- ShutdownHookCallbacksThread
- ShutdownHookUtils
- SimpleClassScanner
- SimpleFileScanner
- SimpleJarEntryScanner
- SingletonDeque
- SingletonEnumeration
- SingletonIterator
- StackTraceUtils
- StandardFileWatchService
- StandardURLStreamHandlerFactory
- StopWatch
- StreamArtifactResourceResolver
- Streams
- StringBuilderWriter
- StringConverter
- StringDeserializer
- StringSerializer
- StringToArrayConverter
- StringToBlockingDequeConverter
- StringToBlockingQueueConverter
- StringToBooleanConverter
- StringToByteConverter
- StringToCharArrayConverter
- StringToCharacterConverter
- StringToClassConverter
- StringToCollectionConverter
- StringToDequeConverter
- StringToDoubleConverter
- StringToDurationConverter
- StringToFloatConverter
- StringToInputStreamConverter
- StringToIntegerConverter
- StringToIterableConverter
- StringToListConverter
- StringToLongConverter
- StringToMultiValueConverter
- StringToNavigableSetConverter
- StringToQueueConverter
- StringToSetConverter
- StringToShortConverter
- StringToSortedSetConverter
- StringToStringConverter
- StringToTransferQueueConverter
- StringUtils
- SubProtocolURLConnectionFactory
- SymbolConstants
- SystemUtils
- ThrowableAction
- ThrowableBiConsumer
- ThrowableBiFunction
- ThrowableConsumer
- ThrowableFunction
- ThrowableSupplier
- ThrowableUtils
- TrueClassFilter
- TrueFileFilter
- TypeArgument
- TypeFinder
- TypeUtils
- URLClassPathHandle
- URLUtils
- UnmodifiableDeque
- UnmodifiableIterator
- UnmodifiableQueue
- Utils
- ValueHolder
- Version
- VersionUtils
- VirtualMachineProcessIdResolver
- Wrapper
- WrapperProcessor
java-test
- AbstractAnnotationProcessingTest
- Ancestor
- AnnotationProcessingTestProcessor
- ArrayTypeModel
- CollectionTypeModel
- Color
- CompilerInvocationInterceptor
- ConfigurationPropertyModel
- DefaultTestService
- GenericTestService
- MapTypeModel
- Model
- Parent
- PrimitiveTypeModel
- SimpleTypeModel
- StringArrayList
- TestAnnotation
- TestService
- TestServiceImpl
jdk-tools
lang-model