use of com.sun.codemodel.JInvocation 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.JInvocation 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.JInvocation in project jsonschema2pojo by joelittlejohn.
the class EnumRule method addFactoryMethod.
private void addFactoryMethod(JDefinedClass _enum, JType backingType) {
JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType);
JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
JVar valueParam = fromValue.param(backingType, "value");
JBlock body = fromValue.body();
JVar constant = body.decl(_enum, "constant");
constant.init(quickLookupMap.invoke("get").arg(valueParam));
JConditional _if = body._if(constant.eq(JExpr._null()));
JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class));
JExpression expr = valueParam;
// if string no need to add ""
if (!isString(backingType)) {
expr = expr.plus(JExpr.lit(""));
}
illegalArgumentException.arg(expr);
_if._then()._throw(illegalArgumentException);
_if._else()._return(constant);
ruleFactory.getAnnotator().enumCreatorMethod(fromValue);
}
use of com.sun.codemodel.JInvocation in project jsonschema2pojo by joelittlejohn.
the class AdditionalPropertiesRule method addSetter.
private void addSetter(JDefinedClass jclass, JType propertyType, JFieldVar field) {
JMethod setter = jclass.method(JMod.PUBLIC, void.class, "setAdditionalProperty");
ruleFactory.getAnnotator().anySetter(setter);
JVar nameParam = setter.param(String.class, "name");
JVar valueParam = setter.param(propertyType, "value");
JInvocation mapInvocation = setter.body().invoke(JExpr._this().ref(field), "put");
mapInvocation.arg(nameParam);
mapInvocation.arg(valueParam);
}
use of com.sun.codemodel.JInvocation in project jsonschema2pojo by joelittlejohn.
the class DefaultRule method getDefaultValue.
private JExpression getDefaultValue(JType fieldType, JsonNode node) {
fieldType = fieldType.unboxify();
if (fieldType.fullName().equals(String.class.getName())) {
return JExpr.lit(node.asText());
} else if (fieldType.fullName().equals(int.class.getName())) {
return JExpr.lit(Integer.parseInt(node.asText()));
} else if (fieldType.fullName().equals(BigInteger.class.getName())) {
return JExpr._new(fieldType).arg(JExpr.lit(node.asText()));
} else if (fieldType.fullName().equals(double.class.getName())) {
return JExpr.lit(Double.parseDouble(node.asText()));
} else if (fieldType.fullName().equals(BigDecimal.class.getName())) {
return JExpr._new(fieldType).arg(JExpr.lit(node.asText()));
} else if (fieldType.fullName().equals(boolean.class.getName())) {
return JExpr.lit(Boolean.parseBoolean(node.asText()));
} else if (fieldType.fullName().equals(getDateTimeType().getName())) {
long millisecs = parseDateToMillisecs(node.asText());
JInvocation newDateTime = JExpr._new(fieldType);
newDateTime.arg(JExpr.lit(millisecs));
return newDateTime;
} else if (fieldType.fullName().equals(LocalDate.class.getName()) || fieldType.fullName().equals(LocalTime.class.getName())) {
JInvocation stringParseableTypeInstance = JExpr._new(fieldType);
stringParseableTypeInstance.arg(JExpr.lit(node.asText()));
return stringParseableTypeInstance;
} else if (fieldType.fullName().equals(long.class.getName())) {
return JExpr.lit(Long.parseLong(node.asText()));
} else if (fieldType.fullName().equals(float.class.getName())) {
return JExpr.lit(Float.parseFloat(node.asText()));
} else if (fieldType.fullName().equals(URI.class.getName())) {
JInvocation invokeCreate = fieldType.owner().ref(URI.class).staticInvoke("create");
return invokeCreate.arg(JExpr.lit(node.asText()));
} else if (fieldType instanceof JDefinedClass && ((JDefinedClass) fieldType).getClassType().equals(ClassType.ENUM)) {
return getDefaultEnum(fieldType, node);
} else {
return JExpr._null();
}
}
Aggregations