use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method searchClassAndSuperClassesForField.
private JFieldVar searchClassAndSuperClassesForField(String property, JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JFieldVar field = fields.get(property);
if (field == null) {
return searchSuperClassesForField(property, jclass);
}
return field;
}
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;
}
Aggregations