org.jvnet.jaxb2_commons.IfInsertPluginImpl Maven / Gradle / Ivy
// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov Date: 27.04.2010 22:24:03
// Home Page: http://members.fortunecity.com/neshkov/dj.html http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: IfInsertPluginImpl.java
package org.jvnet.jaxb2_commons;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JType;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.model.CPluginCustomization;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.tools.xjc.outline.Outline;
import com.sun.tools.xjc.util.DOMUtils;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.bind.DatatypeConverter;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class IfInsertPluginImpl extends Plugin
{
public IfInsertPluginImpl()
{
myCodeModel = new JCodeModel();
}
public String getOptionName()
{
return "Xifins";
}
public String getUsage()
{
return " -Xifins : add specified interfaces to generated class";
}
public List getCustomizationURIs()
{
return Collections.singletonList("http://jaxb.dev.java.net/plugin/if_insertion");
}
public boolean isCustomizationTagName(String nsUri, String localName)
{
return nsUri.equals("http://jaxb.dev.java.net/plugin/if_insertion") && localName.equals("interfaces");
}
private boolean checkMethods(JCodeModel xjcModel, CPluginCustomization c, Class interFace, JDefinedClass implClass, ErrorHandler errorHandler)
throws SAXException
{
boolean res = true;
Method amethod[];
int j = (amethod = interFace.getDeclaredMethods()).length;
for (int i = 0; i < j; i++)
{
Method m = amethod[i];
String mName = m.getName();
Class pClasses[] = m.getParameterTypes();
JType pTypes[] = new JType[pClasses.length];
int iType = 0;
Class aclass[];
int l = (aclass = pClasses).length;
for (int k = 0; k < l; k++)
{
Class pClass = aclass[k];
if (pClass.isPrimitive())
{
if (Boolean.TYPE == pClass)
pTypes[iType] = xjcModel.BOOLEAN;
else if (Byte.TYPE == pClass)
pTypes[iType] = xjcModel.BYTE;
else if (Character.TYPE == pClass)
pTypes[iType] = xjcModel.CHAR;
else if (Double.TYPE == pClass)
pTypes[iType] = xjcModel.DOUBLE;
else if (Float.TYPE == pClass)
pTypes[iType] = xjcModel.FLOAT;
else if (Integer.TYPE == pClass)
pTypes[iType] = xjcModel.INT;
else if (Long.TYPE == pClass)
pTypes[iType] = xjcModel.LONG;
else if (Short.TYPE == pClass)
pTypes[iType] = xjcModel.SHORT;
else
throw new IllegalStateException((new StringBuilder("unknown primitive type ")).append(pClass).toString());
} else
{
pTypes[iType] = xjcModel._ref(pClass);
}
iType++;
}
com.sun.codemodel.JMethod method = implClass.getMethod(mName, pTypes);
if (method == null)
{
String msg = (new StringBuilder("no implementation for method: ")).append(m.toGenericString()).toString();
SAXParseException spe = new SAXParseException(msg, c.locator);
errorHandler.error(spe);
res = false;
}
}
return res;
}
private boolean processInterfaces(JCodeModel xjcModel, CPluginCustomization c, ClassOutline co, ErrorHandler errorHandler)
throws SAXException
{
boolean res = true;
String elemText = DOMUtils.getElementText(c.element);
String classNames[] = elemText.trim().split("\\s+");
try
{
boolean check = true;
String checkValue = c.element.getAttribute("check");
if (checkValue != null && checkValue.length() > 0)
check = DatatypeConverter.parseBoolean(checkValue);
String as[];
int j = (as = classNames).length;
for (int i = 0; i < j; i++)
{
String className = as[i];
String msg = null;
if (check)
try
{
Class clazz = Class.forName(className, false, myLoader);
if (!clazz.isInterface())
msg = (new StringBuilder("not an interface: ")).append(className).toString();
else if (!checkMethods(xjcModel, c, clazz, co.implClass, errorHandler))
msg = (new StringBuilder("class does not implement interface ")).append(className).toString();
}
catch (ClassNotFoundException e)
{
msg = (new StringBuilder("no such interface: ")).append(className).toString();
}
if (msg != null)
{
SAXParseException spe = new SAXParseException(msg, c.locator);
errorHandler.error(spe);
res = false;
} else
{
com.sun.codemodel.JClass iface = myCodeModel.directClass(className);
co.implClass._implements(iface);
}
}
}
catch (IllegalArgumentException iae)
{
String msg = "invalid value of attribute check";
SAXParseException spe = new SAXParseException(msg, c.locator);
errorHandler.error(spe);
res = false;
}
return res;
}
public boolean run(Outline model, Options arg1, ErrorHandler errorHandler)
throws SAXException
{
myLoader = model.getClass().getClassLoader();
JCodeModel xjcModel = model.getCodeModel();
boolean res = true;
for (Iterator iterator = model.getClasses().iterator(); iterator.hasNext();)
{
ClassOutline co = (ClassOutline) iterator.next();
CPluginCustomization c = co.target.getCustomizations().find("http://jaxb.dev.java.net/plugin/if_insertion", "interfaces");
if (c != null)
{
c.markAsAcknowledged();
if (!processInterfaces(xjcModel, c, co, errorHandler))
res = false;
}
}
return res;
}
private static final String NS = "http://jaxb.dev.java.net/plugin/if_insertion";
private static final String ADD_IMPL = "interfaces";
private static final String OPTION = "Xifins";
private static final String CHECK = "check";
private JCodeModel myCodeModel;
private ClassLoader myLoader;
}