/*
 * Copyright 2000-2017 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 com.intellij.openapi.projectRoots.impl;

import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.util.Comparing;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

class WindowsJavaFinder extends JavaHomeFinder {

  @NotNull
  @Override
  protected List<String> findExistingJdks() {
    ArrayList<String> result = new ArrayList<>();
    Set<File> roots = findRootsToScan();
    for (File root : roots) {
       scanFolder(root, result);
    }
    result.sort((o1, o2) -> {
      String name1 = new File(o1).getName();
      String name2 = new File(o2).getName();
      return Comparing.compare(JavaSdkVersion.fromVersionString(name2), JavaSdkVersion.fromVersionString(name1));
    });
    return result;
  }

  private static Set<File> findRootsToScan() {
    TreeSet<File> roots = new TreeSet<>();
    File javaHome = getJavaHome();
    if (javaHome != null) {
      File parentFile = javaHome.getParentFile();
      if (parentFile != null) {
        roots.add(parentFile);
      }
    }
    File[] fsRoots = File.listRoots();
    for (File root : fsRoots) {
      if (root.exists()) {
        File[] children = root.listFiles((dir, name) -> name.contains("Program Files"));
        if (children != null) {
          for (File child : children) {
            if (child.isDirectory()) {
              roots.add(child);
              File[] subFolders = child.listFiles((dir, name) -> name.equals("Java"));
              if (subFolders != null) {
                for (File subFolder : subFolders) {
                  if (subFolder.isDirectory()) {
                    roots.add(subFolder);
                  }
                }
              }
            }
          }
        }
      }
    }
    return roots;
  }
}

@@ -1616,122 +1616,28 @@
     
-File parentFile = javaHome.getParentFile();%0A      if (parentFile != null) %7B%0A        roots.add(parentFile);%0A      %7D
+roots.add(javaHome);
 %0A   
@@ -1753,66 +1753,45 @@
 File
-%5B%5D children = root.listFiles((dir, name) -%3E name.contains(
+ candidate = new File(new File(root, 
 %22Pro
@@ -1802,16 +1802,24 @@
  Files%22)
+, %22Java%22
 );%0A     
@@ -1830,119 +1830,31 @@
 f (c
-hildren != null) %7B%0A          for (File child : children) %7B%0A            if (child.isDirectory()) %7B%0A             
+andidate.isDirectory())
  roo
@@ -1861,20 +1861,24 @@
 ts.add(c
-hild
+andidate
 );%0A     
@@ -1884,85 +1884,76 @@
     
-      File%5B%5D subFolders = child.listFiles((dir, name) -%3E name.equals(
+candidate =  new File(new File(root, %22Program Files (x86)%22), 
 %22Java%22)
-)
 ;%0A  
@@ -1962,272 +1962,58 @@
     
-     
- 
 if (
-subFolders != null) %7B%0A                for (File subFolder : subFolders) %7B%0A                  if (subFolder.isDirectory()) %7B%0A                    roots.add(subFolder);%0A                  %7D%0A                %7D%0A              %7D%0A            %7D%0A          %7D%0A        %7D
+candidate.isDirectory()) roots.add(candidate);
 %0A   

Optimize WindowsJavaFinder