Add JREIndex optimization to JdtCompiler.

http://gwt-code-reviews.appspot.com/1318802/show

Review by: tobyr@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9629 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/javac/JdtCompiler.java b/dev/core/src/com/google/gwt/dev/javac/JdtCompiler.java
index d3b0bc5..fb752ef 100644
--- a/dev/core/src/com/google/gwt/dev/javac/JdtCompiler.java
+++ b/dev/core/src/com/google/gwt/dev/javac/JdtCompiler.java
@@ -16,6 +16,7 @@
 package com.google.gwt.dev.javac;
 
 import com.google.gwt.dev.jdt.TypeRefVisitor;
+import com.google.gwt.dev.jjs.InternalCompilerException;
 import com.google.gwt.dev.util.Name.BinaryName;
 import com.google.gwt.dev.util.collect.Lists;
 import com.google.gwt.dev.util.log.speedtracer.CompilerEventType;
@@ -60,10 +61,12 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.JarURLConnection;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -71,6 +74,8 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
 /**
  * Manages the process of compiling {@link CompilationUnit}s.
@@ -130,6 +135,51 @@
     void process(CompilationUnitBuilder builder,
         CompilationUnitDeclaration cud, List<CompiledClass> compiledClasses);
   }
+
+  /**
+   * Static cache of all the JRE package names.
+   */
+  public static class JreIndex {
+    private static Set<String> packages = readPackages();
+
+    public static boolean contains(String name) {
+      return packages.contains(name);
+    }
+
+    private static void addPackageRecursively(Set<String> packages, String pkg) {
+      if (!packages.add(pkg)) {
+        return;
+      }
+
+      int i = pkg.lastIndexOf('/');
+      if (i != -1) {
+        addPackageRecursively(packages, pkg.substring(0, i));
+      }
+    }
+
+    private static Set<String> readPackages() {
+      HashSet<String> pkgs = new HashSet<String>();
+      String klass = "java/lang/Object.class";
+      URL url = ClassLoader.getSystemClassLoader().getResource(klass);
+      try {
+        JarURLConnection connection = (JarURLConnection) url.openConnection();
+        JarFile f = connection.getJarFile();
+        Enumeration<JarEntry> entries = f.entries();
+        while (entries.hasMoreElements()) {
+          JarEntry e = entries.nextElement();
+          String name = e.getName();
+          if (name.endsWith(".class")) {
+            String pkg = Shared.getSlashedPackageFrom(name);
+            addPackageRecursively(pkgs, pkg);
+          }
+        }
+        return pkgs;
+      } catch (IOException e) {
+        throw new InternalCompilerException("Unable to find JRE", e);
+      }
+    }
+  }
+
   /**
    * Adapts a {@link CompilationUnit} for a JDT compile.
    */
@@ -282,7 +332,10 @@
     }
 
     private boolean isPackage(String slashedPackageName) {
-      // Include class loader check for binary-only annotations.
+      // Test the JRE explicitly, because the classloader trick doesn't work.
+      if (JreIndex.contains(slashedPackageName)) {
+        return true;
+      }
       if (packages.contains(slashedPackageName)) {
         return true;
       }
@@ -294,6 +347,7 @@
         addPackages(slashedPackageName);
         return true;
       }
+      // Include class loader check for binary-only annotations.
       if (getClassLoader().getResource(resourceName) != null) {
         addPackages(slashedPackageName);
         return true;
diff --git a/dev/core/src/com/google/gwt/dev/jdt/AbstractCompiler.java b/dev/core/src/com/google/gwt/dev/jdt/AbstractCompiler.java
index 4ff010b..fce859f 100644
--- a/dev/core/src/com/google/gwt/dev/jdt/AbstractCompiler.java
+++ b/dev/core/src/com/google/gwt/dev/jdt/AbstractCompiler.java
@@ -22,7 +22,6 @@
 import com.google.gwt.dev.javac.GWTProblem;
 import com.google.gwt.dev.javac.JdtCompiler;
 import com.google.gwt.dev.javac.Shared;
-import com.google.gwt.dev.jjs.InternalCompilerException;
 import com.google.gwt.dev.util.CharArrayComparator;
 import com.google.gwt.dev.util.Empty;
 import com.google.gwt.dev.util.Util;
@@ -52,19 +51,14 @@
 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
 import org.eclipse.jdt.internal.compiler.util.Messages;
 
-import java.io.IOException;
-import java.net.JarURLConnection;
 import java.net.URL;
 import java.util.Comparator;
-import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
 
 /**
  * A facade around the JDT compiler to manage on-demand compilation, caching
@@ -338,47 +332,6 @@
       }
     }
 
-    static class JreIndex {
-      private static Set<String> packages = readPackages();
-
-      private static void addPackageRecursively(Set<String> packages, String pkg) {
-        if (!packages.add(pkg)) {
-          return;
-        }
-
-        int i = pkg.lastIndexOf('.');
-        if (i != -1) {
-          addPackageRecursively(packages, pkg.substring(0, i));
-        }
-      }
-
-      public static boolean contains(String name) {
-        return packages.contains(name);
-      }
-
-      private static Set<String> readPackages() {
-        HashSet<String> pkgs = new HashSet<String>();
-        String klass = "java/lang/Object.class";
-        URL url = ClassLoader.getSystemClassLoader().getResource(klass);
-        try {
-          JarURLConnection connection = (JarURLConnection) url.openConnection();
-          JarFile f = connection.getJarFile();
-          Enumeration<JarEntry> entries = f.entries();
-          while (entries.hasMoreElements()) {
-            JarEntry e = entries.nextElement();
-            String name = e.getName();
-            if (name.endsWith(".class")) {
-              String pkg = Shared.getPackageNameFromBinary(name);
-              addPackageRecursively(pkgs, pkg);
-            }
-          }
-          return pkgs;
-        } catch (IOException e) {
-          throw new InternalCompilerException("Unable to find JRE", e);
-        }
-      }
-    }
-
     private class INameEnvironmentImpl implements INameEnvironment {
 
       public INameEnvironmentImpl() {
@@ -483,9 +436,8 @@
       private boolean isPackage(ClassLoader classLoader, String packageName) {
         String packageAsPath = packageName.replace('.', '/');
 
-        // Test the JRE explicitly first, because the following method does
-        // not work for JRE classes.
-        if (JreIndex.contains(packageName)) {
+        // Test the JRE explicitly, because the classloader trick doesn't work.
+        if (JdtCompiler.JreIndex.contains(packageAsPath)) {
           return true;
         }