23
23
import java .util .List ;
24
24
import java .util .Set ;
25
25
import java .util .concurrent .CompletableFuture ;
26
- import org .eclipse . lsp4j . jsonrpc . validation .NonNull ;
26
+ import org .netbeans . api . annotations . common .NonNull ;
27
27
import org .netbeans .api .java .classpath .ClassPath ;
28
28
import org .netbeans .api .java .platform .JavaPlatform ;
29
29
import org .netbeans .api .java .platform .JavaPlatformManager ;
30
30
import org .netbeans .api .java .platform .Specification ;
31
31
import org .netbeans .api .java .project .JavaProjectConstants ;
32
+ import org .netbeans .api .java .queries .CompilerOptionsQuery ;
32
33
import org .netbeans .api .java .queries .UnitTestForSourceQuery ;
33
34
import org .netbeans .api .project .Project ;
34
35
import org .netbeans .api .project .SourceGroup ;
@@ -50,6 +51,7 @@ public class ProjectConfigurationUtils {
50
51
public final static String MODULE_PATH = "--module-path" ;
51
52
public final static String ADD_MODULES = "--add-modules" ;
52
53
public final static String ADD_EXPORTS = "--add-exports" ;
54
+ public final static String ENABLE_PREVIEW = "--enable-preview" ;
53
55
54
56
public static boolean isNonTestRoot (SourceGroup sg ) {
55
57
return UnitTestForSourceQuery .findSources (sg .getRootFolder ()).length == 0 ;
@@ -123,21 +125,72 @@ private static JavaPlatform findPlatform(ClassPath bootCP) {
123
125
return null ;
124
126
}
125
127
128
+ static boolean isPreviewEnabled (@ NonNull Project project , List <FileObject > sourceRoots ) {
129
+ boolean previewEnabled = isPreviewEnabledForAnyProjectSourceRoot (project , sourceRoots );
130
+ previewEnabled = previewEnabled || isPreviewEnabledForAnyContainedProjects (project );
131
+ return previewEnabled ;
132
+ }
133
+
134
+ private static boolean isPreviewEnabledForAnyContainedProjects (@ NonNull Project project ) {
135
+ Set <Project > subProjects = ProjectUtils .getContainedProjects (project , false );
136
+ if (subProjects != null ) {
137
+ for (Project subProject : subProjects ) {
138
+ if (isPreviewEnabledForAnyProjectSourceRoot (subProject , getNonTestRoots (subProject ))) {
139
+ return true ;
140
+ }
141
+ }
142
+ for (Project subProject : subProjects ) {
143
+ if (isPreviewEnabledForAnyContainedProjects (subProject )) {
144
+ return true ;
145
+ }
146
+ }
147
+ }
148
+ return false ;
149
+ }
150
+
151
+ private static boolean isPreviewEnabledForAnyProjectSourceRoot (@ NonNull Project project , List <FileObject > sourceRoots ) {
152
+ if (sourceRoots == null || sourceRoots .isEmpty ()) {
153
+ FileObject root = project .getProjectDirectory ();
154
+ if (root != null && isPreviewEnabledForSource (root )) {
155
+ return true ;
156
+ }
157
+ } else {
158
+ for (FileObject root : sourceRoots ) {
159
+ if (root != null && isPreviewEnabledForSource (root )) {
160
+ return true ;
161
+ }
162
+ }
163
+ }
164
+ return false ;
165
+ }
166
+
167
+ private static boolean isPreviewEnabledForSource (@ NonNull FileObject source ) {
168
+ CompilerOptionsQuery .Result result = CompilerOptionsQuery .getOptions (source );
169
+ return result .getArguments ().contains (ENABLE_PREVIEW );
170
+ }
171
+
126
172
@ NonNull
127
173
public static List <String > launchVMOptions (Project project ) {
128
174
if (project == null ) {
129
175
return new ArrayList <>();
130
176
}
131
177
boolean isModular = ProjectModulePathConfigurationUtils .isModularProject (project );
132
178
if (isModular ) {
133
- return ProjectModulePathConfigurationUtils .getVmOptions (project );
179
+ List <String > vmOptions = ProjectModulePathConfigurationUtils .getVmOptions (project );
180
+ if (isPreviewEnabled (project , getNonTestRoots (project ))) {
181
+ vmOptions .add (ENABLE_PREVIEW );
182
+ }
183
+ return vmOptions ;
134
184
}
135
185
List <String > vmOptions = new ArrayList <>();
136
186
List <FileObject > roots = getNonTestRoots (project );
137
187
if (!roots .isEmpty ()) {
138
188
ClassPath cp = ClassPath .getClassPath (roots .getFirst (), ClassPath .EXECUTE );
139
189
vmOptions .addAll (Arrays .asList (CLASS_PATH , addRoots ("" , cp )));
140
190
}
191
+ if (isPreviewEnabled (project , roots )) {
192
+ vmOptions .add (ENABLE_PREVIEW );
193
+ }
141
194
return vmOptions ;
142
195
}
143
196
@@ -148,14 +201,21 @@ public static List<String> compilerOptions(Project project) {
148
201
}
149
202
boolean isModular = ProjectModulePathConfigurationUtils .isModularProject (project );
150
203
if (isModular ) {
151
- return ProjectModulePathConfigurationUtils .getCompileOptions (project );
204
+ List <String > compileOptions = ProjectModulePathConfigurationUtils .getCompileOptions (project );
205
+ if (isPreviewEnabled (project , getNonTestRoots (project ))) {
206
+ compileOptions .add (ENABLE_PREVIEW );
207
+ }
208
+ return compileOptions ;
152
209
}
153
210
List <String > compileOptions = new ArrayList <>();
154
211
List <FileObject > roots = getNonTestRoots (project );
155
212
if (!roots .isEmpty ()) {
156
213
ClassPath cp = ClassPath .getClassPath (roots .getFirst (), ClassPath .COMPILE );
157
214
compileOptions .addAll (Arrays .asList (CLASS_PATH , addRoots ("" , cp )));
158
215
}
216
+ if (isPreviewEnabled (project , roots )) {
217
+ compileOptions .add (ENABLE_PREVIEW );
218
+ }
159
219
return compileOptions ;
160
220
}
161
221
0 commit comments