From c4c5104a3b942992acc6e8d4b1cf5a78b2886012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 21 Jan 2024 08:13:54 +0100 Subject: [PATCH] Fix lookup of workspace located annotation providers Currently, if a annotation project is part of the workspace (most notable (org.eclipse.pde.api.tools.annotations) it currently does not resolve the binary folders correctly. This fixes the conversion of plugin models to classpath entries by check if a build model is available and if found uses the binary folders as classpath entries instead of the base location. --- .../ApiAnnotationsClasspathContributor.java | 7 ++- .../.settings/.api_filters | 24 +++----- .../pde/core/plugin/PluginRegistry.java | 18 +++--- .../pde/internal/core/ClasspathUtilCore.java | 55 +++++++++++++------ .../OSGiAnnotationsClasspathContributor.java | 8 ++- 5 files changed, 65 insertions(+), 47 deletions(-) diff --git a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiAnnotationsClasspathContributor.java b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiAnnotationsClasspathContributor.java index 9b21e56aa1..15b354f63c 100644 --- a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiAnnotationsClasspathContributor.java +++ b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiAnnotationsClasspathContributor.java @@ -36,9 +36,10 @@ public class ApiAnnotationsClasspathContributor implements IClasspathContributor @Override public List getInitialEntries(BundleDescription project) { - IPluginModelBase model = PluginRegistry.findModel(project); - if (hasApiNature(model)) { - return ClasspathUtilCore.classpathEntries(annotations()).collect(Collectors.toList()); + IPluginModelBase projectModel = PluginRegistry.findModel(project); + if (hasApiNature(projectModel)) { + return ClasspathUtilCore.classpathEntries(annotations().filter(model -> !model.equals(projectModel))) + .collect(Collectors.toList()); } return Collections.emptyList(); } diff --git a/ui/org.eclipse.pde.core/.settings/.api_filters b/ui/org.eclipse.pde.core/.settings/.api_filters index 5d17ac313d..087f6c65b2 100644 --- a/ui/org.eclipse.pde.core/.settings/.api_filters +++ b/ui/org.eclipse.pde.core/.settings/.api_filters @@ -1,5 +1,13 @@ + + + + + + + + @@ -9,20 +17,4 @@ - - - - - - - - - - - - - - - - diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/core/plugin/PluginRegistry.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/core/plugin/PluginRegistry.java index 0b442f897f..9bad4e0ba5 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/core/plugin/PluginRegistry.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/core/plugin/PluginRegistry.java @@ -18,6 +18,7 @@ import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.osgi.service.resolver.BundleDescription; import org.eclipse.osgi.service.resolver.VersionRange; @@ -408,13 +409,16 @@ private static IPluginModelBase[] findModels(String id) { */ public static IBuildModel createBuildModel(IPluginModelBase model) throws CoreException { if (model != null) { - IProject project = model.getUnderlyingResource().getProject(); - if (project != null) { - IFile buildFile = PDEProject.getBuildProperties(project); - if (buildFile.exists()) { - IBuildModel buildModel = new WorkspaceBuildModel(buildFile); - buildModel.load(); - return buildModel; + IResource resource = model.getUnderlyingResource(); + if (resource != null) { + IProject project = resource.getProject(); + if (project != null) { + IFile buildFile = PDEProject.getBuildProperties(project); + if (buildFile.exists()) { + IBuildModel buildModel = new WorkspaceBuildModel(buildFile); + buildModel.load(); + return buildModel; + } } } } diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathUtilCore.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathUtilCore.java index ce616caf81..1e29c4e108 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathUtilCore.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathUtilCore.java @@ -19,7 +19,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; import java.util.stream.Stream; import org.eclipse.core.resources.IResource; @@ -28,6 +31,7 @@ import org.eclipse.jdt.core.IAccessRule; import org.eclipse.jdt.core.IClasspathAttribute; import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.osgi.service.resolver.BundleDescription; import org.eclipse.pde.core.build.IBuild; @@ -82,25 +86,40 @@ private static Collection collectLibraryEntries(IPluginModelBa } public static Stream classpathEntries(Stream models) { - return models.flatMap(model -> { - String location = model.getInstallLocation(); - if (location == null) { - return Stream.empty(); - } - boolean isJarShape = new File(location).isFile(); - IPluginLibrary[] libraries = model.getPluginBase().getLibraries(); - if (isJarShape || libraries.length == 0) { - return Stream.of(IPath.fromOSString(location)); + Map> collect = models.collect(Collectors.partitioningBy(model -> { + IResource resource = model.getUnderlyingResource(); + if (resource != null) { + try { + return resource.getProject().hasNature(JavaCore.NATURE_ID); + } catch (CoreException e) { + // nothing we can do then... + } } - return Arrays.stream(libraries).filter(library -> !IPluginLibrary.RESOURCE.equals(library.getType())) - .map(library -> { - String name = library.getName(); - String expandedName = ClasspathUtilCore.expandLibraryName(name); - IPath path = ClasspathUtilCore.getPath(model, expandedName, isJarShape); - return path; - }).filter(Objects::nonNull); - }).map(path -> JavaCore.newLibraryEntry(path, path, IPath.ROOT, new IAccessRule[0], new IClasspathAttribute[0], - false)); + return false; + })); + List javaModels = collect.get(true); + List externalModels = collect.get(false); + return Stream.concat( + javaModels.stream().map(m -> JavaCore.create(m.getUnderlyingResource().getProject())) + .map(IJavaProject::getPath).map(JavaCore::newProjectEntry), + externalModels.stream().flatMap(model -> { + String location = model.getInstallLocation(); + if (location == null) { + return Stream.empty(); + } + boolean isJarShape = new File(location).isFile(); + IPluginLibrary[] libraries = model.getPluginBase().getLibraries(); + if (isJarShape || libraries.length == 0) { + return Stream.of(IPath.fromOSString(location)); + } + return Arrays.stream(libraries) + .filter(library -> !IPluginLibrary.RESOURCE.equals(library.getType())).map(library -> { + String name = library.getName(); + String expandedName = ClasspathUtilCore.expandLibraryName(name); + return ClasspathUtilCore.getPath(model, expandedName, isJarShape); + }).filter(Objects::nonNull); + }).map(path -> JavaCore.newLibraryEntry(path, path, IPath.ROOT, new IAccessRule[0], + new IClasspathAttribute[0], false))); } private static void addLibraryEntry(IPluginLibrary library, Collection entries) { diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/annotations/OSGiAnnotationsClasspathContributor.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/annotations/OSGiAnnotationsClasspathContributor.java index b5e4b758a9..aaaed1b3cf 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/annotations/OSGiAnnotationsClasspathContributor.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/annotations/OSGiAnnotationsClasspathContributor.java @@ -43,9 +43,11 @@ public class OSGiAnnotationsClasspathContributor implements IClasspathContributo @Override public List getInitialEntries(BundleDescription project) { - IPluginModelBase model = PluginRegistry.findModel(project); - if (model != null) { - return ClasspathUtilCore.classpathEntries(annotations()).collect(Collectors.toList()); + IPluginModelBase projectModel = PluginRegistry.findModel(project); + if (projectModel != null) { + return ClasspathUtilCore + .classpathEntries(annotations().filter(model -> !model.equals(projectModel))) + .collect(Collectors.toList()); } return Collections.emptyList(); }