Skip to content

Commit

Permalink
Use var declarations everywhere. (#404)
Browse files Browse the repository at this point in the history
* Use var declarations everywhere.
Removing final for parameters and local variables (it is a question of taste)

* Removing commons dependency

* Adding new icon

---------

Co-authored-by: Björn Ekryd <bjornatekryd.se>
  • Loading branch information
Ekryd authored Feb 19, 2024
1 parent 4bf1dfb commit a3a21a7
Show file tree
Hide file tree
Showing 103 changed files with 630 additions and 727 deletions.
2 changes: 1 addition & 1 deletion maven-plugin/src/main/java/sortpom/SortMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SortMojo extends AbstractParentMojo {
public void setup(SortPomLogger mavenLogger) throws MojoFailureException {
new ExceptionConverter(
() -> {
PluginParameters pluginParameters =
var pluginParameters =
PluginParameters.builder()
.setPomFile(pomFile)
.setFileOutput(createBackupFile, backupFileExtension, null, keepTimestamp)
Expand Down
2 changes: 1 addition & 1 deletion maven-plugin/src/main/java/sortpom/VerifyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class VerifyMojo extends AbstractParentMojo {
public void setup(SortPomLogger mavenLogger) throws MojoFailureException {
new ExceptionConverter(
() -> {
PluginParameters pluginParameters =
var pluginParameters =
PluginParameters.builder()
.setPomFile(pomFile)
.setFileOutput(
Expand Down
10 changes: 5 additions & 5 deletions maven-plugin/src/test/java/sortpom/SortMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SortMojoTest {
@BeforeEach
void setup() {
sortMojo = new SortMojo();
ReflectionHelper mojoHelper = new ReflectionHelper(sortMojo);
var mojoHelper = new ReflectionHelper(sortMojo);
mojoHelper.setField(sortPom);
mojoHelper.setField("lineSeparator", "\n");
}
Expand All @@ -47,9 +47,9 @@ void executeShouldStartMojo() throws Exception {
void thrownExceptionShouldBeConvertedToMojoException() {
doThrow(new FailureException("Gurka")).when(sortPom).sortPom();

final Executable testMethod = () -> sortMojo.execute();
Executable testMethod = () -> sortMojo.execute();

final MojoFailureException thrown = assertThrows(MojoFailureException.class, testMethod);
var thrown = assertThrows(MojoFailureException.class, testMethod);

assertThat("Unexpected message", thrown.getMessage(), is(equalTo("Gurka")));
}
Expand All @@ -60,9 +60,9 @@ void thrownExceptionShouldBeConvertedToMojoExceptionInSetup() {
.when(sortPom)
.setup(any(SortPomLogger.class), any(PluginParameters.class));

final Executable testMethod = () -> sortMojo.setup(new MavenLogger(null, false));
Executable testMethod = () -> sortMojo.setup(new MavenLogger(null, false));

final MojoFailureException thrown = assertThrows(MojoFailureException.class, testMethod);
var thrown = assertThrows(MojoFailureException.class, testMethod);

assertThat("Unexpected message", thrown.getMessage(), is(equalTo("Gurka")));
}
Expand Down
10 changes: 5 additions & 5 deletions maven-plugin/src/test/java/sortpom/VerifyMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class VerifyMojoTest {
@BeforeEach
void setup() {
verifyMojo = new VerifyMojo();
ReflectionHelper mojoHelper = new ReflectionHelper(verifyMojo);
var mojoHelper = new ReflectionHelper(verifyMojo);
mojoHelper.setField(sortPom);
mojoHelper.setField("lineSeparator", "\n");
mojoHelper.setField("verifyFail", "SORT");
Expand All @@ -47,9 +47,9 @@ void executeShouldStartMojo() throws Exception {
void thrownExceptionShouldBeConvertedToMojoExceptionInExecute() {
doThrow(new FailureException("Gurka")).when(sortPom).verifyPom();

final Executable testMethod = () -> verifyMojo.execute();
Executable testMethod = () -> verifyMojo.execute();

final MojoFailureException thrown = assertThrows(MojoFailureException.class, testMethod);
var thrown = assertThrows(MojoFailureException.class, testMethod);

assertThat("Unexpected message", thrown.getMessage(), is(equalTo("Gurka")));
}
Expand All @@ -60,9 +60,9 @@ void thrownExceptionShouldBeConvertedToMojoExceptionInSetup() {
.when(sortPom)
.setup(any(SortPomLogger.class), any(PluginParameters.class));

final Executable testMethod = () -> verifyMojo.setup(new MavenLogger(null, false));
Executable testMethod = () -> verifyMojo.setup(new MavenLogger(null, false));

final MojoFailureException thrown = assertThrows(MojoFailureException.class, testMethod);
var thrown = assertThrows(MojoFailureException.class, testMethod);

assertThat("Unexpected message", thrown.getMessage(), is(equalTo("Gurka")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,42 @@ class ExceptionConverterTest {

@Test
void noExceptionShouldRunJustFine() throws MojoFailureException {
ExceptionConverter exceptionConverter = new ExceptionConverter(() -> {});
var exceptionConverter = new ExceptionConverter(() -> {});
exceptionConverter.executeAndConvertException();
assertThat(exceptionConverter, is(notNullValue()));
}

@Test
void failureExceptionShouldThrowMojoFailureException() {
FailureException failureException = new FailureException("Gurka");
var failureException = new FailureException("Gurka");

final Executable testMethod =
Executable testMethod =
() ->
new ExceptionConverter(
() -> {
throw failureException;
})
.executeAndConvertException();

final MojoFailureException thrown = assertThrows(MojoFailureException.class, testMethod);
var thrown = assertThrows(MojoFailureException.class, testMethod);

assertThat("Unexpected message", thrown.getMessage(), is(equalTo("Gurka")));
}

@Test
void failureExceptionShouldKeepCause() {
IllegalArgumentException cause = new IllegalArgumentException("not valid");
FailureException failureException = new FailureException("Gurka", cause);
var cause = new IllegalArgumentException("not valid");
var failureException = new FailureException("Gurka", cause);

final Executable testMethod =
Executable testMethod =
() ->
new ExceptionConverter(
() -> {
throw failureException;
})
.executeAndConvertException();

final MojoFailureException thrown = assertThrows(MojoFailureException.class, testMethod);
var thrown = assertThrows(MojoFailureException.class, testMethod);

assertAll(
() -> assertThat("Unexpected message", thrown.getMessage(), is(equalTo("Gurka"))),
Expand Down
34 changes: 15 additions & 19 deletions maven-plugin/src/test/java/sortpom/sort/SortMojoParametersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ void setup() throws SecurityException, IllegalArgumentException {

sortPomImpl = new ReflectionHelper(sortMojo).getField(SortPomImpl.class);
sortPomService = new ReflectionHelper(sortPomImpl).getField(SortPomService.class);
ReflectionHelper sortPomServiceHelper = new ReflectionHelper(sortPomService);
var sortPomServiceHelper = new ReflectionHelper(sortPomService);
fileUtil = sortPomServiceHelper.getField(FileUtil.class);
xmlOutputGenerator = sortPomServiceHelper.getField(XmlOutputGenerator.class);
WrapperFactoryImpl wrapperFactoryImpl = sortPomServiceHelper.getField(WrapperFactoryImpl.class);
var wrapperFactoryImpl = sortPomServiceHelper.getField(WrapperFactoryImpl.class);
xmlProcessingInstructionParser =
sortPomServiceHelper.getField(XmlProcessingInstructionParser.class);

Expand Down Expand Up @@ -85,7 +85,7 @@ void encodingParameter() {
void lineSeparatorParameter() {
assertParameterMoveFromMojoToRestOfApplication("lineSeparator", "\r");

final String lineSeparator =
var lineSeparator =
new ReflectionHelper(xmlOutputGenerator).getField("lineSeparator").toString();

assertThat(lineSeparator, is(equalTo("\r")));
Expand All @@ -95,8 +95,7 @@ void lineSeparatorParameter() {
void parameterNrOfIndentSpaceShouldEndUpInXmlProcessor() {
assertParameterMoveFromMojoToRestOfApplication("nrOfIndentSpace", 6);

final Object indentCharacters =
new ReflectionHelper(xmlOutputGenerator).getField("indentCharacters");
var indentCharacters = new ReflectionHelper(xmlOutputGenerator).getField("indentCharacters");

assertThat(indentCharacters, is(equalTo(" ")));
}
Expand All @@ -122,7 +121,7 @@ void predefinedSortOrderParameter() {
void parameterSortOrderFileShouldEndUpInFileUtil() {
assertParameterMoveFromMojoToRestOfApplication("sortOrderFile", "sortOrderFile.gurka");

Object actual = new ReflectionHelper(fileUtil).getField("customSortOrderFile");
var actual = new ReflectionHelper(fileUtil).getField("customSortOrderFile");

assertThat(actual, is(equalTo("sortOrderFile.gurka")));
}
Expand All @@ -131,8 +130,7 @@ void parameterSortOrderFileShouldEndUpInFileUtil() {
void parameterSortDependenciesShouldEndUpInElementWrapperCreator() {
assertParameterMoveFromMojoToRestOfApplication("sortDependencies", "groupId,scope");

Object sortDependencies =
new ReflectionHelper(elementWrapperCreator).getField("sortDependencies");
var sortDependencies = new ReflectionHelper(elementWrapperCreator).getField("sortDependencies");
assertThat(
sortDependencies.toString(), is("DependencySortOrder{childElementNames=[groupId, scope]}"));
}
Expand All @@ -141,7 +139,7 @@ void parameterSortDependenciesShouldEndUpInElementWrapperCreator() {
void parameterSortDependencyManagementShouldEndUpInElementWrapperCreator() {
assertParameterMoveFromMojoToRestOfApplication("sortDependencyManagement", "scope,groupId");

Object sortDependencies =
var sortDependencies =
new ReflectionHelper(elementWrapperCreator).getField("sortDependencyManagement");
assertThat(
sortDependencies.toString(), is("DependencySortOrder{childElementNames=[scope, groupId]}"));
Expand All @@ -151,7 +149,7 @@ void parameterSortDependencyManagementShouldEndUpInElementWrapperCreator() {
void parameterSortDependencyExclusionsShouldEndUpInElementWrapperCreator() {
assertParameterMoveFromMojoToRestOfApplication("sortDependencyExclusions", "groupId,scope");

Object sortDependencyExclusions =
var sortDependencyExclusions =
new ReflectionHelper(elementWrapperCreator).getField("sortDependencyExclusions");
assertThat(
sortDependencyExclusions.toString(),
Expand All @@ -162,7 +160,7 @@ void parameterSortDependencyExclusionsShouldEndUpInElementWrapperCreator() {
void parameterSortPluginsShouldEndUpInWrapperFactoryImpl() {
assertParameterMoveFromMojoToRestOfApplication("sortPlugins", "alfa,beta");

Object sortPlugins = new ReflectionHelper(elementWrapperCreator).getField("sortPlugins");
var sortPlugins = new ReflectionHelper(elementWrapperCreator).getField("sortPlugins");
assertThat(sortPlugins.toString(), is("DependencySortOrder{childElementNames=[alfa, beta]}"));
}

Expand Down Expand Up @@ -207,10 +205,9 @@ void parameterIndentSchemaLocationShouldEndUpInXmlProcessor() {

@Test
void xmlProcessingInstructionParserShouldGetLogger() throws MojoFailureException {
MavenLogger expectedLogger = new MavenLogger(null, false);
var expectedLogger = new MavenLogger(null, false);
sortMojo.setup(expectedLogger);
SortPomLogger logger =
new ReflectionHelper(xmlProcessingInstructionParser).getField(SortPomLogger.class);
var logger = new ReflectionHelper(xmlProcessingInstructionParser).getField(SortPomLogger.class);
assertThat(logger, not(nullValue()));
assertThat(logger, sameInstance(expectedLogger));
}
Expand All @@ -225,9 +222,8 @@ private void assertParameterMoveFromMojoToRestOfApplication(
throw new RuntimeException(e);
}

for (Object someInstanceThatContainParameter : whereParameterCanBeFound) {
Object actual =
new ReflectionHelper(someInstanceThatContainParameter).getField(parameterName);
for (var someInstanceThatContainParameter : whereParameterCanBeFound) {
var actual = new ReflectionHelper(someInstanceThatContainParameter).getField(parameterName);

assertThat(actual, is(equalTo(parameterValue)));
}
Expand All @@ -243,8 +239,8 @@ private void assertParameterMoveFromMojoToRestOfApplicationForBoolean(
throw new RuntimeException(e);
}

for (Object someInstanceThatContainParameter : whereParameterCanBeFound) {
boolean actual =
for (var someInstanceThatContainParameter : whereParameterCanBeFound) {
var actual =
(boolean) new ReflectionHelper(someInstanceThatContainParameter).getField(parameterName);

assertThat(actual, is(equalTo(true)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ void setup() throws SecurityException, IllegalArgumentException {

sortPomImpl = new ReflectionHelper(verifyMojo).getField(SortPomImpl.class);
sortPomService = new ReflectionHelper(sortPomImpl).getField(SortPomService.class);
ReflectionHelper sortPomServiceHelper = new ReflectionHelper(sortPomService);
var sortPomServiceHelper = new ReflectionHelper(sortPomService);
fileUtil = sortPomServiceHelper.getField(FileUtil.class);
xmlOutputGenerator = sortPomServiceHelper.getField(XmlOutputGenerator.class);
WrapperFactoryImpl wrapperFactoryImpl = sortPomServiceHelper.getField(WrapperFactoryImpl.class);
var wrapperFactoryImpl = sortPomServiceHelper.getField(WrapperFactoryImpl.class);
elementWrapperCreator =
new ReflectionHelper(wrapperFactoryImpl).getField(ElementWrapperCreator.class);
textWrapperCreator =
Expand Down Expand Up @@ -89,7 +89,7 @@ void encodingParameter() {
void lineSeparatorParameter() {
assertParameterMoveFromMojoToRestOfApplication("lineSeparator", "\r");

final String lineSeparator =
var lineSeparator =
new ReflectionHelper(xmlOutputGenerator).getField("lineSeparator").toString();

assertThat("\r", lineSeparator, is(equalTo("\r")));
Expand Down Expand Up @@ -125,16 +125,15 @@ void predefinedSortOrderParameter() {
void parameterSortOrderFileShouldEndUpInFileUtil() {
assertParameterMoveFromMojoToRestOfApplication("sortOrderFile", "sortOrderFile.gurka");

Object actual = new ReflectionHelper(fileUtil).getField("customSortOrderFile");
var actual = new ReflectionHelper(fileUtil).getField("customSortOrderFile");
assertThat(actual, is(equalTo("sortOrderFile.gurka")));
}

@Test
void parameterSortDependenciesShouldEndUpInElementWrapperCreator() {
assertParameterMoveFromMojoToRestOfApplication("sortDependencies", "groupId,scope");

Object sortDependencies =
new ReflectionHelper(elementWrapperCreator).getField("sortDependencies");
var sortDependencies = new ReflectionHelper(elementWrapperCreator).getField("sortDependencies");
assertThat(
sortDependencies.toString(), is("DependencySortOrder{childElementNames=[groupId, scope]}"));
}
Expand All @@ -143,7 +142,7 @@ void parameterSortDependenciesShouldEndUpInElementWrapperCreator() {
void parameterSortDependencyManagementShouldEndUpInElementWrapperCreator() {
assertParameterMoveFromMojoToRestOfApplication("sortDependencyManagement", "scope,groupId");

Object sortDependencies =
var sortDependencies =
new ReflectionHelper(elementWrapperCreator).getField("sortDependencyManagement");
assertThat(
sortDependencies.toString(),
Expand All @@ -154,7 +153,7 @@ void parameterSortDependencyManagementShouldEndUpInElementWrapperCreator() {
void parameterSortPluginsShouldEndUpInWrapperFactoryImpl() {
assertParameterMoveFromMojoToRestOfApplication("sortPlugins", "alfa,beta");

Object sortPlugins = new ReflectionHelper(elementWrapperCreator).getField("sortPlugins");
var sortPlugins = new ReflectionHelper(elementWrapperCreator).getField("sortPlugins");
assertThat(sortPlugins.toString(), is("DependencySortOrder{childElementNames=[alfa, beta]}"));
}

Expand Down Expand Up @@ -201,7 +200,7 @@ void parameterIndentSchemaLocationShouldEndUpInXmlProcessor() {
void parameterVerifyFailShouldEndUpInXmlProcessor() {
assertParameterMoveFromMojoToRestOfApplication("verifyFail", "STOP");

final Object verifyFailType = new ReflectionHelper(sortPomImpl).getField("verifyFailType");
var verifyFailType = new ReflectionHelper(sortPomImpl).getField("verifyFailType");

assertThat(verifyFailType, is(equalTo(VerifyFailType.STOP)));
}
Expand All @@ -216,9 +215,8 @@ private void assertParameterMoveFromMojoToRestOfApplication(
throw new RuntimeException(e);
}

for (Object someInstanceThatContainParameter : whereParameterCanBeFound) {
Object actual =
new ReflectionHelper(someInstanceThatContainParameter).getField(parameterName);
for (var someInstanceThatContainParameter : whereParameterCanBeFound) {
var actual = new ReflectionHelper(someInstanceThatContainParameter).getField(parameterName);

assertThat(actual, is(equalTo(parameterValue)));
}
Expand All @@ -234,8 +232,8 @@ private void assertParameterMoveFromMojoToRestOfApplicationForBoolean(
throw new RuntimeException(e);
}

for (Object someInstanceThatContainParameter : whereParameterCanBeFound) {
boolean actual =
for (var someInstanceThatContainParameter : whereParameterCanBeFound) {
var actual =
(boolean) new ReflectionHelper(someInstanceThatContainParameter).getField(parameterName);

assertThat(actual, is(equalTo(true)));
Expand Down
Binary file added misc/sortpom2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 3 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
Expand Down Expand Up @@ -143,8 +137,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<source>${compileSource}</source>
<target>${compileSource}</target>
<source>${maven.compiler.release}</source>
<target>${maven.compiler.release}</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -462,7 +456,7 @@
<maxmemory>512</maxmemory>
<breakiterator>true</breakiterator>
<quiet>true</quiet>
<source>${compileSource}</source>
<source>${maven.compiler.release}</source>
<verbose>false</verbose>
<linksource>true</linksource>
<links>
Expand Down
2 changes: 1 addition & 1 deletion sorter/src/main/java/sortpom/SortPomImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void sortPom() {

/** Verify that the pom-file is sorted regardless of formatting */
public void verifyPom() {
XmlOrderedResult xmlOrderedResult = getVerificationResult();
var xmlOrderedResult = getVerificationResult();
performVerfificationResult(xmlOrderedResult);
}

Expand Down
Loading

0 comments on commit a3a21a7

Please sign in to comment.