use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addGetMethods.
void addGetMethods(JDefinedClass jclass) {
JFieldRef notFoundVar = getOrAddNotFoundVar(jclass);
JMethod internalGetMethod = this.getInternalGetMethod(jclass);
addPublicGetMethod(jclass, internalGetMethod, notFoundVar);
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addSetMethods.
private void addSetMethods(JDefinedClass jclass) {
JMethod internalSetMethod = getInternalSetMethod(jclass);
addPublicSetMethod(jclass, internalSetMethod);
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addGetPropertyCase.
private void addGetPropertyCase(JDefinedClass jclass, JSwitch propertySwitch, String propertyName, JType propertyType, JsonNode node) {
JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {});
propertySwitch._case(lit(propertyName)).body()._return(invoke(propertyGetter));
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addPublicGetMethod.
private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) {
JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME);
JTypeVar returnType = method.generify("T");
method.type(returnType);
Models.suppressWarnings(method, "unchecked");
JVar nameParam = method.param(String.class, "name");
JBlock body = method.body();
JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value", invoke(internalGetMethod).arg(nameParam).arg(notFoundValue));
JConditional found = method.body()._if(notFoundValue.ne(valueVar));
found._then()._return(cast(returnType, valueVar));
JBlock notFound = found._else();
JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
if (getAdditionalProperties != null) {
notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam)));
} else {
notFound._throw(illegalArgumentInvocation(jclass, nameParam));
}
return method;
}
use of com.sun.codemodel.JMethod 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;
}
Aggregations