Improved robustness against errors in static initializers, as noticed in #174055.
authorJesse Glick <jglick@netbeans.org>
Fri, 09 Oct 2009 13:53:04 -0400
changeset 8329cfc2935df73
parent 831 b06ec6f9ddf1
child 833 0e00857c5827
Improved robustness against errors in static initializers, as noticed in #174055.
openide.util/src/org/openide/util/lookup/MetaInfServicesLookup.java
openide.util/test/unit/src/org/openide/util/lookup/MetaInfServicesLookupTest.java
     1.1 --- a/openide.util/src/org/openide/util/lookup/MetaInfServicesLookup.java	Fri Oct 09 13:35:56 2009 -0400
     1.2 +++ b/openide.util/src/org/openide/util/lookup/MetaInfServicesLookup.java	Fri Oct 09 13:53:04 2009 -0400
     1.3 @@ -487,6 +487,9 @@
     1.4                      } catch (Exception ex) {
     1.5                          LOGGER.log(Level.WARNING, "Cannot create " + object, ex);
     1.6                          object = null;
     1.7 +                    } catch (ExceptionInInitializerError x) { // #174055
     1.8 +                        LOGGER.log(Level.WARNING, "Cannot create " + object, x);
     1.9 +                        object = null;
    1.10                      }
    1.11                  }
    1.12              }
     2.1 --- a/openide.util/test/unit/src/org/openide/util/lookup/MetaInfServicesLookupTest.java	Fri Oct 09 13:35:56 2009 -0400
     2.2 +++ b/openide.util/test/unit/src/org/openide/util/lookup/MetaInfServicesLookupTest.java	Fri Oct 09 13:53:04 2009 -0400
     2.3 @@ -41,6 +41,7 @@
     2.4  
     2.5  package org.openide.util.lookup;
     2.6  
     2.7 +import java.io.ByteArrayInputStream;
     2.8  import java.io.File;
     2.9  import java.io.FileOutputStream;
    2.10  import java.io.IOException;
    2.11 @@ -50,6 +51,8 @@
    2.12  import java.lang.ref.WeakReference;
    2.13  import java.net.URL;
    2.14  import java.net.URLClassLoader;
    2.15 +import java.net.URLConnection;
    2.16 +import java.net.URLStreamHandler;
    2.17  import java.util.ArrayList;
    2.18  import java.util.Collection;
    2.19  import java.util.Collections;
    2.20 @@ -72,6 +75,7 @@
    2.21  import org.bar.Comparator2;
    2.22  import org.netbeans.junit.MockServices;
    2.23  import org.netbeans.junit.NbTestCase;
    2.24 +import org.openide.util.Enumerations;
    2.25  import org.openide.util.Exceptions;
    2.26  import org.openide.util.Lookup;
    2.27  import org.openide.util.LookupEvent;
    2.28 @@ -490,4 +494,42 @@
    2.29  
    2.30          assertNull("Nothing found", loader.value);
    2.31      }
    2.32 +
    2.33 +    public void testInitializerRobustness() throws Exception { // #174055
    2.34 +        check(Broken1.class.getName());
    2.35 +        check(Broken2.class.getName());
    2.36 +    }
    2.37 +    private void check(final String n) {
    2.38 +        assertNull(Lookups.metaInfServices(new ClassLoader() {
    2.39 +            protected @Override Enumeration<URL> findResources(String name) throws IOException {
    2.40 +                if (name.equals("META-INF/services/java.lang.Object")) {
    2.41 +                    return Enumerations.singleton(new URL(null, "dummy:stuff", new URLStreamHandler() {
    2.42 +                        protected URLConnection openConnection(URL u) throws IOException {
    2.43 +                            return new URLConnection(u) {
    2.44 +                                public void connect() throws IOException {}
    2.45 +                                public @Override InputStream getInputStream() throws IOException {
    2.46 +                                    return new ByteArrayInputStream(n.getBytes("UTF-8"));
    2.47 +                                }
    2.48 +                            };
    2.49 +                        }
    2.50 +                    }));
    2.51 +                } else {
    2.52 +                    return Enumerations.empty();
    2.53 +                }
    2.54 +            }
    2.55 +        }).lookup(Object.class));
    2.56 +    }
    2.57 +    public static class Broken1 {
    2.58 +        public Broken1() {
    2.59 +            throw new NullPointerException("broken1");
    2.60 +        }
    2.61 +    }
    2.62 +    public static class Broken2 {
    2.63 +        static {
    2.64 +            if (true) { // otherwise javac complains
    2.65 +                throw new NullPointerException("broken2");
    2.66 +            }
    2.67 +        }
    2.68 +    }
    2.69 +
    2.70  }