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 rest.li by linkedin.
the class JavaDataTemplateGenerator method generateConstructorWithNoArg.
private static void generateConstructorWithNoArg(JDefinedClass cls, JClass newClass) {
final JMethod noArgConstructor = cls.constructor(JMod.PUBLIC);
noArgConstructor.body().invoke(THIS).arg(JExpr._new(newClass));
}
use of com.sun.codemodel.JMethod in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateTyperef.
protected void generateTyperef(JDefinedClass typerefClass, TyperefTemplateSpec typerefSpec) {
typerefClass.javadoc().append(typerefSpec.getSchema().getDoc());
setDeprecatedAnnotationAndJavadoc(typerefSpec.getSchema(), typerefClass);
typerefClass._extends(TyperefInfo.class);
final JVar schemaField = generateSchemaField(typerefClass, typerefSpec.getSchema());
final JMethod constructor = typerefClass.constructor(JMod.PUBLIC);
constructor.body().invoke(SUPER).arg(schemaField);
}
Aggregations