mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167981809
##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -52,144 +41,97 @@
import org.netbeans.spi.project.ProjectConfiguration;
import org.netbeans.spi.project.ProjectConfigurationProvider;
import org.openide.filesystems.FileSystem;
+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 = new
HashSet<String>(Arrays.asList(
- "compiler.err.preview.feature.disabled",
- "compiler.err.preview.feature.disabled.plural")); // NOI18N
private static final String ENABLE_PREVIEW_FLAG = "--enable-preview"; //
NOI18N
- @Override
- public Set<String> getCodes() {
- return Collections.unmodifiableSet(ERROR_CODES);
- }
-
- @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;
- }
-
- }
- 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
- }
+ private final Project prj;
- public String getDescription() {
- return NbBundle.getMessage(EnablePreviewMavenProj.class,
"FIX_EnablePreviewFeature"); // NOI18N
+ private EnablePreviewMavenProj(@NonNull final Project prj) {
+ Parameters.notNull("prj", prj); //NOI18N
+ this.prj = prj;
}
@Override
- public void cancel() {
- }
-
- private static final class ResolveMvnFix implements Fix {
-
- private final Project prj;
-
- ResolveMvnFix(@NonNull final Project prj) {
- Parameters.notNull("prj", prj); //NOI18N
- this.prj = prj;
- }
-
- @Override
- public String getText() {
- return NbBundle.getMessage(EnablePreviewMavenProj.class,
"FIX_EnablePreviewFeature");
- }
-
- @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);
- }
- });
-
- } catch (IOException ex) {
+ public void enablePreview(String newSourceLevel) throws Exception {
+ 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(newSourceLevel));
+
org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom,
operations);
}
- 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();
-
- for (Entry<String, String> entry : properties.entrySet()) {
- if (entry.getKey().equals("exec.args")) { // NOI18Nl
- if (!entry.getValue().contains(ENABLE_PREVIEW_FLAG + "
")) {
- properties.put(entry.getKey(), ENABLE_PREVIEW_FLAG
+ " " + entry.getValue());
- }
- }
- }
- if (mapp != null) {
- ModelHandle2.putMapping(mapp, prj, cfg);
- }
-
+ });
+
+ 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 null;
}
}
- private boolean isMavenProject(Project prj) {
- if (prj == null) {
- return false;
+ private static final class ActionConfig {
+ public final String actionName;
+ public final String propertyName;
+
+ public ActionConfig(String actionName, String propertyName) {
+ this.actionName = actionName;
+ this.propertyName = propertyName;
}
- FileObject prjDir = prj.getProjectDirectory();
- if (prjDir == null) {
- return false;
+ public static ActionConfig runAction(String actionName) {
+ return new ActionConfig(actionName, "exec.args");
+ }
+ public static ActionConfig testAction(String actionName) {
+ return new ActionConfig(actionName, "argLine");
}
-
- FileObject pom = prjDir.getFileObject("pom.xml");
- return (pom != null) && pom.isValid();
-
}
private static class AddMvnCompilerPluginForEnablePreview implements
ModelOperation<POMModel> {
private static final String MAVEN_COMPILER_GROUP_ID =
"org.apache.maven.plugins"; // NOI18N
private static final String MAVEN_COMPILER_ARTIFACT_ID =
"maven-compiler-plugin"; // NOI18N
private static final String COMPILER_ID_PROPERTY = "compilerId"; //
NOI18N
+ private static final String SOURCE = "source"; // NOI18N
+ private static final String TARGET = "target"; // NOI18N
private static final String COMPILER_ARG = "compilerArgs"; // NOI18N
private static final String MAVEN_COMPILER_VERSION = "3.3"; // NOI18N
Review Comment:
can you bump this to `3.11.0`?
`3.11.0` still supports running on JDK 8 but it knows about the `release`
options, modules and other features too. This will allow NB to show `release`
option hint right away in the pom.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists