use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class EnumRule method addFactoryMethod.
private void addFactoryMethod(JDefinedClass _enum, JType backingType) {
JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType);
JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
JVar valueParam = fromValue.param(backingType, "value");
JBlock body = fromValue.body();
JVar constant = body.decl(_enum, "constant");
constant.init(quickLookupMap.invoke("get").arg(valueParam));
JConditional _if = body._if(constant.eq(JExpr._null()));
JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class));
JExpression expr = valueParam;
// if string no need to add ""
if (!isString(backingType)) {
expr = expr.plus(JExpr.lit(""));
}
illegalArgumentException.arg(expr);
_if._then()._throw(illegalArgumentException);
_if._else()._return(constant);
ruleFactory.getAnnotator().enumCreatorMethod(fromValue);
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class AdditionalPropertiesRule method addAdditionalPropertiesField.
private JFieldVar addAdditionalPropertiesField(JDefinedClass jclass, JType propertyType) {
JClass propertiesMapType = jclass.owner().ref(Map.class);
propertiesMapType = propertiesMapType.narrow(jclass.owner().ref(String.class), propertyType.boxify());
JClass propertiesMapImplType = jclass.owner().ref(HashMap.class);
propertiesMapImplType = propertiesMapImplType.narrow(jclass.owner().ref(String.class), propertyType.boxify());
JFieldVar field = jclass.field(JMod.PRIVATE, propertiesMapType, "additionalProperties");
ruleFactory.getAnnotator().additionalPropertiesField(field, jclass, "additionalProperties");
field.init(JExpr._new(propertiesMapImplType));
return field;
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class AdditionalPropertiesRule method apply.
/**
* Applies this schema rule to take the required code generation steps.
* <p>
* If additionalProperties is specified and set to the boolean value
* <code>false</code>, this rule does not make any change to the generated
* Java type (the type does not allow additional properties).
* <p>
* If the additionalProperties node is <code>null</code> (not specified in
* the schema) or empty, then a new bean property named
* "additionalProperties", of type {@link Map}{@literal <String,Object>} is
* added to the generated type (with appropriate accessors). The accessors
* are annotated to allow unrecognised (additional) properties found in JSON
* data to be marshalled/unmarshalled from/to this map.
* <p>
* If the additionalProperties node is present and specifies a schema, then
* an "additionalProperties" map is added to the generated type. This time
* the map values will be restricted and must be instances of a newly
* generated Java type that will be created based on the
* additionalProperties schema provided. If the schema does not specify the
* javaType property, the name of the newly generated type will be derived
* from the nodeName and the suffix 'Property'.
*
* @param nodeName
* the name of the schema node for which the additionalProperties
* node applies
* @param node
* the additionalProperties node itself, found in the schema (may
* be null if not specified in the schema)
* @param jclass
* the Java type that is being generated to represent this schema
* @return the given Java type jclass
*/
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
if (node != null && node.isBoolean() && node.asBoolean() == false) {
// no additional properties allowed
return jclass;
}
if (!this.ruleFactory.getGenerationConfig().isIncludeAdditionalProperties()) {
// no additional properties allowed
return jclass;
}
if (!ruleFactory.getAnnotator().isAdditionalPropertiesSupported()) {
// schema allows additional properties, but serializer library can't support them
return jclass;
}
JType propertyType;
if (node != null && node.size() != 0) {
propertyType = ruleFactory.getSchemaRule().apply(nodeName + "Property", node, jclass, schema);
} else {
propertyType = jclass.owner().ref(Object.class);
}
JFieldVar field = addAdditionalPropertiesField(jclass, propertyType);
addGetter(jclass, field);
addSetter(jclass, propertyType, field);
if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()) {
ruleFactory.getValidRule().apply(nodeName, node, field, schema);
}
if (ruleFactory.getGenerationConfig().isGenerateBuilders()) {
addBuilder(jclass, propertyType, field);
}
return jclass;
}
use of com.sun.codemodel.JFieldVar in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateRecord.
protected void generateRecord(JDefinedClass templateClass, RecordTemplateSpec recordSpec) throws JClassAlreadyExistsException {
templateClass.javadoc().append(recordSpec.getSchema().getDoc());
setDeprecatedAnnotationAndJavadoc(recordSpec.getSchema(), templateClass);
extendRecordBaseClass(templateClass);
if (_pathSpecMethods) {
generatePathSpecMethodsForRecord(recordSpec.getFields(), templateClass);
}
final JFieldVar schemaFieldVar = generateSchemaField(templateClass, recordSpec.getSchema());
generateConstructorWithNoArg(templateClass, schemaFieldVar, _dataMapClass);
generateConstructorWithArg(templateClass, schemaFieldVar, _dataMapClass);
for (RecordTemplateSpec.Field field : recordSpec.getFields()) {
generateRecordFieldAccessors(templateClass, field, generate(field.getType()), schemaFieldVar);
}
recordSpec.getFields().stream().map(RecordTemplateSpec.Field::getCustomInfo).distinct().forEach(customInfo -> generateCustomClassInitialization(templateClass, customInfo));
if (_copierMethods) {
generateCopierMethods(templateClass);
}
}
use of com.sun.codemodel.JFieldVar in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateUnion.
protected void generateUnion(JDefinedClass unionClass, UnionTemplateSpec unionSpec) throws JClassAlreadyExistsException {
extendUnionBaseClass(unionClass);
final JVar schemaField = generateSchemaField(unionClass, unionSpec.getSchema());
generateConstructorWithNoArg(unionClass, schemaField, _dataMapClass);
generateConstructorWithObjectArg(unionClass, schemaField);
for (UnionTemplateSpec.Member member : unionSpec.getMembers()) {
if (member.getClassTemplateSpec() != null) {
generateUnionMemberAccessors(unionClass, member, generate(member.getClassTemplateSpec()), generate(member.getDataClass()), schemaField);
}
}
unionSpec.getMembers().stream().map(UnionTemplateSpec.Member::getCustomInfo).distinct().forEach(customInfo -> generateCustomClassInitialization(unionClass, customInfo));
if (_pathSpecMethods) {
generatePathSpecMethodsForUnion(unionSpec, unionClass);
}
if (_copierMethods) {
generateCopierMethods(unionClass);
}
if (unionSpec.getTyperefClass() != null) {
final TyperefTemplateSpec typerefClassSpec = unionSpec.getTyperefClass();
final JDefinedClass typerefInfoClass = unionClass._class(getJModValue(typerefClassSpec.getModifiers()), escapeReserved(typerefClassSpec.getClassName()));
generateTyperef(typerefInfoClass, typerefClassSpec);
final JFieldVar typerefInfoField = unionClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, TyperefInfo.class, DataTemplateUtil.TYPEREFINFO_FIELD_NAME);
typerefInfoField.init(JExpr._new(typerefInfoClass));
unionClass._implements(HasTyperefInfo.class);
final JMethod typerefInfoMethod = unionClass.method(JMod.PUBLIC, TyperefInfo.class, "typerefInfo");
typerefInfoMethod.body()._return(typerefInfoField);
}
}
Aggregations