Skip to content

Instantly share code, notes, and snippets.

@ricston-git
Created January 4, 2013 10:54
Show Gist options
  • Save ricston-git/4451637 to your computer and use it in GitHub Desktop.
Save ricston-git/4451637 to your computer and use it in GitHub Desktop.
xjc-plugin
package com.ricston;
import java.io.IOException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JFieldVar;
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.BadCommandLineException;
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;
public class XJCPlugin extends Plugin {
public final static String ID = "id";
public final static JType LONG_TYPE = new JCodeModel().LONG;
public final static String ID_GETTER = "getId";
public final static JType VOID_TYPE = new JCodeModel().VOID;
public final static String ID_SETTER = "setId";
@Override
public String getOptionName() {
return "Xexample-plugin";
}
@Override
public int parseArgument(Options opt, String[] args, int i)
throws BadCommandLineException, IOException {
return 1;
}
@Override
public String getUsage() {
return " -Xexample-plugin : xjc example plugin";
}
@Override
public boolean run(Outline model, Options opt, ErrorHandler errorHandler)
throws SAXException {
for (ClassOutline classOutline : model.getClasses()) {
JFieldVar globalId = classOutline.implClass.field(JMod.PRIVATE,
LONG_TYPE, ID);
JMethod idGetterMethod = classOutline.implClass.method(JMod.PUBLIC,
LONG_TYPE, ID_GETTER);
JBlock idGetterBlock = idGetterMethod.body();
idGetterBlock._return(globalId);
JMethod idSetterMethod = classOutline.implClass.method(JMod.PUBLIC,
VOID_TYPE, ID_SETTER);
JVar localId = idSetterMethod.param(LONG_TYPE, "_" + ID);
JBlock idSetterBlock = idSetterMethod.body();
idSetterBlock.assign(globalId, localId);
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment