mbien commented on code in PR #5802: URL: https://github.com/apache/netbeans/pull/5802#discussion_r1180937058
########## java/java.hints/src/org/netbeans/modules/java/hints/errors/EnablePreview.java: ########## @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.java.hints.errors; + +import com.sun.source.util.TreePath; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.lang.model.SourceVersion; +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.api.java.platform.JavaPlatform; +import org.netbeans.api.java.platform.JavaPlatformManager; +import org.netbeans.api.java.queries.SourceLevelQuery; +import org.netbeans.api.java.source.ClasspathInfo.PathKind; +import org.netbeans.api.java.source.CompilationInfo; +import org.netbeans.modules.java.hints.spi.ErrorRule; +import org.netbeans.spi.editor.hints.ChangeInfo; +import org.netbeans.spi.editor.hints.Fix; +import org.openide.filesystems.FileObject; +import org.openide.util.NbBundle; +import org.openide.util.Parameters; +import org.netbeans.modules.java.hints.spi.preview.PreviewEnabler; +import org.netbeans.modules.java.hints.spi.preview.PreviewEnabler.Factory; +import org.openide.modules.SpecificationVersion; +import org.openide.util.Lookup; + +/** + * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide + * the fix for Single Source Java File. + * + * @author Arunava Sinha + */ +public class EnablePreview implements ErrorRule<Void> { + + private static final Set<String> ERROR_CODES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList( + "compiler.err.preview.feature.disabled", //NOI18N + "compiler.err.preview.feature.disabled.plural", // NOI18N + "compiler.err.is.preview"))); // NOI18N + + @Override + public Set<String> getCodes() { + return ERROR_CODES; + } + + @Override + @NonNull + public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) { + final FileObject file = compilationInfo.getFileObject(); + + if (file != null) { + SpecificationVersion platformVersion = null; + + FileObject jlObject = compilationInfo.getClasspathInfo().getClassPath(PathKind.BOOT).findResource("java/lang/Object.class"); + for (JavaPlatform platform : JavaPlatformManager.getDefault().getInstalledPlatforms()) { + if (jlObject == platform.getBootstrapLibraries().findResource("java/lang/Object.class")) { + platformVersion = platform.getSpecification().getVersion(); + break; + } + } Review Comment: i am going to steal this for a different hint, since I needed to know the JDK version before and couldn't find any utility or anything in the lookup which would help there. ########## 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: my mistake, I didn't see that this method was implemented in the super class. I think it would be better to move the method from `AddMvnCompilerPluginForEnablePreview` to `BaseMvnCompilerPluginForEnablePreview`, so that it is not implemented twice, just slightly differently -- 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
