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 FieldCanBeStatic checker #930

Closed
wants to merge 1 commit into from
Closed

Conversation

gaul
Copy link
Contributor

@gaul gaul commented Feb 12, 2018

This finds final fields which are initialized with a literal. Adding
static reduces memory overhead from per-instance to per-class and
allows initialization at compile-time instead of run-time.

@@ -0,0 +1,62 @@
/*
* Copyright 2017 The Error Prone Authors.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/2017/2018 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@BugPattern(
name = "FieldCanBeStatic",
category = JDK,
summary = "A final field with an immutable type can be static.",
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this summary would explain its corresponding bug checker's purpose better with a description like the following?

A final field initialized at compile-time with an instance of an immutable type can be static.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

" private final Object finalMutable = new Object();",
" private final int nonLiteral = new java.util.Random().nextInt();",
"}")
.doTest();
Copy link
Contributor

Choose a reason for hiding this comment

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

@gaul I think it's worth adding another negative case where the final fields are initialized in a constructor, like one would with a POJO. So something like this:

class Person {
  final String name;
  final int age;

  Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@gaul gaul force-pushed the field-can-be-static branch 2 times, most recently from 8f04af8 to 9a6b056 Compare February 13, 2018 07:51
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Mar 13, 2018
Adding static reduces memory overhead from per-instance to per-class and allows
initialization at compile-time instead of run-time.  Found via a proposed
error-prone pull request:

google/error-prone#930

MozReview-Commit-ID: CMj6vqFeCdI

--HG--
extra : rebase_source : 43e91b3d8ff4bd8171ac25bd0b333d09645687a6
@djh82
Copy link

djh82 commented Jul 19, 2019

Any chance this merge conflict can be resolved?

This finds final fields which are initialized with a literal.  Adding
static reduces memory overhead from per-instance to per-class and
allows initialization at compile-time instead of run-time.
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this pull request Oct 2, 2019
Adding static reduces memory overhead from per-instance to per-class and allows
initialization at compile-time instead of run-time.  Found via a proposed
error-prone pull request:

google/error-prone#930

MozReview-Commit-ID: CMj6vqFeCdI

UltraBlame original commit: e0a19b7fc6f6a44a990dacf210e871eda7eeddce
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this pull request Oct 2, 2019
Adding static reduces memory overhead from per-instance to per-class and allows
initialization at compile-time instead of run-time.  Found via a proposed
error-prone pull request:

google/error-prone#930

MozReview-Commit-ID: CMj6vqFeCdI

UltraBlame original commit: e0a19b7fc6f6a44a990dacf210e871eda7eeddce
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this pull request Oct 2, 2019
Adding static reduces memory overhead from per-instance to per-class and allows
initialization at compile-time instead of run-time.  Found via a proposed
error-prone pull request:

google/error-prone#930

MozReview-Commit-ID: CMj6vqFeCdI

UltraBlame original commit: e0a19b7fc6f6a44a990dacf210e871eda7eeddce
@cgdecker cgdecker mentioned this pull request May 20, 2020
cgdecker pushed a commit that referenced this pull request May 20, 2020
This finds final fields which are initialized with a literal.  Adding
static reduces memory overhead from per-instance to per-class and
allows initialization at compile-time instead of run-time.

Fixes #930

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=312137814
cgdecker pushed a commit that referenced this pull request May 20, 2020
This finds final fields which are initialized with a literal.  Adding
static reduces memory overhead from per-instance to per-class and
allows initialization at compile-time instead of run-time.

Fixes #930

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=312137814
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants