Skip to content
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
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1603.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
fix:
description: Disable `WhitespaceAround` Checkstyle rule when palantir-java-format
is applied.
links:
- https://github.com/palantir/gradle-baseline/pull/1603
15 changes: 4 additions & 11 deletions gradle-baseline-java-config/resources/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,7 @@
</module>
<module name="AvoidStarImport"/> <!-- Java Style Guide: No wildcard imports -->
<module name="AvoidStaticImport"> <!-- Java Style Guide: No static imports -->
<property name="excludes" value="
com.google.common.base.Preconditions.*,
com.palantir.logsafe.Preconditions.*,
java.util.Collections.*,
java.util.stream.Collectors.*,
org.apache.commons.lang3.Validate.*,
org.assertj.core.api.Assertions.*,
org.mockito.Mockito.*"/>
<property name="excludes" value="com.google.common.base.Preconditions.*, com.palantir.logsafe.Preconditions.*, java.util.Collections.*, java.util.stream.Collectors.*, org.apache.commons.lang3.Validate.*, org.assertj.core.api.Assertions.*, org.mockito.Mockito.*"/>
</module>
<module name="ClassTypeParameterName"> <!-- Java Style Guide: Type variable names -->
<property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
Expand Down Expand Up @@ -138,12 +131,12 @@
<module name="IllegalImport"> <!-- Java Coding Guidelines: Import the canonical package -->
<property name="id" value="BanShadedClasses"/>
<property name="illegalPkgs" value=".*\.(repackaged|shaded|thirdparty)"/>
<property name="regexp" value="true" />
<property name="regexp" value="true"/>
<message key="import.illegal" value="Must not import repackaged classes."/>
</module>
<module name="IllegalImport">
<property name="illegalPkgs" value="^org\.gradle\.(internal|.*\.internal)"/>
<property name="regexp" value="true" />
<property name="regexp" value="true"/>
<message key="import.illegal" value="Do not rely on gradle internal classes as these may change in minor releases - use org.gradle.api versions instead."/>
</module>
<module name="IllegalImport">
Expand Down Expand Up @@ -397,7 +390,7 @@
<module name="SuppressWarnings">
<property name="format" value="serial"/>
</module>
<module name="SuppressWarningsHolder" /> <!-- Required for SuppressWarningsFilter -->
<module name="SuppressWarningsHolder"/> <!-- Required for SuppressWarningsFilter -->
<module name="TypeName"> <!-- Java Style Guide: Class names -->
<message key="name.invalidPattern" value="Type name ''{0}'' must match pattern ''{1}''."/>
</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,30 @@

package com.palantir.baseline.plugins;

import com.google.common.base.Preconditions;
import com.palantir.baseline.plugins.BaselineFormat.FormatterState;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.io.FileReader;
import java.io.FileWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

/** Extracts Baseline configuration into the configuration directory. */
class BaselineConfig extends AbstractBaselinePlugin {
Expand Down Expand Up @@ -96,25 +108,29 @@ public void execute(Task task) {
Path checkstyleXml = configDir.resolve("checkstyle/checkstyle.xml");

try {
String contents = new String(Files.readAllBytes(checkstyleXml), StandardCharsets.UTF_8);
String replaced = contents.replace(
" <module name=\"Indentation\"> "
+ "<!-- Java Style Guide: Block indentation: +4 spaces -->\n"
+ " <property name=\"arrayInitIndent\" value=\"8\"/>\n"
+ " <property name=\"lineWrappingIndentation\" value=\"8\"/>\n"
+ " </module>\n",
"")
.replace(
" <module name=\"ParenPad\"/> <!-- Java Style Guide: Horizontal whitespace"
+ " -->\n",
"")
.replace(
" <module name=\"LeftCurly\"/> "
+ "<!-- Java Style Guide: Nonempty blocks: K & R style -->\n",
"");
Preconditions.checkState(!contents.equals(replaced), "Patching checkstyle.xml must make a change");
Files.write(checkstyleXml, replaced.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();

InputSource inputSource = new InputSource(new FileReader(checkstyleXml.toFile()));
inputSource.setEncoding("UTF-8");
Document document = builder.parse(inputSource);

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();

removeNode(document, xPath, "//module[@name='Indentation']");
removeNode(document, xPath, "//module[@name='ParenPad']");
removeNode(document, xPath, "//module[@name='LeftCurly']");
removeNode(document, xPath, "//module[@name='WhitespaceAround']");

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new FileWriter(checkstyleXml.toFile()));
transformer.transform(source, result);
} catch (Exception e) {
throw new RuntimeException("Unable to patch " + checkstyleXml, e);
}
}
Expand All @@ -133,5 +149,11 @@ public void execute(Task task) {
});
}
}

private void removeNode(Document document, XPath xPath, String expression) throws XPathExpressionException {
xPath.reset();
Node node = (Node) xPath.compile(expression).evaluate(document, XPathConstants.NODE);
node.getParentNode().removeChild(node);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,11 @@ class BaselineConfigIntegrationTest extends AbstractPluginTest {
!new File(projectDir, '.baseline/checkstyle/checkstyle.xml').readLines().any {
it.contains '<module name="ParenPad">'
}
!new File(projectDir, '.baseline/checkstyle/checkstyle.xml').readLines().any {
it.contains '<module name="LeftCurly">'
}
!new File(projectDir, '.baseline/checkstyle/checkstyle.xml').readLines().any {
it.contains '<module name="WhitespaceAround">'
}
}
}