问题:是否有办法对方法进行代码检查并检查它是否没有参数并在编译前警告我,或者甚至在我的IDE中给出警告.
假设我有一个注释 @Initialize
@Retention(RetentionPolicy.RUNTIME)
public @interface Initialize {
int priority();
}
Run Code Online (Sandbox Code Playgroud)
通过反射,我可以调用带注释的方法 @Initialize
public static void initMethods(Initializable clazz) {
TreeMap<Integer, Method> methods = prioritizedMethods(clazz.getClass().getDeclaredMethods());
methods.forEach((priority, method) -> {
try {
method.setAccessible(true);
Logger.debug("Invoking " + method.getName() + "...");
method.invoke(clazz);
} catch (IllegalAccessException | InvocationTargetException e) {
Logger.debug("Failed to invoke " + method.getName());
e.printStackTrace();
}
});
}
Run Code Online (Sandbox Code Playgroud)
prioritzedMethods(Method[] method) 是我检查注释的地方.
private static TreeMap<Integer, Method> prioritizedMethods(Method[] methods) {
HashMap<Integer, Method> taggedMethods = new HashMap<>();
for (Method method : methods) {
if (method.isAnnotationPresent(Initialize.class)) { …Run Code Online (Sandbox Code Playgroud)