@@ -308,7 +308,7 @@ def get_underlying_type(data_type, allow_lists=True):
308308 return data_type
309309
310310
311- def union_factory_create_method_name (data_type , value_fields_subset ):
311+ def union_create_with_method_name (data_type , value_fields_subset ):
312312 if len (value_fields_subset ) > 0 :
313313 method_suffix = 'And%s' % _capwords (value_fields_subset [0 ].name )
314314 else :
@@ -601,6 +601,8 @@ def __lt__(self, other):
601601 'If this argument is specified, an update Javadoc references file ' +
602602 'will be saved to the given location. It is OK if this file does not ' +
603603 'exist.' )
604+ _CMDLINE_PARSER .add_argument ('--unused-classes-to-generate' , default = None , help = 'Specify types ' +
605+ 'that we want to generate regardless of whether they are used.' )
604606
605607class JavaCodeGenerator (CodeGenerator ):
606608 cmdline_parser = _CMDLINE_PARSER
@@ -2033,6 +2035,9 @@ def update_serializer_visibility(self, data_type_fq_name, visibility):
20332035 self ._serializer_visibility [data_type ], visibility
20342036 )
20352037
2038+ def mark_data_type_as_used (self , data_type ):
2039+ self ._client_data_types .add (data_type )
2040+
20362041
20372042class JavaReferences (object ):
20382043
@@ -2372,6 +2377,9 @@ def generate_all(self):
23722377
23732378 self .generate_client ()
23742379
2380+ # some classes are unused, but we still want them to be generated
2381+ self ._mark_special_unused_classes ()
2382+
23752383 for namespace in self .api .namespaces .values ():
23762384 self .generate_namespace (namespace )
23772385
@@ -2467,6 +2475,55 @@ def generate_namespace(self, namespace):
24672475 if namespace .routes :
24682476 self .generate_namespace_routes (namespace )
24692477
2478+ def _mark_special_unused_classes (self ):
2479+ j = self .j
2480+
2481+ if not self .g .args .unused_classes_to_generate :
2482+ return
2483+
2484+ special_class_names = self .g .args .unused_classes_to_generate .split (', ' )
2485+
2486+ if not special_class_names :
2487+ return
2488+
2489+ special_data_types = [
2490+ unwrap_nullable (data_type )[0 ]
2491+ for namespace in j .stone_api .namespaces .values ()
2492+ for data_type in namespace .data_types
2493+ if data_type .name in special_class_names
2494+ ]
2495+
2496+ all_special_data_types = set ()
2497+
2498+ # mark all special types public and used, and likewise mark all of their
2499+ # referenced types as public and used
2500+
2501+ def _propagate_changes (data_type ):
2502+ all_special_data_types .add (data_type )
2503+
2504+ if is_void_type (data_type ) or not is_user_defined_type (data_type ):
2505+ return
2506+
2507+ field_types = [unwrap_nullable (f .data_type )[0 ] for f in data_type .all_fields ]
2508+ for field_type in field_types :
2509+ if field_type not in all_special_data_types :
2510+ _propagate_changes (field_type )
2511+ if data_type .parent_type :
2512+ if data_type .parent_type not in all_special_data_types :
2513+ _propagate_changes (data_type .parent_type )
2514+
2515+ for data_type in special_data_types :
2516+ _propagate_changes (data_type )
2517+
2518+ for data_type in all_special_data_types :
2519+ if is_user_defined_type (data_type ) and not is_void_type (data_type ):
2520+ data_type_fq_name = j .stone_fq_name (data_type )
2521+
2522+ # mark public
2523+ j .update_data_type_visibility (data_type_fq_name , Visibility .PUBLIC )
2524+ # mark as being referenced somewhere so that we generate
2525+ j .mark_data_type_as_used (data_type )
2526+
24702527 def generate_namespace_routes (self , namespace ):
24712528 assert isinstance (namespace , ApiNamespace ), repr (namespace )
24722529
@@ -2858,15 +2915,15 @@ def generate_route_upload_call(self, route, arg_var):
28582915 JavaClass ('com.dropbox.core.http.HttpRequestor.Uploader' )),
28592916 after = ';' ,
28602917 )
2861- w .out ('return new %s(_uploader);' , j .route_uploader_class (route ))
2918+ w .out ('return new %s(_uploader, this.client.getUserId() );' , j .route_uploader_class (route ))
28622919
28632920 def generate_data_type (self , data_type ):
28642921 """Generate a class definition for a datatype (a struct or a union)."""
28652922 assert is_user_defined_type (data_type ), repr (data_type )
28662923
28672924 j = self .j
28682925
2869- if not ( self .g .args .data_types_only or j .is_used_by_client (data_type ) ):
2926+ if not self .g .args .data_types_only and not j .is_used_by_client (data_type ):
28702927 return
28712928
28722929 with self .class_writer (data_type ) as w :
@@ -2976,8 +3033,8 @@ def generate_data_type_union(self, data_type):
29763033 for field in static_fields :
29773034 singleton_args = ', ' .join (["Tag.%s" % j .field_tag_enum_name (field )])
29783035 w .javadoc (field )
2979- method_name = union_factory_create_method_name (data_type , [])
2980- w .out ('public static final %s %s = %s .%s(%s);' ,
3036+ method_name = union_create_with_method_name (data_type , [])
3037+ w .out ('public static final %s %s = new %s() .%s(%s);' ,
29813038 j .java_class (data_type ),
29823039 j .field_static_instance (field ),
29833040 j .java_class (data_type ),
@@ -2997,7 +3054,14 @@ def generate_data_type_union(self, data_type):
29973054 #
29983055 # Constructors
29993056 #
3000- def _gen_factory_method (data_type , value_fields_subset ):
3057+
3058+ w .out ('' )
3059+ w .javadoc ('Private default constructor, so that object is uninitializable publicly.' )
3060+ with w .block ('private %s()' , j .java_class (data_type )):
3061+ pass
3062+ w .out ('' )
3063+
3064+ def _gen_create_with_method (data_type , value_fields_subset ):
30013065 w .out ('' )
30023066 w .javadoc (data_type ,
30033067 fields = value_fields_subset ,
@@ -3009,18 +3073,18 @@ def _gen_factory_method(data_type, value_fields_subset):
30093073 for f in value_fields_subset
30103074 ],
30113075 ))
3012- method_name = union_factory_create_method_name (data_type , value_fields_subset )
3013- with w .block ('private static %s %s(%s)' , j .java_class (data_type ), method_name , formatted_args ):
3014- w .out ('final %s result = new %s();' , j .java_class (data_type ), j .java_class (data_type ))
3076+ method_name = union_create_with_method_name (data_type , value_fields_subset )
3077+ with w .block ('private %s %s(%s)' , j .java_class (data_type ), method_name , formatted_args ):
3078+ w .out ('%s result = new %s();' , j .java_class (data_type ), j .java_class (data_type ))
30153079 w .out ('result._tag = _tag;' )
30163080 for field in value_fields_subset :
30173081 # don't perform validation in the private constructor
30183082 w .out ('result.%s = %s;' , j .param_name (field ), j .param_name (field ))
30193083 w .out ('return result;' )
30203084
3021- _gen_factory_method (data_type , [])
3085+ _gen_create_with_method (data_type , [])
30223086 for f in value_fields :
3023- _gen_factory_method (data_type , [f ])
3087+ _gen_create_with_method (data_type , [f ])
30243088
30253089 #
30263090 # Field getters/constructors
@@ -3120,8 +3184,8 @@ def generate_data_type_union_field_methods(self, data_type):
31203184 j .java_class (field ),
31213185 ):
31223186 self .generate_field_validation (field , value_name = "value" , omit_arg_name = True , allow_default = False )
3123- method_name = union_factory_create_method_name (data_type , [field ])
3124- w .out ('return %s .%s(Tag.%s, %s);' ,
3187+ method_name = union_create_with_method_name (data_type , [field ])
3188+ w .out ('return new %s() .%s(Tag.%s, %s);' ,
31253189 j .java_class (data_type ),
31263190 method_name ,
31273191 j .field_tag_enum_name (field ),
@@ -3502,8 +3566,8 @@ def generate_route_uploader(self, route):
35023566 params = (('httpUploader' , 'Initiated HTTP upload request' ),),
35033567 throws = (('NullPointerException' , 'if {@code httpUploader} is {@code null}' ),)
35043568 )
3505- with w .block ('public %s(HttpRequestor.Uploader httpUploader)' , j .route_uploader_class (route )):
3506- w .out ('super(httpUploader, %s, %s);' ,
3569+ with w .block ('public %s(HttpRequestor.Uploader httpUploader, String userId )' , j .route_uploader_class (route )):
3570+ w .out ('super(httpUploader, %s, %s, userId );' ,
35073571 w .java_serializer (route .result_data_type ),
35083572 w .java_serializer (route .error_data_type ))
35093573
0 commit comments