MCPcopy Create free account
hub / github.com/apache/fory / generate_field

Method generate_field

compiler/fory_compiler/generators/java.py:1202–1251  ·  view source on GitHub ↗

Generate field declaration with annotations.

(
        self, field: Field, parent_stack: Optional[List[Message]]
    )

Source from the content-addressed store, hash-verified

1200 return lines
1201
1202 def generate_field(
1203 self, field: Field, parent_stack: Optional[List[Message]]
1204 ) -> List[str]:
1205 """Generate field declaration with annotations."""
1206 lines = []
1207
1208 # Generate @ForyField annotation if needed
1209 annotations = []
1210 is_any = (
1211 isinstance(field.field_type, PrimitiveType)
1212 and field.field_type.kind == PrimitiveKind.ANY
1213 )
1214 nullable = field.optional or is_any
1215 if field.tag_id is not None:
1216 annotations.append(f"id = {field.tag_id}")
1217 if annotations:
1218 lines.append(f"@ForyField({', '.join(annotations)})")
1219
1220 use_array_type_use = self.field_uses_array_type_use(field)
1221 array_annotation = self.get_array_annotation(field)
1222 if array_annotation and not use_array_type_use:
1223 lines.append(array_annotation)
1224
1225 primitive_list_annotation = self.get_primitive_list_annotation(field)
1226 int_annotation = self.get_integer_annotation(field.field_type)
1227 use_type_annotation = (
1228 use_array_type_use
1229 or primitive_list_annotation is not None
1230 or int_annotation is not None
1231 )
1232
1233 # Field type
1234 java_type = self.generate_type(
1235 field.field_type,
1236 nullable,
1237 field.element_optional,
1238 field.element_ref,
1239 field,
1240 type_use=use_type_annotation,
1241 parent_stack=parent_stack,
1242 )
1243 if nullable:
1244 java_type = self.apply_top_level_type_use_annotation(java_type, "@Nullable")
1245 if field.ref:
1246 java_type = self.apply_top_level_type_use_annotation(java_type, "@Ref")
1247
1248 lines.append(f"private {java_type} {self.to_camel_case(field.name)};")
1249 lines.append("")
1250
1251 return lines
1252
1253 def generate_getter_setter(
1254 self, field: Field, parent_stack: Optional[List[Message]]

Callers 2

generate_message_fileMethod · 0.95

Calls 8

get_array_annotationMethod · 0.95
generate_typeMethod · 0.95
to_camel_caseMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected