private ModuleComponentIdentifier chooseBestMatchingDependencyWithMetaData(
      ModuleVersionListing versions,
      DependencyMetaData dependency,
      ModuleComponentRepositoryAccess moduleAccess) {
    for (Versioned candidate : sortLatestFirst(versions)) {
      MutableModuleVersionMetaData metaData =
          resolveComponentMetaData(dependency, candidate, moduleAccess);
      ModuleComponentIdentifier candidateIdentifier = metaData.getComponentId();

      // Apply version selection rules
      ModuleComponentSelector requestedComponentSelector =
          DefaultModuleComponentSelector.newSelector(dependency.getRequested());
      VersionSelectionInternal selection =
          new DefaultVersionSelection(requestedComponentSelector, candidateIdentifier);
      versionSelectionRules.apply(selection);

      switch (selection.getState()) {
        case ACCEPTED:
          return candidateIdentifier;
        case REJECTED:
          continue;
        default:
          break;
      }

      // Invoke version matcher
      if (versionMatcher.accept(dependency.getRequested().getVersion(), metaData)) {
        // We already resolved the correct module.
        return candidateIdentifier;
      }
    }
    return null;
  }
 public ModuleComponentIdentifier choose(
     ModuleVersionListing versions,
     DependencyMetaData dependency,
     ModuleComponentRepositoryAccess moduleAccess) {
   if (versionMatcher.needModuleMetadata(dependency.getRequested().getVersion())) {
     return chooseBestMatchingDependencyWithMetaData(versions, dependency, moduleAccess);
   } else {
     return chooseBestMatchingDependency(versions, dependency.getRequested());
   }
 }
 private void transformDependencies(BuildableModuleVersionMetaDataResolveResult result) {
   MutableModuleVersionMetaData metaData = result.getMetaData();
   List<DependencyMetaData> transformed = new ArrayList<DependencyMetaData>();
   for (DependencyMetaData dependency : metaData.getDependencies()) {
     transformed.add(
         dependency.withRequestedVersion(
             dependency.getDescriptor().getDynamicConstraintDependencyRevisionId().getRevision()));
   }
   metaData.setDependencies(transformed);
 }
 private MutableModuleVersionMetaData resolveComponentMetaData(
     DependencyMetaData dependency,
     Versioned candidate,
     ModuleComponentRepositoryAccess moduleAccess) {
   ModuleVersionSelector selector = dependency.getRequested();
   ModuleComponentIdentifier candidateId =
       DefaultModuleComponentIdentifier.newId(
           selector.getGroup(), selector.getName(), candidate.getVersion());
   DependencyMetaData moduleVersionDependency =
       dependency.withRequestedVersion(candidate.getVersion());
   BuildableModuleVersionMetaDataResolveResult descriptorResult =
       new DefaultBuildableModuleVersionMetaDataResolveResult();
   moduleAccess.resolveComponentMetaData(moduleVersionDependency, candidateId, descriptorResult);
   return descriptorResult.getMetaData();
 }
 public void resolve(DependencyMetaData dependency, BuildableModuleVersionResolveResult result) {
   DependencyDescriptor descriptor = dependency.getDescriptor();
   if (descriptor instanceof ProjectDependencyDescriptor) {
     ProjectDependencyDescriptor desc = (ProjectDependencyDescriptor) descriptor;
     LocalComponentMetaData componentMetaData = projectModuleRegistry.findProject(desc);
     result.resolved(
         componentMetaData.toResolveMetaData(), new ProjectArtifactResolver(componentMetaData));
   } else {
     resolver.resolve(dependency, result);
   }
 }