2121import java .io .File ;
2222import java .util .ArrayList ;
2323import java .util .List ;
24+ import java .util .Optional ;
2425import javax .annotation .CheckForNull ;
2526import javax .annotation .Nullable ;
2627import org .sonar .api .SonarProduct ;
@@ -46,78 +47,32 @@ public class PythonVisitorContext extends PythonInputFileContext {
4647 private final List <PreciseIssue > issues ;
4748 private final ProjectConfiguration projectConfiguration ;
4849
49- public PythonVisitorContext (FileInput rootTree , PythonFile pythonFile , @ Nullable File workingDirectory , String packageName ) {
50- this (rootTree , pythonFile , workingDirectory , packageName , new ProjectConfiguration ());
51- }
52-
53- public PythonVisitorContext (FileInput rootTree , PythonFile pythonFile , @ Nullable File workingDirectory , String packageName , ProjectConfiguration projectConfiguration ) {
54- super (pythonFile , workingDirectory , CacheContextImpl .dummyCache (), ProjectLevelSymbolTable .empty ());
55- buildSymbols (rootTree , pythonFile , packageName );
56- var symbolTable = new SymbolTableBuilderV2 (rootTree ).build ();
57- var projectLevelTypeTable = new ProjectLevelTypeTable (ProjectLevelSymbolTable .empty ());
58-
59- this .rootTree = rootTree ;
60- this .parsingException = null ;
61- this .moduleType = new TypeInferenceV2 (projectLevelTypeTable , pythonFile , symbolTable , packageName ).inferModuleType (rootTree );
62- this .typeChecker = new TypeChecker (projectLevelTypeTable );
63- this .projectConfiguration = projectConfiguration ;
64- this .issues = new ArrayList <>();
65- }
66-
67- public PythonVisitorContext (FileInput rootTree , PythonFile pythonFile , @ Nullable File workingDirectory , String packageName ,
68- ProjectLevelSymbolTable projectLevelSymbolTable , CacheContext cacheContext ) {
69- this (rootTree , pythonFile , workingDirectory , packageName , projectLevelSymbolTable , cacheContext , new ProjectConfiguration ());
70- }
71-
72- public PythonVisitorContext (FileInput rootTree , PythonFile pythonFile , @ Nullable File workingDirectory , String packageName ,
73- ProjectLevelSymbolTable projectLevelSymbolTable , CacheContext cacheContext , ProjectConfiguration projectConfiguration ) {
74- super (pythonFile , workingDirectory , cacheContext , projectLevelSymbolTable );
75-
76- buildSymbols (rootTree , pythonFile , packageName , projectLevelSymbolTable );
77- var symbolTable = new SymbolTableBuilderV2 (rootTree ).build ();
78- var projectLevelTypeTable = new ProjectLevelTypeTable (projectLevelSymbolTable );
79-
80- this .rootTree = rootTree ;
81- this .parsingException = null ;
82- this .moduleType = new TypeInferenceV2 (projectLevelTypeTable , pythonFile , symbolTable , packageName ).inferModuleType (rootTree );
83- this .typeChecker = new TypeChecker (projectLevelTypeTable );
84- this .projectConfiguration = projectConfiguration ;
85- this .issues = new ArrayList <>();
86- }
50+ private PythonVisitorContext (FileInput rootTree ,
51+ PythonFile pythonFile ,
52+ @ Nullable File workingDirectory ,
53+ String packageName ,
54+ ProjectLevelSymbolTable projectLevelSymbolTable ,
55+ CacheContext cacheContext ,
56+ SonarProduct sonarProduct ,
57+ ProjectConfiguration projectConfiguration ) {
8758
88- public PythonVisitorContext (FileInput rootTree , PythonFile pythonFile , @ Nullable File workingDirectory , String packageName ,
89- ProjectLevelSymbolTable projectLevelSymbolTable , CacheContext cacheContext , SonarProduct sonarProduct ) {
9059 super (pythonFile , workingDirectory , cacheContext , sonarProduct , projectLevelSymbolTable );
9160 var symbolTableBuilderV2 = new SymbolTableBuilderV2 (rootTree );
9261 var symbolTable = symbolTableBuilderV2 .build ();
9362 buildSymbols (rootTree , pythonFile , packageName , projectLevelSymbolTable );
9463 var projectLevelTypeTable = new ProjectLevelTypeTable (projectLevelSymbolTable );
9564 this .moduleType = new TypeInferenceV2 (projectLevelTypeTable , pythonFile , symbolTable , packageName ).inferModuleType (rootTree );
9665 this .typeChecker = new TypeChecker (projectLevelTypeTable );
97- this .projectConfiguration = new ProjectConfiguration () ;
66+ this .projectConfiguration = projectConfiguration ;
9867 this .rootTree = rootTree ;
9968 this .parsingException = null ;
10069 this .issues = new ArrayList <>();
10170 }
102-
103- private static synchronized void buildSymbols (FileInput rootTree , PythonFile pythonFile , String packageName ) {
104- buildSymbols (rootTree , pythonFile , packageName , ProjectLevelSymbolTable .empty ());
105- }
106-
10771 private static synchronized void buildSymbols (FileInput rootTree , PythonFile pythonFile , String packageName , ProjectLevelSymbolTable projectLevelSymbolTable ) {
10872 var symbolTableBuilder = new SymbolTableBuilder (packageName , pythonFile , projectLevelSymbolTable );
10973 symbolTableBuilder .visitFileInput (rootTree );
11074 }
11175
112- public PythonVisitorContext (PythonFile pythonFile , RecognitionException parsingException ) {
113- super (pythonFile , null , CacheContextImpl .dummyCache (), ProjectLevelSymbolTable .empty ());
114- this .rootTree = null ;
115- this .parsingException = parsingException ;
116- this .typeChecker = new TypeChecker (new ProjectLevelTypeTable (ProjectLevelSymbolTable .empty ()));
117- this .projectConfiguration = new ProjectConfiguration ();
118- this .issues = new ArrayList <>();
119- }
120-
12176 public PythonVisitorContext (PythonFile pythonFile , RecognitionException parsingException , SonarProduct sonarProduct ) {
12277 super (pythonFile , null , CacheContextImpl .dummyCache (), sonarProduct , ProjectLevelSymbolTable .empty ());
12378 this .rootTree = null ;
@@ -156,4 +111,64 @@ public ModuleType moduleType() {
156111 public ProjectConfiguration projectConfiguration () {
157112 return projectConfiguration ;
158113 }
114+
115+ public static class Builder {
116+ private final PythonFile pythonFile ;
117+ private final FileInput rootTree ;
118+
119+ private Optional <ProjectLevelSymbolTable > projectLevelSymbolTable = Optional .empty ();
120+ private Optional <CacheContext > cacheContext = Optional .empty ();
121+ private Optional <SonarProduct > sonarProduct = Optional .empty ();
122+ private Optional <File > workingDirectory = Optional .empty ();
123+ private Optional <ProjectConfiguration > projectConfiguration = Optional .empty ();
124+ private Optional <String > packageName = Optional .empty ();
125+
126+ public Builder (FileInput rootTree , PythonFile pythonFile ) {
127+ this .rootTree = rootTree ;
128+ this .pythonFile = pythonFile ;
129+ }
130+
131+ public Builder workingDirectory (@ Nullable File workingDirectory ) {
132+ this .workingDirectory = Optional .ofNullable (workingDirectory );
133+ return this ;
134+ }
135+
136+ public Builder packageName (String packageName ) {
137+ this .packageName = Optional .ofNullable (packageName );
138+ return this ;
139+ }
140+
141+ public Builder projectLevelSymbolTable (ProjectLevelSymbolTable projectLevelSymbolTable ) {
142+ this .projectLevelSymbolTable = Optional .ofNullable (projectLevelSymbolTable );
143+ return this ;
144+ }
145+
146+ public Builder cacheContext (CacheContext cacheContext ) {
147+ this .cacheContext = Optional .ofNullable (cacheContext );
148+ return this ;
149+ }
150+
151+ public Builder sonarProduct (SonarProduct sonarProduct ) {
152+ this .sonarProduct = Optional .ofNullable (sonarProduct );
153+ return this ;
154+ }
155+
156+ public Builder projectConfiguration (ProjectConfiguration projectConfiguration ) {
157+ this .projectConfiguration = Optional .ofNullable (projectConfiguration );
158+ return this ;
159+ }
160+
161+ public PythonVisitorContext build () {
162+ return new PythonVisitorContext (
163+ rootTree ,
164+ pythonFile ,
165+ workingDirectory .orElse (null ),
166+ packageName .orElse ("" ),
167+ projectLevelSymbolTable .orElseGet (ProjectLevelSymbolTable ::empty ),
168+ cacheContext .orElseGet (CacheContextImpl ::dummyCache ),
169+ sonarProduct .orElse (SonarProduct .SONARQUBE ),
170+ projectConfiguration .orElse (new ProjectConfiguration ())
171+ );
172+ }
173+ }
159174}
0 commit comments