8

I am using a Rest-Webservice in Android. The Web Service could handle JSON and XML, the API is described as an XSD. So I used JAXB to generate classes from the XSD and then I used jackson JSON processor to generate JSON from my instances.

The problem is, that JAXB (xjc) generates classes with JAXB annotations and Android can't handle those. I tried to add the jaxb-api.jar to my android project but the Dalvik won't use core classes.

For my first implementation I manually removed the annotations. But now the XSD was updated and I don't want to do this every time this happens.

Do you have any idea how to handle this problem in a better way? Is there a possibility to use jaxb/xjc without annotations or is there another code generater that could do this? Do you know an easy way to remove annotations from java classes (even if they are printed in multiple lines)? Is there a project setting for Android Eclipse projects, that makes the dalvik to allow core libs?

thx, cathixx

2 Answers 2

5

I had to do some extra researching to make cathixx's answer work since I'm new to Ant, so I'll share this to help others.

These instructions will take Java files with code like:

import javax.xml.bind.annotation.XmlElement;

@XmlRootElement
public class Response {...

...and comment these occurrences out, so it looks like:

/*import javax.xml.bind.annotation.XmlElement;*/

/*@XmlRootElement*/
public class Response {...

First, create a file build.xml (or whatever you want to call it - must be .xml) in a new Eclipse project (a "General" project is fine).

Then, add the following text to the build.xml file:

<?xml version="1.0"?>
<project
    name="CommentOutXmlAnnotations"
    basedir="."
    default="commentOutXmlAnnotations" >

<!-- This Ant script comments out the following lines from the Java files in this directory:
    import javax.xml.bind.annotation.*;
    @Xml*
 -->
    <target
        name="commentOutXmlAnnotations"        
        description="Run" >
            <replaceregexp
                byline="false"
                flags="g" >
                <regexp pattern="(@Xml[A-Za-z0-9]+(\([^)]+\))?|import javax\.xml\.bind\.annotation\.[A-Za-z0-9.]+;)[ \t]*(\r?\n)" />
                <substitution expression="/*\1*/\3" />
                <fileset dir="." >
                    <include name="*.java" />
                </fileset>
            </replaceregexp>        
    </target> 
</project>

Put the *.java files you want to comment out the XML imports and annotations for in the same directory as the build.xml file.

Right-click on the build.xml file in Eclipse, and click "Run As->Ant Build".

You should see output like:

Buildfile: D:\Eclipse_Projects\StripAnnotations\build.xml
commentOutXmlAnnotations:
BUILD SUCCESSFUL
Total time: 403 milliseconds

...and the XML imports and annotations should be commented out of your files.

Done!

Note: if you want to include all *.java files in all subdirectories of the build.xml file (for example, to comment out all XML annotations/imports generated for a bunch of JAXB classes in multiple packages), change the fileset tag to:

<fileset dir="." >
    <include name="**/*.java" />
</fileset>
5
  • So first of all, awesome work! I'm running into a few issues though. It doesn't filter out javax.xml.bind.JAXBElement. Some patterns of XmlElementRefs are commented out, namely when there are child objects for some reason. Some XmlEnumValues aren't commented out, but other's are. @XmlElements isn't either.
    – spy
    Apr 15, 2014 at 1:45
  • Thanks! For javax.xml.bind.JAXBElement, try removing the ".annotation\" part from the regular expression - I don't believe its searching that low (javax.xml.bind.*) in the package structure. For the others, not sure why sometimes it would work for certain occurences and other times it wouldn't. Apr 15, 2014 at 14:30
  • @XmlRootElement doesn't seem to be picked up
    – spy
    Oct 11, 2014 at 16:24
  • @XmlElement() isn't picked up either
    – spy
    Oct 11, 2014 at 16:28
  • @spy looks like your annotations are formatted a bit differently, so you'll need to modify the regular expression to match. Haven't looked at this in a while, but I believe the one shown here will only match import statements and class/method annotations. Oct 11, 2014 at 19:10
2

now, I solved it by myself by commenting all annotations with the following ant script:

<replaceregexp flags="g" byline="false">
  <regexp pattern="(@Xml[A-Za-z0-9]+(\([^)]+\))?|import javax\.xml\.bind\.annotation\.[A-Za-z0-9.]+;)[ \t]*(\r?\n)"/>
  <substitution expression="/*\1*/\3"/>
  <fileset dir="path/to/files">
    <include name="*.java"/>
  </fileset>
</replaceregexp>
1
  • This would be a huge help to me if it works. However, I'm new to ant. Could you give a little more context for how to use this script for someone new to ant? For example, I have a set of .java files in a directory that need to be converted, and I have ant installed. What next? Oct 26, 2012 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.