Skip to content

Commit 3b9ee8d

Browse files
committed
Update MemberUtils.java
1 parent 0ebee59 commit 3b9ee8d

1 file changed

Lines changed: 224 additions & 19 deletions

File tree

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

Lines changed: 224 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,54 +58,194 @@ public abstract class MemberUtils implements Utils {
5858
public final static Predicate<? super Member> NON_PRIVATE_MEMBER_PREDICATE = MemberUtils::isNonPrivate;
5959

6060
/**
61-
* check the specified {@link Member member} is static or not ?
61+
* Checks whether the specified {@link Member} is declared as {@code static}.
6262
*
63-
* @param member {@link Member} instance, e.g, {@link Constructor}, {@link Method} or {@link Field}
64-
* @return Iff <code>member</code> is static one, return <code>true</code>, or <code>false</code>
63+
* <h3>Example Usage</h3>
64+
* <pre>{@code
65+
* public class Example {
66+
* static int staticField;
67+
* int instanceField;
68+
*
69+
* static void staticMethod() {}
70+
* void instanceMethod() {}
71+
* }
72+
*
73+
* Field staticField = Example.class.getField("staticField");
74+
* boolean result1 = MemberUtils.isStatic(staticField); // true
75+
*
76+
* Field instanceField = Example.class.getField("instanceField");
77+
* boolean result2 = MemberUtils.isStatic(instanceField); // false
78+
*
79+
* Method staticMethod = Example.class.getMethod("staticMethod");
80+
* boolean result3 = MemberUtils.isStatic(staticMethod); // true
81+
*
82+
* Method instanceMethod = Example.class.getMethod("instanceMethod");
83+
* boolean result4 = MemberUtils.isStatic(instanceMethod); // false
84+
* }</pre>
85+
*
86+
* @param member the {@link Member} instance to check, such as a {@link Constructor}, {@link Method}, or {@link Field}
87+
* @return <code>true</code> if the member is static; <code>false</code> otherwise
6588
*/
6689
public static boolean isStatic(Member member) {
6790
return member != null && Modifier.isStatic(member.getModifiers());
6891
}
6992

7093
/**
71-
* check the specified {@link Member member} is abstract or not ?
94+
* Checks whether the specified {@link Member} is declared as {@code abstract}.
7295
*
73-
* @param member {@link Member} instance, e.g, {@link Constructor}, {@link Method} or {@link Field}
74-
* @return Iff <code>member</code> is static one, return <code>true</code>, or <code>false</code>
96+
* <h3>Example Usage</h3>
97+
* <pre>{@code
98+
* public abstract class AbstractExample {
99+
* abstract void abstractMethod();
100+
* }
101+
*
102+
* public class ConcreteExample extends AbstractExample {
103+
* void abstractMethod() {}
104+
* }
105+
*
106+
* Method abstractMethod = AbstractExample.class.getMethod("abstractMethod");
107+
* boolean result1 = MemberUtils.isAbstract(abstractMethod); // true
108+
*
109+
* Method concreteMethod = ConcreteExample.class.getMethod("abstractMethod");
110+
* boolean result2 = MemberUtils.isAbstract(concreteMethod); // false
111+
*
112+
* Class<AbstractExample> abstractClass = AbstractExample.class;
113+
* boolean result3 = MemberUtils.isAbstract(abstractClass); // true
114+
*
115+
* Class<ConcreteExample> concreteClass = ConcreteExample.class;
116+
* boolean result4 = MemberUtils.isAbstract(concreteClass); // false
117+
* }</pre>
118+
*
119+
* @param member the {@link Member} instance to check, such as a {@link Method} or {@link Class}
120+
* @return <code>true</code> if the member is abstract; <code>false</code> otherwise
75121
*/
76122
public static boolean isAbstract(Member member) {
77123
return member != null && Modifier.isAbstract(member.getModifiers());
78124
}
79125

126+
/**
127+
* Checks whether the specified {@link Member} is declared as non-static.
128+
*
129+
* <h3>Example Usage</h3>
130+
* <pre>{@code
131+
* public class Example {
132+
* static int staticField;
133+
* int instanceField;
134+
*
135+
* static void staticMethod() {}
136+
* void instanceMethod() {}
137+
* }
138+
*
139+
* Field staticField = Example.class.getField("staticField");
140+
* boolean result1 = MemberUtils.isNonStatic(staticField); // false
141+
*
142+
* Field instanceField = Example.class.getField("instanceField");
143+
* boolean result2 = MemberUtils.isNonStatic(instanceField); // true
144+
*
145+
* Method staticMethod = Example.class.getMethod("staticMethod");
146+
* boolean result3 = MemberUtils.isNonStatic(staticMethod); // false
147+
*
148+
* Method instanceMethod = Example.class.getMethod("instanceMethod");
149+
* boolean result4 = MemberUtils.isNonStatic(instanceMethod); // true
150+
* }</pre>
151+
*
152+
* @param member the {@link Member} instance to check, such as a {@link Constructor}, {@link Method}, or {@link Field}
153+
* @return <code>true</code> if the member is non-static; <code>false</code> otherwise
154+
*/
80155
public static boolean isNonStatic(Member member) {
81156
return member != null && !Modifier.isStatic(member.getModifiers());
82157
}
83158

84159
/**
85-
* check the specified {@link Member member} is final or not ?
160+
* Checks whether the specified {@link Member} is declared as {@code final}.
86161
*
87-
* @param member {@link Member} instance, e.g, {@link Constructor}, {@link Method} or {@link Field}
88-
* @return Iff <code>member</code> is final one, return <code>true</code>, or <code>false</code>
162+
* <h3>Example Usage</h3>
163+
* <pre>{@code
164+
* public class Example {
165+
* final int finalField = 0;
166+
* int nonFinalField;
167+
*
168+
* final void finalMethod() {}
169+
* void nonFinalMethod() {}
170+
* }
171+
*
172+
* Field finalField = Example.class.getField("finalField");
173+
* boolean result1 = MemberUtils.isFinal(finalField); // true
174+
*
175+
* Field nonFinalField = Example.class.getField("nonFinalField");
176+
* boolean result2 = MemberUtils.isFinal(nonFinalField); // false
177+
*
178+
* Method finalMethod = Example.class.getMethod("finalMethod");
179+
* boolean result3 = MemberUtils.isFinal(finalMethod); // true
180+
*
181+
* Method nonFinalMethod = Example.class.getMethod("nonFinalMethod");
182+
* boolean result4 = MemberUtils.isFinal(nonFinalMethod); // false
183+
* }</pre>
184+
*
185+
* @param member the {@link Member} instance to check, such as a {@link Constructor}, {@link Method}, or {@link Field}
186+
* @return <code>true</code> if the member is final; <code>false</code> otherwise
89187
*/
90188
public static boolean isFinal(Member member) {
91189
return member != null && Modifier.isFinal(member.getModifiers());
92190
}
93191

94192
/**
95-
* check the specified {@link Member member} is private or not ?
193+
* Checks whether the specified {@link Member} is declared as {@code private}.
96194
*
97-
* @param member {@link Member} instance, e.g, {@link Constructor}, {@link Method} or {@link Field}
98-
* @return Iff <code>member</code> is private one, return <code>true</code>, or <code>false</code>
195+
* <h3>Example Usage</h3>
196+
* <pre>{@code
197+
* public class Example {
198+
* private int privateField;
199+
* int defaultField;
200+
*
201+
* private void privateMethod() {}
202+
* void defaultMethod() {}
203+
* }
204+
*
205+
* Field privateField = Example.class.getDeclaredField("privateField");
206+
* boolean result1 = MemberUtils.isPrivate(privateField); // true
207+
*
208+
* Field defaultField = Example.class.getField("defaultField");
209+
* boolean result2 = MemberUtils.isPrivate(defaultField); // false
210+
*
211+
* Method privateMethod = Example.class.getDeclaredMethod("privateMethod");
212+
* boolean result3 = MemberUtils.isPrivate(privateMethod); // true
213+
*
214+
* Method defaultMethod = Example.class.getMethod("defaultMethod");
215+
* boolean result4 = MemberUtils.isPrivate(defaultMethod); // false
216+
* }</pre>
217+
*
218+
* @param member the {@link Member} instance to check, such as a {@link Constructor}, {@link Method}, or {@link Field}
219+
* @return <code>true</code> if the member is private; <code>false</code> otherwise
99220
*/
100221
public static boolean isPrivate(Member member) {
101222
return member != null && Modifier.isPrivate(member.getModifiers());
102223
}
103224

104225
/**
105-
* check the specified {@link Member member} is public or not ?
226+
* Checks whether the specified {@link Member} is declared as {@code public}.
106227
*
107-
* @param member {@link Member} instance, e.g, {@link Constructor}, {@link Method} or {@link Field}
108-
* @return Iff <code>member</code> is public one, return <code>true</code>, or <code>false</code>
228+
* <h3>Example Usage</h3>
229+
* <pre>{@code
230+
* public class Example {
231+
* public int publicField;
232+
* private void privateMethod() {}
233+
*
234+
* public void publicMethod() {}
235+
* }
236+
*
237+
* Field publicField = Example.class.getField("publicField");
238+
* boolean result1 = MemberUtils.isPublic(publicField); // true
239+
*
240+
* Method privateMethod = Example.class.getDeclaredMethod("privateMethod");
241+
* boolean result2 = MemberUtils.isPublic(privateMethod); // false
242+
*
243+
* Method publicMethod = Example.class.getMethod("publicMethod");
244+
* boolean result3 = MemberUtils.isPublic(publicMethod); // true
245+
* }</pre>
246+
*
247+
* @param member the {@link Member} instance to check, such as a {@link Constructor}, {@link Method}, or {@link Field}
248+
* @return <code>true</code> if the member is public; <code>false</code> otherwise
109249
*/
110250
public static boolean isPublic(Member member) {
111251
return member != null && Modifier.isPublic(member.getModifiers());
@@ -117,20 +257,85 @@ public static boolean isPublic(Member member) {
117257
* @param member {@link Member} instance, e.g, {@link Constructor}, {@link Method} or {@link Field}
118258
* @return Iff <code>member</code> is non-private one, return <code>true</code>, or <code>false</code>
119259
*/
260+
/**
261+
* Checks whether the specified {@link Member} is declared as non-private.
262+
*
263+
* <h3>Example Usage</h3>
264+
* <pre>{@code
265+
* public class Example {
266+
* private int privateField;
267+
* int defaultField;
268+
* protected int protectedField;
269+
* public int publicField;
270+
*
271+
* private void privateMethod() {}
272+
* void defaultMethod() {}
273+
* protected void protectedMethod() {}
274+
* public void publicMethod() {}
275+
* }
276+
*
277+
* Field privateField = Example.class.getDeclaredField("privateField");
278+
* boolean result1 = MemberUtils.isNonPrivate(privateField); // false
279+
*
280+
* Field defaultField = Example.class.getField("defaultField");
281+
* boolean result2 = MemberUtils.isNonPrivate(defaultField); // true
282+
*
283+
* Field protectedField = Example.class.getField("protectedField");
284+
* boolean result3 = MemberUtils.isNonPrivate(protectedField); // true
285+
*
286+
* Field publicField = Example.class.getField("publicField");
287+
* boolean result4 = MemberUtils.isNonPrivate(publicField); // true
288+
*
289+
* Method privateMethod = Example.class.getDeclaredMethod("privateMethod");
290+
* boolean result5 = MemberUtils.isNonPrivate(privateMethod); // false
291+
*
292+
* Method defaultMethod = Example.class.getMethod("defaultMethod");
293+
* boolean result6 = MemberUtils.isNonPrivate(defaultMethod); // true
294+
*
295+
* Method protectedMethod = Example.class.getMethod("protectedMethod");
296+
* boolean result7 = MemberUtils.isNonPrivate(protectedMethod); // true
297+
*
298+
* Method publicMethod = Example.class.getMethod("publicMethod");
299+
* boolean result8 = MemberUtils.isNonPrivate(publicMethod); // true
300+
* }</pre>
301+
*
302+
* @param member the {@link Member} instance to check, such as a {@link Constructor}, {@link Method}, or {@link Field}
303+
* @return <code>true</code> if the member is non-private; <code>false</code> otherwise
304+
*/
120305
public static boolean isNonPrivate(Member member) {
121306
return member != null && !Modifier.isPrivate(member.getModifiers());
122307
}
123308

124309
/**
125-
* Try to cast to be an instance of {@link Member}
310+
* Attempts to cast the provided object to an instance of {@link Member}.
311+
*
312+
* <p>This method checks whether the given object is an instance of the {@link Member} interface.
313+
* If it is, the object is casted and returned as a {@link Member}. Otherwise, this method returns
314+
* {@code null}.</p>
315+
*
316+
* <h3>Example Usage</h3>
317+
* <pre>{@code
318+
* Field field = Example.class.getField("publicField");
319+
* Member member1 = MemberUtils.asMember(field);
320+
* System.out.println(member1 == field); // true
321+
*
322+
* Method method = Example.class.getMethod("publicMethod");
323+
* Member member2 = MemberUtils.asMember(method);
324+
* System.out.println(member2 == method); // true
325+
*
326+
* String notAMember = "This is not a Member";
327+
* Member member3 = MemberUtils.asMember(notAMember);
328+
* System.out.println(member3 == null); // true
329+
* }</pre>
126330
*
127-
* @param object the object to be casted
128-
* @return {@link Member} if <code>object</code> is {@link Member}
331+
* @param object the object to be casted to a {@link Member}, may be {@code null}
332+
* @return the casted {@link Member} instance if the object is a valid {@link Member}; otherwise,
333+
* returns {@code null}
129334
*/
130335
public static Member asMember(Object object) {
131336
return object instanceof Member ? (Member) object : null;
132337
}
133338

134-
private MemberUtils(){
339+
private MemberUtils() {
135340
}
136341
}

0 commit comments

Comments
 (0)