Skip to content

Commit

Permalink
Merge branch 'main' into renovate/com.fasterxml.jackson.core-jackson-…
Browse files Browse the repository at this point in the history
…databind-2.x
  • Loading branch information
nedtwigg committed Feb 10, 2023
2 parents 4fac2e9 + 4ed7074 commit 5a04060
Show file tree
Hide file tree
Showing 80 changed files with 1,416 additions and 196 deletions.
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Added
* CleanThat Java Refactorer. ([#1560](https://github.com/diffplug/spotless/pull/1560))
* Introduce `LazyArgLogger` to allow for lazy evaluation of log messages in slf4j logging. ([#1565](https://github.com/diffplug/spotless/pull/1565))
### Fixed
* Allow multiple instances of the same npm-based formatter to be used by separating their `node_modules` directories. ([#1565](https://github.com/diffplug/spotless/pull/1565))
* `ktfmt` default style uses correct continuation indent. ([#1562](https://github.com/diffplug/spotless/pull/1562))

## [2.34.1] - 2023-02-05
### Changes
* **POTENTIALLY BREAKING** Bump bytecode from Java 8 to 11 ([#1530](https://github.com/diffplug/spotless/pull/1530) part 2 of [#1337](https://github.com/diffplug/spotless/issues/1337))
### Fixed
* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546))
* `freshmark` fixed on java 15+ ([#1304](https://github.com/diffplug/spotless/pull/1304) fixes [#803](https://github.com/diffplug/spotless/issues/803))

## [2.34.0] - 2023-01-26
### Added
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ If it doesn't work, you can check the JitPack log at `https://jitpack.io/com/git

### Maven

Run `./gradlew publishToMavenLocal` to publish this to your local repository. The maven plugin is not published to JitPack due to [jitpack/jitpack.io#4112](https://github.com/jitpack/jitpack.io/issues/4112).
Run `./gradlew publishToMavenLocal` to publish this to your local repository. You can also use the JitPack artifacts, using the same principles as Gradle above.

## License

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ lib('java.PalantirJavaFormatStep') +'{{yes}} | {{yes}}
lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
extra('java.EclipseJdtFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
lib('java.FormatAnnotationsStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
lib('java.CleanthatJavaStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
lib('json.gson.GsonStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
lib('json.JacksonJsonStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
lib('json.JsonSimpleStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
Expand Down Expand Up @@ -131,6 +132,7 @@ lib('yaml.JacksonYamlStep') +'{{yes}} | {{yes}}
| [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
| [`java.EclipseJdtFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
| [`java.FormatAnnotationsStep`](lib/src/main/java/com/diffplug/spotless/java/FormatAnnotationsStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
| [`java.CleanthatJavaStep`](lib/src/main/java/com/diffplug/spotless/java/CleanthatJavaStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
| [`json.gson.GsonStep`](lib/src/main/java/com/diffplug/spotless/json/gson/GsonStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
| [`json.JacksonJsonStep`](lib/src/main/java/com/diffplug/spotless/json/JacksonJsonStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
| [`json.JsonSimpleStep`](lib/src/main/java/com/diffplug/spotless/json/JsonSimpleStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ VER_DURIAN=1.2.0
VER_JGIT=6.4.0.202211300538-r
VER_JUNIT=5.9.2
VER_ASSERTJ=3.24.2
VER_MOCKITO=5.0.0
VER_MOCKITO=5.1.1
6 changes: 5 additions & 1 deletion gradle/java-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ model {
}
}

if (!version.endsWith('-SNAPSHOT')) {
if (System.env['JITPACK'] == 'true' || version.endsWith('-SNAPSHOT')) {
signing {
setRequired(false)
}
} else {
signing {
String gpg_key = decode64('ORG_GRADLE_PROJECT_gpg_key64')
useInMemoryPgpKeys(gpg_key, System.env['ORG_GRADLE_PROJECT_gpg_passphrase'])
Expand Down
26 changes: 0 additions & 26 deletions gradle/spotless-freshmark.gradle
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@

import java.util.regex.Matcher
import java.util.regex.Pattern


def thisVm() {
String jre = System.getProperty("java.version")
if (jre.startsWith("1.8")) {
return 8
} else {
Matcher matcher = Pattern.compile("(\\d+)").matcher(jre)
if (!matcher.find()) {
throw new IllegalArgumentException("Expected " + jre + " to start with an integer")
}
int version = Integer.parseInt(matcher.group(1))
if (version <= 8) {
throw new IllegalArgumentException("Expected " + jre + " to start with an integer greater than 8")
}
return version
}
}

if (thisVm() >= 15) {
// freshmark doesn't run on JRE 15+
return
}

apply plugin: 'com.diffplug.spotless'

import com.diffplug.gradle.spotless.FreshMarkExtension
Expand Down
2 changes: 2 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jdk:
- openjdk11
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 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 Down Expand Up @@ -42,6 +42,8 @@
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.googlecode.concurrenttrees.radix.ConcurrentRadixTree;
import com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharSequenceNodeFactory;
Expand All @@ -61,6 +63,8 @@
* back to the platform native.
*/
public final class GitAttributesLineEndings {
private static final Logger LOGGER = LoggerFactory.getLogger(GitAttributesLineEndings.class);

// prevent direct instantiation
private GitAttributesLineEndings() {}

Expand Down Expand Up @@ -261,7 +265,7 @@ private static String convertEolToLineEnding(String eol, File file) {
case "crlf":
return LineEnding.WINDOWS.str();
default:
System.err.println(".gitattributes file has unspecified eol value: " + eol + " for " + file + ", defaulting to platform native");
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
return LineEnding.PLATFORM_NATIVE.str();
}
}
Expand Down Expand Up @@ -341,8 +345,7 @@ private static List<AttributesRule> parseRules(@Nullable File file) {
return parsed.getRules();
} catch (IOException e) {
// no need to crash the whole plugin
System.err.println("Problem parsing " + file.getAbsolutePath());
e.printStackTrace();
LOGGER.warn("Problem parsing {}", file.getAbsolutePath(), e);
}
}
return Collections.emptyList();
Expand Down
12 changes: 11 additions & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def NEEDS_GLUE = [
'diktat',
'scalafmt',
'jackson',
'gson'
'gson',
'cleanthat'
]
for (glue in NEEDS_GLUE) {
sourceSets.register(glue) {
Expand All @@ -39,6 +40,12 @@ versionCompatibility {
]
targetSourceSetName = 'ktlint'
}
namespaces.register('Cleanthat') {
versions = [
'2.1',
]
targetSourceSetName = 'cleanthat'
}
}
}

Expand Down Expand Up @@ -100,6 +107,9 @@ dependencies {
flexmarkCompileOnly 'com.vladsch.flexmark:flexmark-all:0.62.2'

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

cleanthatCompileOnly 'io.github.solven-eu.cleanthat:java:2.1'
compatCleanthat2Dot1CompileAndTestOnly 'io.github.solven-eu.cleanthat:java:2.1'
}

// we'll hold the core lib to a high standard
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2023 DiffPlug
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.diffplug.spotless.glue.java;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diffplug.spotless.FormatterFunc;

import eu.solven.cleanthat.config.pojo.CleanthatEngineProperties;
import eu.solven.cleanthat.config.pojo.SourceCodeProperties;
import eu.solven.cleanthat.engine.java.IJdkVersionConstants;
import eu.solven.cleanthat.engine.java.refactorer.JavaRefactorer;
import eu.solven.cleanthat.engine.java.refactorer.JavaRefactorerProperties;
import eu.solven.cleanthat.formatter.LineEnding;

/**
* The glue for CleanThat: it is build over the version in build.gradle, but at runtime it will be executed over
* the version loaded in JarState, which is by default defined in com.diffplug.spotless.java.CleanthatJavaStep#JVM_SUPPORT
*/
public class JavaCleanthatRefactorerFunc implements FormatterFunc {
private static final Logger LOGGER = LoggerFactory.getLogger(JavaCleanthatRefactorerFunc.class);

private String jdkVersion;
private List<String> included;
private List<String> excluded;

public JavaCleanthatRefactorerFunc(String jdkVersion, List<String> included, List<String> excluded) {
this.jdkVersion = jdkVersion == null ? IJdkVersionConstants.JDK_8 : jdkVersion;
this.included = included == null ? Collections.emptyList() : included;
this.excluded = excluded == null ? Collections.emptyList() : excluded;
}

public JavaCleanthatRefactorerFunc() {
this(IJdkVersionConstants.JDK_8, Arrays.asList(JavaRefactorerProperties.WILDCARD), Arrays.asList());
}

@Override
public String apply(String input) throws Exception {
// https://stackoverflow.com/questions/1771679/difference-between-threads-context-class-loader-and-normal-classloader
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try {
// Ensure CleanThat main Thread has its custom classLoader while executing its refactoring
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
return doApply(input);
} finally {
// Restore the originalClassLoader
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}

private String doApply(String input) throws InterruptedException, IOException {
// call some API that uses reflection without taking ClassLoader param
CleanthatEngineProperties engineProperties = CleanthatEngineProperties.builder().engineVersion(jdkVersion).build();

// Spotless will push us LF content
engineProperties.setSourceCode(SourceCodeProperties.builder().lineEnding(LineEnding.LF).build());

JavaRefactorerProperties refactorerProperties = new JavaRefactorerProperties();

refactorerProperties.setIncluded(included);
refactorerProperties.setExcluded(excluded);

JavaRefactorer refactorer = new JavaRefactorer(engineProperties, refactorerProperties);

LOGGER.debug("Processing sourceJdk={} included={} excluded={}", jdkVersion, included, excluded);
LOGGER.debug("Available mutators: {}", JavaRefactorer.getAllIncluded());

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonWriter;
Expand Down Expand Up @@ -57,8 +58,8 @@ public String apply(String inputString) {
if (jsonElement == null) {
throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE);
}
if (gsonConfig.isSortByKeys() && jsonElement.isJsonObject()) {
jsonElement = sortByKeys(jsonElement.getAsJsonObject());
if (gsonConfig.isSortByKeys()) {
jsonElement = sortByKeys(jsonElement);
}
try (StringWriter stringWriter = new StringWriter()) {
JsonWriter jsonWriter = new JsonWriter(stringWriter);
Expand All @@ -72,19 +73,36 @@ public String apply(String inputString) {
return result;
}

private JsonElement sortByKeys(JsonElement jsonElement) {
if (jsonElement.isJsonArray()) {
return sortByKeys(jsonElement.getAsJsonArray());
} else if (jsonElement.isJsonObject()) {
return sortByKeys(jsonElement.getAsJsonObject());
} else {
return jsonElement;
}
}

private JsonElement sortByKeys(JsonObject jsonObject) {
JsonObject result = new JsonObject();
jsonObject.keySet().stream().sorted()
.forEach(key -> {
JsonElement element = jsonObject.get(key);
if (element.isJsonObject()) {
element = sortByKeys(element.getAsJsonObject());
}
result.add(key, element);
JsonElement sorted = sortByKeys(jsonObject.get(key));
result.add(key, sorted);
});
return result;
}

private JsonElement sortByKeys(JsonArray jsonArray) {
var result = new JsonArray();
for (JsonElement element : jsonArray) {
JsonElement sorted = sortByKeys(element);
result.add(sorted);
}

return result;
}

private String generateIndent(int indentSpaces) {
return String.join("", Collections.nCopies(indentSpaces, " "));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 DiffPlug
* Copyright 2022-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 Down Expand Up @@ -78,7 +78,7 @@ private FormattingOptions createFormattingOptions() {
formattingOptions.getStyle(),
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getBlockIndent()),
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
formattingOptions.getDebuggingPrintOpsAfterFormatting());
}
Expand Down
Loading

0 comments on commit 5a04060

Please sign in to comment.