public static enum MethodRef.Type extends Enum<MethodRef.Type>
| 枚举常量和说明 |
|---|
ALL
Annotated on a String attribute, its value will be parsed to class name and method name.
|
CLASS
Only used with
METHOD |
METHOD
Annotated on a String attribute, it has following modes:
Use with another attribute with type=
CLASS(also can set a parent class), reference method from
class by the other attribute's value
Use with MethodRef.defaultClass(), reference method from the determined class
Use with MethodRef.parentClass(), reference method from class by its EnclosingElement(usually a
class)'s annotation's value
Example: 1. |
public static final MethodRef.Type ALL
Example:
//define
@interface UseAll {
@MethodRef //default type is All
String value();
}
//usage
@UseAll("java.lang.String:length")
void func();
MethodRef.splitor()public static final MethodRef.Type CLASS
METHODMETHODpublic static final MethodRef.Type METHOD
CLASS(also can set a parent class), reference method from
class by the other attribute's valueMethodRef.defaultClass(), reference method from the determined classMethodRef.parentClass(), reference method from class by its EnclosingElement(usually a
class)'s annotation's value
Example:
1. Note that if you use Class and Method, there must have and only have 2 attribute with @MethodRef.
And the Class attribute can also set a parent class, if the class value is void.class, the parent
annotation value will be used.
//define
@interface UseClassAndMethod {
@MethodRef(type = Type.CLASS)
Class<? extends Number> type();
@MethodRef(type = Type.METHOD)
String method();
}
//usage
@UseClassAndMethod(type = Integer.class, method = "intValue")
void func();
//define
@interface UseClassAndMethodWithParent {
@interface Parent {
Class<?> value();
}
@MethodRef(type = Type.CLASS, parentClass = Parent.class)
Class<?> type() default void.class;
@MethodRef(type = Type.METHOD)
String method();
}
//usage
@Parent(Integer.class)
class Bar{
@UseClassAndMethodWithParent(method = "intValue")
void func();
}
2.
//define
@interface UseDefaultClass {
@MethodRef(type = Type.METHOD, defaultClass = String.class)
String method();
}
//usage
@UseDefaultClass(method = "length")
void func();
3. Note that the parent annotation must have a Class attribute named 'value'.
//define
@interface UseParentClass {
@interface Parent {
Class<?> value();
}
@MethodRef(type = Type.METHOD, parentClass = Parent.class)
String method();
}
//usage
@Parent(String.class)
class Bar{
@UseParentClass(method = "length")
void func();
}
public static MethodRef.Type[] values()
for (MethodRef.Type c : MethodRef.Type.values()) System.out.println(c);
public static MethodRef.Type valueOf(String name)
name - 要返回的枚举常量的名称。IllegalArgumentException - 如果该枚举类型没有带有指定名称的常量NullPointerException - 如果参数为空值Copyright © 2017. All rights reserved.