diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/util/ReflectionHelper.java b/core-common/src/main/java/org/glassfish/jersey/internal/util/ReflectionHelper.java index e4a202b53d..b4519ac4af 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/util/ReflectionHelper.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/util/ReflectionHelper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -56,7 +56,9 @@ import org.glassfish.jersey.internal.LocalizationMessages; import org.glassfish.jersey.internal.OsgiRegistry; import org.glassfish.jersey.internal.util.collection.ClassTypePair; - +import org.glassfish.jersey.internal.util.collection.LazyValue; +import org.glassfish.jersey.internal.util.collection.Value; +import org.glassfish.jersey.internal.util.collection.Values; import org.osgi.framework.Bundle; import org.osgi.framework.FrameworkUtil; @@ -1469,22 +1471,25 @@ private static boolean checkTypeBounds(final Class type, final Type[] bounds) { private static final Class bundleReferenceClass = AccessController.doPrivileged( classForNamePA("org.osgi.framework.BundleReference", null)); - - /** - * Returns an {@link OsgiRegistry} instance. - * - * @return an {@link OsgiRegistry} instance or {@code null} if the class cannot be instantiated (not in OSGi environment). - */ - public static OsgiRegistry getOsgiRegistryInstance() { + private static final LazyValue osgiInstance = Values.lazy((Value) () -> { try { if (bundleReferenceClass != null) { return OsgiRegistry.getInstance(); } - } catch (final Exception e) { + } catch (final Throwable e) { // Do nothing - instance is null. } return null; + }); + + /** + * Returns an {@link OsgiRegistry} instance. + * + * @return an {@link OsgiRegistry} instance or {@code null} if the class cannot be instantiated (not in OSGi environment). + */ + public static OsgiRegistry getOsgiRegistryInstance() { + return (OsgiRegistry) osgiInstance.get(); } /**