Revision: 10180
Author:   zun...@google.com
Date:     Thu May 12 10:40:46 2011
Log: Rescues cached entries from jar files that are the same, save for the timestamp. Also updates the key value used for the cache to include the prefix for RerootedResource
instances.

Review at http://gwt-code-reviews.appspot.com/1441803

http://code.google.com/p/google-web-toolkit/source/detail?r=10180

Modified:
 /trunk/dev/core/src/com/google/gwt/dev/javac/CachedCompilationUnit.java
 /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationStateBuilder.java
 /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationUnit.java
 /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationUnitBuilder.java
 /trunk/dev/core/src/com/google/gwt/dev/javac/SourceFileCompilationUnit.java
 /trunk/dev/core/src/com/google/gwt/dev/javac/UnitCache.java
 /trunk/dev/core/src/com/google/gwt/dev/resource/Resource.java
/trunk/dev/core/src/com/google/gwt/dev/resource/impl/ResourceOracleImpl.java
 /trunk/dev/core/test/com/google/gwt/dev/javac/MockCompilationUnit.java

=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/javac/CachedCompilationUnit.java Wed May 11 10:50:46 2011 +++ /trunk/dev/core/src/com/google/gwt/dev/javac/CachedCompilationUnit.java Thu May 12 10:40:46 2011
@@ -30,17 +30,46 @@
   private final Collection<CompiledClass> compiledClasses;
   private final ContentId contentId;
   private final Dependencies dependencies;
-  private final String resourceLocation;
-  private final String resourcePath;
-  private final List<JsniMethod> jsniMethods;
-  private final long lastModified;
-  private final MethodArgNamesLookup methodArgNamesLookup;
-  private final String typeName;
   private final boolean isError;
   private final boolean isGenerated;
   private final boolean isSuperSource;
+  private final List<JsniMethod> jsniMethods;
+  private final long lastModified;
+  private final MethodArgNamesLookup methodArgNamesLookup;
   private final CategorizedProblem[] problems;
+  private final String resourceLocation;
+  private final String resourcePath;
   private final DiskCacheToken sourceToken;
+  private final String typeName;
+
+  /**
+ * Shallow copy of a CachedCompiliationUnit, replacing some parameters in the new copy.
+   *
+   * @param unit Unit to clone.
+   * @param lastModified last modified date to replace in the clone
+   * @param resourceLocation location to replace in the clone.
+   */
+ public CachedCompilationUnit(CachedCompilationUnit unit, long lastModified,
+      String resourceLocation) {
+    assert unit != null;
+    this.compiledClasses = unit.getCompiledClasses();
+    this.contentId = unit.getContentId();
+    this.dependencies = unit.getDependencies();
+    this.resourcePath = unit.getResourcePath();
+    this.jsniMethods = unit.getJsniMethods();
+    this.methodArgNamesLookup = unit.getMethodArgs();
+    this.typeName = unit.getTypeName();
+    this.isError = unit.isError();
+    this.isGenerated = unit.isGenerated();
+    this.isSuperSource = unit.isSuperSource();
+    this.problems = unit.problems;
+    this.astToken = unit.astToken;
+    this.sourceToken = unit.sourceToken;
+
+    // Override these fields
+    this.lastModified = lastModified;
+    this.resourceLocation = resourceLocation;
+  }

   /**
    * Create a compilation unit that can be serialized from another
@@ -79,6 +108,11 @@
     this.astToken = new DiskCacheToken(astToken);
     this.sourceToken = new DiskCacheToken(sourceToken);
   }
+
+  @Override
+  public CachedCompilationUnit asCachedCompilationUnit() {
+    return this;
+  }

   @Override
   public Collection<CompiledClass> getCompiledClasses() {
@@ -142,11 +176,6 @@
   public boolean isSuperSource() {
     return isSuperSource;
   }
-
-  @Override
-  protected Object writeReplace() {
-    return this;
-  }

   @Override
   ContentId getContentId() {
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationStateBuilder.java Tue May 3 08:35:13 2011 +++ /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationStateBuilder.java Thu May 12 10:40:46 2011
@@ -1,12 +1,12 @@
 /*
  * Copyright 2009 Google Inc.
- *
+ *
* 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
@@ -371,7 +371,7 @@
   /**
* Build a new compilation state from a source oracle. Allow the caller to
    * specify a compiler delegate that will handle undefined names.
-   *
+   *
    * TODO: maybe use a finer brush than to synchronize the whole thing.
    */
public synchronized CompilationState doBuildFrom(TreeLogger logger, Set<Resource> resources,
@@ -393,14 +393,24 @@
       ResourceCompilationUnitBuilder builder =
           new ResourceCompilationUnitBuilder(typeName, resource);

-      CompilationUnit cachedUnit = unitCache.find(resource.getPath());
-      if (cachedUnit != null) {
-        if (cachedUnit.getLastModified() == resource.getLastModified()) {
-          cachedUnits.put(builder, cachedUnit);
-          compileMoreLater.addValidUnit(cachedUnit);
-          continue;
-        }
+ CompilationUnit cachedUnit = unitCache.find(resource.getPathPrefix() + resource.getPath()); + if (cachedUnit != null && cachedUnit.getLastModified() != resource.getLastModified()) {
         unitCache.remove(cachedUnit);
+        if (!cachedUnit.getContentId().equals(builder.getContentId())) {
+          cachedUnit = null;
+        } else {
+ // Update the cache. The location might have changed since last build
+          // (e.g. jar to file)
+          CachedCompilationUnit updatedUnit =
+ new CachedCompilationUnit(cachedUnit.asCachedCompilationUnit(),
+                  resource.getLastModified(), resource.getLocation());
+          unitCache.add(updatedUnit);
+        }
+      }
+      if (cachedUnit != null) {
+        cachedUnits.put(builder, cachedUnit);
+        compileMoreLater.addValidUnit(cachedUnit);
+        continue;
       }
       builders.add(builder);
     }
@@ -422,7 +432,7 @@

   /**
    * Compile new generated units into an existing state.
-   *
+   *
    * TODO: maybe use a finer brush than to synchronize the whole thing.
    */
synchronized Collection<CompilationUnit> doBuildGeneratedTypes(TreeLogger logger,
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationUnit.java Wed May 11 10:50:46 2011 +++ /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationUnit.java Thu May 12 10:40:46 2011
@@ -209,6 +209,12 @@
    */
   private transient Map<String, String> anonymousClassMap = null;

+  /**
+ * Returns the unit as an instance of {@link CachedCompilationUnit}, making
+   * a copy if necessary.
+   */
+  public abstract CachedCompilationUnit asCachedCompilationUnit();
+
   @Deprecated
   public final boolean constructAnonymousClassMappings(TreeLogger logger) {
     /*
@@ -292,9 +298,11 @@
   public abstract String getResourceLocation();

   /**
-   * Returns the full abstract path of the resource.
+   * Returns the full abstract path of the resource. If a resource has been
+   * re-rooted, this path should include any path prefix that was stripped.
    *
-   * @see {@link com.google.gwt.dev.resource.Resource#getPath()}
+   * @see {@link com.google.gwt.dev.resource.Resource#getPath()} and
+   *      {@link com.google.gwt.dev.resource.Resource#getPathPrefix()}
    */
   public abstract String getResourcePath();

@@ -376,10 +384,12 @@
   }

   /**
- * Subclasses must implement explicit serialization. The canonical serialized
-   * form is {@link CachedCompilationUnit}.
+   * The canonical serialized form of a CompilatinUnit is
+   * {@link CachedCompilationUnit}.
    */
-  protected abstract Object writeReplace();
+  protected final Object writeReplace() {
+    return asCachedCompilationUnit();
+  }

   /**
* Returns the content ID for the source with which this unit was compiled.
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationUnitBuilder.java Wed May 11 10:50:46 2011 +++ /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationUnitBuilder.java Thu May 12 10:40:46 2011
@@ -1,12 +1,12 @@
 /*
  * Copyright 2009 Google Inc.
- *
+ *
* 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
@@ -166,6 +166,13 @@
super(compiledClasses, types, dependencies, jsniMethods, methodArgs, problems);
       this.generatedUnit = generatedUnit;
     }
+
+    @Override
+    public CachedCompilationUnit asCachedCompilationUnit() {
+      long sourceToken = generatedUnit.getSourceToken();
+      assert sourceToken >= 0;
+      return new CachedCompilationUnit(this, sourceToken, astToken);
+    }

     @Override
     public long getLastModified() {
@@ -204,13 +211,6 @@
     public boolean isSuperSource() {
       return false;
     }
-
-    @Override
-    protected Object writeReplace() {
-      long sourceToken = generatedUnit.getSourceToken();
-      assert sourceToken >= 0;
-      return new CachedCompilationUnit(this, sourceToken, astToken);
-    }

     @Override
     ContentId getContentId() {
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/javac/SourceFileCompilationUnit.java Wed May 11 10:50:46 2011 +++ /trunk/dev/core/src/com/google/gwt/dev/javac/SourceFileCompilationUnit.java Thu May 12 10:40:46 2011
@@ -1,12 +1,12 @@
 /*
  * Copyright 2008 Google Inc.
- *
+ *
* 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
@@ -52,6 +52,14 @@
     this.lastModified = lastModified;
     this.contentId = contentId;
   }
+
+  @Override
+  public CachedCompilationUnit asCachedCompilationUnit() {
+    if (sourceToken < 0) {
+ sourceToken = diskCache.transferFromStream(sourceFile.openContents());
+    }
+    return new CachedCompilationUnit(this, sourceToken, astToken);
+  }

   @Override
   public long getLastModified() {
@@ -65,7 +73,7 @@

   @Override
   public String getResourcePath() {
-    return sourceFile.getPath();
+    return sourceFile.getPathPrefix() + sourceFile.getPath();
   }

   @Deprecated
@@ -100,14 +108,6 @@
   public boolean isSuperSource() {
     return sourceFile.wasRerooted();
   }
-
-  @Override
-  protected Object writeReplace() {
-    if (sourceToken < 0) {
- sourceToken = diskCache.transferFromStream(sourceFile.openContents());
-    }
-    return new CachedCompilationUnit(this, sourceToken, astToken);
-  }

   @Override
   ContentId getContentId() {
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/javac/UnitCache.java Wed May 11 10:50:46 2011 +++ /trunk/dev/core/src/com/google/gwt/dev/javac/UnitCache.java Thu May 12 10:40:46 2011
@@ -1,12 +1,12 @@
 /*
  * Copyright 2011 Google Inc.
- *
+ *
* 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
@@ -40,8 +40,9 @@
   CompilationUnit find(ContentId contentId);

   /**
-   * Lookup a {@link CompilationUnit} by resource path.
-   *
+ * Lookup a {@link CompilationUnit} by resource path. This should include any
+   * path prefix that may have been was stripped to reroot the resource.
+   *
    * @see {@link CompilationUnit#getResourcePath()}
    */
   CompilationUnit find(String resourcePath);
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/resource/Resource.java Wed May 11 10:50:46 2011 +++ /trunk/dev/core/src/com/google/gwt/dev/resource/Resource.java Thu May 12 10:40:46 2011
@@ -48,6 +48,14 @@
    * Returns the full abstract path of the resource.
    */
   public abstract String getPath();
+
+  /**
+ * If some prefix was stripped from the path, as is for RerootedResources,
+   * retrieve it back with this method.
+   */
+  public String getPathPrefix() {
+    return "";
+  }

   /**
* Returns a URL for this resource if the resource happens to be based off the
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/resource/impl/ResourceOracleImpl.java Wed May 11 10:50:46 2011 +++ /trunk/dev/core/src/com/google/gwt/dev/resource/impl/ResourceOracleImpl.java Thu May 12 10:40:46 2011
@@ -93,6 +93,12 @@
     public String getPath() {
       return path;
     }
+
+    @Override
+    public String getPathPrefix() {
+      int fullPathLen = resource.getPath().length();
+      return resource.getPath().substring(0, fullPathLen - path.length());
+    }

     @Override
     public InputStream openContents() {
=======================================
--- /trunk/dev/core/test/com/google/gwt/dev/javac/MockCompilationUnit.java Tue May 3 08:35:13 2011 +++ /trunk/dev/core/test/com/google/gwt/dev/javac/MockCompilationUnit.java Thu May 12 10:40:46 2011
@@ -1,12 +1,12 @@
 /*
  * Copyright 2011 Google Inc.
- *
+ *
* 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
@@ -15,6 +15,9 @@
  */
 package com.google.gwt.dev.javac;

+import com.google.gwt.dev.util.DiskCache;
+import com.google.gwt.dev.util.Util;
+
 import org.eclipse.jdt.core.compiler.CategorizedProblem;

 import java.util.Collection;
@@ -44,6 +47,14 @@
     contentId = new ContentId(typeName, source);
     lastModified = nextTimestamp.getAndIncrement();
   }
+
+  @Override
+  public CachedCompilationUnit asCachedCompilationUnit() {
+    DiskCache diskCache = DiskCache.INSTANCE;
+    long sourceToken = diskCache.writeByteArray(Util.getBytes(source));
+ long astToken = diskCache.writeByteArray(Util.getBytes("Dummy AST data"));
+    return new CachedCompilationUnit(this, sourceToken, astToken);
+  }

   @Override
   public Collection<CompiledClass> getCompiledClasses() {
@@ -104,10 +115,6 @@
   public boolean isSuperSource() {
     return false;
   }
-
-  protected Object writeReplace() {
-    return this;
-  }

   @Override
   ContentId getContentId() {

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to