Fri, 11 Jul 2008 14:59:48 -0700
6725036: javac returns incorrect value for lastModifiedTime() when source is a zip file archive
Reviewed-by: darcy
src/share/classes/com/sun/tools/javac/file/ZipFileIndex.java | file | annotate | diff | comparison | revisions | |
test/tools/javac/T6725036.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/share/classes/com/sun/tools/javac/file/ZipFileIndex.java Thu Jul 10 16:50:38 2008 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/file/ZipFileIndex.java Fri Jul 11 14:59:48 2008 -0700 1.3 @@ -32,6 +32,7 @@ 1.4 import java.text.MessageFormat; 1.5 import java.util.ArrayList; 1.6 import java.util.Arrays; 1.7 +import java.util.Calendar; 1.8 import java.util.Collections; 1.9 import java.util.HashMap; 1.10 import java.util.HashSet; 1.11 @@ -1307,11 +1308,18 @@ 1.12 return javatime; 1.13 } 1.14 1.15 - // From java.util.zip 1.16 - private static long dosToJavaTime(int nativetime) { 1.17 - // Bootstrap build problems prevent me from using the code directly 1.18 - // Convert the raw/native time to a long for now 1.19 - return (long)nativetime; 1.20 + // based on dosToJavaTime in java.util.Zip, but avoiding the 1.21 + // use of deprecated Date constructor 1.22 + private static long dosToJavaTime(int dtime) { 1.23 + Calendar c = Calendar.getInstance(); 1.24 + c.set(Calendar.YEAR, ((dtime >> 25) & 0x7f) + 1980); 1.25 + c.set(Calendar.MONTH, ((dtime >> 21) & 0x0f) - 1); 1.26 + c.set(Calendar.DATE, ((dtime >> 16) & 0x1f)); 1.27 + c.set(Calendar.HOUR_OF_DAY, ((dtime >> 11) & 0x1f)); 1.28 + c.set(Calendar.MINUTE, ((dtime >> 5) & 0x3f)); 1.29 + c.set(Calendar.SECOND, ((dtime << 1) & 0x3e)); 1.30 + c.set(Calendar.MILLISECOND, 0); 1.31 + return c.getTimeInMillis(); 1.32 } 1.33 1.34 void setNativeTime(int natTime) {
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/T6725036.java Fri Jul 11 14:59:48 2008 -0700 2.3 @@ -0,0 +1,94 @@ 2.4 +/* 2.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 2.24 + * have any questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6725036 2.30 + * @summary javac returns incorrect value for lastModifiedTime() when 2.31 + * source is a zip file archive 2.32 + */ 2.33 + 2.34 +import java.io.File; 2.35 +import java.util.Date; 2.36 +import java.util.jar.JarEntry; 2.37 +import java.util.jar.JarFile; 2.38 +import javax.tools.JavaFileObject; 2.39 + 2.40 +import com.sun.tools.javac.file.JavacFileManager; 2.41 +import com.sun.tools.javac.file.ZipFileIndex; 2.42 +import com.sun.tools.javac.file.ZipFileIndexArchive; 2.43 +import com.sun.tools.javac.util.Context; 2.44 + 2.45 +public class T6725036 { 2.46 + public static void main(String... args) throws Exception { 2.47 + new T6725036().run(); 2.48 + } 2.49 + 2.50 + void run() throws Exception { 2.51 + String TEST_ENTRY_NAME = "java/lang/String.class"; 2.52 + 2.53 + File f = new File(System.getProperty("java.home")); 2.54 + if (!f.getName().equals("jre")) 2.55 + f = new File(f, "jre"); 2.56 + File rt_jar = new File(new File(f, "lib"), "rt.jar"); 2.57 + 2.58 + JarFile j = new JarFile(rt_jar); 2.59 + JarEntry je = j.getJarEntry(TEST_ENTRY_NAME); 2.60 + long jarEntryTime = je.getTime(); 2.61 + 2.62 + ZipFileIndex zfi = 2.63 + ZipFileIndex.getZipFileIndex(rt_jar, null, false, null, false); 2.64 + long zfiTime = zfi.getLastModified(TEST_ENTRY_NAME); 2.65 + 2.66 + check(je, jarEntryTime, zfi + ":" + TEST_ENTRY_NAME, zfiTime); 2.67 + 2.68 + Context context = new Context(); 2.69 + JavacFileManager fm = new JavacFileManager(context, false, null); 2.70 + ZipFileIndexArchive zfia = new ZipFileIndexArchive(fm, zfi); 2.71 + int sep = TEST_ENTRY_NAME.lastIndexOf("/"); 2.72 + JavaFileObject jfo = 2.73 + zfia.getFileObject(TEST_ENTRY_NAME.substring(0, sep + 1), 2.74 + TEST_ENTRY_NAME.substring(sep + 1)); 2.75 + long jfoTime = jfo.getLastModified(); 2.76 + 2.77 + check(je, jarEntryTime, jfo, jfoTime); 2.78 + 2.79 + if (errors > 0) 2.80 + throw new Exception(errors + " occurred"); 2.81 + } 2.82 + 2.83 + void check(Object ref, long refTime, Object test, long testTime) { 2.84 + if (refTime == testTime) 2.85 + return; 2.86 + System.err.println("Error: "); 2.87 + System.err.println("Expected: " + getText(ref, refTime)); 2.88 + System.err.println(" Found: " + getText(test, testTime)); 2.89 + errors++; 2.90 + } 2.91 + 2.92 + String getText(Object x, long t) { 2.93 + return String.format("%14d", t) + " (" + new Date(t) + ") from " + x; 2.94 + } 2.95 + 2.96 + int errors; 2.97 +}