Tlaloc-ES
Tlaloc-ES

Reputation: 5282

is there any way of replace this code of this way?

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?

Upvotes: 1

Views: 376

Answers (1)

Siva
Siva

Reputation: 176

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. Replace false with your own expression on Handler (HandleWithUser.java).

@WithUser
public void doThing(){
    System.out.println("Hello World");
}

After annotation processing

public void doThing(){
    if (false){
        System.out.println("Hello World");
    }
}

Setup Project Lombok

git clone https://github.com/rzwitserloot/lombok.git
cd lombok
ant eclipse

Open the Lombok project on Eclipse and create the following files.

WithUser.java

package lombok;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.SOURCE)
public @interface WithUser {    
}

HandleWithUser.java

package lombok.javac.handlers;

import static lombok.javac.handlers.JavacHandlerUtil.setGeneratedBy;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.IfStatement;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCIf;
import com.sun.tools.javac.tree.JCTree.JCLiteral;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import org.eclipse.jdt.internal.compiler.ast.FalseLiteral;
import lombok.WithUser;
import lombok.core.AnnotationValues;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
import lombok.javac.JavacTreeMaker;

@ProviderFor(JavacAnnotationHandler.class)
public class HandleWithUser extends JavacAnnotationHandler<WithUser>{

    @Override
    public void handle(AnnotationValues<WithUser> annotationValues, JCAnnotation ast, JavacNode annotationNode) {
        JavacNode annotation = annotationNode.up();
        JCMethodDecl method = (JCMethodDecl)annotation.get();
        List<JCStatement> contents = (List<JCStatement>) method.body.stats;
        JCTree source = annotation.get();
        JavacTreeMaker maker = annotationNode.getTreeMaker();
        JCLiteral falseLiteral = maker.Literal(false);
        JCIf ifStatement = maker.If(falseLiteral, maker.Block(0, contents), null);
        Context context = annotationNode.getContext();
        JCStatement ifBlock = setGeneratedBy(ifStatement, source, context);
        method.body.stats = List.of(ifBlock);
        annotation.rebuild();
    }

}

Generate lombok.jar

ant dist

Test

Make sure you are including lombok.jar in classpath

javac -cp lombok.jar Test.java

For more info, visit https://projectlombok.org/contributing/contributing

Upvotes: 0

Related Questions