0

I'm studying the annotations and I'm trying to create a custom annotation like Lombook annotations (@Getter and @Setter), that is, an annotation that works as a method.

I have my annotation:

package com.example.demo.annotations;

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

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Dragonrider {
    String name();
}

I have a class that use this annotation:

package com.example.demo.model;

import com.example.demo.annotations.Dragonrider;

@Dragonrider(name = "Daenerys")
public class Dragon {
    String name;
    boolean hasFire;
    double weight;

    public Dragon(String name, boolean hasFire, double weight){
        this.name = name;
        this.hasFire = hasFire;
        this.weight = weight;
    }

    public void dracarys() {
        System.out.println("DRACARYS!");
    }

}

I don’t know how to go from here. I have tried to create an Aspect to intercept the annotation like this:

package com.example.demo.annotations;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class DragonriderAspect {
    @After("@annotation(com.example.demo.annotations.Dragonrider)")
    public void fly(){
        System.out.println("Is flying!");
    }
}

But it does not works.

I don't know how the custom annotation works, I guess creating an Aspect isn't what I need.

Victor
  • 253
  • 2
  • 3
  • 12
  • 2
    Annotations on their own **never** do anything, they are purely metadata. There's basically two ways to make annotations do stuff: 1. The Spring way where at runtime it carefully constructs objects/proxies/... for you taking the annotations into account, but in the end creating "normal" objects. 2. the Lombok way where they literally patch the compiler to allow annotations to have effect that they are not meant to have (which I call "cheating" ;-)). What you did with the aspect could work, but only if the relevant objects are created by spring itself (i.e. doing `new Dragon()` won't work). – Joachim Sauer Sep 30 '22 at 11:15
  • 1
    Please read https://www.baeldung.com/java-annotation-processing-builder – Simon Martinelli Sep 30 '22 at 11:15
  • @JoachimSauer this is not cheating. It's called annotation processing checkout the link above – Simon Martinelli Sep 30 '22 at 11:16
  • 1
    @SimonMartinelli: Lombok isn't implemented using annotation processing (or more precisely: not **just** using annotation processing). If you use annotation processing you can't modify the annotated types themselves, only generate additional ones. Lombok hooks into the compiler to do what it does, which is hacky (as noted in the link you provided above as well!). – Joachim Sauer Sep 30 '22 at 11:17
  • I agree with JoachimSauer Lombok wants to improve the language patching in boiler plate code. The result seems an "improved" java, more terse code. However going with such hacking development might hamper things like using the `record` class. For this question: use annotation processing to generate shadow generated-classes. The original classes need not be deployed. This assumes a build pipeline like maven or gradle. – Joop Eggen Sep 30 '22 at 11:39

0 Answers0