use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class TitleRuleTest method applyAddsDescriptionToJavadoc.
@Test
public void applyAddsDescriptionToJavadoc() throws JClassAlreadyExistsException {
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
ObjectMapper mapper = new ObjectMapper();
TextNode titleNode = mapper.createObjectNode().textNode("some title");
JDocComment result = rule.apply("fooBar", titleNode, jclass, null);
assertThat(result, sameInstance(jclass.javadoc()));
assertThat(result.size(), is(1));
assertThat((String) result.get(0), is("some title\n<p>\n"));
}
use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class DescriptionRuleTest method applyAddsDescriptionToJavadoc.
@Test
public void applyAddsDescriptionToJavadoc() throws JClassAlreadyExistsException {
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
ObjectMapper mapper = new ObjectMapper();
TextNode descriptionNode = mapper.createObjectNode().textNode("some description");
JDocComment result = rule.apply("fooBar", descriptionNode, jclass, null);
assertThat(result, sameInstance(jclass.javadoc()));
assertThat(result.size(), is(1));
assertThat((String) result.get(0), is("some description"));
}
use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method makeUnique.
private String makeUnique(String className, JPackage _package) {
try {
JDefinedClass _class = _package._class(className);
_package.remove(_class);
return className;
} catch (JClassAlreadyExistsException e) {
return makeUnique(className + "_", _package);
}
}
use of com.sun.codemodel.JDefinedClass 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.JDefinedClass 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