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

UnusedVariable and MethodCanBeStatic should whitelist serialization methods #1271

Closed
PhilippWendler opened this issue May 3, 2019 · 3 comments · Fixed by #1299
Closed

UnusedVariable and MethodCanBeStatic should whitelist serialization methods #1271

PhilippWendler opened this issue May 3, 2019 · 3 comments · Fixed by #1299

Comments

@PhilippWendler
Copy link

In Error-Prone 2.3.3, the following class triggers warnings:

public class Test {
  private void writeObject(java.io.ObjectOutputStream stream) throws IOException {}

  private void readObject(java.io.ObjectInputStream stream)
      throws IOException, ClassNotFoundException {}

  private void readObjectNoData() throws ObjectStreamException {}
}

The warnings are:

    [javac] Test.java:27: warning: [UnusedVariable] The parameter 'stream' is never read.
    [javac]   private void readObject(java.io.ObjectInputStream stream)
    [javac]                                                     ^
    [javac]     (see https://errorprone.info/bugpattern/UnusedVariable)
    [javac]   Did you mean 'private void readObject()'?
    [javac] Test.java:25: warning: [UnusedVariable] The parameter 'stream' is never read.
    [javac]   private void writeObject(java.io.ObjectOutputStream stream) throws IOException {}
    [javac]                                                       ^
    [javac]     (see https://errorprone.info/bugpattern/UnusedVariable)
    [javac]   Did you mean 'private void writeObject() throws IOException {}'?
    [javac] Test.java:25: warning: [MethodCanBeStatic] A private method that does not reference the enclosing instance can be static
    [javac]   private void writeObject(java.io.ObjectOutputStream stream) throws IOException {}
    [javac]   ^
    [javac]     (see https://errorprone.info/bugpattern/MethodCanBeStatic)
    [javac]   Did you mean 'private static void writeObject(java.io.ObjectOutputStream stream) throws IOException {}'?

Of course, these methods need to be declared as they are. Please whitelist them.

Having a readObject method that does not use its parameter occurs for example for singletons.

@PhilippWendler PhilippWendler changed the title UnusedVariable and MethodCanBeStatic should whiteliste serialization methods UnusedVariable and MethodCanBeStatic should whitelist serialization methods May 3, 2019
@graememorgan
Copy link
Member

Doesn't your class need to implement java.io.Serializable for implementing those methods to be meaningful?

@PhilippWendler
Copy link
Author

Of course, but I just forgot to add this to the minimal test case.

UnusedVariable warnings occur even if implements Serializable is added, though the warning from MethodCanBeStatic seems to be gone.

@graememorgan
Copy link
Member

Thanks. Will be fixed in the next release.

@ronshapiro ronshapiro mentioned this issue Jun 20, 2019
ronshapiro pushed a commit that referenced this issue Jun 20, 2019
…UnusedMethod, and MethodCanBeStatic.

Fixes #1271

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=253954393
ronshapiro pushed a commit that referenced this issue Jun 21, 2019
…UnusedMethod, and MethodCanBeStatic.

Fixes #1271

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

Successfully merging a pull request may close this issue.

2 participants