mardi 28 novembre 2023

Adding my static variable in java annotataed class

How i can to add my static variable to class, like in Log4j2?

For example:

import lombok.extern.log4j.Log4j2;

@Log4j2
public class TestClass {
    public void test() {
        log.info("test");
    }
}

and generated source class looks like:

public class TestClass {
    @Generated
    private static final Logger log = LogManager.getLogger(TestClass.class);
    
    public void test() {
        log.info("test");
    }
}

I created the class that extends javax.annotation.processing.AbstractProcessor and try to modify the original class by adding some static string, but I don't know how to do it right :/

@SupportedAnnotationTypes("TestAnnotation")
@SupportedSourceVersion(SourceVersion.RELEASE_17)
public class TestProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (Element element : roundEnv.getElementsAnnotatedWith(TestAnnotation.class)) {
            if (element.getKind().isClass()) {
                // ???
            }
        }
        return true;
    }
}

I want something like:

@TestAnnotation
public class TestClass {
    public void test() {
        System.out.println(testString); // testString is inserted by TestProcessor 
    }
}




Aucun commentaire:

Enregistrer un commentaire