Getting rid of the comment above Eclipse-generated serialVersionUID
Asked Answered
M

2

35

This has become a pet peeve of mine. I write a class, and implement Serializible. Then eclipse warns me that I don't have a serialVersionUID, so I select "Add generated serialVersionUID" or "Add default serialVersionUID" and I end up with something like this:

  /**
   * 
   */
  private static final long serialVersionUID = 4049849541314027178L;

Most of the time I don't want to add a comment, so I have to go and delete the comment. I would rather the default be no comment, but I've looked through the code templates in preferences and haven't figured out how to change this. I simply want it to look like this:

  private static final long serialVersionUID = 4049849541314027178L;
Moltke answered 7/2, 2014 at 15:6 Comment(2)
always wanted to know how to do that tooAriana
From "it looks like the only option then is to turn off the comments for all auto-generated fields.", the answer is no. I have created How to prevent the auto-generation of comments just for quick-fixing the serialVersionUID warning? in response to address that issue.Reger
Y
41

It's using the template for any eclipse-generated field.

You can change it in

 Preferences -> 
     Java -> 
        Code Style -> 
           Code Templates -> 
              Comments -> 
                 Fields

... either globally or per-project.

Yablon answered 7/2, 2014 at 15:13 Comment(4)
Yes but it will delete auto-generated comments above every generated field. I thought here we want to get rid of auto-generated comment only above this concrete field.Ryun
Thanks, though as @Ryun pointed out, it looks like the only option then is to turn off the comments for all auto-generated fields. That's not too bad, though.Moltke
Upvote because serialVersionUID is like the only field I generate on a regular basis.Aceae
I have extended upon your answer and cited your solution. I explained that the auto-generation of this SerialVersionID comment is just a special case. Normally this generation runs when you generate element comment, but the important fact is it runs automatically when you quick-fix the serial warningReger
R
1

To explain it further from the first answer, the auto-generated comments for the serialVersionUID is simply a comment template generation that applies to all fields, including this one. It runs when you generate element comment (Alt+Shift+J) for that field from the Source section of the Toolbar.

The important fact is that the generation of field comments also runs automatically when you quick-fix

The serializable class [ClassName] does not declare a static final serialVersionUID field of type long"

Disabling/changing generated comments for all fields

You can prevent the generation of comments for all fields by clicking edit and blanking the comment after performing what the original answerer @slim answered, as in

Toolbar -> Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments -> Fields

After quick-fixing the serializable warning, note that the comment is no longer auto-generated.

Disabling/changing the automatic generation of the comment just for SerialVersionUID

Unfortunately, there is no option to prevent the auto-generation of comments for the SerialVersionUID through quick-fixing without blanking the comment generation template for all fields. There is no such option when searching for "Comment" or "Serial" in Preferences.

Fortunately, it doesn't matter much since for other fields, it generates only when you generate element comment for that field. And you can also prefer to comment or Javadoc fields manually.

Example

This code excerpt (only fields shown) shows that warning about serialization.

public class SomePanel extends JPanel {
    private String name;
    /* ... */
}

You quick-fix this warning, and you generate element comment the name field. Without blanking the template, you get this, which you do not want for serialVersionUID:

public class SomePanel extends JPanel {
    /**
     *
     */
    private static final long serialVersionUID = -5173652620102412871L;
    /**
     * 
     */
    private String name;
    /* ... */
}

After blanking the template for comment generation of fields, you get, as expected:

public class SomePanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private String name;
    /* ... */
}
Reger answered 4/7, 2018 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.