You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by mb...@apache.org on 2022/10/25 08:08:21 UTC

[netbeans] branch delivery updated: #4847: do not use newer classes in older distributions.

This is an automated email from the ASF dual-hosted git repository.

mbalin pushed a commit to branch delivery
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/delivery by this push:
     new 26532bf88e #4847: do not use newer classes in older distributions.
     new 83da1847af Merge pull request #4850 from sdedic/gradle/unloadable-project
26532bf88e is described below

commit 26532bf88e3168daf75949f9504fb166925528cd
Author: Svata Dedic <sv...@oracle.com>
AuthorDate: Mon Oct 24 19:39:23 2022 +0200

    #4847: do not use newer classes in older distributions.
---
 .../gradle/tooling/NbProjectInfoBuilder.java       | 20 ++++-
 .../modules/gradle/GradleBaseProjectInternal.java  | 86 ++++++++++++++++++++++
 .../data/projects/oldgradle/basic/build.gradle     | 23 ++++++
 .../data/projects/oldgradle/basic/settings.gradle  | 21 ++++++
 .../oldgradle/basic/src/main/java/test/App.java    | 19 +++++
 .../modules/gradle/api/GradleBaseProjectTest.java  | 59 ++++++++++++++-
 6 files changed, 224 insertions(+), 4 deletions(-)

diff --git a/extide/gradle/netbeans-gradle-tooling/src/main/java/org/netbeans/modules/gradle/tooling/NbProjectInfoBuilder.java b/extide/gradle/netbeans-gradle-tooling/src/main/java/org/netbeans/modules/gradle/tooling/NbProjectInfoBuilder.java
index 39fb118014..63fd8dbf86 100644
--- a/extide/gradle/netbeans-gradle-tooling/src/main/java/org/netbeans/modules/gradle/tooling/NbProjectInfoBuilder.java
+++ b/extide/gradle/netbeans-gradle-tooling/src/main/java/org/netbeans/modules/gradle/tooling/NbProjectInfoBuilder.java
@@ -200,9 +200,18 @@ class NbProjectInfoBuilder {
         runAndRegisterPerf(model, "dependencies", this::detectDependencies);
         runAndRegisterPerf(model, "artifacts", this::detectArtifacts);
         detectDistributions(model);
-        runAndRegisterPerf(model, "detectExtensions", this::detectExtensions);
-        runAndRegisterPerf(model, "detectPlugins2", this::detectAdditionalPlugins);
-        runAndRegisterPerf(model, "taskDependencies", this::detectTaskDependencies);
+        
+        // introspection is only allowed for gradle 7.4 and above.
+        // TODO: investigate if some of the instrospection could be done for earlier Gradles.
+        sinceGradle("7.0", () -> {
+            runAndRegisterPerf(model, "detectExtensions", this::detectExtensions);
+        });
+        sinceGradle("7.0", () -> {
+            runAndRegisterPerf(model, "detectPlugins2", this::detectAdditionalPlugins);
+        });
+        sinceGradle("7.0", () -> {
+            runAndRegisterPerf(model, "taskDependencies", this::detectTaskDependencies);
+        });
         runAndRegisterPerf(model, "taskProperties", this::detectTaskProperties);
         runAndRegisterPerf(model, "artifacts", this::detectConfigurationArtifacts);
         storeGlobalTypes(model);
@@ -351,6 +360,10 @@ class NbProjectInfoBuilder {
         model.getInfo().put("configurationArtifacts", data); // NOI18N
     }
     
+    /**
+     * Relies on PluginManagerInternal.findPluginIdForClass (gradle 7.1) and PluginRegistry.findPluginForClass (gradle 7.0)
+     * @param model 
+     */
     private void detectAdditionalPlugins(NbProjectInfoModel model) {
         final PluginManagerInternal pmi;
         PluginRegistry reg;
@@ -854,6 +867,7 @@ class NbProjectInfoBuilder {
         model.getInfo().put("project_rootDir", project.getRootDir());
         model.getInfo().put("gradle_user_home", project.getGradle().getGradleUserHomeDir());
         model.getInfo().put("gradle_home", project.getGradle().getGradleHomeDir());
+        model.getInfo().put("gradle_version", project.getGradle().getGradleVersion());
 
         Set<Configuration> visibleConfigurations = configurationsToSave();
         model.getInfo().put("configurations", visibleConfigurations.stream().map(conf->conf.getName()).collect(Collectors.toCollection(HashSet::new )));
diff --git a/extide/gradle/src/org/netbeans/modules/gradle/GradleBaseProjectInternal.java b/extide/gradle/src/org/netbeans/modules/gradle/GradleBaseProjectInternal.java
new file mode 100644
index 0000000000..ff97e6ddcd
--- /dev/null
+++ b/extide/gradle/src/org/netbeans/modules/gradle/GradleBaseProjectInternal.java
@@ -0,0 +1,86 @@
+/*
+ * 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.gradle;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.Map;
+import org.netbeans.modules.gradle.spi.GradleFiles;
+import org.netbeans.modules.gradle.spi.ProjectInfoExtractor;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ * Values that could be eventually exposed through GradleBaseProject or other APIs,
+ * but are currently not, but they are useful for e.g. tests.
+ * @author sdedic
+ */
+public class GradleBaseProjectInternal {
+    /**
+     * Gradle version for fallback info, or when the version info was not reported (assuming some error).
+     * The version should be old enough to fail all reasonable later-than checks.
+     */
+    public static final String VERSION_UNKNOWN = "1.0"; // NOI18N
+    
+    /**
+     * Gradle version that actually loaded the project.
+     */
+    private final String gradleVersion;
+    
+    /**
+     * Gradle home directory
+     */
+    private final File gradleHome;
+
+    /**
+     * Returns version of Gradle that has loaded the project information. Returns 
+     * @return 
+     */
+    public String getGradleVersion() {
+        return gradleVersion;
+    }
+
+    /**
+     * Returns the gradle home directory. May return {@code null}.
+     * @return gradle home directory, or {@code null}.
+     */
+    public File getGradleHome() {
+        return gradleHome;
+    }
+
+    GradleBaseProjectInternal(String gradleVersion, File gradleHome) {
+        this.gradleVersion = gradleVersion;
+        this.gradleHome = gradleHome;
+    }
+    
+    @ServiceProvider(service = ProjectInfoExtractor.class)
+    public static class Extractor implements ProjectInfoExtractor {
+
+        @Override
+        public Result fallback(GradleFiles files) {
+            return new DefaultResult(new GradleBaseProjectInternal(VERSION_UNKNOWN, null), Collections.emptySet());
+        }
+
+        @Override
+        public Result extract(Map<String, Object> props, Map<Class, Object> otherInfo) {
+            String ver = (String)props.getOrDefault("gradle_version", VERSION_UNKNOWN);
+            File home = (File)props.get("gradle_home");
+            return new DefaultResult(new GradleBaseProjectInternal(ver, home), Collections.emptySet());
+        }
+    }
+}
diff --git a/extide/gradle/test/unit/data/projects/oldgradle/basic/build.gradle b/extide/gradle/test/unit/data/projects/oldgradle/basic/build.gradle
new file mode 100644
index 0000000000..6301ee7677
--- /dev/null
+++ b/extide/gradle/test/unit/data/projects/oldgradle/basic/build.gradle
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+apply plugin: 'java'
+apply plugin: 'application'
+
+mainClassName = 'test.App'
diff --git a/extide/gradle/test/unit/data/projects/oldgradle/basic/settings.gradle b/extide/gradle/test/unit/data/projects/oldgradle/basic/settings.gradle
new file mode 100644
index 0000000000..877a63d3ae
--- /dev/null
+++ b/extide/gradle/test/unit/data/projects/oldgradle/basic/settings.gradle
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+
+rootProject.name="simple"
+
diff --git a/extide/gradle/test/unit/data/projects/oldgradle/basic/src/main/java/test/App.java b/extide/gradle/test/unit/data/projects/oldgradle/basic/src/main/java/test/App.java
new file mode 100644
index 0000000000..917e9b0884
--- /dev/null
+++ b/extide/gradle/test/unit/data/projects/oldgradle/basic/src/main/java/test/App.java
@@ -0,0 +1,19 @@
+package test;
+
+import java.util.Collections;
+
+public class App {
+    private static final String PREFIX = "org.netbeans.gradle.javaexec.test.";
+    public static void main(String[] args) {
+        for (int i = 0; i < args.length; i++) {
+            System.err.println("args." + i + "=" + args[i]);
+        }
+        String[] props = Collections.list(System.getProperties().propertyNames()).stream().
+            filter(n -> n.toString().startsWith(PREFIX)).
+            map(Object::toString).sorted().toArray(i -> new String[i]);
+
+        for (int i = 0; i < props.length; i++) {
+            System.err.println("prop." + i + "=" + props[i].substring(PREFIX.length()) + "=" + System.getProperty(props[i]));
+        }
+    }
+}
diff --git a/extide/gradle/test/unit/src/org/netbeans/modules/gradle/api/GradleBaseProjectTest.java b/extide/gradle/test/unit/src/org/netbeans/modules/gradle/api/GradleBaseProjectTest.java
index 82aca6aa88..2fe874a4b1 100644
--- a/extide/gradle/test/unit/src/org/netbeans/modules/gradle/api/GradleBaseProjectTest.java
+++ b/extide/gradle/test/unit/src/org/netbeans/modules/gradle/api/GradleBaseProjectTest.java
@@ -20,13 +20,17 @@ package org.netbeans.modules.gradle.api;
 
 import java.io.File;
 import java.util.List;
-import java.util.Optional;
 import static junit.framework.TestCase.assertNotNull;
+import org.gradle.tooling.GradleConnector;
+import org.gradle.tooling.ProjectConnection;
 import org.netbeans.api.project.Project;
 import org.netbeans.api.project.ProjectManager;
 import org.netbeans.api.project.ui.OpenProjects;
 import org.netbeans.modules.gradle.AbstractGradleProjectTestCase;
+import org.netbeans.modules.gradle.GradleBaseProjectInternal;
 import org.netbeans.modules.gradle.ProjectTrust;
+import org.netbeans.modules.gradle.api.execute.GradleDistributionManager;
+import org.netbeans.modules.gradle.api.execute.GradleDistributionManager.GradleDistribution;
 import org.netbeans.modules.gradle.options.GradleExperimentalSettings;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
@@ -205,4 +209,57 @@ public class GradleBaseProjectTest extends AbstractGradleProjectTestCase {
         assertEquals(":p1:jar", external.getPath());
         assertEquals(":p1", external.getProjectPath());
     }
+    
+    private void assertProjectLoadedWithNoProblems(Project p, String expectedVersion) {
+        GradleBaseProject gbp = GradleBaseProject.get(p);
+        assertNotNull(gbp);
+        assertTrue(NbGradleProject.get(p).getQuality().atLeast(NbGradleProject.Quality.FULL));
+        assertEquals(0, gbp.getProblems().size());
+        GradleBaseProjectInternal baseInternal = NbGradleProject.get(p).projectLookup(GradleBaseProjectInternal.class);
+        assertNotNull(baseInternal);
+        assertEquals(expectedVersion, baseInternal.getGradleVersion());
+    }
+    
+    private Project makeProjectWithWrapper(String subdir, String gradleVersion) throws Exception {
+        FileObject src = FileUtil.toFileObject(getDataDir()).getFileObject(subdir);
+        projectDir = FileUtil.copyFile(src, FileUtil.toFileObject(getWorkDir()), src.getNameExt());
+        
+        GradleDistribution dist = GradleDistributionManager.get().defaultDistribution();
+        GradleConnector gconn = GradleConnector.newConnector();
+        gconn = gconn.useGradleUserHomeDir(dist.getGradleUserHome());
+        if (dist.isAvailable()) {
+            gconn = gconn.useInstallation(dist.getDistributionDir());
+        } else {
+            gconn = gconn.useDistribution(dist.getDistributionURI());
+        }
+        try (ProjectConnection c = gconn.forProjectDirectory(FileUtil.toFile(projectDir)).connect()) {
+            c.newBuild().forTasks("wrapper").addArguments("wrapper", "--gradle-version", gradleVersion).run();
+        }
+        
+        Project p = ProjectManager.getDefault().findProject(projectDir);
+        assertNotNull(p);
+        ProjectTrust.getDefault().trustProject(p);
+        
+        OpenProjects.getDefault().open(new Project[] { p }, true);
+        OpenProjects.getDefault().openProjects().get();
+        
+        NbGradleProject.get(p).toQuality("Load data", NbGradleProject.Quality.FULL, false).toCompletableFuture().get();
+        return p;
+    }
+    
+    public void testOldGradle683ProjectLoads() throws Exception {
+        Project p = makeProjectWithWrapper("projects/oldgradle/basic", "6.8.3");
+        assertProjectLoadedWithNoProblems(p, "6.8.3");
+    }
+
+    public void testOldGradle700ProjectLoads() throws Exception {
+        Project p = makeProjectWithWrapper("projects/oldgradle/basic", "7.0");
+        assertProjectLoadedWithNoProblems(p, "7.0");
+    }
+
+    public void testOldGradle710ProjectLoads() throws Exception {
+        Project p = makeProjectWithWrapper("projects/oldgradle/basic", "7.1");
+        assertProjectLoadedWithNoProblems(p, "7.1");
+    }
 }
+


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

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