简体   繁体   中英

How to suppress serial warning for a static block where a final static variable is initialized and manipulated?

I have a

public final static java.util.Map<String, Type> VAR;
static {
    VAR = new java.util.HashMap<>();
    VAR.put("a", new com.google.common.reflect.TypeToken<List<Integer>>(){}.getType());
}

Where the missing serialVersionUID in TypeToken (of guava 18.0) causes a [serial] warning, which I want to suppress because its not useful. I'd like to avoid adding @SuppressWarnings("serial") to the class because it's too broad. Neither the static block not the declaration accepts the @SuppressWarnings annotation. I can' move the initialization to a static method because other the constant might not have been initialized (compiler error).

I'm using Java 1.7.

This is one possibility:

  public final static Map<String, Type> VAR;

  private static Type listOfIntegerType()
  {
      @SuppressWarnings("serial")
      TypeToken<List<Integer>> t = new TypeToken<List<Integer>>(){};
      return t.getType();
  }

  static {
      VAR = new HashMap<>();
      VAR.put("a", listOfIntegerType());
  }

Don't use a static block:

@SuppressWarnings("serial")
public final static java.util.List VAR = new java.util.LinkedList() {
   ...
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM