com.sun.tools.xjc.addon.krasa.PrimitiveFixerPlugin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of krasa-jaxb-tools Show documentation
Show all versions of krasa-jaxb-tools Show documentation
JAXB plugins for generation of Bean Validation Annotations (JSR-303) and replacing primitive types
package com.sun.tools.xjc.addon.krasa;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
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 org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class PrimitiveFixerPlugin extends Plugin {
private static final String OPTION_NAME = "XReplacePrimitives";
@Override
public String getOptionName() {
return OPTION_NAME;
}
@Override
public String getUsage() {
return "-" + OPTION_NAME
+ " : Replaces primitive types of fields and methods by proper Class: int to java.lang.Integer, long to java.lang.Long, boolean to java.lang.Boolean \n";
}
@Override
public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) throws SAXException {
for (ClassOutline co : outline.getClasses()) {
HashMap hashMap = new HashMap();
hashMap.put("int", Integer.class);
hashMap.put("long", Long.class);
hashMap.put("boolean", Boolean.class);
Map fields = co.implClass.fields();
Map getterSetterMap = getMethodsMap(co);
for (Map.Entry stringJFieldVarEntry : fields.entrySet()) {
JFieldVar fieldVar = stringJFieldVarEntry.getValue();
JType type = fieldVar.type();
if (type.isPrimitive()) {
Class o = hashMap.get(type.name());
if (o != null) {
JCodeModel jCodeModel = new JCodeModel();
JClass newType = jCodeModel.ref(o);
fieldVar.type(newType);
String name = fieldVar.name().substring(0, 1).toUpperCase() + fieldVar.name().substring(1);
JMethod jMethod = getterSetterMap.get("get" + name);
setReturnType(newType, jMethod);
jMethod = getterSetterMap.get("set" + name);
setParameter(newType, jMethod);
jMethod = getterSetterMap.get("is" + name);
setReturnType(newType, jMethod);
}
}
}
}
return true;
}
private void setParameter(JClass newType, JMethod jMethod) {
if (jMethod != null) {
JVar jVar = jMethod.listParams()[0];
jVar.type(newType);
}
}
private void setReturnType(JType type, JMethod jMethod) {
if (jMethod != null) {
jMethod.type(type);
}
}
private Map getMethodsMap(ClassOutline co) {
Map methodsMap = new HashMap();
Collection methods = co.implClass.methods();
for (JMethod method : methods) {
String name = method.name();
if (method.type().isPrimitive() && (name.startsWith("is") || name.startsWith("get") || name.startsWith("set"))) {
methodsMap.put(name, method);
}
}
return methodsMap;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy