use of com.sun.codemodel.JDefinedClass in project jsonschema2pojo by joelittlejohn.
the class DefaultRule method getDefaultValue.
private JExpression getDefaultValue(JType fieldType, JsonNode node) {
fieldType = fieldType.unboxify();
if (fieldType.fullName().equals(String.class.getName())) {
return JExpr.lit(node.asText());
} else if (fieldType.fullName().equals(int.class.getName())) {
return JExpr.lit(Integer.parseInt(node.asText()));
} else if (fieldType.fullName().equals(BigInteger.class.getName())) {
return JExpr._new(fieldType).arg(JExpr.lit(node.asText()));
} else if (fieldType.fullName().equals(double.class.getName())) {
return JExpr.lit(Double.parseDouble(node.asText()));
} else if (fieldType.fullName().equals(BigDecimal.class.getName())) {
return JExpr._new(fieldType).arg(JExpr.lit(node.asText()));
} else if (fieldType.fullName().equals(boolean.class.getName())) {
return JExpr.lit(Boolean.parseBoolean(node.asText()));
} else if (fieldType.fullName().equals(getDateTimeType().getName())) {
long millisecs = parseDateToMillisecs(node.asText());
JInvocation newDateTime = JExpr._new(fieldType);
newDateTime.arg(JExpr.lit(millisecs));
return newDateTime;
} else if (fieldType.fullName().equals(LocalDate.class.getName()) || fieldType.fullName().equals(LocalTime.class.getName())) {
JInvocation stringParseableTypeInstance = JExpr._new(fieldType);
stringParseableTypeInstance.arg(JExpr.lit(node.asText()));
return stringParseableTypeInstance;
} else if (fieldType.fullName().equals(long.class.getName())) {
return JExpr.lit(Long.parseLong(node.asText()));
} else if (fieldType.fullName().equals(float.class.getName())) {
return JExpr.lit(Float.parseFloat(node.asText()));
} else if (fieldType.fullName().equals(URI.class.getName())) {
JInvocation invokeCreate = fieldType.owner().ref(URI.class).staticInvoke("create");
return invokeCreate.arg(JExpr.lit(node.asText()));
} else if (fieldType instanceof JDefinedClass && ((JDefinedClass) fieldType).getClassType().equals(ClassType.ENUM)) {
return getDefaultEnum(fieldType, node);
} else {
return JExpr._null();
}
}
use of com.sun.codemodel.JDefinedClass in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generatePathSpecMethodsForCollection.
private void generatePathSpecMethodsForCollection(JDefinedClass templateClass, DataSchema schema, JClass childClass, String wildcardMethodName) throws JClassAlreadyExistsException {
if (hasNestedFields(schema)) {
final JDefinedClass fieldsNestedClass = generatePathSpecNestedClass(templateClass);
final JClass itemsFieldType = getCodeModel().ref(childClass.fullName() + ".Fields");
final JMethod constantField = fieldsNestedClass.method(JMod.PUBLIC, itemsFieldType, wildcardMethodName);
constantField.body()._return(JExpr._new(itemsFieldType).arg(JExpr.invoke("getPathComponents")).arg(_pathSpecClass.staticRef("WILDCARD")));
}
}
use of com.sun.codemodel.JDefinedClass in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generatePathSpecMethodsForRecord.
private void generatePathSpecMethodsForRecord(List<RecordTemplateSpec.Field> fieldSpecs, JDefinedClass templateClass) throws JClassAlreadyExistsException {
final JDefinedClass fieldsNestedClass = generatePathSpecNestedClass(templateClass);
for (RecordTemplateSpec.Field field : fieldSpecs) {
JClass fieldsRefType = _pathSpecClass;
if (hasNestedFields(field.getSchemaField().getType())) {
final JClass fieldType = generate(field.getType());
fieldsRefType = getCodeModel().ref(fieldType.fullName() + ".Fields");
}
final JMethod constantField = fieldsNestedClass.method(JMod.PUBLIC, fieldsRefType, escapeReserved(field.getSchemaField().getName()));
constantField.body()._return(JExpr._new(fieldsRefType).arg(JExpr.invoke("getPathComponents")).arg(field.getSchemaField().getName()));
if (!field.getSchemaField().getDoc().isEmpty()) {
constantField.javadoc().append(field.getSchemaField().getDoc());
}
setDeprecatedAnnotationAndJavadoc(constantField, field.getSchemaField());
}
final JVar staticFields = templateClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, fieldsNestedClass, "_fields").init(JExpr._new(fieldsNestedClass));
final JMethod staticFieldsAccessor = templateClass.method(JMod.PUBLIC | JMod.STATIC, fieldsNestedClass, "fields");
staticFieldsAccessor.body()._return(staticFields);
}
use of com.sun.codemodel.JDefinedClass 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);
}
}
use of com.sun.codemodel.JDefinedClass in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generate.
public JClass generate(ClassTemplateSpec classTemplateSpec) {
final JClass result;
if (classTemplateSpec == null) {
result = null;
} else {
if (classTemplateSpec.getSchema() == null) {
// this is for custom class, package override is not applicable.
result = getCodeModel().directClass(classTemplateSpec.getFullName());
} else if (PredefinedJavaClasses.containsKey(classTemplateSpec.getSchema())) {
final Class<?> nativeJavaClass = PredefinedJavaClasses.get(classTemplateSpec.getSchema());
result = getCodeModel().ref(nativeJavaClass);
} else if (classTemplateSpec.getSchema().isPrimitive()) {
result = generatePrimitive((PrimitiveTemplateSpec) classTemplateSpec);
} else {
try {
final JDefinedClass definedClass = defineClass(classTemplateSpec);
populateClassContent(classTemplateSpec, definedClass);
result = definedClass;
} catch (JClassAlreadyExistsException e) {
throw new IllegalArgumentException(classTemplateSpec.getFullName());
}
}
}
return result;
}
Aggregations