Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Feb 16, 2023
1 parent 6555254 commit b543266
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,28 @@ private void addSourceTasks() {
TaskProvider<Task> checkAllProvider = tasks.register(CheckFormat.NAME);
checkAllProvider.configure((checkAll) -> checkAll.setDescription(CheckFormat.DESCRIPTION));
tasks.named(JavaBasePlugin.CHECK_TASK_NAME).configure((check) -> check.dependsOn(checkAllProvider));
this.project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets()
.all((sourceSet) -> addSourceTasks(sourceSet, checkAllProvider, formatAllProvider));
this.project.getConvention()
.getPlugin(JavaPluginConvention.class)
.getSourceSets()
.all((sourceSet) -> addSourceTasks(sourceSet, checkAllProvider, formatAllProvider));
});
}

private void addSourceTasks(SourceSet sourceSet, TaskProvider<Task> checkAllProvider, TaskProvider<Task> formatAllProvider) {
private void addSourceTasks(SourceSet sourceSet, TaskProvider<Task> checkAllProvider,
TaskProvider<Task> formatAllProvider) {
TaskProvider<CheckFormat> checkTaskProvider = addFormatterTask(sourceSet, CheckFormat.class, CheckFormat.NAME,
CheckFormat.DESCRIPTION);
checkTaskProvider.configure((checkTask) -> checkTask.setReportLocation(
new File(this.project.getBuildDir(), "reports/format/" + sourceSet.getName() + "/check-format.txt")));
checkAllProvider.configure((checkAll) -> checkAll.dependsOn(checkTaskProvider));
TaskProvider<Format> formatTaskProvider = addFormatterTask(sourceSet, Format.class, Format.NAME, Format.DESCRIPTION);
TaskProvider<Format> formatTaskProvider = addFormatterTask(sourceSet, Format.class, Format.NAME,
Format.DESCRIPTION);
formatTaskProvider.configure((format) -> format.conventionMapping("encoding", () -> "UTF-8"));
formatAllProvider.configure((formatAll) -> formatAll.dependsOn(formatTaskProvider));
}

private <T extends FormatterTask> TaskProvider<T> addFormatterTask(SourceSet sourceSet, Class<T> taskType, String name,
String desc) {
private <T extends FormatterTask> TaskProvider<T> addFormatterTask(SourceSet sourceSet, Class<T> taskType,
String name, String desc) {
String taskName = sourceSet.getTaskName(name, null);
TaskProvider<T> provider = this.project.getTasks().register(taskName, taskType);
provider.configure((task) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public class CheckFormat extends FormatterTask {

@TaskAction
public void checkFormatting() throws IOException, InterruptedException {
List<File> problems = formatFiles().filter(FileEdit::hasEdits).map(FileEdit::getFile)
.collect(Collectors.toList());
List<File> problems = formatFiles().filter(FileEdit::hasEdits)
.map(FileEdit::getFile)
.collect(Collectors.toList());
this.reportLocation.getParentFile().mkdirs();
if (!problems.isEmpty()) {
StringBuilder message = new StringBuilder("Formatting violations found in the following files:\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import io.spring.format.formatter.intellij.state.State;

/**
* {@link Monitor} that looks for a {@code spring-javaformat-gradle-plugin}
* declaration in the build.gradle file.
* {@link Monitor} that looks for a {@code spring-javaformat-gradle-plugin} declaration in
* the build.gradle file.
*
* @author Phillip Webb
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ void replaceAppliesToIntellijDocument() throws Exception {
assertThat(adapter.get()).isEqualTo("help");
verify(intellijDocument).replaceString(3, 5, "p");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ public final void execute() throws MojoExecutionException, MojoFailureException
}

private Stream<File> resolve(List<String> directories) {
return directories.stream().map(directory -> FileUtils.resolveFile(this.project.getBasedir(), directory))
.filter(this::include);
return directories.stream()
.map(directory -> FileUtils.resolveFile(this.project.getBasedir(), directory))
.filter(this::include);
}

private boolean include(File file) {
Expand Down Expand Up @@ -168,8 +169,10 @@ private List<File> scan(File directory) {
scanner.setCaseSensitive(false);
scanner.setFollowSymlinks(false);
scanner.scan();
return Arrays.asList(scanner.getIncludedFiles()).stream().map(name -> new File(directory, name))
.collect(Collectors.toList());
return Arrays.asList(scanner.getIncludedFiles())
.stream()
.map(name -> new File(directory, name))
.collect(Collectors.toList());
}

private boolean hasLength(Object[] array) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ protected void execute(List<File> files, Charset encoding, String lineSeparator)
getLog().debug("skipping validation as per configuration.");
return;
}
List<File> problems = getFormatter().formatFiles(files, encoding, lineSeparator).filter(FileEdit::hasEdits)
.map(FileEdit::getFile).collect(Collectors.toList());
List<File> problems = getFormatter().formatFiles(files, encoding, lineSeparator)
.filter(FileEdit::hasEdits)
.map(FileEdit::getFile)
.collect(Collectors.toList());
if (!problems.isEmpty()) {
StringBuilder message = new StringBuilder("Formatting violations found in the following files:\n");
problems.stream().forEach((f) -> message.append(" * " + f + "\n"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public void verify(File base, String lineSeparator, boolean spaces) throws IOExc
String formated = new String(Files.readAllBytes(base.toPath().resolve(JAVA_FILE)), StandardCharsets.UTF_8);
String indent = (!spaces) ? " " : " ";
assertThat(formated).contains("Simple." + lineSeparator + " *" + lineSeparator + " * @author")
.contains("public class Simple {").contains(indent + "public static void main");
.contains("public class Simple {")
.contains(indent + "public static void main");
}

public static void main(String[] args) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ public Collection<FileSetCheck> load(PropertyResolver propertyResolver) {
System.out.println(getClass().getResource("spring-checkstyle.xml"));
Configuration config = loadConfiguration(getClass().getResourceAsStream("spring-checkstyle.xml"),
propertyResolver);
return Arrays.stream(config.getChildren()).filter(this.moduleFactory::nonFiltered).map(this::load)
.collect(Collectors.toList());
return Arrays.stream(config.getChildren())
.filter(this.moduleFactory::nonFiltered)
.map(this::load)
.collect(Collectors.toList());
}

private Configuration loadConfiguration(InputStream inputStream, PropertyResolver propertyResolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public class SpringHideUtilityClassConstructor extends HideUtilityClassConstruct
annotations.add("org.springframework.context.annotation.Configuration");
annotations.add("org.springframework.boot.autoconfigure.SpringBootApplication");
annotations.add("org.springframework.boot.autoconfigure.EnableAutoConfiguration");
Set<String> shortNames = annotations.stream().map((name) -> name.substring(name.lastIndexOf(".") + 1))
.collect(Collectors.toSet());
Set<String> shortNames = annotations.stream()
.map((name) -> name.substring(name.lastIndexOf(".") + 1))
.collect(Collectors.toSet());
annotations.addAll(shortNames);
BYPASS_ANNOTATIONS = Collections.unmodifiableSet(annotations);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public void beginTree(DetailAST rootAST) {
@Override
public void visitToken(DetailAST ast) {
switch (ast.getType()) {
case TokenTypes.METHOD_DEF:
visitMethodDef(ast);
case TokenTypes.IMPORT:
visitImport(ast);
break;
case TokenTypes.METHOD_DEF:
visitMethodDef(ast);
case TokenTypes.IMPORT:
visitImport(ast);
break;
}
}

Expand Down Expand Up @@ -172,8 +172,8 @@ private void log(DetailAST method, String key) {
}

public void setUnlessImports(String unlessImports) {
this.unlessImports = Collections.unmodifiableList(
Arrays.stream(unlessImports.split(",")).map(String::trim).collect(Collectors.toList()));
this.unlessImports = Collections
.unmodifiableList(Arrays.stream(unlessImports.split(",")).map(String::trim).collect(Collectors.toList()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ private void checkSinceTag(DetailAST ast, TextBlock javadoc) {
}
String[] text = javadoc.getText();
DetailAST interfaceOrAnnotationDef = getInterfaceOrAnnotationDef(ast);
boolean privateType = !isPublicOrProtected(ast) && (interfaceOrAnnotationDef == null || !isPublicOrProtected(interfaceOrAnnotationDef));
boolean privateType = !isPublicOrProtected(ast)
&& (interfaceOrAnnotationDef == null || !isPublicOrProtected(interfaceOrAnnotationDef));
boolean innerType = ast.getParent() != null && ast.getParent().getType() != TokenTypes.COMPILATION_UNIT;
boolean found = false;
for (int i = 0; i < text.length; i++) {
Expand Down Expand Up @@ -231,7 +232,7 @@ private DetailAST getInterfaceOrAnnotationDef(DetailAST ast) {

private DetailAST findParent(DetailAST ast, int... classDefs) {
while (ast != null) {
for (int classDef: classDefs) {
for (int classDef : classDefs) {
if (ast.getType() == classDef) {
return ast;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class SpringMethodOrderCheck extends AbstractSpringCheck {

private static final List<String> EXPECTED_ORDER = Collections
.unmodifiableList(Arrays.asList("equals", "hashCode", "toString"));
.unmodifiableList(Arrays.asList("equals", "hashCode", "toString"));

@Override
public int[] getAcceptableTokens() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ private void visitQuestion(DetailAST ast) {
private boolean requiresParens(DetailAST expression) {
if (expression != null && expression.getChildCount() > 1) {
switch (expression.getType()) {
case TokenTypes.METHOD_CALL:
case TokenTypes.DOT:
return false;
case TokenTypes.METHOD_CALL:
case TokenTypes.DOT:
return false;
}
return true;
}
Expand All @@ -86,13 +86,13 @@ private boolean isSimpleEqualsExpression(DetailAST expression) {

private boolean isEqualsTestAllowed(DetailAST ast) {
switch (this.equalsTest) {
case ANY:
return true;
case NEVER:
return false;
case NEVER_FOR_NULLS:
DetailAST equal = ast.findFirstToken(TokenTypes.EQUAL);
return equal.findFirstToken(TokenTypes.LITERAL_NULL) == null;
case ANY:
return true;
case NEVER:
return false;
case NEVER_FOR_NULLS:
DetailAST equal = ast.findFirstToken(TokenTypes.EQUAL);
return equal.findFirstToken(TokenTypes.LITERAL_NULL) == null;
}
throw new IllegalStateException("Unsupported equals test " + this.equalsTest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void recordLevel(AuditEvent event) {

private void recordLocalizedMessage(String message, String... args) {
recordMessage(new Violation(0, Definitions.CHECKSTYLE_BUNDLE, message, args, null, Violation.class, null)
.getViolation());
.getViolation());
}

private void recordMessage(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ private void printDebugInfo(File file) throws CheckstyleException {
}

public static Collection<Parameter> paramaters() throws IOException {
ArrayList<Parameter> parameters = Arrays.stream(SOURCES_DIR.listFiles(SpringChecksTests::sourceFile)).sorted()
.map(Parameter::new).collect(Collectors.toCollection(ArrayList::new));
ArrayList<Parameter> parameters = Arrays.stream(SOURCES_DIR.listFiles(SpringChecksTests::sourceFile))
.sorted()
.map(Parameter::new)
.collect(Collectors.toCollection(ArrayList::new));
parameters.add(new Parameter(new File(SOURCES_DIR, "nopackageinfo/NoPackageInfo.java")));
return parameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void loadShouldLoadChecks() {
@Test
public void loadWithExcludeShouldExcludeChecks() {
Set<String> excludes = Collections
.singleton("com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck");
.singleton("com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck");
Collection<FileSetCheck> checks = load(excludes);
assertThat(checks).hasSize(4);
TreeWalker treeWalker = (TreeWalker) checks.toArray()[3];
Expand All @@ -75,7 +75,7 @@ private Collection<FileSetCheck> load(Set<String> excludes) {
new PackageObjectFactory(getClass().getPackage().getName(), getClass().getClassLoader()), excludes);
context.add("moduleFactory", filteredModuleFactory);
Collection<FileSetCheck> checks = new SpringConfigurationLoader(context, filteredModuleFactory)
.load(getPropertyResolver());
.load(getPropertyResolver());
return checks;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ protected void prepareWraps(int kind) {
}

private void applyPreparators(Phase preWrapping, int kind, ASTNode astRoot, TokenManager tokenManager) {
this.preparators.stream().filter((preparator) -> preparator.getPhase() == preWrapping)
.forEach((preparator) -> preparator.apply(kind, tokenManager, astRoot));
this.preparators.stream()
.filter((preparator) -> preparator.getPhase() == preWrapping)
.forEach((preparator) -> preparator.apply(kind, tokenManager, astRoot));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ protected void prepareWraps(int kind) {
}

private void applyPreparators(Phase preWrapping, int kind, ASTNode astRoot, TokenManager tokenManager) {
this.preparators.stream().filter((preparator) -> preparator.getPhase() == preWrapping)
.forEach((preparator) -> preparator.apply(kind, tokenManager, astRoot));
this.preparators.stream()
.filter((preparator) -> preparator.getPhase() == preWrapping)
.forEach((preparator) -> preparator.apply(kind, tokenManager, astRoot));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1684,4 +1684,5 @@ private void handleParenthesesPositions(int openingParenIndex, int closingParenI
}

// @formatter:on

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void formatCodeWithV11BaselineCanFormatOn11OrHigher(String version) throws Excep
@ValueSource(strings = "8")
void formatCodeWithV11BaselineCannotFormatOn8(String version) throws Exception {
assertThatExceptionOfType(ContainerLaunchException.class)
.isThrownBy(() -> runFormatter(JavaBaseline.V11, version));
.isThrownBy(() -> runFormatter(JavaBaseline.V11, version));
}

private void runFormatter(JavaBaseline baseline, String version) throws IOException, Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@ private String readContent(Reader reader) throws IOException {
}
return result.toString();
}

}

0 comments on commit b543266

Please sign in to comment.