users@jaxb.java.net

getting simple xjc plugin to work

From: jason white <jason_white_01_at_hotmail.com>
Date: Thu, 09 Mar 2006 13:25:08 +0000

All, I need serious help with this - have been struggling for a day or so
trying to get a very simple first xjc plugin to work...I feel like I have
done everything I need to do and have followed all instructions (that I can
find) on the web. Am now getting the "unrecognized parameter
-Xhibernate-java" error and don't know where to go next.....

-jason

here is the code and ant script:

package src;

import org.xml.sax.ErrorHandler;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.tools.xjc.outline.Outline;

import java.util.*;


/**
* @author jwhite
*
*/
public class XjcHibernatePlugin extends Plugin {

        @Override
        public String getOptionName() {
                return "Xhibernate-java";
        }

        @Override
        public String getUsage() {
                return " -Xhibernate-java : enable hibernate codegen for
generated code";
        }

    @Override
        public boolean run(Outline outline, Options opt, ErrorHandler errorHandler)
{

            //for the code in the bean, for each class known to this object
        for (ClassOutline classOutline : outline.getClasses()) {

                //get the declaration
            JDefinedClass implClass = classOutline.implClass;

            //add the phrase "implements hibernatable" to the class
declaration
            implementHibernatable(implClass);
        }
        return true;
        }

    //make xjc add "implements hibernatable" to the classes
    private void implementHibernatable(JDefinedClass implClass)
    {
            implClass.direct("public void testMethod(){}");
    }
}


<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
    <classpath refid="classpath" />
  </taskdef>


          <target name="doXjc" description="Run xjc with given parameters from ant"
depends="jar">
                <mkdir dir="gen-src" />
                <xjc schema="decodes.xsd" destdir=".">
                    <produces dir="gen-src/com.hb.datahub.api.spoke.aa"
includes="**/*.java" />
                        <arg value="-Xhibernate-java" />
                </xjc>
        </target>