-
Notifications
You must be signed in to change notification settings - Fork 134
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
Add ImmutablesInterfaceDefaultValue #1761
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
70 changes: 70 additions & 0 deletions
70
...prone/src/main/java/com/palantir/baseline/errorprone/ImmutablesInterfaceDefaultValue.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,70 @@ | ||
/* | ||
* (c) Copyright 2020 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.BugPattern.LinkType; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.matchers.Matchers; | ||
import com.sun.source.tree.MethodTree; | ||
import com.sun.source.tree.Tree; | ||
import javax.lang.model.element.Modifier; | ||
|
||
/** | ||
* Checks that interface default methods in an immutables.org @Value.Immutable are | ||
* annotated with | ||
* <a href="https://immutables.github.io/immutable.html#default-attributes">@Value.Default</a> , | ||
* <a href="https://immutables.github.io/immutable.html#derived-attributes">@Value.Derived</a> , or | ||
* <a href="https://immutables.github.io/immutable.html#lazy-attributes">@Value.Lazy</a> . | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "ImmutablesInterfaceDefaultValue", | ||
linkType = LinkType.CUSTOM, | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
severity = BugPattern.SeverityLevel.ERROR, | ||
summary = "@Value.Immutable interface default methods should be annotated with" | ||
+ " @Value.Default, @Value.Derived, or @Value.Lazy") | ||
public final class ImmutablesInterfaceDefaultValue extends BugChecker implements MethodTreeMatcher { | ||
|
||
private static final Matcher<Tree> MISSING_ANNOTATION_MATCHER = Matchers.allOf( | ||
Matchers.enclosingClass(Matchers.hasAnnotation("org.immutables.value.Value.Immutable")), | ||
Matchers.hasModifier(Modifier.DEFAULT), | ||
Matchers.not(Matchers.anyOf( | ||
Matchers.hasAnnotation("org.immutables.value.Value.Default"), | ||
Matchers.hasAnnotation("org.immutables.value.Value.Derived"), | ||
Matchers.hasAnnotation("org.immutables.value.Value.Lazy")))); | ||
|
||
@Override | ||
public Description matchMethod(MethodTree tree, VisitorState state) { | ||
if (MISSING_ANNOTATION_MATCHER.matches(tree, state)) { | ||
SuggestedFix.Builder builder = SuggestedFix.builder(); | ||
String annotation = SuggestedFixes.qualifyType(state, builder, "org.immutables.value.Value.Default"); | ||
return buildDescription(tree) | ||
.addFix(builder.prefixWith(tree, "@" + annotation + " ").build()) | ||
.build(); | ||
} | ||
return Description.NO_MATCH; | ||
} | ||
} |
163 changes: 163 additions & 0 deletions
163
...e/src/test/java/com/palantir/baseline/errorprone/ImmutablesInterfaceDefaultValueTest.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,163 @@ | ||
/* | ||
* (c) Copyright 2020 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.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ImmutablesInterfaceDefaultValueTest { | ||
|
||
@Test | ||
public void testFailsWhenDefaultMethodNotAnnotated() { | ||
helper().addSourceLines( | ||
"Test.java", | ||
"import org.immutables.value.*;", | ||
"public class Test {", | ||
" @Value.Immutable", | ||
" public interface InterfaceWithValueDefault {", | ||
" String value();", | ||
"", | ||
" // BUG: Diagnostic contains: @Value.Immutable interface" | ||
+ " default methods should be annotated with" | ||
+ " @Value.Default, @Value.Derived, or @Value.Lazy", | ||
" default String defaultValue() {", | ||
" return \"default\";", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void testPassesWhenDefaultMethodAnnotatedValueDefault() { | ||
helper().addSourceLines( | ||
"Test.java", | ||
"import org.immutables.value.*;", | ||
"public class Test {", | ||
" @Value.Immutable", | ||
" public interface InterfaceWithValueDefault {", | ||
" String value();", | ||
"", | ||
" @Value.Default", | ||
" default String defaultValue() {", | ||
" return \"default\";", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void testPassesWhenDefaultMethodAnnotatedValueDerived() { | ||
helper().addSourceLines( | ||
"Test.java", | ||
"import org.immutables.value.*;", | ||
"public class Test {", | ||
" @Value.Immutable", | ||
" public interface InterfaceWithValueDefault {", | ||
" String value();", | ||
"", | ||
" @Value.Derived", | ||
" default String derivedValue() {", | ||
" return value();", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void testPassesWhenDefaultMethodAnnotatedValueLazy() { | ||
helper().addSourceLines( | ||
"Test.java", | ||
"import org.immutables.value.*;", | ||
"public class Test {", | ||
" @Value.Immutable", | ||
" public interface InterfaceWithValueDefault {", | ||
" String value();", | ||
"", | ||
" @Value.Lazy", | ||
" default String lazyValue() {", | ||
" return value();", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void refactorsMissingDefaultValueAnnotation() { | ||
refactoring() | ||
.addInputLines( | ||
"Test.java", | ||
"import org.immutables.value.*;", | ||
"public class Test {", | ||
" @Value.Immutable", | ||
" public interface InterfaceWithValueDefault {", | ||
" String value();", | ||
"", | ||
" // BUG: Diagnostic contains: " | ||
+ "@Value.Immutable interface default methods should be annotated @Value.Default", | ||
" default String defaultValue() {", | ||
" return \"default\";", | ||
" }", | ||
"", | ||
" @Value.Derived", | ||
" default String derivedValue() {", | ||
" return value();", | ||
" }", | ||
"", | ||
" @Value.Lazy", | ||
" default String lazyValue() {", | ||
" return value();", | ||
" }", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"import org.immutables.value.*;", | ||
"public class Test {", | ||
" @Value.Immutable", | ||
" public interface InterfaceWithValueDefault {", | ||
" String value();", | ||
"", | ||
" @Value.Default", | ||
" default String defaultValue() {", | ||
" return \"default\";", | ||
" }", | ||
"", | ||
" @Value.Derived", | ||
" default String derivedValue() {", | ||
" return value();", | ||
" }", | ||
"", | ||
" @Value.Lazy", | ||
" default String lazyValue() {", | ||
" return value();", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
private CompilationTestHelper helper() { | ||
return CompilationTestHelper.newInstance(ImmutablesInterfaceDefaultValue.class, getClass()); | ||
} | ||
|
||
private RefactoringValidator refactoring() { | ||
return RefactoringValidator.of(ImmutablesInterfaceDefaultValue.class, getClass()); | ||
} | ||
} |
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,8 @@ | ||
type: improvement | ||
improvement: | ||
description: |- | ||
Add ImmutablesInterfaceDefaultValue | ||
|
||
@Value.Immutable interface default methods should be annotated @Value.Default | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1761 |
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.
I don't think this should be an error? Default methods are regularly used as helper instance methods rather than anything an
@Value.Default
, and this is a totally valid use of it.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.
Ah I guess you need to check for no args?
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.
Even no args is a perfectly valid use of the default methods in
@Value.Immutable
interfaces, to use as helper methods. I'm trying to an example externally, this is used a lot internally.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.
:sigh:, I guess we can't really do anything here then, because this will suddenly start failing everyone? I think annotating with one of the annotations is not a change? And then I thought we have some automation for baseline bumps to upgrade people's code if we can.
So I guess question is are there cases where you'd legitimately not want any annotations, and adding an annotation would break the world?
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.
A few valid uses I found I can link to:
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.
Adding an annotation is 100% a breaking change (and in all currently working cases not what you want). If you serialise the immutable, suddenly you end up saving more data. It adds new methods to the builder which you may not want.
We can't tell whether someone forgot to add a
@Value.Default
or they're using adefault
method as a helper function that should not be persisted/in the builder - that's up the dev to decide. If they have forgotten to add a@Value.Default
then it should be obvious elsewhere when they can't use it in a builder.I don't think this check is possible to implement statically.
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.
for the 4 examples you gave, are they all not validly annotated as
@Value.Derived
? or is the whole idea someone might directly set them as well in the builder.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.
This is true except for the most important case, of interfaces declaring configuration options. A forgotten
Value.Default
will only be exposed there once someone tries to override a default config and the override will not take - which can be difficult to notice in the first place.I think this check applied only to things referenced in the service configuration classes/interfaces would be a great benefit
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 see why configuration definitions are any different. We regularly use default methods in configuration definitions for values that are trivially derived.