Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do source-to-source java refactoring in gradle?

I got some automatically generated Java code. I am willing to refactor automatically before compiling. It is mostly class rename and package modification.

Are there any gradle or ant tasks available for this?

like image 405
Teocali Avatar asked Jan 23 '14 08:01

Teocali


3 Answers

I got some automatically generated java code I would like to automaticaly refactor before compiling it. It is mostly class rename and package modification.

[off topic comment: You should fix the code that generate the code. Generating code automatically and then modify it using another tool does not look like correct approach.]

Eclipse provides refactoring API that can be used in programs(without eclipse). Original tool was JDT(I had used it), I guess the new solution is LTK - not tried.

like image 170
Jayan Avatar answered Sep 19 '22 14:09

Jayan


If you're trying to control the package and class names generated by xjc you can provide an external binding file.

You would say something like:

<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <jxb:bindings schemaLocation="your.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="the.package.name.you.want" />
    </jxb:schemaBindings>
  </jxb:bindings>
</jxb:bindings>

And for each class name you want to customize you would have:

<jxb:bindings node="//xs:complexType[@name='UglyTypeName']">
  <jxb:class name="MyNiceClass"/>
</jxb:bindings>

Save this up into a bindings.xjb file, for example, and run xjc like so:

xjc your.xsd -b bindings.xjb
like image 38
Edward Samson Avatar answered Sep 20 '22 14:09

Edward Samson


as you said you use "xjc" to generate the code and you want "mostly class rename and package modification" maybe some of the "xjc" options would do what you want:

-p <pkg>           :  specifies the target package
-b <file/dir>      :  specify external bindings files (each <file> must have its own -b)
                      If a directory is given, **/*.xjb is searched

With "-p" you can define the target package to which the generated code should belong. For using the bindings option have a look here for further information http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html

edit Another approach could be: to move the generated files in the directory you want and then using the replace plugin to replace the package specification in the copied source files

<plugin>
   <artifactId>maven-resources-plugin</artifactId>
   <executions>
      <execution>
         <id>copy-resources</id>
         <phase>process-resources</phase>
         <goals>
            <goal>copy-resources</goal>
         </goals>
         <configuration>
            <outputDirectory>the.directory.for.the.classes.to.move.to</outputDirectory>
            <resources>
               <resource>
                  <directory>the.directory.of.ajc.classes</directory>
                  ... do here the appropriate settings ...
               </resource>
            </resources>
         </configuration>
      </execution>
   </executions>
</plugin>

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.2</version>
    <executions>
        <execution>
            <phase>process-sources</phase>
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includes>
            <include>**/src/main/gen/**/**.java</include>
        </includes>
        <token>^package the.one.you.want.to.replace</token>
        <value>package you.want.the.classes.to.be</value>
        <regexFlags>
            <regexFlag>MULTILINE</regexFlag>
        </regexFlags>
    </configuration>
</plugin>
like image 30
SubOptimal Avatar answered Sep 22 '22 14:09

SubOptimal