{"record":{"id":"ac1085374ba42087","repo":"apache/hadoop","slug":"element-instanceof-linkedelement-element-getcl","errorCode":null,"errorMessage":"!(element instanceof LinkedElement), element.getClass()=\" + element.getClass()","messagePattern":"!\\(element instanceof LinkedElement\\), element\\.getClass\\(\\)=\" \\+ element\\.getClass\\(\\)","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java","lineNumber":159,"sourceCode":"    return null;\n  }\n\n  @Override\n  public boolean contains(final K key) {\n    return get(key) != null;\n  }\n\n  @Override\n  public E put(final E element) {\n    // validate element\n    if (element == null) {\n      throw new NullPointerException(\"Null element is not supported.\");\n    }\n    LinkedElement e = null;\n    try {\n      e = (LinkedElement)element;\n    } catch (ClassCastException ex) {\n      throw new HadoopIllegalArgumentException(\n          \"!(element instanceof LinkedElement), element.getClass()=\"\n          + element.getClass());\n    }\n\n    // find index\n    final int index = getIndex(element);\n\n    // remove if it already exists\n    final E existing = remove(index, element);\n\n    // insert the element to the head of the linked list\n    modification++;\n    size++;\n    e.setNext(entries[index]);\n    entries[index] = e;\n\n    return existing;\n  }","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java#L141-L177","documentation":"Elements stored in LightWeightGSet must implement LinkedElement - the set chains hash collisions through the elements themselves instead of allocating wrapper nodes. put() catches the ClassCastException from the cast and rethrows HadoopIllegalArgumentException naming the element's actual class, telling you the type violates the GSet contract.","triggerScenarios":"put(element) where the element class does not implement LinkedElement: passing a plain POJO, or a subclass hierarchy from which the interface was dropped.","commonSituations":"Porting a custom cache to GSet; modifying inode-like classes in HDFS forks; test code inserting stand-in objects that implement only equals/hashCode.","solutions":["Make the element implement LinkedElement (getNext/setNext chaining), following existing element classes in the codebase.","Type the reference as GSet<K, E extends LinkedElement> so misuse becomes a compile error.","If the class cannot be modified, use a wrapper-based structure (java.util.HashSet) instead of GSet."],"exampleFix":"// before\nclass MyEntry { // plain POJO\n  ...\n}\ngset.put(myEntry); // HadoopIllegalArgumentException\n\n// after\nclass MyEntry implements LightWeightGSet.LinkedElement {\n  private LinkedElement next;\n  @Override public LinkedElement getNext() { return next; }\n  @Override public void setNext(LinkedElement n) { next = n; }\n}\ngset.put(myEntry);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static <E> boolean isStorableInGSet(E element) {\n  return element != null && (element instanceof LinkedElement);\n}","tryCatchPattern":null,"preventionTips":["Encode the contract in generics: declare GSet<K, E extends LinkedElement>.","Copy getNext/setNext chaining from an existing GSet element class rather than inventing it."],"tags":["collections","type-mismatch","hadoop-common"],"backgroundTag":"invalid-element-type","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}