Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer safe-logging Preconditions to hadoop. #2552

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions baseline-error-prone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
testImplementation 'org.slf4j:slf4j-api'
testImplementation 'org.apache.commons:commons-lang3'
testImplementation 'commons-lang:commons-lang'
testImplementation 'org.apache.hadoop:hadoop-client-api'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.immutables:value::annotations'
testImplementation 'org.jooq:jooq'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ public final class PreferSafeLoggingPreconditions extends BugChecker implements
.onClass("com.google.common.base.Preconditions")
.withNameMatching(Pattern.compile("checkArgument|checkState|checkNotNull"));

private static final Matcher<ExpressionTree> HADOOP_PRECONDITIONS_MATCHER = MethodMatchers.staticMethod()
.onClass("org.apache.hadoop.util.Preconditions")
.withNameMatching(Pattern.compile("checkArgument|checkState|checkNotNull"));

private static final Matcher<ExpressionTree> METHOD_MATCHER = Matchers.anyOf(
PRECONDITIONS_MATCHER,
HADOOP_PRECONDITIONS_MATCHER,
MethodMatchers.staticMethod().onClass("java.util.Objects").named("requireNonNull"),
MethodMatchers.staticMethod()
.onClass("org.apache.commons.lang3.Validate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ public void testValidateValidStateParams() {
passValidate("Validate.validState(param != \"string\", \"message %s\", \"a\");");
}

@Test
public void testHadoopRequireNonNullConstantString() {
failHadoop("Preconditions.checkNotNull(param, \"constant\");");
}

@Test
public void testHadoopRequireNonNullNonConstantString() {
passHadoop("Preconditions.checkNotNull(param, String.format(\"constant %s\", param));");
}

@Test
public void testPreconditionsAutoFixShortNames() {
RefactoringValidator.of(PreferSafeLoggingPreconditions.class, getClass())
Expand Down Expand Up @@ -684,6 +694,37 @@ public void testAutoFixArgsPassedToGuava() {
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}

@Test
public void testAutoFixArgsPassedToHadoop() {
RefactoringValidator.of(PreferSafeLoggingPreconditions.class, getClass())
.addInputLines(
"Test.java",
"import org.apache.hadoop.util.Preconditions;",
"import com.palantir.logsafe.SafeArg;",
"class Test {",
" void f(String param) {",
" Preconditions.checkNotNull(param, \"constant\", SafeArg.of(\"name\", null));",
" Preconditions.checkArgument(true, \"constant\", SafeArg.of(\"name\", null));",
" Preconditions.checkState(true, \"constant\", SafeArg.of(\"name\", null));",
" }",
"}")
.addOutputLines(
"Test.java",
"import org.apache.hadoop.util.Preconditions;",
"import com.palantir.logsafe.SafeArg;",
"class Test {",
" void f(String param) {",
" com.palantir.logsafe.Preconditions.checkNotNull(param, \"constant\", SafeArg.of(\"name\","
+ " null));",
" com.palantir.logsafe.Preconditions.checkArgument(true, \"constant\", SafeArg.of(\"name\","
+ " null));",
" com.palantir.logsafe.Preconditions.checkState(true, \"constant\", SafeArg.of(\"name\","
+ " null));",
" }",
"}")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}

private void passObjects(String precondition) {
compilationHelper
.addSourceLines(
Expand Down Expand Up @@ -723,6 +764,19 @@ private void passValidate(String precondition) {
.doTest();
}

private void passHadoop(String precondition) {
compilationHelper
.addSourceLines(
"Test.java",
"import org.apache.hadoop.util.Preconditions;",
"class Test {",
" void f(String param) {",
" " + precondition,
" }",
"}")
.doTest();
}

private void failObjects(String precondition) {
compilationHelper
.addSourceLines(
Expand All @@ -737,6 +791,20 @@ private void failObjects(String precondition) {
.doTest();
}

private void failHadoop(String precondition) {
compilationHelper
.addSourceLines(
"Test.java",
"import org.apache.hadoop.util.Preconditions;",
"class Test {",
" void f(String param) {",
" // BUG: Diagnostic contains: call can be replaced",
" " + precondition,
" }",
"}")
.doTest();
}

private void failGuava(String precondition) {
compilationHelper
.addSourceLines(
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2552.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Prefer safe-logging Preconditions to org.apache.hadoop.util.Preconditions.
links:
- https://github.com/palantir/gradle-baseline/pull/2552
2 changes: 2 additions & 0 deletions versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ net.bytebuddy:byte-buddy:1.14.1 (2 constraints: bd16a34f)
net.bytebuddy:byte-buddy-agent:1.14.1 (1 constraints: 410b3fde)
net.lingala.zip4j:zip4j:1.3.2 (1 constraints: 0805fb35)
one.util:streamex:0.8.1 (1 constraints: 0b050436)
org.apache.hadoop:hadoop-client-api:3.3.4 (1 constraints: 0c050736)
org.apiguardian:apiguardian-api:1.1.2 (7 constraints: 9d791b5f)
org.assertj:assertj-core:3.24.2 (3 constraints: e62ace9f)
org.hamcrest:hamcrest:2.2 (2 constraints: 43187376)
Expand All @@ -132,3 +133,4 @@ org.opentest4j:opentest4j:1.2.0 (2 constraints: cd205b49)
org.reactivestreams:reactive-streams:1.0.3 (1 constraints: ef07e77b)
org.spockframework:spock-core:2.1-M2-groovy-3.0 (2 constraints: e622905a)
org.spockframework:spock-junit4:2.1-M2-groovy-3.0 (1 constraints: 241154df)
org.xerial.snappy:snappy-java:1.1.8.2 (1 constraints: 4b0f5d8c)
1 change: 1 addition & 0 deletions versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ com.palantir.conjure.java.api:* = 2.34.0
com.palantir.conjure.java.runtime:* = 7.49.0
com.palantir.tokens:auth-tokens = 3.17.0
com.palantir.tritium:* = 0.65.0
org.apache.hadoop:hadoop-client-api = 3.3.4
commons-io:* = 2.11.0
junit:junit = 4.13.2
net.lingala.zip4j:zip4j = 1.3.2
Expand Down