Getting rid of the comment above Eclipse-generated serialVersionUID

限于喜欢 提交于 2019-12-10 12:28:44

问题


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;

回答1:


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.




回答2:


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;
    /* ... */
}


来源:https://stackoverflow.com/questions/21631089/getting-rid-of-the-comment-above-eclipse-generated-serialversionuid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!