-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,65 @@ | ||||
/* | ||||
* (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.util.ASTHelpers; | ||||
import com.sun.source.tree.MethodTree; | ||||
import com.sun.tools.javac.code.Symbol.ClassSymbol; | ||||
import com.sun.tools.javac.code.Symbol.MethodSymbol; | ||||
|
||||
/** | ||||
* Checks that interface default methods in an immutables.org @Value.Immutable are marked with @Value.Default. | ||||
*/ | ||||
@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 @Value.Default") | ||||
public final class ImmutablesInterfaceDefaultValue extends BugChecker implements MethodTreeMatcher { | ||||
|
||||
@Override | ||||
public Description matchMethod(MethodTree tree, VisitorState state) { | ||||
ClassSymbol enclosingClass = ASTHelpers.enclosingClass(ASTHelpers.getSymbol(tree)); | ||||
if (enclosingClass != null | ||||
&& ASTHelpers.hasAnnotation(enclosingClass, "org.immutables.value.Value.Immutable", state)) { | ||||
MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree); | ||||
if (methodSymbol != null | ||||
&& methodSymbol.isDefault() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps this would read slightly better as a series of matchers? Line 48 in 0f716d5
But unclear what is more idiomatic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if would look like this:
Meaning you can collapse the if-statement to just:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, will pull these matchers out |
||||
&& !ASTHelpers.hasAnnotation(methodSymbol, "org.immutables.value.Value.Default", state) | ||||
&& !ASTHelpers.hasAnnotation(methodSymbol, "org.immutables.value.Value.Derived", state) | ||||
&& !ASTHelpers.hasAnnotation(methodSymbol, "org.immutables.value.Value.Lazy", 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; | ||||
} | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* (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 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()); | ||
} | ||
} |
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 |
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.
Can we detect if an immutables style annotation is used with
defaultAsDefault = true
? Example: https://immutables.github.io/style.html#custom-immutable-annotationThere 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.
Good question, haven't test that yet.
I've seen varying
.*Immutable(s)?Style
annotations used across projects, and might be useful to encourage consistent usage.