JD.UNMODThis error is reported when an attempt to modify an unmodifiable collection is detected. Such attempts cause runtime exceptions. Example 112 public static void main(String[] args) { 13 argsCol = createCollection(args); 14 for (final Iterator<String> iterator = argsCol.iterator(); iterator.hasNext();) { 15 final String arg = iterator.next(); 16 if ("version".equals(arg)) { 17 printVersion(); 18 iterator.remove(); 19 } 20 } 21 run(); 22 } 23 24 public static Collection<String> createCollection(final String[] array) { 25 return Arrays.asList(array); 26 } JD.UNMOD is reported for the snippet on line 18: attempt to modify the unmodifiable collection 'argsCol' via its iterator. Example 212 public static void main(String[] args) { 13 argsCol = createCollection(args); 14 for (final Iterator<String> iterator = argsCol.iterator(); iterator.hasNext();) { 15 final String arg = iterator.next(); 16 if ("version".equals(arg)) { 17 printVersion(); 18 iterator.remove(); 19 } 20 } 21 run(); 22 } 23 24 public static Collection<String> createCollection(final String[] array) { 25 final List<String> list = new ArrayList<String>(); 26 for (final String str : array) { 27 list.add(str); 28 } 29 return list; 30 } The problem from the previous snippet is fixed: the code is still modifying 'argsCol' via its iterator, however the 'argsCol' is not unmodifiable here; see the modified 'createCollection' method. JD.UNMOD is not reported here. ExtensionThis checker can be extended through the Klocwork knowledge base. See Tuning Java analysis for more information. |