use of com.sun.codemodel.JMethod 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.JMethod in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addSetProperty.
private void addSetProperty(JDefinedClass jclass, JBlock callSite, String propertyName, JType propertyType, JVar valueVar, JsonNode node) {
JMethod propertySetter = jclass.getMethod(getSetterName(propertyName, node), new JType[] { propertyType });
JConditional isInstance = callSite._if(valueVar._instanceof(propertyType.boxify().erasure()));
isInstance._then().invoke(propertySetter).arg(cast(propertyType.boxify(), valueVar));
isInstance._else()._throw(illegalArgumentInvocation(jclass, propertyName, propertyType, valueVar));
}
use of com.sun.codemodel.JMethod 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.JMethod in project jsonschema2pojo by joelittlejohn.
the class EnumRule method addToString.
private void addToString(JDefinedClass _enum, JFieldVar valueField) {
JMethod toString = _enum.method(JMod.PUBLIC, String.class, "toString");
JBlock body = toString.body();
JExpression toReturn = JExpr._this().ref(valueField);
if (!isString(valueField.type())) {
toReturn = toReturn.plus(JExpr.lit(""));
}
body._return(toReturn);
toString.annotate(Override.class);
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class PropertiesRule method addOverrideBuilder.
private void addOverrideBuilder(JDefinedClass thisJDefinedClass, JMethod parentBuilder, JVar parentParam) {
if (thisJDefinedClass.getMethod(parentBuilder.name(), new JType[] { parentParam.type() }) == null) {
JMethod builder = thisJDefinedClass.method(parentBuilder.mods().getValue(), thisJDefinedClass, parentBuilder.name());
builder.annotate(Override.class);
JVar param = builder.param(parentParam.type(), parentParam.name());
JBlock body = builder.body();
body.invoke(JExpr._super(), parentBuilder).arg(param);
body._return(JExpr._this());
}
}
Aggregations