Skip to content

Commit

Permalink
Support @CompileTimeConstant for enum fields.
Browse files Browse the repository at this point in the history
The support for *class* fields was introduced in unknown commit. This CL merely extends it to enum fields, considering the similarity between them. There is also a feature request b/149290598 filed a few years back.

Specifically, this CL aims to support the following use case (taken from b/149290598):
```
public class Example {

  public enum ExampleEnum {
    A("a"), B("b");

    @CompileTimeConstant final String s;

    ExampleEnum(@CompileTimeConstant final String s) {
      this.s = s;
    }

    void invokeCtcMethod() {
      ctcMethod(s);
    }

    private void ctcMethod(@CompileTimeConstant String string) {}
  }
}
```

PiperOrigin-RevId: 529230341
  • Loading branch information
java-team-github-bot authored and Error Prone Team committed May 3, 2023
1 parent c76f797 commit be4e826
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ protected Boolean defaultAction(Tree node, Void unused) {
Symbol.VarSymbol varSymbol = (Symbol.VarSymbol) getSymbol(node);
Symbol owner = varSymbol.owner;
ElementKind ownerKind = owner.getKind();
// Check that the identifier is a formal method/constructor parameter or a class
// Check that the identifier is a formal method/constructor parameter, or a class/enum
// field.
if (ownerKind != ElementKind.METHOD
&& ownerKind != ElementKind.CONSTRUCTOR
&& ownerKind != ElementKind.CLASS) {
&& ownerKind != ElementKind.CLASS
&& ownerKind != ElementKind.ENUM) {
return false;
}
// Check that the symbol is final
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ public Description matchAssignment(AssignmentTree node, VisitorState state) {
if (assignedSymbol == null || assignedSymbol.owner == null) {
return Description.NO_MATCH;
}
if (assignedSymbol.owner.getKind() != ElementKind.CLASS) {
if (assignedSymbol.owner.getKind() != ElementKind.CLASS
&& assignedSymbol.owner.getKind() != ElementKind.ENUM) {
return Description.NO_MATCH;
}
if (!hasCompileTimeConstantAnnotation(state, assignedSymbol)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,76 @@ public void noDiagnostic_whenInvokingMethodWithFinalField() {
.doTest();
}

@Test
public void reportsDiagnostic_whenConstantEnumFieldDeclaredWithoutFinal() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.CompileTimeConstant;",
"public enum Test {",
" A(\"A\");",
" // BUG: Diagnostic contains: . Did you mean to make 's' final?",
" @CompileTimeConstant String s;",
" Test(@CompileTimeConstant String s) {",
" this.s = s;",
" }",
"}")
.doTest();
}

@Test
public void noDiagnostic_whenConstantEnumFieldDeclaredFinal() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.CompileTimeConstant;",
"public enum Test {",
" A(\"A\");",
" @CompileTimeConstant final String s;",
" Test(@CompileTimeConstant String s) {",
" this.s = s;",
" }",
"}")
.doTest();
}

@Test
public void reportsDiagnostic_whenInitialisingFinalEnumFieldWithNonConstant() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.CompileTimeConstant;",
"public enum Test {",
" A(\"A\");",
" @CompileTimeConstant final String s;",
" Test(String s) {",
" // BUG: Diagnostic contains: Non-compile-time constant expression",
" this.s = s;",
" }",
"}")
.doTest();
}

@Test
public void noDiagnostic_whenInvokingMethodWithFinalEnumField() {
compilationHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.CompileTimeConstant;",
"public enum Test {",
" A(\"A\");",
" @CompileTimeConstant final String s;",
" Test(@CompileTimeConstant String s) {",
" this.s = s;",
" }",
" void invokeCTCMethod() {",
" ctcMethod(s);",
" }",
" void ctcMethod(@CompileTimeConstant String s) {}",
"}")
.doTest();
}

@Test
public void nonConstantField_positive() {
compilationHelper
Expand Down

0 comments on commit be4e826

Please sign in to comment.