You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "mbien (via GitHub)" <gi...@apache.org> on 2023/05/02 06:51:52 UTC

[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1182155204


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -49,148 +43,146 @@
 import org.netbeans.modules.maven.model.pom.POMModel;
 import org.netbeans.modules.maven.model.pom.POMQName;
 import org.netbeans.modules.maven.model.pom.Plugin;
+import org.netbeans.modules.maven.model.pom.PluginContainer;
+import org.netbeans.modules.maven.model.pom.Properties;
 import org.netbeans.spi.project.ProjectConfiguration;
 import org.netbeans.spi.project.ProjectConfigurationProvider;
-import org.openide.filesystems.FileSystem;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
  * the fix for Maven type project.
  *
  * @author arusinha
  */
-public class EnablePreviewMavenProj implements ErrorRule<Void> {
+public class EnablePreviewMavenProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",           // NOI18N
-            "compiler.err.preview.feature.disabled.plural",    // NOI18N
-            "compiler.err.is.preview")));                      // NOI18N
     private static final String ENABLE_PREVIEW_FLAG = "--enable-preview";   // NOI18N
 
-    @Override
-    public Set<String> getCodes() {
-        return ERROR_CODES;
+    private final Project prj;
+
+    private EnablePreviewMavenProj(@NonNull final Project prj) {
+        Parameters.notNull("prj", prj); //NOI18N
+        this.prj = prj;
     }
 
     @Override
-    @NonNull
-    public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
-
-        if (SourceVersion.latest() != compilationInfo.getSourceVersion()) {
-            return Collections.<Fix>emptyList();
-        }
-
-        Fix fix = null;
-        final FileObject file = compilationInfo.getFileObject();
-        if (file != null) {
-            final Project prj = FileOwnerQuery.getOwner(file);
-            if (isMavenProject(prj)) {
-                fix = new EnablePreviewMavenProj.ResolveMvnFix(prj);
-            } else {
-                fix = null;
+    public void enablePreview(String newSourceLevel) throws Exception {
+        final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+        pom.getFileSystem().runAtomicAction(() -> {
+            List<ModelOperation<POMModel>> operations = new ArrayList<>();
+            operations.add(new AddMvnCompilerPluginForEnablePreview(newSourceLevel));
+            org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+        });
+
+        ProjectConfiguration cfg = prj.getLookup().lookup(ProjectConfigurationProvider.class).getActiveConfiguration();
+
+        ActionConfig[] actions = new ActionConfig[] {
+            ActionConfig.runAction("run"), // NOI18N
+            ActionConfig.runAction("debug"), // NOI18N
+            ActionConfig.runAction("profile"), // NOI18N
+            ActionConfig.runAction("run.single.main"), // NOI18N
+            ActionConfig.runAction("debug.single.main"), // NOI18N
+            ActionConfig.runAction("profile.single.main"), // NOI18N
+            ActionConfig.testAction("test"), // NOI18N
+            ActionConfig.testAction("test.single"), // NOI18N
+            ActionConfig.testAction("debug.test.single"), // NOI18N
+            ActionConfig.testAction("profile.test.single"), // NOI18N
+        };
+        for (ActionConfig action : actions) {
+            NetbeansActionMapping mapp = ModelHandle2.getMapping(action.actionName, prj, cfg);
+            Map<String, String> properties = mapp.getProperties();
+            String existingValue = properties.getOrDefault(action.propertyName, "");
+
+            if (!existingValue.contains(ENABLE_PREVIEW_FLAG)) {
+                properties.put(action.propertyName, ENABLE_PREVIEW_FLAG + (existingValue .isEmpty() ? "" : " ") + existingValue);
+                ModelHandle2.putMapping(mapp, prj, cfg);
             }
 
         }
-        return (fix != null) ? Collections.<Fix>singletonList(fix) : Collections.<Fix>emptyList();
     }
 
     @Override
-    public String getId() {
-        return EnablePreviewMavenProj.class.getName();
-    }
-
-    @Override
-    public String getDisplayName() {
-        return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature"); // NOI18N
-    }
-
-    public String getDescription() {
-        return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature"); // NOI18N
-    }
-
-    @Override
-    public void cancel() {
+    public boolean canChangeSourceLevel() {
+        CheckCanChangeSourceLevel canChange = new CheckCanChangeSourceLevel();
+        try {
+            final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+            pom.getFileSystem().runAtomicAction(() -> {
+                List<ModelOperation<POMModel>> operations = Collections.singletonList(canChange);
+                org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+            });
+        } catch (IOException ex) {
+            LOG.log(Level.FINE, null, ex);
+        }
+        return canChange.canChangeSourceLevel;
     }
+    private static final Logger LOG = Logger.getLogger(EnablePreviewMavenProj.class.getName());
 
-    private static final class ResolveMvnFix implements Fix {
+    private static final class ActionConfig {
+        public final String actionName;
+        public final String propertyName;
 
-        private final Project prj;
-
-        ResolveMvnFix(@NonNull final Project prj) {
-            Parameters.notNull("prj", prj); //NOI18N
-            this.prj = prj;
+        public ActionConfig(String actionName, String propertyName) {
+            this.actionName = actionName;
+            this.propertyName = propertyName;
         }
-
-        @Override
-        public String getText() {
-            return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature");
+        public static ActionConfig runAction(String actionName) {
+            return new ActionConfig(actionName, "exec.args");
         }
+        public static ActionConfig testAction(String actionName) {
+            return new ActionConfig(actionName, "argLine");
+        }
+    }
 
-        @Override
-        public ChangeInfo implement() throws Exception {
-
-            try {
-
-                final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
-                pom.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
-                    @Override
-                    public void run() throws IOException {
-                        List<ModelOperation<POMModel>> operations = new ArrayList<ModelOperation<POMModel>>();
-                        operations.add(new AddMvnCompilerPluginForEnablePreview());
-                        org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+    private static class BaseMvnCompilerPluginForEnablePreview {
+
+        protected static final String MAVEN_COMPILER_GROUP_ID = "org.apache.maven.plugins"; // NOI18N
+        protected static final String MAVEN_COMPILER_ARTIFACT_ID = "maven-compiler-plugin"; // NOI18N
+        protected static final String COMPILER_ID_PROPERTY = "compilerId"; // NOI18N
+        protected static final String RELEASE = "release"; // NOI18N
+        protected static final String RELEASE_PROPERTY = "maven.compiler.release"; // NOI18N
+        protected static final String SOURCE = "source"; // NOI18N
+        protected static final String SOURCE_PROPERTY = "maven.compiler.source"; // NOI18N
+        protected static final String TARGET = "target"; // NOI18N
+        protected static final String TARGET_PROPERTY = "maven.compiler.target"; // NOI18N
+        protected static final String COMPILER_ARG = "compilerArgs"; // NOI18N
+        protected static final String MAVEN_COMPILER_VERSION = "3.11.0"; // NOI18N
+        protected static final String ARG = "arg";// NOI18N
+
+        protected Plugin searchMavenCompilerPlugin(final Build build) {
+            List<Plugin> plugins = build.getPlugins();
+            if (plugins != null) {
+                for (Plugin plugin : plugins) {
+                    if (MAVEN_COMPILER_GROUP_ID.equals(plugin.getGroupId())
+                            && MAVEN_COMPILER_ARTIFACT_ID.equals(plugin.getArtifactId())) {
+                        return plugin;
                     }
-                });
-
-            } catch (IOException ex) {
+                }
             }
-            ProjectConfiguration cfg = prj.getLookup().lookup(ProjectConfigurationProvider.class).getActiveConfiguration();
-
-            for (String action : new String[]{"run", "debug", "profile"}) { // NOI18N
-
-                NetbeansActionMapping mapp = ModelHandle2.getMapping(action, prj, cfg);
-                Map<String, String> properties = mapp.getProperties();
+            return null;
+        }

Review Comment:
   awesome, thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists