use of com.sun.codemodel.JClass in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateArray.
protected void generateArray(JDefinedClass arrayClass, ArrayTemplateSpec arrayDataTemplateSpec) throws JClassAlreadyExistsException {
final JClass itemJClass = generate(arrayDataTemplateSpec.getItemClass());
final JClass dataJClass = generate(arrayDataTemplateSpec.getItemDataClass());
if (CodeUtil.isDirectType(arrayDataTemplateSpec.getSchema().getItems())) {
arrayClass._extends(_directArrayBaseClass.narrow(itemJClass));
} else {
extendWrappingArrayBaseClass(itemJClass, arrayClass);
}
/** see {@link #schemaForArrayItemsOrMapValues} */
final DataSchema bareSchema = new ArrayDataSchema(schemaForArrayItemsOrMapValues(arrayDataTemplateSpec.getCustomInfo(), arrayDataTemplateSpec.getSchema().getItems()));
final JVar schemaField = generateSchemaField(arrayClass, bareSchema);
generateConstructorWithNoArg(arrayClass, _dataListClass);
generateConstructorWithInitialCapacity(arrayClass, _dataListClass);
generateConstructorWithCollection(arrayClass, itemJClass);
generateConstructorWithArg(arrayClass, schemaField, _dataListClass, itemJClass, dataJClass);
if (_pathSpecMethods) {
generatePathSpecMethodsForCollection(arrayClass, arrayDataTemplateSpec.getSchema(), itemJClass, "items");
}
generateCustomClassInitialization(arrayClass, arrayDataTemplateSpec.getCustomInfo());
if (_copierMethods) {
generateCopierMethods(arrayClass);
}
}
use of com.sun.codemodel.JClass 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.JClass 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;
}
use of com.sun.codemodel.JClass in project rest.li by linkedin.
the class JavaRequestBuilderGenerator method methodMetadataMapInit.
private JVar methodMetadataMapInit(JDefinedClass facadeClass, ActionSchemaArray resourceActions, ActionSchemaArray entityActions, JBlock staticInit) {
// CreateMetadata (only for actions right now)
final JClass MetadataMapClass = getCodeModel().ref(HashMap.class).narrow(_stringClass, getCodeModel().ref(DynamicRecordMetadata.class));
final JVar requestMetadataMap = staticInit.decl(MetadataMapClass, "requestMetadataMap").init(JExpr._new(MetadataMapClass));
final JClass fieldDefListClass = getCodeModel().ref(ArrayList.class).narrow(getCodeModel().ref(FieldDef.class).narrow(getCodeModel().ref(Object.class).wildcard()));
// get all actions into a single ActionSchemaArray
final int resourceActionsSize = resourceActions == null ? 0 : resourceActions.size();
final int entityActionsSize = entityActions == null ? 0 : entityActions.size();
final ActionSchemaArray allActionSchema = new ActionSchemaArray(resourceActionsSize + entityActionsSize);
allActionSchema.addAll(resourceActions == null ? new ActionSchemaArray() : resourceActions);
allActionSchema.addAll(entityActions == null ? new ActionSchemaArray() : entityActions);
for (ActionSchema actionSchema : allActionSchema) {
final String varName = actionSchema.getName() + "Params";
final JVar currMethodParams = staticInit.decl(fieldDefListClass, varName).init(JExpr._new(fieldDefListClass));
final ParameterSchemaArray parameters = actionSchema.getParameters();
for (ParameterSchema parameterSchema : parameters == null ? new ParameterSchemaArray() : parameters) {
final JInvocation fieldDefParam = createFieldDef(parameterSchema.getName(), parameterSchema.getType(), facadeClass);
staticInit.add(currMethodParams.invoke("add").arg(fieldDefParam));
}
final String methodName = actionSchema.getName();
final JInvocation newSchema = createMetadata(methodName, currMethodParams);
staticInit.add(requestMetadataMap.invoke("put").arg(methodName).arg(newSchema));
}
return requestMetadataMap;
}
use of com.sun.codemodel.JClass in project rest.li by linkedin.
the class JavaRequestBuilderGenerator method getKeyClass.
/**
* Get the key class. In case of collection with a simple key, return the one specified by "type" in the "identifier". Otherwise, get both "type" and "params", and return ComplexKeyResource
* parameterized by those two.
*
* @param collection
* @param facadeClass
*
* @return JClass as described above
*/
private JClass getKeyClass(CollectionSchema collection, JDefinedClass facadeClass) {
final JClass keyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
if (collection.getIdentifier().getParams() == null) {
return keyClass;
}
final JClass paramsClass = getJavaBindingType(collection.getIdentifier().getParams(), facadeClass).valueClass;
return getCodeModel().ref(ComplexResourceKey.class).narrow(keyClass, paramsClass);
}
Aggregations