/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.jps.model.java.impl;

import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileSystemUtil;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.containers.ContainerUtil;

import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.Set;

/**
 * @author nik
 */
public class JavaSdkUtil {
  public static List<File> getJdkClassesRoots(File home, boolean isJre) {
    FileFilter jarFileFilter = new FileFilter() {
      @Override
      @SuppressWarnings({"HardCodedStringLiteral"})
      public boolean accept(File f) {
        return !f.isDirectory() && f.getName().endsWith(".jar");
      }
    };

    File[] jarDirs;
    if (SystemInfo.isMac && !home.getName().startsWith("mockJDK")) {
      File openJdkRtJar = new File(home, "jre/lib/rt.jar");
      if (openJdkRtJar.exists() && !openJdkRtJar.isDirectory()) {
        File libDir = new File(home, "lib");
        File classesDir = openJdkRtJar.getParentFile();
        File libExtDir = new File(openJdkRtJar.getParentFile(), "ext");
        File libEndorsedDir = new File(libDir, "endorsed");
        jarDirs = new File[]{libEndorsedDir, libDir, classesDir, libExtDir};
      }
      else {
        File libDir = new File(home, "lib");
        File classesDir = new File(home, "../Classes");
        File libExtDir = new File(libDir, "ext");
        File libEndorsedDir = new File(libDir, "endorsed");
        jarDirs = new File[]{libEndorsedDir, libDir, classesDir, libExtDir};
      }
    }
    else {
      File libDir = isJre ? new File(home, "lib") : new File(home, "jre/lib");
      File libExtDir = new File(libDir, "ext");
      File libEndorsedDir = new File(libDir, "endorsed");
      jarDirs = new File[]{libEndorsedDir, libDir, libExtDir};
    }

    Set<String> pathFilter = ContainerUtil.newTroveSet(FileUtil.PATH_HASHING_STRATEGY);
    List<File> rootFiles = ContainerUtil.newArrayList();
    for (File jarDir : jarDirs) {
      if (jarDir != null && jarDir.isDirectory()) {
        File[] jarFiles = jarDir.listFiles(jarFileFilter);
        for (File jarFile : jarFiles) {
          String jarFileName = jarFile.getName();
          if (jarFileName.equals("alt-rt.jar") || jarFileName.equals("alt-string.jar")) {
            continue;  // filter out alternative implementations
          }
          String canonicalPath = FileSystemUtil.resolveSymLink(jarFile);
          if (canonicalPath == null || !pathFilter.add(canonicalPath)) {
            continue;  // filter out duplicate (symbolically linked) .jar files commonly found in OS X JDK distributions
          }
          rootFiles.add(jarFile);
        }
      }
    }

    File classesZip = new File(home, "lib/classes.zip");
    if (classesZip.isFile()) {
      rootFiles.add(classesZip);
    }

    File classesDir = new File(home, "classes");
    if (rootFiles.isEmpty() && classesDir.isDirectory()) {
      rootFiles.add(classesDir);
    }

    return rootFiles;
  }
}

@@ -719,30 +719,24 @@
 util.io.File
-System
 Util;%0Aimport
@@ -753,83 +753,80 @@
 lij.
-openapi.util.io.FileUtil;%0Aimport com.intellij.util.containers.ContainerUtil
+util.containers.ContainerUtil;%0Aimport org.jetbrains.annotations.Nullable
 ;%0A%0Ai
@@ -872,16 +872,44 @@
 Filter;%0A
+import java.io.IOException;%0A
 import j
@@ -3005,37 +3005,24 @@
 h = 
-FileSystemUtil.resolveSymLink
+getCanonicalPath
 (jar
@@ -3594,11 +3594,189 @@
 les;%0A  %7D
+%0A%0A  @Nullable%0A  private static String getCanonicalPath(File file) %7B%0A    try %7B%0A      return file.getCanonicalPath();%0A    %7D%0A    catch (IOException e) %7B%0A      return null;%0A    %7D%0A  %7D
 %0A%7D%0A

Fix tests on Windows