8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
authorsundar
Thu, 17 Jul 2014 16:47:06 +0530
changeset 25586 cac6440168e2
parent 25585 0968d8ab046a
child 25587 abe2821cc591
child 25675 556f675a6a12
8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date Reviewed-by: jlaskey
nashorn/make/build.xml
nashorn/samples/javafoovars.js
nashorn/samples/jsobj_example.js
nashorn/samples/ziplist.js
nashorn/src/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java
nashorn/test/script/nosecurity/JDK-8050964.js
--- a/nashorn/make/build.xml	Tue Jul 15 16:54:01 2014 +0530
+++ b/nashorn/make/build.xml	Thu Jul 17 16:47:06 2014 +0530
@@ -392,6 +392,7 @@
        verbose="${testng.verbose}" haltonfailure="true" useDefaultListeners="false" listeners="${testng.listeners}" workingDir="${basedir}">
       <jvmarg line="${ext.class.path}"/>
       <jvmarg line="${run.test.jvmargs} -Xmx${run.test.xmx}"/>
+      <sysproperty key="nashorn.jar" value="${dist.dir}/nashorn.jar"/>
       <propertyset>
         <propertyref prefix="nashorn."/>
       </propertyset>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/javafoovars.js	Thu Jul 17 16:47:06 2014 +0530
@@ -0,0 +1,103 @@
+#// Usage: jjs javafoovars.js -- <directory>
+
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ * 
+ *   - Neither the name of Oracle nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+ 
+// This example demonstrates Java subclassing by Java.extend
+// and javac Compiler and Tree API. This example counts number
+// of variables called "foo" in the given java source files!
+if (arguments.length == 0) {
+    print("Usage: jjs javafoovars.js -- <directory>");
+    exit(1);
+}
+ 
+// Java types used
+var File = Java.type("java.io.File");
+var Files = Java.type("java.nio.file.Files");
+var FileVisitOption = Java.type("java.nio.file.FileVisitOption");
+var StringArray = Java.type("java.lang.String[]");
+var ToolProvider = Java.type("javax.tools.ToolProvider");
+var Tree = Java.type("com.sun.source.tree.Tree");
+var TreeScanner = Java.type("com.sun.source.util.TreeScanner");
+var VariableTree = Java.type("com.sun.source.tree.VariableTree");
+ 
+// count "foo"-s in the given .java files
+function countFoo() {
+    // get the system compiler tool
+    var compiler = ToolProvider.systemJavaCompiler;
+    // get standard file manager
+    var fileMgr = compiler.getStandardFileManager(null, null, null);
+    // Using Java.to convert script array (arguments) to a Java String[]
+    var compUnits = fileMgr.getJavaFileObjects(
+        Java.to(arguments, StringArray));
+    // create a new compilation task
+    var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
+    // subclass SimpleTreeVisitor - to count variables called "foo"
+    var FooCounterVisitor = Java.extend(TreeScanner);
+    var fooCount = 0;
+ 
+    var visitor = new FooCounterVisitor() {
+        visitVariable: function (node, p) {
+            if (node.name.toString() == "foo") {
+                fooCount++;
+            }
+        }
+    }
+ 
+    for each (var cu in task.parse()) {
+        cu.accept(visitor, null);
+    }
+    return fooCount;
+}
+ 
+// for each ".java" file in directory (recursively) count "foo".
+function main(dir) {
+    var totalCount = 0;
+    Files.walk(dir.toPath(), FileVisitOption.FOLLOW_LINKS).
+      forEach(function(p) {
+        var name = p.toFile().absolutePath;
+        if (name.endsWith(".java")) {
+            var count = 0;
+            try {
+                count = countFoo(p.toFile().getAbsolutePath());
+            } catch (e) {
+                print(e);
+            }
+            if (count != 0) {
+                print(name + ": " + count);
+            }
+            totalCount += count;
+        }
+      });
+    print("Total foo count: " + totalCount);
+}
+ 
+main(new File(arguments[0]));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/jsobj_example.js	Thu Jul 17 16:47:06 2014 +0530
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ * 
+ *   - Neither the name of Oracle nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// Flexible script object using AbstractJSObject subclass
+
+var AbstractJSObject = Java.type("jdk.nashorn.api.scripting.AbstractJSObject");
+
+// JSObject example that uses a map for properties and 
+// falls back to with methods on a java object (for missing
+// properties
+
+function makeJSObj(map, fallback) {
+    return new AbstractJSObject() {
+        getMember: function(name) {
+            if (map.containsKey(name)) {
+                return map.get(name);
+            }
+
+            var val = fallback[name];
+            if (typeof val == 'function') {
+                return function() {
+                   var a = arguments;
+                   switch (a.length) {
+                       case 0: return fallback[name](); 
+                       case 1: return fallback[name](a[0]);
+                       case 2: return fallback[name](a[0], a[1]);
+                       case 3: return fallback[name](a[0], a[1], a[2]);
+                       case 4: return fallback[name](a[0], a[1], a[2], a[3]);
+                   }
+                }
+            }
+        }
+    }
+}
+
+var m = new java.util.HashMap();
+m.put("foo", 42);
+m.put("bar", 'hello');
+
+var obj = makeJSObj(m, new java.io.File("."));
+
+print(obj.foo);
+print(obj.bar);
+print(obj.getAbsolutePath());
+print(obj.isDirectory());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/ziplist.js	Thu Jul 17 16:47:06 2014 +0530
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ * 
+ *   - Neither the name of Oracle nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+if (arguments.length == 0) {
+    print("Usage: jjs ziplist <zip-file>");
+    exit(1);
+}
+
+// list the content details of a .zip or .jar file
+var file = arguments[0];
+
+// java classes used
+var Attributes = Java.type("java.util.jar.Attributes");
+var FileTime = Java.type("java.nio.file.attribute.FileTime");
+var JarFile = Java.type("java.util.jar.JarFile");
+var ZipEntry = Java.type("java.util.zip.ZipEntry");
+var ZipFile = Java.type("java.util.zip.ZipFile");
+
+var zf = file.endsWith(".jar")? new JarFile(file) : new ZipFile(file);
+
+var entries = zf.entries();
+// make overall output a valid JSON
+var zfObj = {
+    name: zf.name,
+    comment: zf.comment,
+    size: zf.size(),
+    entries: []
+};
+
+while (entries.hasMoreElements()) {
+    zfObj.entries.push(entries.nextElement());
+}
+
+print(JSON.stringify(zfObj, function (key, value) {
+   if (value instanceof ZipEntry) {
+       return Object.bindProperties({}, value);
+   } else if (value instanceof FileTime) {
+       return value.toString();
+   } else if (value instanceof Attributes) {
+       var attrs = {};
+       var itr = value.entrySet().iterator();
+       while (itr.hasNext()) {
+           var n = itr.next();
+           attrs[n.key] = String(n.value);
+       }
+       return attrs;
+   }
+
+   return value;
+}, ' '));
+
+zf.close();
--- a/nashorn/src/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java	Tue Jul 15 16:54:01 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java	Thu Jul 17 16:47:06 2014 +0530
@@ -36,9 +36,9 @@
 import java.security.AccessController;
 import java.security.MessageDigest;
 import java.security.PrivilegedAction;
-import java.sql.Date;
 import java.text.SimpleDateFormat;
 import java.util.Base64;
+import java.util.Date;
 import java.util.Map;
 import java.util.TreeMap;
 import jdk.nashorn.internal.codegen.types.Type;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/nosecurity/JDK-8050964.js	Thu Jul 17 16:47:06 2014 +0530
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
+ *
+ * Make sure that nashorn.jar has only 'compact1' dependency.
+ *
+ * @test
+ * @option -scripting
+ * @run
+ */
+
+// assume that this script is run with "nashorn.jar" System
+// property set to relative path of nashorn.jar from the current
+// directory of test execution.
+
+if (typeof fail != 'function') {
+    fail = print;
+}
+
+var System = java.lang.System;
+var File = java.io.File;
+var nashornJar = new File(System.getProperty("nashorn.jar"));
+if (! nashornJar.isAbsolute()) {
+    nashornJar = new File(".", nashornJar);
+}
+
+// run jdep on nashorn.jar - only summary but print profile info
+`jdeps -s -P ${nashornJar.absolutePath}`
+
+// check for "(compact1)" in output from jdep tool
+if (! /(compact1)/.test($OUT)) {
+    fail("non-compact1 dependency: " + $OUT);
+}