我正在尝试编写一个注释,以帮助我找出分配给模型类的memberVariable的String是否为空。如果是空的,则不要继续注册。
我编写了以下代码,但我很困惑,我将如何告诉Annotation,要检查什么值以及如果String为空,注释将如何通知我。
IsStringEmpty.java
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface IsStringEmpty {
String value();
}
StringEmptinessChecker.java
public class StringEmptinessChecker {
public boolean process(Object instance) {
Class<?> clazz = instance.getClass();
for (Method m : clazz.getDeclaredMethods()) {
if (m.isAnnotationPresent(IsStringEmpty.class)) {
IsStringEmpty annotation = m.getAnnotation(IsStringEmpty.class);
String val = annotation.value();
return val.isEmpty();
}
return false;
}
return false;
}
}
Main.java
public void foo() {
MyModel model = new MyModel();
@IsStringEmpty()??????????
model.setName(nameET.getText().toString());
// if nameEt.getText().toString() is empty. Dont make network call for registratoin
}