-
Notifications
You must be signed in to change notification settings - Fork 135
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
Gauges should be registered with registerWithReplacement
#1123
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f673be
Gauges should be registered with `registerWithReplacement`
carterkozak a35150e
Add generated changelog entries
carterkozak ec64263
Test for a known bug, add a refaster rule
carterkozak f71d454
workaround incorrect upstream `renameMethodInvocation`
carterkozak c6584a4
refaster java 11...
carterkozak eb62e23
style
carterkozak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...e-error-prone/src/main/java/com/palantir/baseline/errorprone/UnsafeGaugeRegistration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* (c) Copyright 2018 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.AbstractReturnValueIgnored; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.matchers.method.MethodMatchers; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.tools.javac.code.Symbol; | ||
|
||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "UnsafeGaugeRegistration", | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
linkType = BugPattern.LinkType.CUSTOM, | ||
providesFix = BugPattern.ProvidesFix.REQUIRES_HUMAN_ATTENTION, | ||
severity = BugPattern.SeverityLevel.WARNING, | ||
summary = "Using TaggedMetricRegistry.gauge is equivalent to map.putIfAbsent, and can result in subtle " | ||
+ "resource leaks. Prefer replacing existing gauge values.\n" | ||
// This check may begin to fail after a version upgrade, where fixes aren't automatically applied | ||
+ "This can be fixed automatically using " | ||
+ "./gradlew compileJava compileTestJava -PerrorProneApply=UnsafeGaugeRegistration") | ||
public final class UnsafeGaugeRegistration extends AbstractReturnValueIgnored { | ||
|
||
private static final String TAGGED_REGISTRY = "com.palantir.tritium.metrics.registry.TaggedMetricRegistry"; | ||
private static final String REGISTER_WITH_REPLACEMENT = "registerWithReplacement"; | ||
private static final Matcher<ExpressionTree> MATCHER = MethodMatchers.instanceMethod() | ||
.onDescendantOf(TAGGED_REGISTRY) | ||
.named("gauge") | ||
.withParameters("com.palantir.tritium.metrics.registry.MetricName", "com.codahale.metrics.Gauge"); | ||
|
||
@Override | ||
public Matcher<? super ExpressionTree> specializedMatcher() { | ||
return MATCHER; | ||
} | ||
|
||
// Override matchMethodInvocation from AbstractReturnValueIgnored to apply our suggested fix. | ||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) { | ||
Description description = super.matchMethodInvocation(methodInvocationTree, state); | ||
if (Description.NO_MATCH.equals(description) | ||
|| !hasRegisterWithReplacement(state) | ||
|| TestCheckUtils.isTestCode(state)) { | ||
return Description.NO_MATCH; | ||
} | ||
return buildDescription(methodInvocationTree) | ||
.addFix(SuggestedFixes.renameMethodInvocation(methodInvocationTree, REGISTER_WITH_REPLACEMENT, state)) | ||
.build(); | ||
} | ||
|
||
/** | ||
* TaggedMetricRegistry.registerWithReplacement was added in Tritium 0.16.1, avoid flagging older versions. | ||
*/ | ||
private static boolean hasRegisterWithReplacement(VisitorState state) { | ||
Symbol symbol = state.getSymbolFromString(TAGGED_REGISTRY); | ||
if (!(symbol instanceof Symbol.ClassSymbol)) { | ||
return false; | ||
} | ||
Symbol.ClassSymbol classSymbol = (Symbol.ClassSymbol) symbol; | ||
for (Symbol enclosed : classSymbol.getEnclosedElements()) { | ||
if (enclosed instanceof Symbol.MethodSymbol) { | ||
Symbol.MethodSymbol enclosedMethod = (Symbol.MethodSymbol) enclosed; | ||
if (enclosedMethod.name.contentEquals(REGISTER_WITH_REPLACEMENT)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...ror-prone/src/test/java/com/palantir/baseline/errorprone/UnsafeGaugeRegistrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* (c) Copyright 2018 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.errorprone.BugCheckerRefactoringTestHelper; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class UnsafeGaugeRegistrationTest { | ||
|
||
@Test | ||
void testFix() { | ||
RefactoringValidator.of(new UnsafeGaugeRegistration(), getClass()) | ||
.addInputLines( | ||
"Test.java", | ||
"import com.palantir.tritium.metrics.registry.MetricName;", | ||
"import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;", | ||
"class Test {", | ||
" void f(TaggedMetricRegistry registry, MetricName name) {", | ||
" registry.gauge(name, () -> 1);", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"import com.palantir.tritium.metrics.registry.MetricName;", | ||
"import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;", | ||
"class Test {", | ||
" void f(TaggedMetricRegistry registry, MetricName name) {", | ||
" registry.registerWithReplacement(name, () -> 1);", | ||
" }", | ||
"}" | ||
) | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void testKnownBug() { | ||
RefactoringValidator.of(new UnsafeGaugeRegistration(), getClass()) | ||
.addInputLines( | ||
"Test.java", | ||
"import com.codahale.metrics.Gauge;", | ||
"import com.palantir.tritium.metrics.registry.MetricName;", | ||
"import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;", | ||
"class Test {", | ||
" void f(TaggedMetricRegistry registry, MetricName name, Gauge<?> gauge) {", | ||
" registry.gauge(name, gauge);", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"import com.codahale.metrics.Gauge;", | ||
"import com.palantir.tritium.metrics.registry.MetricName;", | ||
"import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;", | ||
"class Test {", | ||
" void f(TaggedMetricRegistry registry, MetricName name, Gauge<?> gauge) {", | ||
// This isn't right. Filed https://github.com/google/error-prone/issues/1451 | ||
" registry.gauge(name, registerWithReplacement);", | ||
" }", | ||
"}" | ||
) | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void testNegative() { | ||
CompilationTestHelper.newInstance(UnsafeGaugeRegistration.class, getClass()).addSourceLines( | ||
"Test.java", | ||
"import com.codahale.metrics.Gauge;", | ||
"import com.palantir.tritium.metrics.registry.MetricName;", | ||
"import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;", | ||
"class Test {", | ||
" Gauge<?> f(TaggedMetricRegistry registry, MetricName name) {", | ||
" return registry.gauge(name, () -> 1);", | ||
" }", | ||
"}").doTest(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...line-refaster-rules/src/main/java/com/palantir/baseline/refaster/TritiumReplaceGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.refaster; | ||
|
||
import com.codahale.metrics.Gauge; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.palantir.tritium.metrics.registry.MetricName; | ||
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry; | ||
|
||
/** | ||
* Remove unnecessary 'remove' invocation prior to a 'registerWithReplacement'. | ||
* This refaster rule pairs with the 'UnsafeGaugeRegistration' error-prone rule | ||
* to replace 'registry.remove(name); registry.gauge(name, value);' with a | ||
* single 'registerWithReplacement' call. | ||
* This refaster rule intentionally doesn't check the 'gauge' method in order to | ||
* avoid creating changes that don't compile when older versions of Tritium are | ||
* present. | ||
*/ | ||
public final class TritiumReplaceGauge<T> { | ||
|
||
@BeforeTemplate | ||
void before(TaggedMetricRegistry registry, MetricName name, Gauge<T> gauge) { | ||
registry.remove(name); | ||
registry.registerWithReplacement(name, gauge); | ||
} | ||
|
||
@AfterTemplate | ||
void after(TaggedMetricRegistry registry, MetricName name, Gauge<T> gauge) { | ||
registry.registerWithReplacement(name, gauge); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...-refaster-rules/src/test/java/com/palantir/baseline/refaster/TritiumReplaceGaugeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class TritiumReplaceGaugeTest { | ||
|
||
@Test | ||
public void test() { | ||
RefasterTestHelper | ||
.forRefactoring(TritiumReplaceGauge.class) | ||
.withInputLines( | ||
"Test", | ||
"import com.codahale.metrics.Gauge;", | ||
"import com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry;", | ||
"import com.palantir.tritium.metrics.registry.MetricName;", | ||
"public class Test {", | ||
" void f(DefaultTaggedMetricRegistry registry) {", | ||
" MetricName name = MetricName.builder().safeName(\"foo\").build();", | ||
" registry.remove(name);", | ||
" registry.registerWithReplacement(name, () -> 1);", | ||
" }", | ||
"}") | ||
.hasOutputLines( | ||
"import com.codahale.metrics.Gauge;", | ||
"import com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry;", | ||
"import com.palantir.tritium.metrics.registry.MetricName;", | ||
"public class Test {", | ||
" void f(DefaultTaggedMetricRegistry registry) {", | ||
" MetricName name = MetricName.builder().safeName(\"foo\").build();", | ||
" registry.registerWithReplacement(name, () -> 1);", | ||
" ", | ||
" }", | ||
"}"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type: improvement | ||
improvement: | ||
description: |- | ||
Using TaggedMetricRegistry.gauge is equivalent to map.putIfAbsent, and | ||
can result in subtle resource leaks. Prefer replacing existing gauge | ||
values using `registerWithReplacement`. | ||
|
||
This check doesn't apply unless a new enough version of Tritium | ||
is available on the compilation classpath. | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have a template to replace following pattern with
registry.registerWithReplacement(name, gauge);
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to create a change that doesn't compile on old versions of Tritium (pre 0.16.1). The error-prone check only runs on new tritium, and transforms
registry.gauge(name, gauge)
intoregistry.registerWithReplacement(name, gauge)
, allowing this rule to do the rest.Documented in the class level javadoc :-)