List<Generation> getGenerations() {
   List<Generation> result = new ArrayList<Generation>();
   ModuleRevision current = getModule().getCurrentRevision();
   result.add((Generation) current.getRevisionInfo());
   ModuleWiring wiring = current.getWiring();
   if (wiring != null) {
     List<ModuleWire> hostWires = wiring.getProvidedModuleWires(HostNamespace.HOST_NAMESPACE);
     if (hostWires != null) {
       for (ModuleWire hostWire : hostWires) {
         result.add((Generation) hostWire.getRequirer().getRevisionInfo());
       }
     }
   }
   return result;
 }
  @SuppressWarnings("unchecked")
  private <A> A adapt0(Class<A> adapterType) {
    if (AccessControlContext.class.equals(adapterType)) {
      Generation current = (Generation) module.getCurrentRevision().getRevisionInfo();
      ProtectionDomain domain = current.getDomain();
      return (A)
          (domain == null ? null : new AccessControlContext(new ProtectionDomain[] {domain}));
    }

    if (BundleContext.class.equals(adapterType)) {
      try {
        return (A) getBundleContext();
      } catch (SecurityException e) {
        return null;
      }
    }

    if (BundleRevision.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      return (A) module.getCurrentRevision();
    }

    if (BundleRevisions.class.equals(adapterType)) {
      return (A) module.getRevisions();
    }

    if (BundleStartLevel.class.equals(adapterType)) {
      return (A) module;
    }

    if (BundleWiring.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      ModuleRevision revision = module.getCurrentRevision();
      if (revision == null) {
        return null;
      }
      return (A) revision.getWiring();
    }

    if (BundleDTO.class.equals(adapterType)) {
      // Unfortunately we need to lock here to make sure the BSN and version
      // are consistent in case of updates
      readLock();
      try {
        return (A) DTOBuilder.newBundleDTO(this);
      } finally {
        readUnlock();
      }
    }

    if (BundleStartLevelDTO.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      return (A) DTOBuilder.newBundleStartLevelDTO(this, module);
    }

    if (BundleRevisionDTO.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      return (A) DTOBuilder.newBundleRevisionDTO(module.getCurrentRevision());
    }

    if (BundleRevisionDTO[].class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      // No need to lock the database here since the ModuleRevisions object does the
      // proper locking for us.
      return (A) DTOBuilder.newArrayBundleRevisionDTO(module.getRevisions());
    }

    if (BundleWiringDTO.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      readLock();
      try {
        return (A) DTOBuilder.newBundleWiringDTO(module.getCurrentRevision());
      } finally {
        readUnlock();
      }
    }

    if (BundleWiringDTO[].class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      readLock();
      try {
        return (A) DTOBuilder.newArrayBundleWiringDTO(module.getRevisions());
      } finally {
        readUnlock();
      }
    }

    if (ServiceReferenceDTO[].class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      BundleContextImpl current = getBundleContextImpl();
      ServiceReference<?>[] references =
          (current == null)
              ? null
              : equinoxContainer.getServiceRegistry().getRegisteredServices(current);
      return (A) DTOBuilder.newArrayServiceReferenceDTO(references);
    }

    if (getBundleId() == 0) {
      if (Framework.class.equals(adapterType)) {
        return (A) this;
      }

      if (FrameworkStartLevel.class.equals(adapterType)) {
        return (A) equinoxContainer.getStorage().getModuleContainer().getFrameworkStartLevel();
      }

      if (FrameworkWiring.class.equals(adapterType)) {
        return (A) equinoxContainer.getStorage().getModuleContainer().getFrameworkWiring();
      }

      if (FrameworkDTO.class.equals(adapterType)) {
        BundleContextImpl current = getBundleContextImpl();
        Map<String, String> configuration = equinoxContainer.getConfiguration().getConfiguration();
        readLock();
        try {
          return (A) DTOBuilder.newFrameworkDTO(current, configuration);
        } finally {
          readUnlock();
        }
      }

      if (FrameworkStartLevelDTO.class.equals(adapterType)) {
        return (A)
            DTOBuilder.newFrameworkStartLevelDTO(
                equinoxContainer.getStorage().getModuleContainer().getFrameworkStartLevel());
      }
    }

    // Equinox extras
    if (Module.class.equals(adapterType)) {
      return (A) module;
    }
    if (ProtectionDomain.class.equals(adapterType)) {
      Generation current = (Generation) module.getCurrentRevision().getRevisionInfo();
      return (A) current.getDomain();
    }
    return null;
  }
  private List<String> getNativePaths() {
    ModuleRevision revision = generation.getRevision();
    ModuleWiring wiring = revision.getWiring();
    if (wiring == null) {
      // unresolved?  should not be possible
      return Collections.emptyList();
    }
    if ((revision.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
      List<ModuleWire> hosts = wiring.getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
      if (hosts == null) {
        // unresolved or invalid?  should not be possible
        return Collections.emptyList();
      }
      if (!hosts.isEmpty()) {
        // just use the first host wiring
        wiring = hosts.get(0).getProviderWiring();
      }
    }

    List<ModuleWire> nativeCode = wiring.getRequiredModuleWires(NativeNamespace.NATIVE_NAMESPACE);
    if (nativeCode.isEmpty()) {
      return Collections.emptyList();
    }

    // just taking the first paths for the revision, we sorted correctly when transforming to the
    // requirement
    for (ModuleWire moduleWire : nativeCode) {
      if (moduleWire.getRequirement().getRevision().equals(revision)) {
        @SuppressWarnings("unchecked")
        List<String> result =
            (List<String>)
                nativeCode
                    .get(0)
                    .getRequirement()
                    .getAttributes()
                    .get(REQUIREMENT_NATIVE_PATHS_ATTRIBUTE);
        if (result != null) return result;
        // this must be a multi-clause Bundle-NativeCode header, need to check for the correct one
        // in the index
        try {
          FilterImpl filter =
              FilterImpl.newInstance(
                  moduleWire
                      .getRequirement()
                      .getDirectives()
                      .get(NativeNamespace.REQUIREMENT_FILTER_DIRECTIVE));
          int index = -1;
          Map<String, Object> capabilityAttrs = moduleWire.getCapability().getAttributes();
          for (FilterImpl child : filter.getChildren()) {
            index++;
            if (child.matches(capabilityAttrs)) {
              break;
            }
          }
          if (index != -1) {
            @SuppressWarnings("unchecked")
            List<String> indexResult =
                (List<String>)
                    nativeCode
                        .get(0)
                        .getRequirement()
                        .getAttributes()
                        .get(REQUIREMENT_NATIVE_PATHS_ATTRIBUTE + '.' + index);
            if (indexResult != null) return indexResult;
          }
        } catch (InvalidSyntaxException e) {
          throw new RuntimeException(e);
        }
      }
    }
    return Collections.emptyList();
  }