use of com.sun.codemodel.JType in project jsonschema2pojo by joelittlejohn.
the class TypeRuleTest method applyGeneratesNullAsObject.
@Test
public void applyGeneratesNullAsObject() {
JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "null");
JType result = rule.apply("fooBar", objectNode, jpackage, null);
assertThat(result.fullName(), is(Object.class.getName()));
}
use of com.sun.codemodel.JType in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addPublicWithMethod.
private JMethod addPublicWithMethod(JDefinedClass jclass, JMethod internalSetMethod) {
JMethod method = jclass.method(PUBLIC, jclass, BUILDER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar valueParam = method.param(Object.class, "value");
JBlock body = method.body();
JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();
// if we have additional properties, then put value.
JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
if (getAdditionalProperties != null) {
JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam).arg(cast(additionalPropertiesType, valueParam)));
} else // else throw exception.
{
notFound._throw(illegalArgumentInvocation(jclass, nameParam));
}
body._return(_this());
return method;
}
use of com.sun.codemodel.JType in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addInternalGetMethodJava6.
private JMethod addInternalGetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
JBlock body = method.body();
JConditional propertyConditional = null;
if (propertiesNode != null) {
for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext(); ) {
Map.Entry<String, JsonNode> property = properties.next();
String propertyName = property.getKey();
JsonNode node = property.getValue();
String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
JType propertyType = jclass.fields().get(fieldName).type();
JExpression condition = lit(propertyName).invoke("equals").arg(nameParam);
if (propertyConditional == null) {
propertyConditional = body._if(condition);
} else {
propertyConditional = propertyConditional._elseif(condition);
}
JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {});
propertyConditional._then()._return(invoke(propertyGetter));
}
}
JClass extendsType = jclass._extends();
JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else();
if (extendsType != null && extendsType instanceof JDefinedClass) {
JDefinedClass parentClass = (JDefinedClass) extendsType;
JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
} else {
lastBlock._return(notFoundParam);
}
return method;
}
use of com.sun.codemodel.JType 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;
}
use of com.sun.codemodel.JType in project jsonschema2pojo by joelittlejohn.
the class AdditionalPropertiesRule method apply.
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* If additionalProperties is specified and set to the boolean value
* <code>false</code>, this rule does not make any change to the generated
* Java type (the type does not allow additional properties).
* <p>
* If the additionalProperties node is <code>null</code> (not specified in
* the schema) or empty, then a new bean property named
* "additionalProperties", of type {@link Map}{@literal <String,Object>} is
* added to the generated type (with appropriate accessors). The accessors
* are annotated to allow unrecognised (additional) properties found in JSON
* data to be marshalled/unmarshalled from/to this map.
* <p>
* If the additionalProperties node is present and specifies a schema, then
* an "additionalProperties" map is added to the generated type. This time
* the map values will be restricted and must be instances of a newly
* generated Java type that will be created based on the
* additionalProperties schema provided. If the schema does not specify the
* javaType property, the name of the newly generated type will be derived
* from the nodeName and the suffix 'Property'.
*
* @param nodeName
* the name of the schema node for which the additionalProperties
* node applies
* @param node
* the additionalProperties node itself, found in the schema (may
* be null if not specified in the schema)
* @param jclass
* the Java type that is being generated to represent this schema
* @return the given Java type jclass
*/
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
if (node != null && node.isBoolean() && node.asBoolean() == false) {
// no additional properties allowed
return jclass;
}
if (!this.ruleFactory.getGenerationConfig().isIncludeAdditionalProperties()) {
// no additional properties allowed
return jclass;
}
if (!ruleFactory.getAnnotator().isAdditionalPropertiesSupported()) {
// schema allows additional properties, but serializer library can't support them
return jclass;
}
JType propertyType;
if (node != null && node.size() != 0) {
propertyType = ruleFactory.getSchemaRule().apply(nodeName + "Property", node, jclass, schema);
} else {
propertyType = jclass.owner().ref(Object.class);
}
JFieldVar field = addAdditionalPropertiesField(jclass, propertyType);
addGetter(jclass, field);
addSetter(jclass, propertyType, field);
if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()) {
ruleFactory.getValidRule().apply(nodeName, node, field, schema);
}
if (ruleFactory.getGenerationConfig().isGenerateBuilders()) {
addBuilder(jclass, propertyType, field);
}
return jclass;
}
Aggregations