use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method searchSuperClassesForField.
/**
* This is recursive with searchClassAndSuperClassesForField
*/
private JFieldVar searchSuperClassesForField(String property, JDefinedClass jclass) {
JClass superClass = jclass._extends();
JDefinedClass definedSuperClass = definedClassOrNullFromType(superClass);
if (definedSuperClass == null) {
return null;
}
return searchClassAndSuperClassesForField(property, definedSuperClass);
}
use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addInternalSetMethodJava7.
private JMethod addInternalSetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar valueParam = method.param(Object.class, "value");
JBlock body = method.body();
JSwitch propertySwitch = body._switch(nameParam);
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();
addSetPropertyCase(jclass, propertySwitch, propertyName, propertyType, valueParam, node);
}
}
JBlock defaultBlock = propertySwitch._default().body();
JClass extendsType = jclass._extends();
if (extendsType != null && extendsType instanceof JDefinedClass) {
JDefinedClass parentClass = (JDefinedClass) extendsType;
JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
defaultBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
} else {
defaultBlock._return(FALSE);
}
return method;
}
use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addInternalGetMethodJava7.
private JMethod addInternalGetMethodJava7(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();
JSwitch propertySwitch = body._switch(nameParam);
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();
addGetPropertyCase(jclass, propertySwitch, propertyName, propertyType, node);
}
}
JClass extendsType = jclass._extends();
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) });
propertySwitch._default().body()._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
} else {
propertySwitch._default().body()._return(notFoundParam);
}
return method;
}
use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addInternalSetMethodJava6.
private JMethod addInternalSetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar valueParam = method.param(Object.class, "value");
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);
propertyConditional = propertyConditional == null ? propertyConditional = body._if(condition) : propertyConditional._elseif(condition);
JBlock callSite = propertyConditional._then();
addSetProperty(jclass, callSite, propertyName, propertyType, valueParam, node);
callSite._return(TRUE);
}
}
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_SETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
} else {
lastBlock._return(FALSE);
}
return method;
}
use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class TypeRuleTest method applyGeneratesCustomObject.
@Test
public void applyGeneratesCustomObject() {
JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "object");
JDefinedClass mockObjectType = mock(JDefinedClass.class);
ObjectRule mockObjectRule = mock(ObjectRule.class);
when(mockObjectRule.apply("fooBar", objectNode, jpackage, null)).thenReturn(mockObjectType);
when(ruleFactory.getObjectRule()).thenReturn(mockObjectRule);
JType result = rule.apply("fooBar", objectNode, jpackage, null);
assertThat(result, is((JType) mockObjectType));
}
Aggregations