mkarneim / pojobuilder

A Java Code Generator for Pojo Builders
Other
334 stars 44 forks source link

generated classes adding the @generatedPojo annotaion automatically using gradle build #147

Closed 19031987 closed 5 years ago

19031987 commented 6 years ago

Dear,

I am trying to generating classes using JAXB and need builder pattern on those classes, now I am manually adding the annotation on top of the generated class and creating builder classes,

Could you please suggest me how to avoid manual work and get things automated.

and another issue is that , automatic generated class has "ABCD" properties in the class when I run the generator then I have duplicate methods like withABCD(), and withaBCD()

mkarneim commented 6 years ago

I have no solution for avoiding this manual work. Somehow you need to tell PB for what classes it should generate the builders.

Concerning you second question it would be helpful if you open another ticket for it and provide a complete code example.

mkarneim commented 6 years ago

@mkuss gave me a hint about how to automatically add the @GeneratePojoBuilder to existing source files with Gradle.

Essentially you add a new task to your gradle file that patches the generated jaxb sources by calling replaceAll on the file contents.

Here is a code snippet with the relevant part:

task addGeneratePojoBuilderAnnotations() << {

  file("src/gen/java").eachFileRecurse(groovy.io.FileType.FILES) {
    if (it.name.endsWith('.java')) {
      println "Annotating $it"
      String sourceCode = it.getText('UTF-8')

      def lastIndent = -1
      def classnames = []
      sourceCode = sourceCode.replaceAll(/(?m:^(\s*)(public\s*(?:static )?class\s+)(\w+))/) { String all, String indent, String modifier, String classname ->
        int classLevel = indent.length()/4
        classnames[classLevel] = classname
        classnames.subList(classLevel, classnames.size()-1).clear()
        String builderName =  classnames.join('') + 'JaxbBuilder'
        return indent + '@net.karneim.pojobuilder.GeneratePojoBuilder(withBuilderInterface=de.sanacorp.common.builder.Builder.class, withBuilderProperties=true, withName="' + builderName + '", withCopyMethod=true)\n' + all
      }
      it.setText(sourceCode, 'UTF-8')
    }
  }
}
19031987 commented 6 years ago

Thanks I will check it

drekbour commented 5 years ago

Solution is clever in an evil kind of way but it presumably breaks Gradle's UP-TO-DATE mechanism by post-editing the generated file. I think perhaps the requestor wanted this... https://github.com/highsource/jaxb2-annotate-plugin

<xsd:appinfo>
  <annox:annotate target="class">@net.karneim.pojobuilder.GeneratePojoBuilder(intoPackage="my.package")</annox:annotate>
</xsd:appinfo>

... which allows you officially declare PB annotation be added to all classes.

This issue should be closed with an update to the documentation (I did not test the above!) into the cookbook.

drekbour commented 5 years ago

Docs updated, please close. https://github.com/mkarneim/pojobuilder/wiki/Combining-PojoBuilder-and-JAXB https://github.com/drekbour/pojobuilder-jaxb-example