21
21
import java .io .File ;
22
22
import java .util .ArrayList ;
23
23
import java .util .List ;
24
+ import java .util .Optional ;
24
25
import javax .annotation .CheckForNull ;
25
26
import javax .annotation .Nullable ;
26
27
import org .sonar .api .SonarProduct ;
@@ -46,78 +47,32 @@ public class PythonVisitorContext extends PythonInputFileContext {
46
47
private final List <PreciseIssue > issues ;
47
48
private final ProjectConfiguration projectConfiguration ;
48
49
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 ) {
87
58
88
- public PythonVisitorContext (FileInput rootTree , PythonFile pythonFile , @ Nullable File workingDirectory , String packageName ,
89
- ProjectLevelSymbolTable projectLevelSymbolTable , CacheContext cacheContext , SonarProduct sonarProduct ) {
90
59
super (pythonFile , workingDirectory , cacheContext , sonarProduct , projectLevelSymbolTable );
91
60
var symbolTableBuilderV2 = new SymbolTableBuilderV2 (rootTree );
92
61
var symbolTable = symbolTableBuilderV2 .build ();
93
62
buildSymbols (rootTree , pythonFile , packageName , projectLevelSymbolTable );
94
63
var projectLevelTypeTable = new ProjectLevelTypeTable (projectLevelSymbolTable );
95
64
this .moduleType = new TypeInferenceV2 (projectLevelTypeTable , pythonFile , symbolTable , packageName ).inferModuleType (rootTree );
96
65
this .typeChecker = new TypeChecker (projectLevelTypeTable );
97
- this .projectConfiguration = new ProjectConfiguration () ;
66
+ this .projectConfiguration = projectConfiguration ;
98
67
this .rootTree = rootTree ;
99
68
this .parsingException = null ;
100
69
this .issues = new ArrayList <>();
101
70
}
102
-
103
- private static synchronized void buildSymbols (FileInput rootTree , PythonFile pythonFile , String packageName ) {
104
- buildSymbols (rootTree , pythonFile , packageName , ProjectLevelSymbolTable .empty ());
105
- }
106
-
107
71
private static synchronized void buildSymbols (FileInput rootTree , PythonFile pythonFile , String packageName , ProjectLevelSymbolTable projectLevelSymbolTable ) {
108
72
var symbolTableBuilder = new SymbolTableBuilder (packageName , pythonFile , projectLevelSymbolTable );
109
73
symbolTableBuilder .visitFileInput (rootTree );
110
74
}
111
75
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
-
121
76
public PythonVisitorContext (PythonFile pythonFile , RecognitionException parsingException , SonarProduct sonarProduct ) {
122
77
super (pythonFile , null , CacheContextImpl .dummyCache (), sonarProduct , ProjectLevelSymbolTable .empty ());
123
78
this .rootTree = null ;
@@ -156,4 +111,64 @@ public ModuleType moduleType() {
156
111
public ProjectConfiguration projectConfiguration () {
157
112
return projectConfiguration ;
158
113
}
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
+ }
159
174
}
0 commit comments