Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JAXB (xjc) generated classes in android

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

like image 376
cathixx Avatar asked Sep 06 '12 14:09

cathixx


1 Answers

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>
like image 71
Sean Barbeau Avatar answered Oct 21 '22 14:10

Sean Barbeau