1515
1616import java .util .Arrays ;
1717import java .util .Collections ;
18+ import java .util .Comparator ;
1819import java .util .HashMap ;
1920import java .util .HashSet ;
2021import java .util .LinkedHashSet ;
4445import org .eclipse .jdt .core .search .SearchRequestor ;
4546import org .eclipse .jdt .ui .text .java .IJavaCompletionProposal ;
4647import org .eclipse .jface .operation .IRunnableWithProgress ;
48+ import org .eclipse .osgi .service .resolver .BaseDescription ;
4749import org .eclipse .osgi .service .resolver .BundleDescription ;
4850import org .eclipse .osgi .service .resolver .BundleSpecification ;
4951import org .eclipse .osgi .service .resolver .ExportPackageDescription ;
@@ -188,6 +190,9 @@ public FindClassResolutionsOperation(IProject project, CompilationUnit cu, Strin
188190 fCollector = collector ;
189191 }
190192
193+ private static final Comparator <BaseDescription > BY_DESCENDING_VERSION = Comparator
194+ .comparing (BaseDescription ::getVersion ).reversed ();
195+
191196 @ Override
192197 public void run (IProgressMonitor monitor ) {
193198 int idx = fClassName .lastIndexOf ('.' );
@@ -198,7 +203,7 @@ public void run(IProgressMonitor monitor) {
198203 }
199204
200205 Set <IPackageFragment > packagesToExport = new HashSet <>();
201- Map <String , ExportPackageDescription > validPackages = getValidPackages (typeName , fClassName , packageName ,
206+ Map <String , List < ExportPackageDescription > > validPackages = getValidPackages (typeName , fClassName , packageName ,
202207 packagesToExport , monitor );
203208 if (validPackages != null ) {
204209
@@ -215,32 +220,35 @@ public void run(IProgressMonitor monitor) {
215220 && (allowMultipleFixes || !fCollector .isDone ());) {
216221 var entry = validPackagesIter .next ();
217222 String qualifiedType = entry .getKey ();
218- ExportPackageDescription currentPackage = entry .getValue ();
223+ List < ExportPackageDescription > currentPackages = entry .getValue ();
219224 // if package is already visible, skip over
220- if ( visiblePackages .contains (currentPackage )) {
221- continue ;
222- }
223- // if currentPackage will resolve class and is valid, pass it to collector
224- fCollector . addResolutionModification ( fProject , currentPackage , fCompilationUnit , qualifiedType );
225+ currentPackages . stream (). sorted ( BY_DESCENDING_VERSION ). filter ( p -> ! visiblePackages .contains (p ))
226+ . forEach ( p -> {
227+ // If package will resolve class and is valid, pass it
228+ fCollector . addResolutionModification ( fProject , p , fCompilationUnit , qualifiedType );
229+ } );
225230 }
226231 // additionally add require bundle proposals
227232 Set <String > bundleNames = getCurrentBundleNames ();
228- validPackages .forEach ((key , currentPackage ) -> {
229- BundleDescription desc = currentPackage .getExporter ();
230- // Ignore already required bundles and duplicate proposals (currently we do not consider version constraints)
231- if (desc != null && !bundleNames .contains (desc .getName ())) {
232- fCollector .addRequireBundleModification (fProject , currentPackage , 3 , fCompilationUnit , key );
233- bundleNames .add (desc .getName ());
234- }
233+ validPackages .forEach ((key , currentPackages ) -> {
234+ currentPackages .stream ().sorted (BY_DESCENDING_VERSION ).forEach (currentPackage -> {
235+ BundleDescription desc = currentPackage .getExporter ();
236+ // Ignore already required bundles and duplicate proposals
237+ // (currently we do not consider version constraints)
238+ if (desc != null && !bundleNames .contains (desc .getName ())) {
239+ fCollector .addRequireBundleModification (fProject , currentPackage , 3 , fCompilationUnit , key );
240+ bundleNames .add (desc .getName ());
241+ }
242+ });
235243 });
236244 }
237245 }
238246
239- private Map <String , ExportPackageDescription > getValidPackages (String typeName , String qualifiedTypeToImport ,
247+ private Map <String , List < ExportPackageDescription > > getValidPackages (String typeName , String qualifiedTypeToImport ,
240248 String packageName , Set <IPackageFragment > packagesToExport , IProgressMonitor monitor ) {
241249 SubMonitor subMonitor = SubMonitor .convert (monitor , 3 );
242250
243- Map <String , ExportPackageDescription > validPackages = null ;
251+ Map <String , List < ExportPackageDescription > > validPackages = null ;
244252 ImportPackageSpecification [] importPkgs = null ;
245253 IPluginModelBase model = PluginRegistry .findModel (fProject );
246254 if (model != null && model .getBundleDescription () != null ) {
@@ -252,7 +260,7 @@ private Map<String, ExportPackageDescription> getValidPackages(String typeName,
252260 if (packageName != null ) {
253261 if (!isImportedPackage (packageName , importPkgs )) {
254262 List <ExportPackageDescription > packages = getValidPackages (packageName );
255- validPackages = !packages .isEmpty () ? Map .of (qualifiedTypeToImport , packages . getLast () ) : Map .of ();
263+ validPackages = !packages .isEmpty () ? Map .of (qualifiedTypeToImport , packages ) : Map .of ();
256264 }
257265 subMonitor .split (1 );
258266 } else {
@@ -276,7 +284,7 @@ private Map<String, ExportPackageDescription> getValidPackages(String typeName,
276284 * if no valid package to import was found
277285 * @return the set of packages to import
278286 */
279- private Map <String , ExportPackageDescription > findValidPackagesContainingSimpleType (String aTypeName ,
287+ private Map <String , List < ExportPackageDescription > > findValidPackagesContainingSimpleType (String aTypeName ,
280288 ImportPackageSpecification [] importPkgs , Set <IPackageFragment > packagesToExport , IProgressMonitor monitor ) {
281289 SubMonitor subMonitor = SubMonitor .convert (monitor );
282290
@@ -334,13 +342,10 @@ public void acceptSearchMatch(SearchMatch aMatch) throws CoreException {
334342 }
335343
336344 // finally create the list of ExportPackageDescriptions
337- Map <String , ExportPackageDescription > exportDescriptions = new HashMap <>(packages .size ());
338- ExportPackageDescription [] knownPackages = state .getExportedPackages ();
339- for (ExportPackageDescription knownPackage : knownPackages ) {
340- if (packages .containsKey (knownPackage .getName ())) {
341- exportDescriptions .put (qualifiedTypeNames .get (knownPackage .getName ()), knownPackage );
342- }
343- }
345+ Map <String , List <ExportPackageDescription >> exportDescriptions = Arrays
346+ .stream (state .getExportedPackages ()).filter (p -> packages .containsKey (p .getName ()))
347+ .collect (Collectors .groupingBy (p -> qualifiedTypeNames .get (p .getName ())));
348+
344349 if (exportDescriptions .isEmpty ()) {
345350 // no packages to import found, maybe there are packages to export
346351 packagesToExport .addAll (packages .values ());
0 commit comments