Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider Java v3.0

...

Code Block
bgColor#FFCCCC
public class Overloader {
  private static String display(ArrayList<Integer>ArrayList&lt;Integer&gt; a) {
    return "ArrayList"&quot;ArrayList&quot;;
  }

  private static String display(LinkedList<String>LinkedList&lt;String&gt; l) {
    return "LinkedList"&quot;LinkedList&quot;;
  }

  private static String display(List<?>List&lt;?&gt; l) {
    return "&quot;List is not recognized"&quot;;
  }

  public static void main(String[] args) {
    // Array of lists
    List<?>List&lt;?&gt;[] invokeAll = new List<?>List&lt;?&gt;[] {new ArrayList<Integer>ArrayList&lt;Integer&gt;(), 
    new LinkedList<String>LinkedList&lt;String&gt;(), new Vector<Integer>Vector&lt;Integer&gt;()};

    for(List<?>List&lt;?&gt; i : invokeAll) {
      System.out.println(display(i));
    }
  }
}

...

Code Block
bgColor#ccccff
class Overloader {
public class Overloader {
  private static String display(List<?>List&lt;?&gt; l) {
    return (l instanceof ArrayList ? "Arraylist"&quot;Arraylist&quot; : (l instanceof LinkedList ? "LinkedList"&quot;LinkedList&quot;
    : "&quot;List is not recognized"&quot;));
  }

  public static void main(String[] args) {
    List<?>List&lt;?&gt;[] invokeAll = new List<?>List&lt;?&gt;[] {new ArrayList<Integer>ArrayList&lt;Integer&gt;(), 
    new LinkedList<String>LinkedList&lt;String&gt;(), new Vector<Integer>Vector&lt;Integer&gt;()};

    for(List<?>List&lt;?&gt; i : invokeAll) {
      System.out.println(display(i));
    }
  }
}

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[API 06|AA. Java References#API 06]\] [Interface Collection|http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html]
\[[Bloch 08|AA. Java References#Bloch 08]\] Item 41: Use overloading judiciously

...

MET32-J. Ensure that constructors do not call overridable methods      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12. Methods (MET)      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MET34-J. Follow the general contract when implementing the compareTo method