@@ -853,7 +853,7 @@ public static string CreatePackageDefForBaseMethodRequestBuilder(this CustomT4Ho
853853 {
854854 var sb = new StringBuilder ( ) ;
855855 sb . Append ( host . CreatePackageDefinition ( ) ) ;
856- var importFormat = @"import {0}.{1}.{2};" ;
856+ const string importFormat = @"import {0}.{1}.{2};" ;
857857 sb . AppendFormat ( importFormat ,
858858 host . CurrentNamespace ( ) ,
859859 GetPrefixForRequests ( ) ,
@@ -866,21 +866,24 @@ public static string CreatePackageDefForBaseMethodRequestBuilder(this CustomT4Ho
866866 host . CurrentType . TypeRequest ( ) ) ;
867867 sb . Append ( "\n " ) ;
868868
869- foreach ( var method in host . CurrentType . AsOdcmMethod ( ) . WithOverloads ( ) )
870- {
871- sb = ImportClassesOfMethodParameters ( method , importFormat , sb ) ;
872- }
869+ var imports = host . CurrentType . AsOdcmMethod ( ) . WithOverloads ( ) . SelectMany ( x => ImportClassesOfMethodParameters ( x ) ) ;
870+ sb . Append ( imports . Any ( ) ? imports . Aggregate ( ( x , y ) => $ "{ x } { Environment . NewLine } { y } ") : string . Empty ) ;
873871 return sb . ToString ( ) ;
874872 }
875873
874+ public static string ImportClassesOfMethodParametersAsString ( OdcmMethod method , string importFormat = "import {0}.{1}.{2};" , string importTypeToExclude = null )
875+ {
876+ var imports = ImportClassesOfMethodParameters ( method , importFormat , importTypeToExclude ) ;
877+ return imports . Any ( ) ? imports . Aggregate ( ( x , y ) => $ "{ x } { Environment . NewLine } { y } ") : string . Empty ;
878+ }
879+
876880 /// <summary>
877881 /// Appends import statements of types that appear as a method's parameters
878882 /// </summary>
879883 /// <param name="method">Method whose parameter types will be consumed</param>
880884 /// <param name="importFormat">import format, e.g. "import {0}.{1}.{2}"</param>
881- /// <param name="sb">StringBuilder object currently in use</param>
882- /// <returns>StringBuilder object with import statements inserted</returns>
883- public static StringBuilder ImportClassesOfMethodParameters ( OdcmMethod method , string importFormat , StringBuilder sb , string importTypeToExclude = null )
885+ /// <returns>Hashset with import statements to insert</returns>
886+ public static HashSet < string > ImportClassesOfMethodParameters ( OdcmMethod method , string importFormat = "import {0}.{1}.{2};" , string importTypeToExclude = null )
884887 {
885888 var importStatements = new HashSet < string > ( ) ;
886889 var appendEnumSet = false ;
@@ -899,16 +902,14 @@ public static StringBuilder ImportClassesOfMethodParameters(OdcmMethod method, s
899902 }
900903 }
901904 if ( ! ( method ? . ReturnType == null || method . ReturnType is OdcmPrimitiveType ) )
902- sb . AppendFormat ( importFormat , method . ReturnType . Namespace . Name . AddPrefix ( ) , method . ReturnType . GetPackagePrefix ( ) , method . OdcmMethodReturnType ( ) ) ;
903-
904- sb . Append ( string . Join ( string . Empty , importStatements ) ) ;
905+ importStatements . Add ( string . Format ( importFormat , method . ReturnType . Namespace . Name . AddPrefix ( ) , method . ReturnType . GetPackagePrefix ( ) , method . OdcmMethodReturnType ( ) ) ) ;
905906
906907 if ( appendEnumSet )
907908 {
908- sb . Append ( "import java.util.EnumSet;" + Environment . NewLine ) ;
909+ importStatements . Add ( "import java.util.EnumSet;" ) ;
909910 }
910911
911- return sb ;
912+ return importStatements ;
912913 }
913914
914915 public static string CreatePackageDefForIBaseMethodBodyRequest ( this CustomT4Host host )
@@ -1184,29 +1185,29 @@ public static string CreatePackageDef(this CustomT4Host host)
11841185
11851186 // determine current namespace and generate method imports if applicable
11861187 string @namespace ;
1187- var methodImports = new StringBuilder ( ) ;
1188- var importFormat = "import {0}.{1}.{2};" + Environment . NewLine ;
1188+ var methodImports = new HashSet < string > ( ) ;
1189+ const string importFormat = "import {0}.{1}.{2};" ;
11891190 const string graphServiceEntityName = "GraphService" ;
11901191 const string interfaceTemplatePrefix = "I" ;
11911192 switch ( host . CurrentType )
11921193 {
11931194 case OdcmProperty p :
11941195 @namespace = p . Type . Namespace . Name . AddPrefix ( ) ;
11951196 if ( p . Class . GetTypeString ( ) != graphServiceEntityName )
1196- methodImports . AppendFormat ( importFormat , p . Class . Namespace . Name . AddPrefix ( ) , p . Class . GetPackagePrefix ( ) , p . Class . GetTypeString ( ) ) ;
1197+ methodImports . Add ( string . Format ( importFormat , p . Class . Namespace . Name . AddPrefix ( ) , p . Class . GetPackagePrefix ( ) , p . Class . GetTypeString ( ) ) ) ;
11971198 if ( ! ( p . Projection . Type is OdcmPrimitiveType ) )
1198- methodImports . AppendFormat ( importFormat , p . Projection . Type . Namespace . Name . AddPrefix ( ) , p . Projection . Type . GetPackagePrefix ( ) , p . Projection . Type . GetTypeString ( ) ) ;
1199- p . Projection ? . Type ? . AsOdcmClass ( ) ? . MethodsAndOverloads ( ) ? . Distinct ( ) ? . ToList ( ) ? . ForEach ( o => ImportClassesOfMethodParameters ( o , importFormat , methodImports ) ) ;
1199+ methodImports . Add ( string . Format ( importFormat , p . Projection . Type . Namespace . Name . AddPrefix ( ) , p . Projection . Type . GetPackagePrefix ( ) , p . Projection . Type . GetTypeString ( ) ) ) ;
1200+ p . Projection ? . Type ? . AsOdcmClass ( ) ? . MethodsAndOverloads ( ) ? . Distinct ( ) ? . SelectMany ( o => ImportClassesOfMethodParameters ( o ) ) ? . ToList ( ) ? . ForEach ( x => methodImports . Add ( x ) ) ;
12001201 break ;
12011202 case OdcmMethod m :
1202- m . WithDistinctOverloads ( ) . ForEach ( o => ImportClassesOfMethodParameters ( o , importFormat , methodImports ) ) ;
1203+ m . WithDistinctOverloads ( ) . SelectMany ( o => ImportClassesOfMethodParameters ( o ) ) ? . ToList ( ) ? . ForEach ( x => methodImports . Add ( x ) ) ;
12031204 goto default ;
12041205 case OdcmClass c :
12051206 if ( c . GetTypeString ( ) != graphServiceEntityName )
1206- methodImports . AppendFormat ( importFormat , c . Namespace . Name . AddPrefix ( ) , c . GetPackagePrefix ( ) , c . GetTypeString ( ) ) ;
1207+ methodImports . Add ( string . Format ( importFormat , c . Namespace . Name . AddPrefix ( ) , c . GetPackagePrefix ( ) , c . GetTypeString ( ) ) ) ;
12071208
12081209 var importTypeToExclude = host . TemplateFile . EndsWith ( "BaseEntityRequest.java.tt" ) ? host . TemplateName : string . Empty ;
1209- c ? . MethodsAndOverloads ( ) ? . Distinct ( ) ? . ToList ( ) ? . ForEach ( o => ImportClassesOfMethodParameters ( o , importFormat , methodImports , importTypeToExclude ) ) ;
1210+ c ? . MethodsAndOverloads ( ) ? . Distinct ( ) ? . SelectMany ( o => ImportClassesOfMethodParameters ( o , importTypeToExclude : importTypeToExclude ) ) ? . ToList ( ) ? . ForEach ( x => methodImports . Add ( x ) ) ;
12101211 c ? . NavigationProperties ( ) ? . Where ( x => x . IsCollection ) ? . Select ( x => x . Projection . Type ) ? . Distinct ( ) ? . ToList ( ) ? . ForEach ( x =>
12111212 ImportRequestBuilderTypes ( host , x , methodImports , importFormat , interfaceTemplatePrefix , true )
12121213 ) ;
@@ -1224,18 +1225,18 @@ public static string CreatePackageDef(this CustomT4Host host)
12241225 host . TemplateInfo . OutputParentDirectory . Replace ( "_" , "." ) ,
12251226 host . CurrentModel . GetNamespace ( ) . AddPrefix ( ) ,
12261227 fullyQualifiedImport ,
1227- methodImports . ToString ( ) ) ;
1228+ methodImports . Any ( ) ? methodImports . Aggregate ( ( x , y ) => $ " { x } { Environment . NewLine } { y } " ) : string . Empty ) ;
12281229 }
1229- private static void ImportRequestBuilderTypes ( CustomT4Host host , OdcmType x , StringBuilder methodImports , string importFormat , string interfaceTemplatePrefix , bool includeCollectionTypes )
1230+ private static void ImportRequestBuilderTypes ( CustomT4Host host , OdcmType x , HashSet < string > methodImports , string importFormat , string interfaceTemplatePrefix , bool includeCollectionTypes )
12301231 {
12311232 if ( includeCollectionTypes )
1232- methodImports . AppendFormat ( importFormat , x . Namespace . Name . AddPrefix ( ) , GetPrefixForRequests ( ) , x . ITypeCollectionRequestBuilder ( ) ) ;
1233- methodImports . AppendFormat ( importFormat , x . Namespace . Name . AddPrefix ( ) , GetPrefixForRequests ( ) , x . ITypeRequestBuilder ( ) ) ;
1233+ methodImports . Add ( string . Format ( importFormat , x . Namespace . Name . AddPrefix ( ) , GetPrefixForRequests ( ) , x . ITypeCollectionRequestBuilder ( ) ) ) ;
1234+ methodImports . Add ( string . Format ( importFormat , x . Namespace . Name . AddPrefix ( ) , GetPrefixForRequests ( ) , x . ITypeRequestBuilder ( ) ) ) ;
12341235 if ( ! host . TemplateInfo . TemplateName . StartsWith ( interfaceTemplatePrefix ) )
12351236 {
12361237 if ( includeCollectionTypes )
1237- methodImports . AppendFormat ( importFormat , x . Namespace . Name . AddPrefix ( ) , GetPrefixForRequests ( ) , x . TypeCollectionRequestBuilder ( ) ) ;
1238- methodImports . AppendFormat ( importFormat , x . Namespace . Name . AddPrefix ( ) , GetPrefixForRequests ( ) , x . TypeRequestBuilder ( ) ) ;
1238+ methodImports . Add ( string . Format ( importFormat , x . Namespace . Name . AddPrefix ( ) , GetPrefixForRequests ( ) , x . TypeCollectionRequestBuilder ( ) ) ) ;
1239+ methodImports . Add ( string . Format ( importFormat , x . Namespace . Name . AddPrefix ( ) , GetPrefixForRequests ( ) , x . TypeRequestBuilder ( ) ) ) ;
12391240 }
12401241 }
12411242 /// <summary>
0 commit comments