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());
}
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class PropertyRule method apply.
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* This rule adds a property to a given Java class according to the Java
* Bean spec. A private field is added to the class, along with accompanying
* accessor methods.
* <p>
* If this rule's schema mapper is configured to include builder methods
* (see {@link GenerationConfig#isGenerateBuilders()} ),
* then a builder method of the form <code>withFoo(Foo foo);</code> is also
* added.
*
* @param nodeName
* the name of the property to be applied
* @param node
* the node describing the characteristics of this property
* @param jclass
* the Java class which should have this property added
* @return the given jclass
*/
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
String propertyName = ruleFactory.getNameHelper().getPropertyName(nodeName, node);
JType propertyType = ruleFactory.getSchemaRule().apply(nodeName, node, jclass, schema);
node = resolveRefs(node, schema);
int accessModifier = ruleFactory.getGenerationConfig().isIncludeAccessors() ? JMod.PRIVATE : JMod.PUBLIC;
JFieldVar field = jclass.field(accessModifier, propertyType, propertyName);
propertyAnnotations(nodeName, node, schema, field);
formatAnnotation(field, node);
ruleFactory.getAnnotator().propertyField(field, jclass, nodeName, node);
if (ruleFactory.getGenerationConfig().isIncludeAccessors()) {
JMethod getter = addGetter(jclass, field, nodeName, node);
ruleFactory.getAnnotator().propertyGetter(getter, nodeName);
propertyAnnotations(nodeName, node, schema, getter);
JMethod setter = addSetter(jclass, field, nodeName, node);
ruleFactory.getAnnotator().propertySetter(setter, nodeName);
propertyAnnotations(nodeName, node, schema, setter);
}
if (ruleFactory.getGenerationConfig().isGenerateBuilders()) {
addBuilder(jclass, field);
}
if (node.has("pattern")) {
ruleFactory.getPatternRule().apply(nodeName, node.get("pattern"), field, schema);
}
ruleFactory.getDefaultRule().apply(nodeName, node.get("default"), field, schema);
ruleFactory.getMinimumMaximumRule().apply(nodeName, node, field, schema);
ruleFactory.getMinItemsMaxItemsRule().apply(nodeName, node, field, schema);
ruleFactory.getMinLengthMaxLengthRule().apply(nodeName, node, field, schema);
if (isObject(node) || isArray(node)) {
ruleFactory.getValidRule().apply(nodeName, node, field, schema);
}
return jclass;
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class PropertyRule method addBuilder.
private JMethod addBuilder(JDefinedClass c, JFieldVar field) {
JMethod builder = c.method(JMod.PUBLIC, c, getBuilderName(field.name()));
JVar param = builder.param(field.type(), field.name());
JBlock body = builder.body();
body.assign(JExpr._this().ref(field), param);
body._return(JExpr._this());
return builder;
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class PropertyRule method addSetter.
private JMethod addSetter(JDefinedClass c, JFieldVar field, String jsonPropertyName, JsonNode node) {
JMethod setter = c.method(JMod.PUBLIC, void.class, getSetterName(jsonPropertyName, node));
JVar param = setter.param(field.type(), field.name());
JBlock body = setter.body();
body.assign(JExpr._this().ref(field), param);
return setter;
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class SerializableHelper method processMethodCollectionForSerializableSupport.
private static void processMethodCollectionForSerializableSupport(Iterator<JMethod> methods, DataOutputStream dataOutputStream) throws IOException {
TreeMap<String, JMethod> sortedMethods = new TreeMap<String, JMethod>();
while (methods.hasNext()) {
JMethod method = methods.next();
//Collect non-private methods
if ((method.mods().getValue() & JMod.PRIVATE) != JMod.PRIVATE) {
sortedMethods.put(method.name(), method);
}
}
for (JMethod method : sortedMethods.values()) {
dataOutputStream.writeUTF(method.name());
dataOutputStream.writeInt(method.mods().getValue());
if (method.type() != null) {
dataOutputStream.writeUTF(method.type().fullName());
}
for (JVar param : method.params()) {
dataOutputStream.writeUTF(param.type().fullName());
}
}
}
Aggregations