3131import org .openrewrite .internal .StringUtils ;
3232import org .openrewrite .java .JavaIsoVisitor ;
3333import org .openrewrite .java .tree .*;
34+ import org .openrewrite .marker .Markup ;
3435import org .openrewrite .maven .MavenDownloadingException ;
36+ import org .openrewrite .maven .MavenDownloadingExceptions ;
3537import org .openrewrite .maven .internal .MavenPomDownloader ;
3638import org .openrewrite .maven .tree .*;
3739import org .openrewrite .semver .ExactVersion ;
@@ -275,8 +277,12 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
275277 } catch (Exception ignore ) {
276278 }
277279
278- if (shouldRemoveRedundantDependency (dependency , m .getSimpleName ())) {
279- return null ;
280+ try {
281+ if (shouldRemoveRedundantDependency (dependency , m .getSimpleName (), gp .getMavenRepositories (), ctx )) {
282+ return null ;
283+ }
284+ } catch (MavenDownloadingException e ) {
285+ return Markup .error (m , e );
280286 }
281287 }
282288 return m ;
@@ -295,23 +301,12 @@ private boolean isDependencyManagmentMethod(String methodName) {
295301 return DEPENDENCY_MANAGEMENT_METHODS .contains (methodName );
296302 }
297303
298- private boolean shouldRemoveRedundantDependency (Dependency dependency , String configurationName ) {
299- if (( groupPattern != null && !matchesGlob (dependency .getGroupId (), groupPattern )) ||
300- (artifactPattern != null && !matchesGlob (dependency .getArtifactId (), artifactPattern ))) {
304+ private boolean shouldRemoveRedundantDependency (@ Nullable Dependency dependency , String configurationName , List < MavenRepository > repositories , ExecutionContext ctx ) throws MavenDownloadingException {
305+ if (dependency == null || (( groupPattern != null && !matchesGlob (dependency .getGroupId (), groupPattern ))
306+ || (artifactPattern != null && !matchesGlob (dependency .getArtifactId (), artifactPattern ) ))) {
301307 return false ;
302308 }
303309
304- String dependencyManagedVersion = null ;
305- if (platforms .get (configurationName ) != null && !platforms .get (configurationName ).isEmpty ()) {
306- dependencyManagedVersion = platforms .get (configurationName )
307- .stream ()
308- .map (e -> e .getManagedDependency (dependency .getGroupId (), dependency .getArtifactId (), null , null ))
309- .filter (Objects ::nonNull )
310- .map (ResolvedManagedDependency ::getVersion )
311- .max (VERSION_COMPARATOR )
312- .orElse (null );
313- }
314-
315310 for (Map .Entry <String , List <ResolvedDependency >> entry : directDependencies .entrySet ()) {
316311 for (ResolvedDependency d : entry .getValue ()) {
317312 // ignore self
@@ -323,18 +318,39 @@ private boolean shouldRemoveRedundantDependency(Dependency dependency, String co
323318 if (d .getDependencies () == null ) {
324319 continue ;
325320 }
326-
327- ResolvedDependency resolvedDependency = d .findDependency (dependency .getGroupId (), dependency .getArtifactId ());
328- if (resolvedDependency != null && (dependency .getVersion () == null ||
329- (dependencyManagedVersion == null && matchesConfiguration (configurationName , entry .getKey ()) && dependency .getVersion ().equals (resolvedDependency .getVersion ())) ||
330- (dependencyManagedVersion != null && VERSION_COMPARATOR .compare (dependency .getVersion (), dependencyManagedVersion ) <= 0 ))) {
321+ if (matchesConfiguration (configurationName , entry .getKey ())
322+ && d .findDependency (dependency .getGroupId (), dependency .getArtifactId ()) != null
323+ && dependsOnNewerVersion (dependency .getGav (), d .getGav ().asGroupArtifactVersion (), repositories , ctx )) {
331324 return true ;
332325 }
333326 }
334327 }
335328 return false ;
336329 }
337330
331+ private boolean dependsOnNewerVersion (GroupArtifactVersion searchGav , GroupArtifactVersion toSearch , List <MavenRepository > repositories , ExecutionContext ctx ) throws MavenDownloadingException {
332+ if (toSearch .getVersion () == null ) {
333+ return false ;
334+ }
335+ if (searchGav .asGroupArtifact ().equals (toSearch .asGroupArtifact ())) {
336+ return searchGav .getVersion () == null || matchesComparator (toSearch .getVersion (), searchGav .getVersion ());
337+ }
338+ MavenPomDownloader mpd = new MavenPomDownloader (ctx );
339+ try {
340+ List <ResolvedDependency > resolved = mpd .download (toSearch , null , null , repositories )
341+ .resolve (emptyList (), mpd , repositories , ctx )
342+ .resolveDependencies (Scope .Runtime , mpd , ctx );
343+ for (ResolvedDependency r : resolved ) {
344+ if (Objects .equals (searchGav .getGroupId (), r .getGroupId ()) && Objects .equals (searchGav .getArtifactId (), r .getArtifactId ())) {
345+ return searchGav .getVersion () == null || matchesComparator (r .getVersion (), searchGav .getVersion ());
346+ }
347+ }
348+ } catch (MavenDownloadingExceptions e ) {
349+ throw e .getExceptions ().get (0 );
350+ }
351+ return false ;
352+ }
353+
338354 boolean matchesConfiguration (String configA , String configB ) {
339355 if (configA .equals ("runtimeOnly" ) && configB .equals ("implementation" )) {
340356 return true ;
0 commit comments