-
Notifications
You must be signed in to change notification settings - Fork 13
Add FindDuplicateClasses recipe #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/main/java/org/openrewrite/java/dependencies/search/FindDuplicateClasses.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,136 @@ | ||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| * Copyright 2025 the original author or authors. | ||||||||||||||||||||||||||||||||||
| * <p> | ||||||||||||||||||||||||||||||||||
| * Licensed 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 | ||||||||||||||||||||||||||||||||||
| * <p> | ||||||||||||||||||||||||||||||||||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||
| * <p> | ||||||||||||||||||||||||||||||||||
| * 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.openrewrite.java.dependencies.search; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import lombok.EqualsAndHashCode; | ||||||||||||||||||||||||||||||||||
| import lombok.Value; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.ExecutionContext; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.ScanningRecipe; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.SourceFile; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.TreeVisitor; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.java.JavaVisitor; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.java.dependencies.table.DuplicateClassesReport; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.java.marker.JavaProject; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.java.marker.JavaSourceSet; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.java.tree.J; | ||||||||||||||||||||||||||||||||||
| import org.openrewrite.java.tree.JavaType; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import java.util.*; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import static java.util.Collections.emptyList; | ||||||||||||||||||||||||||||||||||
| import static java.util.stream.Collectors.joining; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @EqualsAndHashCode(callSuper = false) | ||||||||||||||||||||||||||||||||||
| @Value | ||||||||||||||||||||||||||||||||||
| public class FindDuplicateClasses extends ScanningRecipe<FindDuplicateClasses.Accumulator> { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| transient DuplicateClassesReport report = new DuplicateClassesReport(this); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public String getDisplayName() { | ||||||||||||||||||||||||||||||||||
| return "Find duplicate classes on the classpath"; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public String getDescription() { | ||||||||||||||||||||||||||||||||||
| return "Detects classes that appear in multiple dependencies on the classpath. " + | ||||||||||||||||||||||||||||||||||
| "This is similar to what the Maven duplicate-finder-maven-plugin does. " + | ||||||||||||||||||||||||||||||||||
| "Duplicate classes can cause runtime issues when different versions " + | ||||||||||||||||||||||||||||||||||
| "of the same class are loaded."; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+42
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public static class Accumulator { | ||||||||||||||||||||||||||||||||||
| Set<ProjectSourceSet> seen = new HashSet<>(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Value | ||||||||||||||||||||||||||||||||||
| private static class ProjectSourceSet { | ||||||||||||||||||||||||||||||||||
| String projectName; | ||||||||||||||||||||||||||||||||||
| String sourceSetName; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public Accumulator getInitialValue(ExecutionContext ctx) { | ||||||||||||||||||||||||||||||||||
| return new Accumulator(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) { | ||||||||||||||||||||||||||||||||||
| return new JavaVisitor<ExecutionContext>() { | ||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { | ||||||||||||||||||||||||||||||||||
| Optional<JavaSourceSet> maybeSourceSet = cu.getMarkers().findFirst(JavaSourceSet.class); | ||||||||||||||||||||||||||||||||||
| if (!maybeSourceSet.isPresent()) { | ||||||||||||||||||||||||||||||||||
| return cu; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| JavaSourceSet sourceSet = maybeSourceSet.get(); | ||||||||||||||||||||||||||||||||||
| String projectName = cu.getMarkers().findFirst(JavaProject.class) | ||||||||||||||||||||||||||||||||||
| .map(JavaProject::getProjectName) | ||||||||||||||||||||||||||||||||||
| .orElse("<unknown>"); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ProjectSourceSet pss = new ProjectSourceSet(projectName, sourceSet.getName()); | ||||||||||||||||||||||||||||||||||
| if (!acc.seen.add(pss)) { | ||||||||||||||||||||||||||||||||||
| return cu; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Map<String, List<JavaType.FullyQualified>> gavToTypes = sourceSet.getGavToTypes(); | ||||||||||||||||||||||||||||||||||
| if (gavToTypes == null || gavToTypes.isEmpty()) { | ||||||||||||||||||||||||||||||||||
| return cu; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Invert the mapping: type name -> list of GAVs containing that type | ||||||||||||||||||||||||||||||||||
| Map<String, List<String>> typeToGavs = new HashMap<>(); | ||||||||||||||||||||||||||||||||||
| for (Map.Entry<String, List<JavaType.FullyQualified>> entry : gavToTypes.entrySet()) { | ||||||||||||||||||||||||||||||||||
| String gav = entry.getKey(); | ||||||||||||||||||||||||||||||||||
| for (JavaType.FullyQualified type : entry.getValue()) { | ||||||||||||||||||||||||||||||||||
| typeToGavs.computeIfAbsent(type.getFullyQualifiedName(), k -> new ArrayList<>()).add(gav); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Report duplicates (types appearing in more than one GAV) | ||||||||||||||||||||||||||||||||||
| for (Map.Entry<String, List<String>> entry : typeToGavs.entrySet()) { | ||||||||||||||||||||||||||||||||||
| List<String> gavs = entry.getValue(); | ||||||||||||||||||||||||||||||||||
| if (gavs.size() > 1) { | ||||||||||||||||||||||||||||||||||
| String typeName = entry.getKey(); | ||||||||||||||||||||||||||||||||||
| // Skip module-info and package-info files (multi-release JAR false positives) | ||||||||||||||||||||||||||||||||||
| if (typeName.contains("module-info") || typeName.endsWith("package-info")) { | ||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| String additionalDeps = gavs.size() > 2 | ||||||||||||||||||||||||||||||||||
| ? gavs.subList(2, gavs.size()).stream().collect(joining(", ")) | ||||||||||||||||||||||||||||||||||
| : ""; | ||||||||||||||||||||||||||||||||||
jkschneider marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+113
to
+115
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
| report.insertRow(ctx, new DuplicateClassesReport.Row( | ||||||||||||||||||||||||||||||||||
| projectName, | ||||||||||||||||||||||||||||||||||
| sourceSet.getName(), | ||||||||||||||||||||||||||||||||||
| typeName, | ||||||||||||||||||||||||||||||||||
| gavs.get(0), | ||||||||||||||||||||||||||||||||||
| gavs.get(1), | ||||||||||||||||||||||||||||||||||
| additionalDeps | ||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return cu; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public Collection<? extends SourceFile> generate(Accumulator acc, ExecutionContext ctx) { | ||||||||||||||||||||||||||||||||||
| return emptyList(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
59 changes: 59 additions & 0 deletions
59
src/main/java/org/openrewrite/java/dependencies/table/DuplicateClassesReport.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed 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 | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.openrewrite.java.dependencies.table; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreType; | ||
| import lombok.Value; | ||
| import org.openrewrite.Column; | ||
| import org.openrewrite.DataTable; | ||
| import org.openrewrite.Recipe; | ||
|
|
||
| @JsonIgnoreType | ||
| public class DuplicateClassesReport extends DataTable<DuplicateClassesReport.Row> { | ||
| public DuplicateClassesReport(Recipe recipe) { | ||
| super(recipe, | ||
| "Duplicate classes report", | ||
| "Lists classes that appear in multiple dependencies on the classpath"); | ||
| } | ||
|
|
||
| @Value | ||
| public static class Row { | ||
|
|
||
| @Column(displayName = "Project name", | ||
| description = "The project containing the duplicate.") | ||
| String projectName; | ||
|
|
||
| @Column(displayName = "Source set", | ||
| description = "The source set containing the duplicate (e.g., main, test).") | ||
| String sourceSet; | ||
|
|
||
| @Column(displayName = "Type name", | ||
| description = "The fully qualified name of the duplicate class.") | ||
| String typeName; | ||
|
|
||
| @Column(displayName = "Dependency 1", | ||
| description = "The first dependency containing the class (group:artifact:version).") | ||
| String dependency1; | ||
|
|
||
| @Column(displayName = "Dependency 2", | ||
| description = "The second dependency containing the class (group:artifact:version).") | ||
| String dependency2; | ||
|
|
||
| @Column(displayName = "Additional dependencies", | ||
| description = "Any additional dependencies beyond the first two that also contain this class, comma-separated.") | ||
| String additionalDependencies; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.