Skip to content

Commit

Permalink
Allow to apply cleanups individually / cleanup dependencies
Browse files Browse the repository at this point in the history
Allow to apply only one cleanup at a time and don'T pull in all
transitive dependencies.
  • Loading branch information
laeubi committed Jan 25, 2025
1 parent ab13859 commit cff4428
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 23 deletions.
45 changes: 45 additions & 0 deletions tycho-cleancode-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,57 @@
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.ui</artifactId>
<version>3.33.200</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.core.manipulation</artifactId>
<version>1.21.300</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.ltk.core.refactoring</artifactId>
<version>3.14.600</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.ui.workbench</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.ui.ide</artifactId>
<version>3.22.400</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -41,10 +42,12 @@
public class CleanUp extends AbstractEclipseBuild<CleanupResult> {

private Map<String, String> customProfile;
private boolean applyCleanupsIndividually;

CleanUp(Path projectDir, boolean debug, Map<String, String> customProfile) {
CleanUp(Path projectDir, boolean debug, Map<String, String> customProfile, boolean applyCleanupsIndividually) {
super(projectDir, debug);
this.customProfile = customProfile;
this.applyCleanupsIndividually = applyCleanupsIndividually;
}

@Override
Expand All @@ -54,27 +57,38 @@ protected CleanupResult createResult(IProject project) throws Exception {
ICleanUp[] cleanups = getCleanups(result, options);
if (cleanups.length > 0) {
List<ICompilationUnit> units = getCompilationUnits(project);
final CleanUpRefactoring refactoring = new CleanUpRefactoring(project.getName());
for (ICompilationUnit cu : units) {
refactoring.addCompilationUnit(cu);
}
refactoring.setUseOptionsFromProfile(false);
for (ICleanUp cleanUp : cleanups) {
refactoring.addCleanUp(cleanUp);
}
final RefactoringStatus status = refactoring.checkAllConditions(this);
if (status.isOK()) {
Change change = refactoring.createChange(this);
change.initializeValidationData(this);
PerformChangeOperation performChangeOperation = new PerformChangeOperation(change);
performChangeOperation.run(this);
} else if (status.hasError()) {
throw new RuntimeException("Refactoring failed: " + status);
if (applyCleanupsIndividually) {
for (ICleanUp cleanUp : cleanups) {
applyCleanups(project, new ICleanUp[] { cleanUp }, units);
}
} else {
applyCleanups(project, cleanups, units);
}
}
return result;
}

private void applyCleanups(IProject project, ICleanUp[] cleanups, List<ICompilationUnit> units)
throws CoreException {
final CleanUpRefactoring refactoring = new CleanUpRefactoring(project.getName());
for (ICompilationUnit cu : units) {
refactoring.addCompilationUnit(cu);
}
refactoring.setUseOptionsFromProfile(false);
for (ICleanUp cleanUp : cleanups) {
refactoring.addCleanUp(cleanUp);
}
final RefactoringStatus status = refactoring.checkAllConditions(this);
if (status.isOK()) {
Change change = refactoring.createChange(this);
change.initializeValidationData(this);
PerformChangeOperation performChangeOperation = new PerformChangeOperation(change);
performChangeOperation.run(this);
} else if (status.hasError()) {
throw new RuntimeException("Refactoring failed: " + status);
}
}

private List<ICompilationUnit> getCompilationUnits(IProject project) throws JavaModelException {
IJavaProject javaProject = JavaCore.create(project);
List<ICompilationUnit> units = new ArrayList<ICompilationUnit>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class CleanUpMojo extends AbstractEclipseBuildMojo<CleanupResult> {
@Parameter
private Map<String, String> cleanUpProfile;

@Parameter
private boolean applyCleanupsIndividually;

@Override
protected String[] getRequireBundles() {
return new String[] { "org.eclipse.jdt.ui" };
Expand All @@ -52,7 +55,7 @@ protected String getName() {

@Override
protected CleanUp createExecutable() {
return new CleanUp(project.getBasedir().toPath(), debug, cleanUpProfile);
return new CleanUp(project.getBasedir().toPath(), debug, cleanUpProfile, applyCleanupsIndividually);
}

@Override
Expand All @@ -65,6 +68,7 @@ protected void handleResult(CleanupResult result)
builder.h3("The following cleanups where applied:");
result.cleanups().forEach(cleanup -> {
builder.addListItem(cleanup);
getLog().info("CleanUp: " + cleanup);
});
builder.newLine();
builder.newLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@ protected void handleResult(QuickFixResult result) throws MojoFailureException {
MarkdownBuilder builder = new MarkdownBuilder(reportFileName);
List<String> fixes = result.fixes().toList();
builder.h3("The following " + (fixes.size() > 0 ? "warnings" : "warning") + " has been resolved:");
builder.newLine();
fixes.forEach(fix -> builder.addListItem(fix));
fixes.forEach(fix -> {
builder.addListItem(fix);
getLog().info("QuickFix: " + fix);
});
builder.newLine();
builder.newLine();
builder.write();
for (String fix : fixes) {
getLog().info("Fixed: " + fix);
}
}

@Override
Expand Down

0 comments on commit cff4428

Please sign in to comment.