0

I have the following HashMap:

Map<String, String[]> aMap = new HashMap<String, String[]>()

and a method with the following signature:

private boolean verifyFileName(String fileName, String[] allowableValues)

I invoke the method like this:

Map.Entry<String, String[]> pair = (Map.Entry)it.next();

...

verifyFileName(file.getName(), pair.getValue())

The problem is that verifyFileName throws an error saying that its second argument is an ArrayList. When I change the signature of verifyFileName to (String fileName, ArrayList<String> allowableValues) the error disappears.

What's going on here?

EDIT: Here's the JUnit test I use the HashMap in (written in Groovy):

   @Test
    public void verifyGeneratedAndCompiledFiles() { 
        Iterator it = DIRECTORIES_TO_CHECK.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String[]> pair = it.next();
            int count = 0
            File createdFolder = new File(pair.getKey())
            for (File file: createdFolder.listFiles()) {
               println('checking for ' + file.getName())               
                if (file.length() == 0) {
                    fail(createdFolder.getPath() + ' folder contains an empty file')
                }

                if (!verifyFileName(file.getName(), pair.getValue())) {
                    fail(createdFolder.getPath() + ' folder is missing ' + file.getName())
                }
                count++
            }

            if (count != 2) {
                fail(createdFolder.getName() + " folder contains an incorrect number of files")
            }

            count = 0
        }       
    }

and here's where I declare and initialize DIRECTORIES_TO_CHECK:

  private static final Map<String, String[]> DIRECTORIES_TO_CHECK
    static {
        Map<String, String[]> aMap = new HashMap<String, String[]>()
        aMap.put(DIRECTORIES_ROOT + "subproj/build/classes/generatedSource", ["Subproj.class", "Subproj2.class"])
        aMap.put(DIRECTORIES_ROOT + "sub2/build/classes/generatedSource", ["Sub2.class", "Sub22.class"])
        aMap.put(DIRECTORIES_ROOT + "subproj/generated/java", ["Subproj.java, Subproj2.java"])
        aMap.put(DIRECTORIES_ROOT + "sub2/generated/java", ["Sub2.java", "Sub22.java"])
        DIRECTORIES_TO_CHECK = Collections.unmodifiableMap(aMap)
    }
k5_
  • 5,450
  • 2
  • 19
  • 27
Adam
  • 8,752
  • 12
  • 54
  • 96
  • What's `pair.getValue()`? – Grzegorz Górkiewicz Feb 28 '17 at 19:45
  • Why do you have a cast at `(Map.Entry)it.next();`? Trouble keeping your generic parameters in condition? Using raw types will certainly make it easier to have class cast exceptions during runtime. – Kayaman Feb 28 '17 at 19:47
  • @Kayaman I copied an "iterate over a hashmap" snippet from another question. It's left over from that. Could it be causing problems? – Adam Feb 28 '17 at 19:48
  • 2
    Yes. You're casting to (or just using) a raw type and losing the compile time type checking. Most likely the reason is that you've been copying code randomly without understanding what it does. Now you have a piece of garbage that doesn't work. – Kayaman Feb 28 '17 at 19:49
  • 1
    Sounds like a poluted heap. How did you get the HashMap? Somewhere you used an unsafe case and filled ArrayList into the Map. – k5_ Feb 28 '17 at 19:50
  • 1
    Are you creating the HashMap with groovy? that fragment is not valid java. – k5_ Feb 28 '17 at 19:59
  • @k5_ Yes. I'll edit the question – Adam Feb 28 '17 at 20:00
  • 1
    ["Sub2.java", "Sub22.java"] is an `ArrayList` in groovy – k5_ Feb 28 '17 at 20:01
  • @k5_ Thanks a lot. I know this question has some issues, thanks for assisting anyway. If you make your comment an answer I'll accept it – Adam Feb 28 '17 at 20:02
  • @Kayaman Could you make your criticism more constructive? Do you have any good references you could suggest? – Adam Feb 28 '17 at 20:16
  • 1
    It's really hard to provide constructive commentary from the given material. Read the tutorials, learn the language, don't copy/paste code that you don't understand... – Kayaman Feb 28 '17 at 20:20
  • Based on this tutorial: http://www.sergiy.ca/how-to-iterate-over-a-map-in-java/ It seems like my mistake was using generics *and* casting. I should pick one or the other, or better yet use a for each loop. – Adam Feb 28 '17 at 20:30

0 Answers0