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

Add ImmutablesInterfaceDefaultValue #1761

Closed
wants to merge 3 commits into from
Closed
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
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;
}
}
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",
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

@jkozlowski jkozlowski May 12, 2021

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?

Copy link
Contributor

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 a default 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.

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't tell whether someone forgot to add a @Value.Default or they're using a default 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.

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

Copy link
Member

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.

" 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());
}
}
8 changes: 8 additions & 0 deletions changelog/@unreleased/pr-1761.v2.yml
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