javacode-generationscaffoldingjavapoet

How can I write Annotation member with using JavaPoet?


I want to generate an annotation with its members. The structure of annotation is

public className (@Value("${topics.className.command}") String className){ } Because of $ sign I take error. Pls help me how can I write this structure? The places I wrote as className are variable. This will take from users.

`ParameterSpec parameterOfConst = ParameterSpec.builder(String.class, className )
                .addAnnotation(
                        AnnotationSpec.builder(value)
                                .addMember("{$topics." +"", "$S", className.toLowerCase() + ".command}")
                                .build())`

Solution

  • ParameterSpec parameterOfConst = ParameterSpec.builder(String.class, className)
            .addAnnotation(
                    AnnotationSpec.builder(value)
                            .addMember("value", "\"$${topics." + className.toLowerCase() + ".command}\"")
                            .build())
            .build();
    

    That is the answer of my question