I' am trying to replace this code:
public void doThing(){
if(User.getInstance().isLoggin()){
.....
}
}
by this
@withUser
public void doThing(){
....
}
I saw about the interceptors and annotation, but I cannot do it.
Is this possible?
It is absolutely possible. Project Lombok provides the exact feature what you are looking for. You just need to create your own annotation by extending EclipseAnnotationHandler or JavacAnnotationHandler depending upon your requirement.
A sample annotation handler (WithUser) is given below for reference. If used on method, the entire method will become enclosed by
if(false){...}block. Replacefalsewith your own expression on Handler (HandleWithUser.java).After annotation processing
Setup Project Lombok
Open the Lombok project on Eclipse and create the following files.
WithUser.java
HandleWithUser.java
Generate lombok.jar
Test
Make sure you are including lombok.jar in classpath
For more info, visit https://projectlombok.org/contributing/contributing