Skip to content
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

removeUnusedImports - JDK15 TextBlocks - Fails when running jdk11 #1589

Merged
merged 18 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ dependencies {

gsonCompileOnly 'com.google.code.gson:gson:2.10.1'

cleanthatCompileOnly 'io.github.solven-eu.cleanthat:java:2.6'
compatCleanthat2Dot1CompileAndTestOnly 'io.github.solven-eu.cleanthat:java:2.6'
String VER_CLEANTHAT="2.8"
cleanthatCompileOnly "io.github.solven-eu.cleanthat:java:$VER_CLEANTHAT"
compatCleanthat2Dot1CompileAndTestOnly "io.github.solven-eu.cleanthat:java:$VER_CLEANTHAT"

gherkinCompileOnly 'io.cucumber:gherkin-utils:8.0.2'
gherkinCompileOnly 'org.slf4j:slf4j-api:2.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ private String doApply(String input) throws InterruptedException, IOException {
LOGGER.debug("Processing sourceJdk={} included={} excluded={}", jdkVersion, included, excluded, includeDraft);
LOGGER.debug("Available mutators: {}", JavaRefactorer.getAllIncluded());

// Spotless calls steps always with LF eol.
return refactorer.doFormat(input, LineEnding.LF);
return refactorer.doFormat(input);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class CleanthatJavaStep {
private static final String MAVEN_COORDINATE = "io.github.solven-eu.cleanthat:java";

// CleanThat changelog is available at https://github.com/solven-eu/cleanthat/blob/master/CHANGES.MD
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(11, "2.6");
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(11, "2.8");

// prevent direct instantiation
private CleanthatJavaStep() {}
Expand Down Expand Up @@ -83,8 +83,8 @@ public static boolean defaultIncludeDraft() {
public static FormatterStep create(String groupArtifact,
String version,
String sourceJdkVersion,
List<String> excluded,
List<String> included,
List<String> excluded,
boolean includeDraft,
Provisioner provisioner) {
Objects.requireNonNull(groupArtifact, "groupArtifact");
Expand All @@ -94,7 +94,7 @@ public static FormatterStep create(String groupArtifact,
Objects.requireNonNull(version, "version");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new JavaRefactorerState(NAME, groupArtifact, version, sourceJdkVersion, excluded, included, includeDraft, provisioner),
() -> new JavaRefactorerState(NAME, groupArtifact, version, sourceJdkVersion, included, excluded, includeDraft, provisioner),
JavaRefactorerState::createFormat);
}

Expand All @@ -120,7 +120,7 @@ static final class JavaRefactorerState implements Serializable {
final boolean includeDraft;

JavaRefactorerState(String stepName, String version, Provisioner provisioner) throws IOException {
this(stepName, MAVEN_COORDINATE, version, defaultSourceJdk(), defaultExcludedMutators(), defaultMutators(), defaultIncludeDraft(), provisioner);
this(stepName, MAVEN_COORDINATE, version, defaultSourceJdk(), defaultMutators(), defaultExcludedMutators(), defaultIncludeDraft(), provisioner);
}

JavaRefactorerState(String stepName,
Expand All @@ -131,10 +131,7 @@ static final class JavaRefactorerState implements Serializable {
List<String> excluded,
boolean includeDraft,
Provisioner provisioner) throws IOException {
// https://github.com/diffplug/spotless/issues/1583
if (!version.endsWith("-SNAPSHOT")) {
JVM_SUPPORT.assertFormatterSupported(version);
}
JVM_SUPPORT.assertFormatterSupported(version);
ModuleHelper.doOpenInternalPackagesIfRequired();
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
this.stepName = stepName;
Expand Down Expand Up @@ -162,17 +159,9 @@ FormatterFunc createFormat() {
throw new IllegalStateException("Issue executing the formatter", e);
}

// https://github.com/diffplug/spotless/issues/1583
if (!version.endsWith("-SNAPSHOT")) {
return JVM_SUPPORT.suggestLaterVersionOnError(version, input -> {
return (String) formatterMethod.invoke(formatter, input);
});
} else {
return input -> {
return (String) formatterMethod.invoke(formatter, input);
};
}
return JVM_SUPPORT.suggestLaterVersionOnError(version, input -> {
return (String) formatterMethod.invoke(formatter, input);
});
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,22 +15,47 @@
*/
package com.diffplug.spotless.java;

import java.util.Arrays;
import java.util.Objects;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.Provisioner;

/** Uses google-java-format, but only to remove unused imports. */
/** Uses google-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */
public class RemoveUnusedImportsStep {
static final String NAME = "removeUnusedImports";

static final String GJF = "google-java-format";
static final String CLEANTHAT = "cleanthat-javaparser-unnecessaryimport";

// https://github.com/solven-eu/cleanthat/blob/master/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators/UnnecessaryImport.java
private static final String CLEANTHAT_MUTATOR = "UnnecessaryImport";

// prevent direct instantiation
private RemoveUnusedImportsStep() {}

static final String NAME = "removeUnusedImports";
public static final String defaultFormatter() {
return GJF;
}

public static FormatterStep create(Provisioner provisioner) {
// The default importRemover is GJF
return create(GJF, provisioner);
}

public static FormatterStep create(String unusedImportRemover, Provisioner provisioner) {
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new GoogleJavaFormatStep.State(NAME, GoogleJavaFormatStep.defaultVersion(), provisioner),
GoogleJavaFormatStep.State::createRemoveUnusedImportsOnly);

if (GJF.equals(unusedImportRemover)) {
return FormatterStep.createLazy(NAME,
() -> new GoogleJavaFormatStep.State(NAME, GoogleJavaFormatStep.defaultVersion(), provisioner),
GoogleJavaFormatStep.State::createRemoveUnusedImportsOnly);
} else if (CLEANTHAT.equals(unusedImportRemover)) {
return FormatterStep.createLazy(NAME,
() -> new CleanthatJavaStep.JavaRefactorerState(NAME, CleanthatJavaStep.defaultGroupArtifact(), CleanthatJavaStep.defaultVersion(), "99.9", Arrays.asList(CLEANTHAT_MUTATOR), Arrays.asList(), false, provisioner),
CleanthatJavaStep.JavaRefactorerState::createFormat);
} else {
throw new IllegalArgumentException("Invalid unusedImportRemover: " + unusedImportRemover);
}
}
}
3 changes: 3 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Added
* `removeUnusedImport` can be configured to rely on `cleanthat-javaparser-unnecessaryimport`. Default remains `google-java-format`. ([#1589](https://github.com/diffplug/spotless/pull/1589))
### Changes
* Bump default `cleanthat` version to latest `2.6` -> `2.8`. ([#1589](https://github.com/diffplug/spotless/pull/1589)
* Support configuration of mirrors for P2 repositories ([#1629](https://github.com/diffplug/spotless/issues/1629)):
```
spotless {
Expand Down
23 changes: 18 additions & 5 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ spotless {
target 'src/*/java/**/*.java'
```

### removeUnusedImports

```
spotless {
java {
removeUnusedImports()
// optional: you may switch for `google-java-format` as underlying engine to `cleanthat-javaparser-unnecessaryimport`
// which enables processing any language level source file with a JDK8+ Runtime
removeUnusedImports().engine('cleanthat-javaparser-unnecessaryimport')
```

### google-java-format

[homepage](https://github.com/google/google-java-format). [changelog](https://github.com/google/google-java-format/releases).
Expand Down Expand Up @@ -272,11 +283,13 @@ spotless {
cleanthat()
// optional: you can specify a specific version and/or config file
cleanthat()
.groupArtifact('1.7') // default is 'io.github.solven-eu.cleanthat:java'
.version('2.1') // You may force a past of -SNAPSHOT
.sourceCompatibility('1.7') // default is '1.7'
.addMutator('your.custom.MagicMutator')
.excludeMutator('UseCollectionIsEmpty')
.groupArtifact('io.github.solven-eu.cleanthat:java') // Optional. Default is 'io.github.solven-eu.cleanthat:java'
.version('2.8') // You may force a custom version of Cleanthat
.sourceCompatibility('1.7') // default is '1.7'
.addMutator('SafeAndConsensual') // Default includes the SafeAndConsensual composite mutator
.addMutator('your.custom.MagicMutator') // List of mutators: https://github.com/solven-eu/cleanthat/blob/master/MUTATORS.generated.MD
.excludeMutator('UseCollectionIsEmpty') // You may exclude some mutators (from Composite ones)
.includeDraft(false) // You may exclude draft mutators (from Composite ones)
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ private FormatterStep createStep() {

/** Removes any unused imports. */
public void removeUnusedImports() {
addStep(RemoveUnusedImportsStep.create(provisioner()));
addStep(RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.defaultFormatter(), provisioner()));
}

public void removeUnusedImports(String formatter) {
addStep(RemoveUnusedImportsStep.create(formatter, provisioner()));
}

/** Uses the <a href="https://github.com/google/google-java-format">google-java-format</a> jar to format source code. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,22 @@ void multipleBlocksShouldWork() throws IOException {
gradleRunner().withArguments("spotlessApply").build();
gradleRunner().withArguments("spotlessApply").build();
}

@Test
void removeUnusedImportsWithCleanthat() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
" id 'java'",
"}",
"repositories { mavenCentral() }",
"",
"spotless {",
" java { removeUnusedImports('cleanthat-javaparser-unnecessaryimport') }",
"}");

setFile("src/main/java/test.java").toResource("java/removeunusedimports/Jdk17TextBlockUnformatted.test");
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/java/test.java").sameAsResource("java/removeunusedimports/Jdk17TextBlockFormatted.test");
}
}
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Added
* `removeUnusedImport` can be configured to rely on `cleanthat-javaparser-unnecessaryimport`. Default remains `google-java-format`. ([#1589](https://github.com/diffplug/spotless/pull/1589))
* The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)).
* Added support for Gherkin feature files ([#1649](https://github.com/diffplug/spotless/issues/1649)).
### Fixed
* Fix non deterministic computation of cache fingerprint when using multiple formatters. ([#1643](https://github.com/diffplug/spotless/pull/1643) fixes [#1642](https://github.com/diffplug/spotless/pull/1642))
### Changes
* Bump default `cleanthat` version to latest `2.6` -> `2.8`. ([#1589](https://github.com/diffplug/spotless/pull/1589)
* **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions &lt; `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))

Expand Down
15 changes: 12 additions & 3 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ any other maven phase (i.e. compile) then it can be configured as below;
</configuration>
```

### removeUnusedImports

```xml
<removeUnusedImports>
<engine>google-java-format</engine> <!-- optional. Defaults to `google-java-format`. Can be switched to `cleanthat-javaparser-unnecessaryimport` (e.g. to process JDK17 source files with a JDK8+ Runtime) -->
</removeUnusedImports>
```

### google-java-format

[homepage](https://github.com/google/google-java-format). [changelog](https://github.com/google/google-java-format/releases). [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java).
Expand Down Expand Up @@ -285,17 +293,18 @@ These mechanisms already exist for the Gradle plugin.

```xml
<cleanthat>
<version>2.0</version> <!-- optional version of Cleanthat -->
<version>2.8</version> <!-- optional version of Cleanthat -->
<sourceJdk>${maven.compiler.source}</sourceJdk> <!-- optional. Default to ${maven.compiler.source} else '1.7' -->
<mutators>
<mutator>*</mutator> <!-- optional. Default to '*' to include all mutators -->
<mutator>SafeAndConsensual</mutator> <!-- optional. Default to 'SafeAndConsensual' to include all mutators -->
</mutators>
<mutators> <!-- List of mutators: https://github.com/solven-eu/cleanthat/tree/master/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators -->
<mutators> <!-- List of mutators: https://github.com/solven-eu/cleanthat/blob/master/MUTATORS.generated.MD -->
<mutator>LiteralsFirstInComparisons</mutator> <!-- You may alternatively list the requested mutators -->
</mutators>
<excludedMutators>
<excludedMutator>OptionalNotEmpty</excludedMutator> <!-- You can discard specific rules -->
</excludedMutators>
<includeDraft>false</includeDraft> <!-- optional. Default to false, not to include draft mutators from Composite mutators -->
</cleanthat>
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,15 +15,20 @@
*/
package com.diffplug.spotless.maven.java;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class RemoveUnusedImports implements FormatterStepFactory {

@Parameter
private String engine = RemoveUnusedImportsStep.defaultFormatter();

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
return RemoveUnusedImportsStep.create(config.getProvisioner());
return RemoveUnusedImportsStep.create(engine, config.getProvisioner());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.shafthq.shaft.tools.tms;



import static io.restassured.RestAssured.*;

public class XrayIntegrationHelper {
private static String getLinkJIRATicketRequestBody() {
return """
{
"update":{
"issuelinks":[
{
"add":{
"type":{
"name":"Relates"
},
"outwardIssue":{
"key":"${TICKET_ID}"
}
}
}
]
}
}
""";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.shafthq.shaft.tools.tms;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.shaft.cli.FileActions;
import com.shaft.tools.io.ReportManager;
import io.github.shafthq.shaft.tools.io.helpers.ReportManagerHelper;
import io.restassured.config.RestAssuredConfig;
import io.restassured.config.SSLConfig;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Calendar;
import java.util.List;

import static io.restassured.RestAssured.*;
import static io.restassured.config.EncoderConfig.encoderConfig;


public class XrayIntegrationHelper {
private static String getLinkJIRATicketRequestBody() {
return """
{
"update":{
"issuelinks":[
{
"add":{
"type":{
"name":"Relates"
},
"outwardIssue":{
"key":"${TICKET_ID}"
}
}
}
]
}
}
""";
}
}
Loading