You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2010/06/16 01:36:51 UTC

svn commit: r955089 - in /avro/trunk: CHANGES.txt lang/java/build.xml lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java lang/java/src/test/java/org/apache/avro/specific/TestSpecificCompiler.java

Author: cutting
Date: Tue Jun 15 23:36:51 2010
New Revision: 955089

URL: http://svn.apache.org/viewvc?rev=955089&view=rev
Log:
AVRO-150. Java: fix compiler to not re-generate up-to-date code.  Contributed by John Yu.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/build.xml
    avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java
    avro/trunk/lang/java/src/test/java/org/apache/avro/specific/TestSpecificCompiler.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=955089&r1=955088&r2=955089&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Jun 15 23:36:51 2010
@@ -43,6 +43,9 @@ Avro 1.4.0 (unreleased)
 
     AVRO-447. Describe implicit protocol "system" error in spec. (cutting)
 
+    AVRO-150. Java: fix compiler to not re-generate up-to-date code.
+    (John Yu via cutting)
+
   BUG FIXES
 
     AVRO-502. Memory leak from parsing JSON schema.

Modified: avro/trunk/lang/java/build.xml
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/build.xml?rev=955089&r1=955088&r2=955089&view=diff
==============================================================================
--- avro/trunk/lang/java/build.xml (original)
+++ avro/trunk/lang/java/build.xml Tue Jun 15 23:36:51 2010
@@ -140,25 +140,29 @@
     </condition>
   </target>
 
-  <target name="javacc" depends="ivy-retrieve-build">
+  <target name="javacc-check" depends="ivy-retrieve-build">
+  <uptodate property="javacc.uptodate"
+            srcfile="${java.src.dir}/org/apache/avro/genavro/genavro.jj"
+            targetfile="${build.dir}/src/org/apache/avro/genavro"/>
+  </target>
+
+  <target name="javacc" depends="javacc-check" unless="javacc.uptodate">
     <mkdir dir="${build.dir}/src/org/apache/avro/genavro"/>
-    <copy tofile="${ivy.lib}/javacc.jar" file="${ivy.lib}/javacc-5.0.jar"
-        overwrite="true"/>
+    <copy tofile="${ivy.lib}/javacc.jar" file="${ivy.lib}/javacc-5.0.jar"/>
     <javacc target="${java.src.dir}/org/apache/avro/genavro/genavro.jj"
             outputdirectory="${build.dir}/src/org/apache/avro/genavro"
             javacchome="${ivy.lib}"/>
   </target>
 
   <target name="compile" depends="javacc,ivy-retrieve">
-    <java-compiler
-       excludes="**/ipc/** **/*Requestor.java **/*Responder.java
-       **/tool/** **/mapred/**">
-      <src path="${build.dir}/src"/>
+    <property name="depends.on.generated"
+	      value="**/ipc/** **/*Requestor.java **/*Responder.java
+		     **/tool/** **/mapred/**"/>
+    <java-compiler excludes="${depends.on.generated}">
       <src path="${java.src.dir}"/>
     </java-compiler>
     <java-avro-compiler/>
-    <java-compiler>
-      <src path="${build.dir}/src"/>
+    <java-compiler includes="${depends.on.generated}">
       <src path="${java.src.dir}"/>
     </java-compiler>
     <copy todir="${build.classes}"> 

Modified: avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java?rev=955089&r1=955088&r2=955089&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java Tue Jun 15 23:36:51 2010
@@ -79,11 +79,13 @@ public class SpecificCompiler {
     String contents;
 
     /**
-     * Writes output to path destination directory, creating directories as
-     * necessary.  Returns the created file.
+     * Writes output to path destination directory when it is newer than src,
+     * creating directories as necessary.  Returns the created file.
      */
-    File writeToDestination(File destDir) throws IOException {
+    File writeToDestination(File src, File destDir) throws IOException {
       File f = new File(destDir, path);
+      if (src != null && f.exists() && f.lastModified() >= src.lastModified())
+        return f;                                 // already up to date: ignore
       f.getParentFile().mkdirs();
       FileWriter fw = new FileWriter(f);
       try {
@@ -103,14 +105,14 @@ public class SpecificCompiler {
   public static void compileProtocol(File src, File dest) throws IOException {
     Protocol protocol = Protocol.parse(src);
     SpecificCompiler compiler = new SpecificCompiler(protocol);
-    compiler.compileToDestination(dest);
+    compiler.compileToDestination(src, dest);
   }
 
   /** Generates Java classes for a schema. */
   public static void compileSchema(File src, File dest) throws IOException {
     Schema schema = Schema.parse(src);
     SpecificCompiler compiler = new SpecificCompiler(schema);
-    compiler.compileToDestination(dest);
+    compiler.compileToDestination(src, dest);
   }
 
   static String mangle(String word) {
@@ -164,13 +166,14 @@ public class SpecificCompiler {
     return out;
   }
 
-  private void compileToDestination(File dst) throws IOException {
+  private void compileToDestination(File src, File dst) throws IOException {
     for (Schema schema : queue) {
       OutputFile o = compile(schema);
-      o.writeToDestination(dst);
+      File outputFile = new File(dst, o.path);
+      o.writeToDestination(src, dst);
     }
     if (protocol != null) {
-      compileInterface(protocol).writeToDestination(dst);
+      compileInterface(protocol).writeToDestination(src, dst);
     }
   }
 

Modified: avro/trunk/lang/java/src/test/java/org/apache/avro/specific/TestSpecificCompiler.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/java/org/apache/avro/specific/TestSpecificCompiler.java?rev=955089&r1=955088&r2=955089&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/java/org/apache/avro/specific/TestSpecificCompiler.java (original)
+++ avro/trunk/lang/java/src/test/java/org/apache/avro/specific/TestSpecificCompiler.java Tue Jun 15 23:36:51 2010
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -186,6 +187,40 @@ public class TestSpecificCompiler {
     }
     assertEquals("Missed generated protocol!", 1, count);
   }
+  
+  @Test
+  public void testNeedCompile() throws IOException, InterruptedException {
+    String schema = "" +
+      "{ \"name\": \"Foo\", \"type\": \"record\", " +
+      "  \"fields\": [ {\"name\": \"package\", \"type\": \"string\" }," +
+      "                {\"name\": \"short\", \"type\": \"Foo\" } ] }";
+    File inputFile = File.createTempFile("input", "avsc");
+    FileWriter fw = new FileWriter(inputFile);
+    fw.write(schema);
+    fw.close();
+    
+    File outputDir = new File(System.getProperty("test.dir") + 
+      System.getProperty("file.separator") + "test_need_compile");
+    File outputFile = new File(outputDir, "Foo.java");
+    outputFile.delete();
+    assertTrue(!outputFile.exists());
+    outputDir.delete();
+    assertTrue(!outputDir.exists());
+    SpecificCompiler.compileSchema(inputFile, outputDir);
+    assertTrue(outputDir.exists());
+    assertTrue(outputFile.exists());
+
+    long lastModified = outputFile.lastModified();
+    Thread.sleep(1000);  //granularity of JVM doesn't seem to go below 1 sec
+    SpecificCompiler.compileSchema(inputFile, outputDir);
+    assertEquals(lastModified, outputFile.lastModified());
+    
+    fw = new FileWriter(inputFile);
+    fw.write(schema);
+    fw.close();
+    SpecificCompiler.compileSchema(inputFile, outputDir);
+    assertTrue(lastModified != outputFile.lastModified());
+  }
 
   /**
    * Checks that a schema passes through the SpecificCompiler, and,
@@ -225,7 +260,7 @@ public class TestSpecificCompiler {
     File dstDir = AvroTestUtil.tempFile("realCompiler");
     List<File> javaFiles = new ArrayList<File>();
     for (OutputFile o : outputs) {
-      javaFiles.add(o.writeToDestination(dstDir));
+      javaFiles.add(o.writeToDestination(null, dstDir));
     }
 
     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();