简体   繁体   中英

AddLinkEntry Class is a raw type. References to generic type AddLinkEntry Class<T> should be parameterized

I have the following the line which throws "AddLinkEntry is a raw type. References to generic type AddLinkEntry should be parameterized" warning

List<AddLinkEntry> addLinkCache = new ArrayList<AddLinkEntry>();

and there is the AddLinkEntry class

public class AddLinkEntry<T> {

  /**
   * Function that Adds a link between this object and given object
   */
  private IAddLinkToObjectFunctional<T> objectFunctional;

  /**
   * ID of given object
   */
  private Id id;

  /**
   * Constructs a new {AddLinkEntry<T>}.
   * 
   * @param objectFunctional
   *        Function that Adds a link between this object and given object
   * @param id
   *        ID of given object
   */
  public AddLinkEntry(IAddLinkToObjectFunctional<T> objectFunctional, Id id) {
    this.objectFunctional = objectFunctional;
    this.id = id;
  }

  /**
   * Retrieve a function that Adds a link between this object and given object
   */
  public IAddLinkToObjectFunctional<T> executeLinkage() {
    return objectFunctional;
  }

  /**
   * Retrieve an Id
   */
  public Id getId() {
    return id;
  }

  @FunctionalInterface
  public interface IAddLinkToObjectFunctional<T> {

    /**
     * Adds a link between this object and given object
     * 
     * @param objectNode a given object to link
     */
    void addLink(T objectNode);
  }
}

I would like to know what causes the warning and how to get rid of it without using suppress-warnings?

我认为您还需要对要存储的类型的AddLinkEntry进行参数化,例如List<AddLinkEntry<String>> addLinkCache = new ArrayList<AddLinkEntry<String>>();

As also pointed out by Eran that warning is due to direct use of raw type "AddLinkEntry" , being the class generic itself you should give the more specific type like Integer,String etc whose instance you want to hold in list for static type checking by java otherwise you are just inviting "Classcastexception".

So , List<AddLinkEntry<SpecificType>> will work fine.

For details of warning as it might not come in simple javac ,

use flag -Xlint:rawtypes

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