use of com.sun.codemodel.JMethod in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generatePathSpecMethodsForUnion.
private void generatePathSpecMethodsForUnion(UnionTemplateSpec unionSpec, JDefinedClass unionClass) throws JClassAlreadyExistsException {
final JDefinedClass fieldsNestedClass = generatePathSpecNestedClass(unionClass);
for (UnionTemplateSpec.Member member : unionSpec.getMembers()) {
JClass fieldsRefType = _pathSpecClass;
if (hasNestedFields(member.getSchema())) {
final JClass unionMemberClass = generate(member.getClassTemplateSpec());
fieldsRefType = getCodeModel().ref(unionMemberClass.fullName() + ".Fields");
}
final JMethod accessorMethod = fieldsNestedClass.method(JMod.PUBLIC, fieldsRefType, CodeUtil.getUnionMemberName(member.getSchema()));
accessorMethod.body()._return(JExpr._new(fieldsRefType).arg(JExpr.invoke("getPathComponents")).arg(member.getSchema().getUnionMemberKey()));
}
}
use of com.sun.codemodel.JMethod in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateRecordFieldAccessors.
private void generateRecordFieldAccessors(JDefinedClass templateClass, RecordTemplateSpec.Field field, JClass type, JVar schemaFieldVar) {
final RecordDataSchema.Field schemaField = field.getSchemaField();
final DataSchema fieldSchema = schemaField.getType();
final boolean isDirect = CodeUtil.isDirectType(fieldSchema);
final String wrappedOrDirect;
if (isDirect) {
wrappedOrDirect = (field.getCustomInfo() == null ? "Direct" : "CustomType");
} else {
wrappedOrDirect = "Wrapped";
}
final String capitalizedName = CodeUtil.capitalize(schemaField.getName());
final String fieldFieldName = "FIELD_" + capitalizedName;
final JFieldVar fieldField = templateClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, RecordDataSchema.Field.class, fieldFieldName);
fieldField.init(schemaFieldVar.invoke("getField").arg(schemaField.getName()));
// Generate has method.
final JMethod has = templateClass.method(JMod.PUBLIC, getCodeModel().BOOLEAN, "has" + capitalizedName);
addAccessorDoc(templateClass, has, schemaField, "Existence checker");
setDeprecatedAnnotationAndJavadoc(has, schemaField);
final JBlock hasBody = has.body();
JExpression res = JExpr.invoke("contains").arg(fieldField);
hasBody._return(res);
if (_recordFieldRemove) {
// Generate remove method.
final String removeName = "remove" + capitalizedName;
final JMethod remove = templateClass.method(JMod.PUBLIC, getCodeModel().VOID, removeName);
addAccessorDoc(templateClass, remove, schemaField, "Remover");
setDeprecatedAnnotationAndJavadoc(remove, schemaField);
final JBlock removeBody = remove.body();
removeBody.invoke("remove").arg(fieldField);
}
final String getterName = JavaCodeUtil.getGetterName(getCodeModel(), type, capitalizedName);
if (_recordFieldAccessorWithMode) {
// Getter method with mode.
final JMethod getterWithMode = templateClass.method(JMod.PUBLIC, type, getterName);
addAccessorDoc(templateClass, getterWithMode, schemaField, "Getter");
setDeprecatedAnnotationAndJavadoc(getterWithMode, schemaField);
JVar modeParam = getterWithMode.param(_getModeClass, "mode");
final JBlock getterWithModeBody = getterWithMode.body();
res = JExpr.invoke("obtain" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type)).arg(modeParam);
getterWithModeBody._return(res);
}
// Getter method without mode.
final JMethod getterWithoutMode = templateClass.method(JMod.PUBLIC, type, getterName);
addAccessorDoc(templateClass, getterWithoutMode, schemaField, "Getter");
setDeprecatedAnnotationAndJavadoc(getterWithoutMode, schemaField);
JCommentPart returnComment = getterWithoutMode.javadoc().addReturn();
if (schemaField.getOptional()) {
getterWithoutMode.annotate(Nullable.class);
returnComment.add("Optional field. Always check for null.");
} else {
getterWithoutMode.annotate(Nonnull.class);
returnComment.add("Required field. Could be null for partial record.");
}
final JBlock getterWithoutModeBody = getterWithoutMode.body();
res = JExpr.invoke("obtain" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type)).arg(_strictGetMode);
getterWithoutModeBody._return(res);
// Determine dataClass
final JClass dataClass = generate(field.getDataClass());
final String setterName = "set" + capitalizedName;
if (_recordFieldAccessorWithMode) {
// Setter method with mode
final JMethod setterWithMode = templateClass.method(JMod.PUBLIC, templateClass, setterName);
addAccessorDoc(templateClass, setterWithMode, schemaField, "Setter");
setDeprecatedAnnotationAndJavadoc(setterWithMode, schemaField);
JVar param = setterWithMode.param(type, "value");
JVar modeParam = setterWithMode.param(_setModeClass, "mode");
JInvocation inv = setterWithMode.body().invoke("put" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type));
dataClassArg(inv, dataClass).arg(param).arg(modeParam);
setterWithMode.body()._return(JExpr._this());
}
// Setter method without mode
final JMethod setter = templateClass.method(JMod.PUBLIC, templateClass, setterName);
addAccessorDoc(templateClass, setter, schemaField, "Setter");
setDeprecatedAnnotationAndJavadoc(setter, schemaField);
JVar param = setter.param(type, "value");
param.annotate(Nonnull.class);
JCommentPart paramDoc = setter.javadoc().addParam(param);
paramDoc.add("Must not be null. For more control, use setters with mode instead.");
JInvocation inv = setter.body().invoke("put" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type));
dataClassArg(inv, dataClass).arg(param).arg(_disallowNullSetMode);
setter.body()._return(JExpr._this());
// Setter method without mode for unboxified type
if (!type.unboxify().equals(type)) {
final JMethod unboxifySetter = templateClass.method(JMod.PUBLIC, templateClass, setterName);
addAccessorDoc(templateClass, unboxifySetter, schemaField, "Setter");
setDeprecatedAnnotationAndJavadoc(unboxifySetter, schemaField);
param = unboxifySetter.param(type.unboxify(), "value");
inv = unboxifySetter.body().invoke("put" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type));
dataClassArg(inv, dataClass).arg(param).arg(_disallowNullSetMode);
unboxifySetter.body()._return(JExpr._this());
}
}
use of com.sun.codemodel.JMethod in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateConstructorWithMap.
private void generateConstructorWithMap(JDefinedClass cls, JClass valueClass) {
final JMethod argConstructor = cls.constructor(JMod.PUBLIC);
final JVar m = argConstructor.param(_mapClass.narrow(_stringClass, valueClass), "m");
argConstructor.body().invoke(THIS).arg(JExpr.invoke("newDataMapOfSize").arg(m.invoke("size")));
argConstructor.body().invoke("putAll").arg(m);
}
use of com.sun.codemodel.JMethod in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateConstructorWithCollection.
private void generateConstructorWithCollection(JDefinedClass cls, JClass elementClass) {
final JMethod argConstructor = cls.constructor(JMod.PUBLIC);
final JVar c = argConstructor.param(_collectionClass.narrow(elementClass), "c");
argConstructor.body().invoke(THIS).arg(JExpr._new(_dataListClass).arg(c.invoke("size")));
argConstructor.body().invoke("addAll").arg(c);
}
use of com.sun.codemodel.JMethod in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateUnionMemberAccessors.
private void generateUnionMemberAccessors(JDefinedClass unionClass, UnionTemplateSpec.Member member, JClass memberClass, JClass dataClass, JVar schemaField) {
final DataSchema memberType = member.getSchema();
final boolean isDirect = CodeUtil.isDirectType(memberType);
final String wrappedOrDirect;
if (isDirect) {
wrappedOrDirect = (member.getCustomInfo() == null ? "Direct" : "CustomType");
} else {
wrappedOrDirect = "Wrapped";
}
final String memberKey = memberType.getUnionMemberKey();
final String capitalizedName = CodeUtil.getUnionMemberName(memberType);
final String memberFieldName = "MEMBER_" + capitalizedName;
final JFieldVar memberField = unionClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, DataSchema.class, memberFieldName);
memberField.init(schemaField.invoke("getType").arg(memberKey));
final String setterName = "set" + capitalizedName;
// Generate builder.
final JMethod createMethod = unionClass.method(JMod.PUBLIC | JMod.STATIC, unionClass, "create");
JVar param = createMethod.param(memberClass, "value");
final JVar newUnionVar = createMethod.body().decl(unionClass, "newUnion", JExpr._new(unionClass));
createMethod.body().invoke(newUnionVar, setterName).arg(param);
createMethod.body()._return(newUnionVar);
// Is method.
final JMethod is = unionClass.method(JMod.PUBLIC, getCodeModel().BOOLEAN, "is" + capitalizedName);
final JBlock isBody = is.body();
JExpression res = JExpr.invoke("memberIs").arg(memberKey);
isBody._return(res);
// Getter method.
final String getterName = "get" + capitalizedName;
final JMethod getter = unionClass.method(JMod.PUBLIC, memberClass, getterName);
final JBlock getterBody = getter.body();
res = JExpr.invoke("obtain" + wrappedOrDirect).arg(memberField).arg(JExpr.dotclass(memberClass)).arg(memberKey);
getterBody._return(res);
// Setter method.
final JMethod setter = unionClass.method(JMod.PUBLIC, Void.TYPE, setterName);
param = setter.param(memberClass, "value");
final JInvocation inv = setter.body().invoke("select" + wrappedOrDirect).arg(memberField).arg(JExpr.dotclass(memberClass));
dataClassArg(inv, dataClass).arg(memberKey).arg(param);
}
Aggregations