use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addHashCode.
private void addHashCode(JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JMethod hashCode = jclass.method(JMod.PUBLIC, int.class, "hashCode");
Class<?> hashCodeBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.HashCodeBuilder.class : org.apache.commons.lang.builder.HashCodeBuilder.class;
JBlock body = hashCode.body();
JClass hashCodeBuilderClass = jclass.owner().ref(hashCodeBuilder);
JInvocation hashCodeBuilderInvocation = JExpr._new(hashCodeBuilderClass);
if (!jclass._extends().fullName().equals(Object.class.getName())) {
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("hashCode"));
}
for (JFieldVar fieldVar : fields.values()) {
if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("append").arg(fieldVar);
}
body._return(hashCodeBuilderInvocation.invoke("toHashCode"));
hashCode.annotate(Override.class);
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addEquals.
private void addEquals(JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JMethod equals = jclass.method(JMod.PUBLIC, boolean.class, "equals");
JVar otherObject = equals.param(Object.class, "other");
Class<?> equalsBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.EqualsBuilder.class : org.apache.commons.lang.builder.EqualsBuilder.class;
JBlock body = equals.body();
body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE);
body._if(otherObject._instanceof(jclass).eq(JExpr.FALSE))._then()._return(JExpr.FALSE);
JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject));
JClass equalsBuilderClass = jclass.owner().ref(equalsBuilder);
JInvocation equalsBuilderInvocation = JExpr._new(equalsBuilderClass);
if (!jclass._extends().fullName().equals(Object.class.getName())) {
equalsBuilderInvocation = equalsBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("equals").arg(otherObject));
}
for (JFieldVar fieldVar : fields.values()) {
if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
equalsBuilderInvocation = equalsBuilderInvocation.invoke("append").arg(fieldVar).arg(rhsVar.ref(fieldVar.name()));
}
JInvocation reflectionEquals = jclass.owner().ref(equalsBuilder).staticInvoke("reflectionEquals");
reflectionEquals.arg(JExpr._this());
reflectionEquals.arg(otherObject);
body._return(equalsBuilderInvocation.invoke("isEquals"));
equals.annotate(Override.class);
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class EnumRule method addValueField.
private JFieldVar addValueField(JDefinedClass _enum, JType type) {
JFieldVar valueField = _enum.field(JMod.PRIVATE | JMod.FINAL, type, VALUE_FIELD_NAME);
JMethod constructor = _enum.constructor(JMod.PRIVATE);
JVar valueParam = constructor.param(type, VALUE_FIELD_NAME);
JBlock body = constructor.body();
body.assign(JExpr._this().ref(valueField), valueParam);
return valueField;
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class EnumRule method addQuickLookupMap.
private JFieldVar addQuickLookupMap(JDefinedClass _enum, JType backingType) {
JClass lookupType = _enum.owner().ref(Map.class).narrow(backingType.boxify(), _enum);
JFieldVar lookupMap = _enum.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, lookupType, "CONSTANTS");
JClass lookupImplType = _enum.owner().ref(HashMap.class).narrow(backingType.boxify(), _enum);
lookupMap.init(JExpr._new(lookupImplType));
JForEach forEach = _enum.init().forEach(_enum, "c", JExpr.invoke("values"));
JInvocation put = forEach.body().invoke(lookupMap, "put");
put.arg(forEach.var().ref("value"));
put.arg(forEach.var());
return lookupMap;
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class EnumRule method apply.
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* A Java {@link Enum} is created, with constants for each of the enum
* values present in the schema. The enum name is derived from the nodeName,
* and the enum type itself is created as an inner class of the owning type.
* In the rare case that no owning type exists (the enum is the root of the
* schema), then the enum becomes a public class in its own right.
* <p>
* The actual JSON value for each enum constant is held in a property called
* "value" in the generated type. A static factory method
* <code>fromValue(String)</code> is added to the generated enum, and the
* methods are annotated to allow Jackson to marshal/unmarshal values
* correctly.
*
* @param nodeName
* the name of the property which is an "enum"
* @param node
* the enum node
* @param container
* the class container (class or package) to which this enum
* should be added
* @return the newly generated Java type that was created to represent the
* given enum
*/
@Override
public JType apply(String nodeName, JsonNode node, JClassContainer container, Schema schema) {
JDefinedClass _enum;
try {
_enum = createEnum(node, nodeName, container);
} catch (ClassAlreadyExistsException e) {
return e.getExistingClass();
}
schema.setJavaTypeIfEmpty(_enum);
if (node.has("javaInterfaces")) {
addInterfaces(_enum, node.get("javaInterfaces"));
}
// copy our node; remove the javaType as it will throw off the TypeRule for our case
ObjectNode typeNode = (ObjectNode) node.deepCopy();
typeNode.remove("javaType");
// If type is specified on the enum, get a type rule for it. Otherwise, we're a string.
// (This is different from the default of Object, which is why we don't do this for every case.)
JType backingType = node.has("type") ? ruleFactory.getTypeRule().apply(nodeName, typeNode, container, schema) : container.owner().ref(String.class);
JFieldVar valueField = addValueField(_enum, backingType);
// override toString only if we have a sensible string to return
if (isString(backingType)) {
addToString(_enum, valueField);
}
addValueMethod(_enum, valueField);
addEnumConstants(node.path("enum"), _enum, node.path("javaEnumNames"), backingType);
addFactoryMethod(_enum, backingType);
return _enum;
}
Aggregations