Skip to content

Commit

Permalink
Merge pull request #1143 from uhafner/openrewrite-var
Browse files Browse the repository at this point in the history
Apply several OpenRewrite rules
  • Loading branch information
uhafner committed Sep 12, 2024
2 parents fefa0f3 + 280822c commit 80ba46e
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 102 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,24 @@
<artifactId>rewrite-maven-plugin</artifactId>
<version>${rewrite-maven-plugin.version}</version>
<configuration>
<exclusions>
<exclusion>**/docker/**</exclusion>
<exclusion>**/AbstractComparableTest.java</exclusion>
<exclusion>**/ResourceExtractorTest.java</exclusion>
</exclusions>
<activeRecipes>
<recipe>org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions</recipe>
<recipe>org.openrewrite.maven.BestPractices</recipe>
<recipe>org.openrewrite.staticanalysis.MissingOverrideAnnotation</recipe>
<recipe>org.openrewrite.staticanalysis.CommonStaticAnalysis</recipe>
<recipe>org.openrewrite.staticanalysis.RemoveExtraSemicolons</recipe>
<recipe>org.openrewrite.java.migrate.Java8toJava11</recipe>
<recipe>org.openrewrite.java.RemoveUnusedImports</recipe>
<recipe>org.openrewrite.java.migrate.util.SequencedCollection</recipe>
<recipe>org.openrewrite.java.migrate.lang.var.UseVarForObject</recipe>
<recipe>org.openrewrite.java.format.RemoveTrailingWhitespace</recipe>
<recipe>org.openrewrite.java.format.BlankLines</recipe>
<recipe>org.openrewrite.java.format.EmptyNewlineAtEndOfFile</recipe>
<recipe>org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions</recipe>
</activeRecipes>
</configuration>
<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/util/Ensure.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public void isNotBlank(final String explanation, final Object... args) {
}

private boolean isBlank() {
String string = getValue();
var string = getValue();
if (string.isEmpty()) {

Check warning on line 482 in src/main/java/edu/hm/hafner/util/Ensure.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 482 is only partially covered, one branch is missing
return true;

Check warning on line 483 in src/main/java/edu/hm/hafner/util/Ensure.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered line

Line 483 is not covered by tests

Check warning on line 483 in src/main/java/edu/hm/hafner/util/Ensure.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered line

Line 483 is not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class FilteredLog implements Serializable {

private final String title;
private final int maxLines;
private int lines = 0;
private int lines;

private final List<String> infoMessages = new ArrayList<>();
private final List<String> errorMessages = new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/util/Generated.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import static java.lang.annotation.RetentionPolicy.CLASS;

/**
* This annotation is used to mark source code that has been generated or is somehow not relevant for style checking or
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/edu/hm/hafner/util/LineRangeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public final boolean addAll(final Iterable<? extends LineRange> ranges) {
*/
private void ensure(final int n) {
if (data.length < n) {

Check warning on line 127 in src/main/java/edu/hm/hafner/util/LineRangeList.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 127 (ConditionalsBoundaryMutator)
Raw output
Survived mutations:
- changed conditional boundary (org.pitest.mutationtest.engine.gregor.mutators.ConditionalsBoundaryMutator)
byte[] buf = new byte[Math.max(n, data.length * 2)];
var buf = new byte[Math.max(n, data.length * 2)];

Check warning on line 128 in src/main/java/edu/hm/hafner/util/LineRangeList.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 128 (MathMutator)
Raw output
Survived mutations:
- Replaced integer multiplication with division (org.pitest.mutationtest.engine.gregor.mutators.MathMutator)
System.arraycopy(data, 0, buf, 0, len);
data = buf;
}
Expand Down Expand Up @@ -204,7 +204,7 @@ public ListIterator<LineRange> listIterator(final int index) {
*/
public void trim() {
if (len != data.length) {

Check warning on line 206 in src/main/java/edu/hm/hafner/util/LineRangeList.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 206 is only partially covered, one branch is missing

Check warning on line 206 in src/main/java/edu/hm/hafner/util/LineRangeList.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 206 (NegateConditionalsMutator)
Raw output
Survived mutations:
- negated conditional (org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator)
byte[] small = new byte[len];
var small = new byte[len];
System.arraycopy(data, 0, small, 0, len);
data = small;
}
Expand Down Expand Up @@ -390,8 +390,8 @@ private void adjust(final int diff) {
* @return the changed line range
*/
public LineRange rewrite(final LineRange other) {
Cursor c = copy();
LineRange old = c.next();
var c = copy();
var old = c.next();
int oldSize = c.position - position;

Check warning on line 395 in src/main/java/edu/hm/hafner/util/LineRangeList.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 395 (MathMutator)
Raw output
Survived mutations:
- Replaced integer subtraction with addition (org.pitest.mutationtest.engine.gregor.mutators.MathMutator)
int newSize = sizeOf(other);
adjust(newSize - oldSize);

Check warning on line 397 in src/main/java/edu/hm/hafner/util/LineRangeList.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 397 (VoidMethodCallMutator)
Raw output
Survived mutations:
- removed call to edu/hm/hafner/util/LineRangeList$Cursor::adjust (org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator)
Expand Down Expand Up @@ -420,8 +420,8 @@ public void add(final LineRange v) {
* @return the deleted element
*/
public LineRange delete() {
Cursor c = copy();
LineRange old = c.next();
var c = copy();
var old = c.next();
adjust(position - c.position);
return old;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/hm/hafner/util/LookaheadStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class LookaheadStream implements AutoCloseable {
private final Iterator<String> lineIterator;
private final String fileName;

private boolean isLookaheadFilled = false;
private boolean isLookaheadFilled;
private String lookaheadLine = StringUtils.EMPTY;
private int line = 0;
private int line;

/**
* Wraps the specified stream of lines into a {@link LookaheadStream}.
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/edu/hm/hafner/util/PathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -42,7 +41,7 @@ public class PathUtil {
*/
public boolean exists(final String fileName) {
try {
return Files.exists(Paths.get(fileName));
return Files.exists(Path.of(fileName));
}
catch (IllegalArgumentException ignore) {
return false;
Expand Down Expand Up @@ -83,7 +82,7 @@ public boolean exists(final String fileName, final String directory) {
*/
public String getAbsolutePath(final String path) {
try {
return getAbsolutePath(Paths.get(path));
return getAbsolutePath(Path.of(path));
}
catch (IllegalArgumentException ignored) {
return makeUnixPath(path);
Expand Down Expand Up @@ -126,7 +125,7 @@ public String getAbsolutePath(final Path path) {
*/
public String getRelativePath(final Path base, final String path) {
try {
return getRelativePath(base, Paths.get(path));
return getRelativePath(base, Path.of(path));
}
catch (IllegalArgumentException ignored) {
return makeUnixPath(path);
Expand All @@ -149,7 +148,7 @@ public String getRelativePath(final Path base, final String path) {
*/
public String getRelativePath(final String base, final String path) {
try {
return getRelativePath(Paths.get(base), Paths.get(path));
return getRelativePath(Path.of(base), Path.of(path));
}
catch (IllegalArgumentException ignored) {
return makeUnixPath(path);
Expand All @@ -172,7 +171,7 @@ public String getRelativePath(final String base, final String path) {
*/
public String getRelativePath(final Path base, final Path path) {
try {
Path normalizedBase = normalize(base);
var normalizedBase = normalize(base);
if (path.isAbsolute()) {
return makeUnixPath(normalizedBase.relativize(normalize(path)).toString());
}
Expand Down Expand Up @@ -212,7 +211,7 @@ public String getRelativePath(final Path relative) {
*/
public String getRelativePath(final String relative) {
try {
return getRelativePath(Paths.get(relative));
return getRelativePath(Path.of(relative));
}
catch (IllegalArgumentException ignored) {
// ignore and return the path as such
Expand All @@ -234,7 +233,7 @@ public String createAbsolutePath(@CheckForNull final String directory, final Str
if (isAbsolute(fileName) || StringUtils.isBlank(directory)) {
return makeUnixPath(fileName);
}
String path = makeUnixPath(Objects.requireNonNull(directory));
var path = makeUnixPath(Objects.requireNonNull(directory));

String separator;
if (path.endsWith(SLASH)) {
Expand All @@ -245,7 +244,7 @@ public String createAbsolutePath(@CheckForNull final String directory, final Str
}

try {
String normalized = FilenameUtils.normalize(String.join(separator, path, fileName));
var normalized = FilenameUtils.normalize(String.join(separator, path, fileName));
return makeUnixPath(normalized == null ? fileName : normalized);
}
catch (IllegalArgumentException ignored) {
Expand Down Expand Up @@ -279,7 +278,7 @@ private Path normalize(final Path path) throws IOException {
}

private String makeUnixPath(final String fileName) {
String unixStyle = fileName.replace(BACK_SLASH, SLASH);
var unixStyle = fileName.replace(BACK_SLASH, SLASH);
if (unixStyle.matches(DRIVE_LETTER_PREFIX)) {
unixStyle = StringUtils.capitalize(unixStyle);
}
Expand Down
28 changes: 12 additions & 16 deletions src/main/java/edu/hm/hafner/util/ResourceExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Arrays;
import java.util.Enumeration;
Expand Down Expand Up @@ -44,19 +40,19 @@ public ResourceExtractor(final Class<?> targetClass) {

@VisibleForTesting
ResourceExtractor(final Class<?> targetClass, final ProtectionDomain protectionDomain) {
CodeSource codeSource = protectionDomain.getCodeSource();
var codeSource = protectionDomain.getCodeSource();
if (codeSource == null) {
throw new IllegalArgumentException("There is no CodeSource for " + targetClass);
}
URL location = codeSource.getLocation();
var location = codeSource.getLocation();
if (location == null) {
throw new IllegalArgumentException("There is no CodeSource location for " + targetClass);
}
String locationPath = location.getPath();
var locationPath = location.getPath();
if (StringUtils.isBlank(locationPath)) {
throw new IllegalArgumentException("The CodeSource location path is not set for " + targetClass);
}
Path entryPoint = new File(locationPath).toPath();
var entryPoint = new File(locationPath).toPath();
readingFromJarFile = Files.isRegularFile(entryPoint);
if (readingFromJarFile) {
extractor = new JarExtractor(entryPoint);
Expand Down Expand Up @@ -90,7 +86,7 @@ public void extract(final Path targetDirectory, final String source, final Strin
throw new IllegalArgumentException(
"Target directory must be an existing directory: " + targetDirectory); // implement
}
String[] allSources = Arrays.copyOf(sources, sources.length + 1);
var allSources = Arrays.copyOf(sources, sources.length + 1);
allSources[sources.length] = source;
extractor.extractFiles(targetDirectory, allSources);
}
Expand Down Expand Up @@ -124,7 +120,7 @@ private static class FolderExtractor extends Extractor {
public void extractFiles(final Path targetDirectory, final String... sources) {
try {
for (String source : sources) {
Path targetFile = targetDirectory.resolve(source);
var targetFile = targetDirectory.resolve(source);
Files.createDirectories(targetFile);
copy(targetFile, source);
}
Expand Down Expand Up @@ -155,11 +151,11 @@ private static class JarExtractor extends Extractor {
@Override
public void extractFiles(final Path targetDirectory, final String... sources) {
Set<String> remaining = Arrays.stream(sources).collect(Collectors.toSet());
try (JarFile jar = new JarFile(getEntryPoint().toFile())) {
try (var jar = new JarFile(getEntryPoint().toFile())) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
var entry = entries.nextElement();
var name = entry.getName();
if (remaining.contains(name)) {
copy(targetDirectory, jar, entry, name);
remaining.remove(name);
Expand All @@ -176,15 +172,15 @@ public void extractFiles(final Path targetDirectory, final String... sources) {

private void copy(final Path targetDirectory, final JarFile jar, final JarEntry entry, final String name)
throws IOException {
Path targetFile = targetDirectory.resolve(name);
var targetFile = targetDirectory.resolve(name);
if (!targetFile.normalize().startsWith(targetDirectory)) {

Check warning on line 176 in src/main/java/edu/hm/hafner/util/ResourceExtractor.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 176 is only partially covered, one branch is missing
throw new IllegalArgumentException("Corrupt jar structure, contains invalid path: " + name);

Check warning on line 177 in src/main/java/edu/hm/hafner/util/ResourceExtractor.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered line

Line 177 is not covered by tests
}
Path parent = targetFile.getParent();
var parent = targetFile.getParent();
if (parent != null) {

Check warning on line 180 in src/main/java/edu/hm/hafner/util/ResourceExtractor.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 180 is only partially covered, one branch is missing
Files.createDirectories(parent);
}
try (InputStream inputStream = jar.getInputStream(entry); OutputStream outputStream = Files.newOutputStream(targetFile)) {
try (var inputStream = jar.getInputStream(entry); var outputStream = Files.newOutputStream(targetFile)) {
IOUtils.copy(inputStream, outputStream);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public SAXParser createSaxParser() {
var factory = createSaxParserFactory();
configureSaxParserFactory(factory);

Check warning on line 154 in src/main/java/edu/hm/hafner/util/SecureXmlParserFactory.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 154 (VoidMethodCallMutator)
Raw output
Survived mutations:
- removed call to edu/hm/hafner/util/SecureXmlParserFactory::configureSaxParserFactory (org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator)

SAXParser parser = factory.newSAXParser();
var parser = factory.newSAXParser();
secureParser(parser);

Check warning on line 157 in src/main/java/edu/hm/hafner/util/SecureXmlParserFactory.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 157 (VoidMethodCallMutator)
Raw output
Survived mutations:
- removed call to edu/hm/hafner/util/SecureXmlParserFactory::secureParser (org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator)
return parser;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/edu/hm/hafner/util/TreeString.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ String getLabel() {
TreeString split(final String prefix) {
Ensure.that(getLabel().startsWith(prefix)).isTrue();

Check warning on line 71 in src/main/java/edu/hm/hafner/util/TreeString.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 71 (VoidMethodCallMutator)
Raw output
Survived mutations:
- removed call to edu/hm/hafner/util/Ensure$BooleanCondition::isTrue (org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator)

char[] suffix = new char[label.length - prefix.length()];
var suffix = new char[label.length - prefix.length()];
System.arraycopy(label, prefix.length(), suffix, 0, suffix.length);

var middle = new TreeString(parent, prefix);
Expand All @@ -94,7 +94,7 @@ TreeString getParent() {
*/
private int depth() {
int i = 0;
for (TreeString p = this; p != null; p = p.parent) {
for (var p = this; p != null; p = p.parent) {
i++;
}
return i;
Expand All @@ -121,10 +121,10 @@ public int hashCode() {
*/
@Override
public String toString() {
char[][] tokens = new char[depth()][];
var tokens = new char[depth()][];
int i = tokens.length;
int sz = 0;
for (TreeString p = this; p != null; p = p.parent) {
for (var p = this; p != null; p = p.parent) {
tokens[--i] = p.label;
sz += p.label.length;
}
Expand All @@ -144,8 +144,8 @@ public String toString() {
* the table containing the existing strings
*/
void dedup(final Map<String, char[]> table) {
String l = getLabel();
char[] v = table.get(l);
var l = getLabel();
var v = table.get(l);
if (v == null) {
table.put(l, label);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/edu/hm/hafner/util/TreeStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public Child intern(final String string) {
if (plen > 0) {
if (plen < entry.getKey().length()) {
// insert a node between this and entry.value
Child child = entry.getValue();
String prefix = string.substring(0, plen);
Child middle = child.split(prefix);
var child = entry.getValue();
var prefix = string.substring(0, plen);
var middle = child.split(prefix);

// add 'middle' instead of 'child'
children.remove(entry.getKey());
Expand All @@ -107,7 +107,7 @@ public Child intern(final String string) {
}

// no common prefix. an entirely new node.
Child t = children.get(string);
var t = children.get(string);
if (t == null) {

Check warning on line 111 in src/main/java/edu/hm/hafner/util/TreeStringBuilder.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 111 is only partially covered, one branch is missing
t = new Child(new TreeString(getNode(), string));
children.put(string, t);
Expand All @@ -134,7 +134,7 @@ private void makeWritable() {
* @return the node
*/
private Child split(final String prefix) {
String suffix = getNode().getLabel().substring(prefix.length());
var suffix = getNode().getLabel().substring(prefix.length());

var middle = new Child(getNode().split(prefix));
middle.makeWritable();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/edu/hm/hafner/util/AbstractEqualsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void shouldReturnFalseOnEqualsNull() {
@SuppressWarnings("EqualsWithItself")
@Test
void shouldReturnTrueOnEqualsThis() {
Object sut = createSut();
var sut = createSut();

assertThat(sut.equals(sut)).isTrue();
}
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/edu/hm/hafner/util/ArchitectureRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.junit.jupiter.params.ParameterizedTest;

import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget;
import com.tngtech.archunit.core.domain.JavaCall;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaConstructorCall;
Expand Down Expand Up @@ -188,7 +187,7 @@ private static class ExceptionHasNoContext extends DescribedPredicate<JavaConstr

@Override
public boolean test(final JavaConstructorCall javaConstructorCall) {
ConstructorCallTarget target = javaConstructorCall.getTarget();
var target = javaConstructorCall.getTarget();
if (!target.getRawParameterTypes().isEmpty()) {
return false;
}
Expand Down
Loading

0 comments on commit 80ba46e

Please sign in to comment.