Skip to content

Commit 381c03b

Browse files
RayanRalmeta-codesync[bot]
authored andcommitted
Extract enum/struct/exception metadata into separate helper files
Summary: Move enum, struct, and exception metadata generation from inline code in ThriftMetadataHandler into separate helper classes: - ThriftMetadataHandlerEnums: per-service enum metadata with value batching - ThriftMetadataHandlerStructs: per-service struct metadata with union support - ThriftMetadataHandlerExceptions: per-service exception metadata This splits large generated files into smaller compilation units to avoid JVM method size limits, and prepares for full whisker migration. The ThriftMetadataHandler getters now delegate to these helper classes. New whisker templates use prototype properties added in D97970494. Reviewed By: echistyakov Differential Revision: D97973923 fbshipit-source-id: f97ecca1f64a5852ccd4646b9648532e14058bfd
1 parent 29d2221 commit 381c03b

141 files changed

Lines changed: 3397 additions & 630 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

third-party/thrift/src/thrift/compiler/generate/t_mstch_java_generator.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,22 @@ class t_mstch_java_generator : public t_mstch_generator {
12841284
cache[metadata_handler_id],
12851285
"ThriftMetadataHandler",
12861286
package_dir / metadata_handler_filename);
1287+
render_to_file(
1288+
cache[metadata_handler_id],
1289+
"ThriftMetadataHandlerEnums",
1290+
package_dir /
1291+
fmt::format("{}ThriftMetadataHandlerEnums.java", service_name));
1292+
render_to_file(
1293+
cache[metadata_handler_id],
1294+
"ThriftMetadataHandlerStructs",
1295+
package_dir /
1296+
fmt::format("{}ThriftMetadataHandlerStructs.java", service_name));
1297+
render_to_file(
1298+
cache[metadata_handler_id],
1299+
"ThriftMetadataHandlerExceptions",
1300+
package_dir /
1301+
fmt::format(
1302+
"{}ThriftMetadataHandlerExceptions.java", service_name));
12871303
}
12881304
}
12891305

third-party/thrift/src/thrift/compiler/generate/templates/java/ThriftMetadataHandler.mustache

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
package {{service:program.javaPackage}};
2323

2424
import java.util.ArrayList;
25-
import java.util.Arrays;
26-
import java.util.Collections;
27-
import java.util.HashMap;
2825
import java.util.List;
2926
import java.util.Map;
3027
import com.facebook.thrift.metadata.ThriftEnum;
@@ -52,9 +49,6 @@ public class {{service:javaCapitalName}}ThriftMetadataHandler implements com.fac
5249
{{/each}}
5350

5451
private static final ThriftService THRIFT_SERVICE;
55-
private static final Map<String, ThriftEnum> ENUMS;
56-
private static final Map<String, ThriftStruct> STRUCTS;
57-
private static final Map<String, ThriftException> EXCEPTIONS;
5852

5953
{{#if (object.notnull? service:extends)}}
6054
private static final com.facebook.thrift.server.ThriftMetadataHandler PARENT_HANDLER =
@@ -73,15 +67,6 @@ public class {{service:javaCapitalName}}ThriftMetadataHandler implements com.fac
7367
{{/if (object.notnull? service:extends)}}
7468
.setFunctions(functions)
7569
.build();
76-
77-
Map<String, ThriftEnum> enums = new HashMap<>();
78-
ENUMS = Collections.unmodifiableMap(enums);
79-
80-
Map<String, ThriftStruct> structs = new HashMap<>();
81-
STRUCTS = Collections.unmodifiableMap(structs);
82-
83-
Map<String, ThriftException> exceptions = new HashMap<>();
84-
EXCEPTIONS = Collections.unmodifiableMap(exceptions);
8570
}
8671

8772
@Override
@@ -111,16 +96,16 @@ public class {{service:javaCapitalName}}ThriftMetadataHandler implements com.fac
11196

11297
@Override
11398
public Map<String, ThriftEnum> getEnums() {
114-
return ENUMS;
99+
return {{service:javaCapitalName}}ThriftMetadataHandlerEnums.getEnums();
115100
}
116101

117102
@Override
118103
public Map<String, ThriftStruct> getStructs() {
119-
return STRUCTS;
104+
return {{service:javaCapitalName}}ThriftMetadataHandlerStructs.getStructs();
120105
}
121106

122107
@Override
123108
public Map<String, ThriftException> getExceptions() {
124-
return EXCEPTIONS;
109+
return {{service:javaCapitalName}}ThriftMetadataHandlerExceptions.getExceptions();
125110
}
126111
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{{!
2+
3+
Copyright (c) Meta Platforms, Inc. and affiliates.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
}}
18+
{{#import "common/types" as types}}
19+
{{> AutoGenerated}}
20+
21+
package {{service:program.javaPackage}};
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
import com.facebook.thrift.metadata.ThriftEnum;
26+
27+
{{#each (array.enumerate service:referenced_enum_batches) as |batch_index enums|}}
28+
{{#each enums as |enum|}}
29+
{{#if enum.has_multiple_value_batches?}}
30+
{{#each (array.enumerate enum.value_batches) as |vb_index vals|}}
31+
class {{service:javaCapitalName}}ThriftMetadataHandlerEnums_{{batch_index}}_{{vb_index}} {
32+
static void addElements(Map<Integer, String> elements) {
33+
{{#each vals as |val|}}
34+
elements.put({{val.value}}, "{{val.name}}");
35+
{{/each}}
36+
}
37+
}
38+
{{/each}}
39+
{{/if enum.has_multiple_value_batches?}}
40+
{{/each}}
41+
42+
class {{service:javaCapitalName}}ThriftMetadataHandlerEnums_{{batch_index}} {
43+
static void addEnums(Map<String, ThriftEnum> enums) {
44+
{{#each enums as |enum|}}
45+
{
46+
Map<Integer, String> elements = new HashMap<>();
47+
{{#if enum.has_multiple_value_batches?}}
48+
{{#each (array.enumerate enum.value_batches) as |vb_index vals|}}
49+
{{service:javaCapitalName}}ThriftMetadataHandlerEnums_{{batch_index}}_{{vb_index}}.addElements(elements);
50+
{{/each}}
51+
{{#else}}
52+
{{#each enum.values as |val|}}
53+
elements.put({{val.value}}, "{{val.name}}");
54+
{{/each}}
55+
{{/if enum.has_multiple_value_batches?}}
56+
enums.put("{{enum.java_qualified_name}}", new ThriftEnum.Builder()
57+
.setName("{{enum.java_qualified_name}}")
58+
.setElements(elements)
59+
.build());
60+
}
61+
{{/each}}
62+
}
63+
}
64+
{{/each}}
65+
66+
class {{service:javaCapitalName}}ThriftMetadataHandlerEnums {
67+
static Map<String, ThriftEnum> getEnums() {
68+
Map<String, ThriftEnum> enums = new HashMap<>();
69+
{{#each (array.enumerate service:referenced_enum_batches) as |batch_index enums|}}
70+
{{service:javaCapitalName}}ThriftMetadataHandlerEnums_{{batch_index}}.addEnums(enums);
71+
{{/each}}
72+
return enums;
73+
}
74+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{{!
2+
3+
Copyright (c) Meta Platforms, Inc. and affiliates.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
}}
18+
{{#import "common/types" as types}}
19+
{{#import "common/metadata" as metadata}}
20+
{{> AutoGenerated}}
21+
22+
package {{service:program.javaPackage}};
23+
24+
import java.util.Arrays;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import com.facebook.thrift.metadata.ThriftEnumType;
28+
import com.facebook.thrift.metadata.ThriftException;
29+
import com.facebook.thrift.metadata.ThriftField;
30+
import com.facebook.thrift.metadata.ThriftListType;
31+
import com.facebook.thrift.metadata.ThriftMapType;
32+
import com.facebook.thrift.metadata.ThriftPrimitiveType;
33+
import com.facebook.thrift.metadata.ThriftSetType;
34+
import com.facebook.thrift.metadata.ThriftStructType;
35+
import com.facebook.thrift.metadata.ThriftType;
36+
37+
{{#each (array.enumerate service:referenced_exception_batches) as |batch_index exceptions|}}
38+
class {{service:javaCapitalName}}ThriftMetadataHandlerExceptions_{{batch_index}} {
39+
static void addExceptions(Map<String, ThriftException> exceptions) {
40+
{{#each exceptions as |exception|}}
41+
exceptions.put("{{exception.java_qualified_name}}", new ThriftException.Builder()
42+
.setName("{{exception.java_qualified_name}}")
43+
.setFields(Arrays.asList(
44+
{{#each (array.enumerate exception.fields with_last=true) as |_i field is_last|}}
45+
new ThriftField.Builder()
46+
.setId({{field.id}})
47+
.setType({{#partial metadata.thrift_type_expression_metadata type=field.type}})
48+
.setName("{{field.name}}")
49+
.setIsOptional({{#if field.optional?}}true{{#else}}false{{/if field.optional?}})
50+
.build(){{#if (not is_last)}},{{/if}}
51+
{{/each}}
52+
))
53+
.build());
54+
{{/each}}
55+
}
56+
}
57+
{{/each}}
58+
59+
class {{service:javaCapitalName}}ThriftMetadataHandlerExceptions {
60+
static Map<String, ThriftException> getExceptions() {
61+
Map<String, ThriftException> exceptions = new HashMap<>();
62+
{{#each (array.enumerate service:referenced_exception_batches) as |batch_index exceptions|}}
63+
{{service:javaCapitalName}}ThriftMetadataHandlerExceptions_{{batch_index}}.addExceptions(exceptions);
64+
{{/each}}
65+
return exceptions;
66+
}
67+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{{!
2+
3+
Copyright (c) Meta Platforms, Inc. and affiliates.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
}}
18+
{{#import "common/types" as types}}
19+
{{#import "common/metadata" as metadata}}
20+
{{> AutoGenerated}}
21+
22+
package {{service:program.javaPackage}};
23+
24+
import java.util.Arrays;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import com.facebook.thrift.metadata.ThriftEnumType;
28+
import com.facebook.thrift.metadata.ThriftField;
29+
import com.facebook.thrift.metadata.ThriftListType;
30+
import com.facebook.thrift.metadata.ThriftMapType;
31+
import com.facebook.thrift.metadata.ThriftPrimitiveType;
32+
import com.facebook.thrift.metadata.ThriftSetType;
33+
import com.facebook.thrift.metadata.ThriftStruct;
34+
import com.facebook.thrift.metadata.ThriftStructType;
35+
import com.facebook.thrift.metadata.ThriftType;
36+
37+
{{#each (array.enumerate service:referenced_struct_batches) as |batch_index definitions|}}
38+
class {{service:javaCapitalName}}ThriftMetadataHandlerStructs_{{batch_index}} {
39+
static void addStructs(Map<String, ThriftStruct> structs) {
40+
{{#each definitions as |struct|}}
41+
structs.put("{{struct.java_qualified_name}}", new ThriftStruct.Builder()
42+
.setName("{{struct.java_qualified_name}}")
43+
.setFields(Arrays.asList(
44+
{{#if struct.union?}}
45+
new ThriftField.Builder()
46+
.setId(-32768)
47+
.setType(ThriftType.fromTPrimitive(ThriftPrimitiveType.THRIFT_I16_TYPE))
48+
.setName("_union_id")
49+
.setIsOptional(false)
50+
.build(){{#if struct.fields?}},{{/if struct.fields?}}
51+
{{/if struct.union?}}
52+
{{#each (array.enumerate struct.fields with_last=true) as |_i field is_last|}}
53+
new ThriftField.Builder()
54+
.setId({{field.id}})
55+
.setType({{#partial metadata.thrift_type_expression_metadata type=field.type}})
56+
.setName("{{field.name}}")
57+
.setIsOptional({{#if field.optional?}}true{{#else}}false{{/if field.optional?}})
58+
.build(){{#if (not is_last)}},{{/if}}
59+
{{/each}}
60+
))
61+
.setIsUnion(false)
62+
.build());
63+
{{/each}}
64+
}
65+
}
66+
{{/each}}
67+
68+
class {{service:javaCapitalName}}ThriftMetadataHandlerStructs {
69+
static Map<String, ThriftStruct> getStructs() {
70+
Map<String, ThriftStruct> structs = new HashMap<>();
71+
{{#each (array.enumerate service:referenced_struct_batches) as |batch_index _definitions|}}
72+
{{service:javaCapitalName}}ThriftMetadataHandlerStructs_{{batch_index}}.addStructs(structs);
73+
{{/each}}
74+
return structs;
75+
}
76+
}

third-party/thrift/src/thrift/compiler/test/fixtures/adapter/out/java/gen-java/test/fixtures/adapter/AdapterServiceThriftMetadataHandler.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
package test.fixtures.adapter;
99

1010
import java.util.ArrayList;
11-
import java.util.Arrays;
12-
import java.util.Collections;
13-
import java.util.HashMap;
1411
import java.util.List;
1512
import java.util.Map;
1613
import com.facebook.thrift.metadata.ThriftEnum;
@@ -65,9 +62,6 @@ private static void addFunctions_0(List<ThriftFunction> functions) {
6562
}
6663

6764
private static final ThriftService THRIFT_SERVICE;
68-
private static final Map<String, ThriftEnum> ENUMS;
69-
private static final Map<String, ThriftStruct> STRUCTS;
70-
private static final Map<String, ThriftException> EXCEPTIONS;
7165

7266

7367
static {
@@ -77,15 +71,6 @@ private static void addFunctions_0(List<ThriftFunction> functions) {
7771
.setName("test.fixtures.adapter.AdapterService")
7872
.setFunctions(functions)
7973
.build();
80-
81-
Map<String, ThriftEnum> enums = new HashMap<>();
82-
ENUMS = Collections.unmodifiableMap(enums);
83-
84-
Map<String, ThriftStruct> structs = new HashMap<>();
85-
STRUCTS = Collections.unmodifiableMap(structs);
86-
87-
Map<String, ThriftException> exceptions = new HashMap<>();
88-
EXCEPTIONS = Collections.unmodifiableMap(exceptions);
8974
}
9075

9176
@Override
@@ -115,16 +100,16 @@ public com.facebook.thrift.server.ThriftMetadataHandler getParentHandler() {
115100

116101
@Override
117102
public Map<String, ThriftEnum> getEnums() {
118-
return ENUMS;
103+
return AdapterServiceThriftMetadataHandlerEnums.getEnums();
119104
}
120105

121106
@Override
122107
public Map<String, ThriftStruct> getStructs() {
123-
return STRUCTS;
108+
return AdapterServiceThriftMetadataHandlerStructs.getStructs();
124109
}
125110

126111
@Override
127112
public Map<String, ThriftException> getExceptions() {
128-
return EXCEPTIONS;
113+
return AdapterServiceThriftMetadataHandlerExceptions.getExceptions();
129114
}
130115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Autogenerated by Thrift
3+
*
4+
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5+
* @generated
6+
*/
7+
8+
package test.fixtures.adapter;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
import com.facebook.thrift.metadata.ThriftEnum;
13+
14+
15+
class AdapterServiceThriftMetadataHandlerEnums {
16+
static Map<String, ThriftEnum> getEnums() {
17+
Map<String, ThriftEnum> enums = new HashMap<>();
18+
return enums;
19+
}
20+
}

0 commit comments

Comments
 (0)