Warn about maltyped collection elements.
1.1 --- a/openide.util/src/org/openide/util/NbCollections.java Tue Nov 28 23:31:03 2006 +0000
1.2 +++ b/openide.util/src/org/openide/util/NbCollections.java Wed Nov 29 17:53:19 2006 +0000
1.3 @@ -34,6 +34,8 @@
1.4 import java.util.NoSuchElementException;
1.5 import java.util.RandomAccess;
1.6 import java.util.Set;
1.7 +import java.util.logging.Level;
1.8 +import java.util.logging.Logger;
1.9
1.10 /**
1.11 * Utilities for working with generics.
1.12 @@ -47,13 +49,15 @@
1.13 public class NbCollections {
1.14
1.15 private NbCollections() {}
1.16 +
1.17 + private static final Logger LOG = Logger.getLogger(NbCollections.class.getName());
1.18
1.19 /**
1.20 * Create a typesafe copy of a raw set.
1.21 * @param rawSet an unchecked set
1.22 * @param type the desired supertype of the entries
1.23 * @param strict true to throw a <code>ClassCastException</code> if the raw set has an invalid entry,
1.24 - * false to quietly skip over such entries
1.25 + * false to skip over such entries (warnings may be logged)
1.26 * @return a typed set guaranteed to contain only entries assignable
1.27 * to the named type (or they may be null)
1.28 * @throws ClassCastException if some entry in the raw set was not well-typed, and only if <code>strict</code> was true
1.29 @@ -68,6 +72,8 @@
1.30 } catch (ClassCastException x) {
1.31 if (strict) {
1.32 throw x;
1.33 + } else {
1.34 + LOG.log(Level.WARNING, "Element {0} not assignable to {1}", new Object[] {e, type});
1.35 }
1.36 }
1.37 }
1.38 @@ -79,7 +85,7 @@
1.39 * @param rawList an unchecked list
1.40 * @param type the desired supertype of the entries
1.41 * @param strict true to throw a <code>ClassCastException</code> if the raw list has an invalid entry,
1.42 - * false to quietly skip over such entries
1.43 + * false to skip over such entries (warnings may be logged)
1.44 * @return a typed list guaranteed to contain only entries assignable
1.45 * to the named type (or they may be null)
1.46 * @throws ClassCastException if some entry in the raw list was not well-typed, and only if <code>strict</code> was true
1.47 @@ -94,6 +100,8 @@
1.48 } catch (ClassCastException x) {
1.49 if (strict) {
1.50 throw x;
1.51 + } else {
1.52 + LOG.log(Level.WARNING, "Element {0} not assignable to {1}", new Object[] {e, type});
1.53 }
1.54 }
1.55 }
1.56 @@ -106,7 +114,7 @@
1.57 * @param keyType the desired supertype of the keys
1.58 * @param valueType the desired supertype of the values
1.59 * @param strict true to throw a <code>ClassCastException</code> if the raw map has an invalid key or value,
1.60 - * false to quietly skip over such map entries
1.61 + * false to skip over such map entries (warnings may be logged)
1.62 * @return a typed map guaranteed to contain only keys and values assignable
1.63 * to the named types (or they may be null)
1.64 * @throws ClassCastException if some key or value in the raw map was not well-typed, and only if <code>strict</code> was true
1.65 @@ -121,6 +129,8 @@
1.66 } catch (ClassCastException x) {
1.67 if (strict) {
1.68 throw x;
1.69 + } else {
1.70 + LOG.log(Level.WARNING, "Entry {0} not assignable to <{1},{2}>", new Object[] {e, keyType, valueType});
1.71 }
1.72 }
1.73 }
2.1 --- a/openide.util/test/unit/src/org/openide/util/NbCollectionsTest.java Tue Nov 28 23:31:03 2006 +0000
2.2 +++ b/openide.util/test/unit/src/org/openide/util/NbCollectionsTest.java Wed Nov 29 17:53:19 2006 +0000
2.3 @@ -38,6 +38,7 @@
2.4 import java.util.NoSuchElementException;
2.5 import java.util.Scanner;
2.6 import java.util.Set;
2.7 +import java.util.logging.Level;
2.8 import org.netbeans.junit.NbTestCase;
2.9
2.10 /**
2.11 @@ -49,6 +50,10 @@
2.12 public NbCollectionsTest(String name) {
2.13 super(name);
2.14 }
2.15 +
2.16 + protected Level logLevel() {
2.17 + return Level.SEVERE;
2.18 + }
2.19
2.20 public void testCheckedSetByCopy() throws Exception {
2.21 Set s = new HashSet();