/**
   * @param <T> the return type
   * @param retvalClass the returned value's {@link Class}
   * @param jndi the JNDI path to the resource
   * @return the resource at the specified {@code jndi} path
   */
  private static <T> T lookup(Class<T> retvalClass, String jndi) {
    try {

      return retvalClass.cast(InitialContext.doLookup(jndi));
    } catch (NamingException ex) {
      throw new IllegalArgumentException(
          "failed to lookup instance of " + retvalClass + " at " + jndi, ex);
    }
  }
 @Nullable
 public static <T> T cast(@NotNull Class<T> type, UnnamedConfigurable configurable) {
   if (configurable instanceof ConfigurableWrapper) {
     ConfigurableWrapper wrapper = (ConfigurableWrapper) configurable;
     if (wrapper.myConfigurable == null) {
       Class<?> configurableType = wrapper.getExtensionPoint().getConfigurableType();
       if (configurableType != null) {
         if (!type.isAssignableFrom(configurableType)) {
           return null; // do not create configurable that cannot be cast to the specified type
         }
       } else if (type == Configurable.Assistant.class || type == OptionalConfigurable.class) {
         return null; // do not create configurable from ConfigurableProvider which replaces
                      // OptionalConfigurable
       }
     }
     configurable = wrapper.getConfigurable();
   }
   return type.isInstance(configurable) ? type.cast(configurable) : null;
 }