use of com.sun.codemodel.JInvocation in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addToString.
private void addToString(JDefinedClass jclass) {
JMethod toString = jclass.method(JMod.PUBLIC, String.class, "toString");
Class<?> toStringBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.ToStringBuilder.class : org.apache.commons.lang.builder.ToStringBuilder.class;
JBlock body = toString.body();
JInvocation reflectionToString = jclass.owner().ref(toStringBuilder).staticInvoke("reflectionToString");
reflectionToString.arg(JExpr._this());
body._return(reflectionToString);
toString.annotate(Override.class);
}
use of com.sun.codemodel.JInvocation in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addConstructors.
private void addConstructors(JDefinedClass jclass, JsonNode node, Schema schema, boolean onlyRequired) {
LinkedHashSet<String> classProperties = getConstructorProperties(node, onlyRequired);
LinkedHashSet<String> combinedSuperProperties = getSuperTypeConstructorPropertiesRecursive(node, schema, onlyRequired);
// no properties to put in the constructor => default constructor is good enough.
if (classProperties.isEmpty() && combinedSuperProperties.isEmpty()) {
return;
}
// add a no-args constructor for serialization purposes
JMethod noargsConstructor = jclass.constructor(JMod.PUBLIC);
noargsConstructor.javadoc().add("No args constructor for use in serialization");
// add the public constructor with property parameters
JMethod fieldsConstructor = jclass.constructor(JMod.PUBLIC);
JBlock constructorBody = fieldsConstructor.body();
JInvocation superInvocation = constructorBody.invoke("super");
Map<String, JFieldVar> fields = jclass.fields();
Map<String, JVar> classFieldParams = new HashMap<String, JVar>();
for (String property : classProperties) {
JFieldVar field = fields.get(property);
if (field == null) {
throw new IllegalStateException("Property " + property + " hasn't been added to JDefinedClass before calling addConstructors");
}
fieldsConstructor.javadoc().addParam(property);
JVar param = fieldsConstructor.param(field.type(), field.name());
constructorBody.assign(JExpr._this().ref(field), param);
classFieldParams.put(property, param);
}
List<JVar> superConstructorParams = new ArrayList<JVar>();
for (String property : combinedSuperProperties) {
JFieldVar field = searchSuperClassesForField(property, jclass);
if (field == null) {
throw new IllegalStateException("Property " + property + " hasn't been added to JDefinedClass before calling addConstructors");
}
JVar param = classFieldParams.get(property);
if (param == null) {
param = fieldsConstructor.param(field.type(), field.name());
}
fieldsConstructor.javadoc().addParam(property);
superConstructorParams.add(param);
}
for (JVar param : superConstructorParams) {
superInvocation.arg(param);
}
}
use of com.sun.codemodel.JInvocation in project jsonschema2pojo by joelittlejohn.
the class AdditionalPropertiesRule method addBuilder.
private void addBuilder(JDefinedClass jclass, JType propertyType, JFieldVar field) {
JMethod builder = jclass.method(JMod.PUBLIC, jclass, "withAdditionalProperty");
JVar nameParam = builder.param(String.class, "name");
JVar valueParam = builder.param(propertyType, "value");
JBlock body = builder.body();
JInvocation mapInvocation = body.invoke(JExpr._this().ref(field), "put");
mapInvocation.arg(nameParam);
mapInvocation.arg(valueParam);
body._return(JExpr._this());
}
use of com.sun.codemodel.JInvocation in project jsonschema2pojo by joelittlejohn.
the class DefaultRule method getDefaultEnum.
private JExpression getDefaultEnum(JType fieldType, JsonNode node) {
JInvocation invokeFromValue = ((JClass) fieldType).staticInvoke("fromValue");
invokeFromValue.arg(node.asText());
return invokeFromValue;
}
use of com.sun.codemodel.JInvocation in project jsonschema2pojo by joelittlejohn.
the class DefaultRule method getDefaultSet.
/**
* Creates a default value for a set property by:
* <ol>
* <li>Creating a new {@link LinkedHashSet} with the correct generic type
* <li>Using {@link Arrays#asList(Object...)} to initialize the set with the
* correct default values
* </ol>
*
* @param fieldType
* the java type that applies for this field ({@link Set} with
* some generic type argument)
* @param node
* the node containing default values for this set
* @return an expression that creates a default value that can be assigned
* to this field
*/
private JExpression getDefaultSet(JType fieldType, JsonNode node) {
JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class);
setImplClass = setImplClass.narrow(setGenericType);
JInvocation newSetImpl = JExpr._new(setImplClass);
if (node instanceof ArrayNode && node.size() > 0) {
JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
for (JsonNode defaultValue : node) {
invokeAsList.arg(getDefaultValue(setGenericType, defaultValue));
}
newSetImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
return JExpr._null();
}
return newSetImpl;
}
Aggregations