我想创建一个注释,它搜索以注释中给出的单词开头的方法名称并执行此方法。
我是注释新手,我知道有一些内置注释,例如:
@override, @suppressWarnigs, @documented, @Retention, @deprecated, @target
有没有更多的注释?
我想创建一个注释,它搜索以注释中给出的单词开头的方法名称并执行此方法。
我是注释新手,我知道有一些内置注释,例如:
@override, @suppressWarnigs, @documented, @Retention, @deprecated, @target
有没有更多的注释?
I am sure there are good guides there but here is a fast one, forgive me for any typos :).
You can create your own annotation easily:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExecuteMethod {
String methodToExecute();
}
You can annotate your method with it.
@ExecuteMethod(methodToExecute = "MethodToExecute")
...
The code linked to the annotation looks like this:
public class MethodExecutor{
private Method method;
public MethodExecutor(Method method){
this.method = method;
}
public boolean executeMethod(){
if(method.isAnnotationPresent(ExecuteMethod.class)){
ExecuteMethod executeMethodAnnot=method.getAnnotation(ExecuteMethod.class);
String methodName = executeMethodAnnot.methodToExecute();
.... your code that calls the method here
}
}
You also need a piece of code to check and execute this annotation at the point you want it done:
for(Method m : classToCheck.getMethods()) {
if(m.isAnnotationPresent(ExecuteMethod.class)) {
MethodExecturor methorExectuor = new MethodExecutor(m);
methodExecutor.executeMethod(m)
}
}