8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code
authorsundar
Tue, 19 Aug 2014 18:14:45 +0530
changeset 26070 1c348e8d9b88
parent 26069 3f5132a21ab5
child 26071 430e39f6731f
8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code Reviewed-by: attila, lagergren, jlaskey
nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/Main.java
nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/MethodGenerator.java
nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/NullVisitor.java
nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ScriptClassInfoCollector.java
nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ScriptClassInstrumentor.java
nashorn/make/build.xml
nashorn/make/project.properties
nashorn/samples/find_nonfinals.js
nashorn/samples/try_with_resource.js
--- a/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/Main.java	Tue Aug 19 11:25:14 2014 +0200
+++ b/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/Main.java	Tue Aug 19 18:14:45 2014 +0530
@@ -32,6 +32,7 @@
 import java.io.PrintWriter;
 import jdk.internal.org.objectweb.asm.ClassReader;
 import jdk.internal.org.objectweb.asm.ClassWriter;
+import jdk.internal.org.objectweb.asm.Opcodes;
 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
 
 /**
@@ -39,6 +40,11 @@
  *
  */
 public class Main {
+    /**
+     * ASM version to be used by nasgen tool.
+     */
+    public static final int ASM_VERSION = Opcodes.ASM5;
+
     private static final boolean DEBUG = Boolean.getBoolean("nasgen.debug");
 
     private interface ErrorReporter {
--- a/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/MethodGenerator.java	Tue Aug 19 11:25:14 2014 +0200
+++ b/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/MethodGenerator.java	Tue Aug 19 18:14:45 2014 +0530
@@ -32,7 +32,6 @@
 import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD;
 import static jdk.internal.org.objectweb.asm.Opcodes.ANEWARRAY;
 import static jdk.internal.org.objectweb.asm.Opcodes.ARETURN;
-import static jdk.internal.org.objectweb.asm.Opcodes.ASM4;
 import static jdk.internal.org.objectweb.asm.Opcodes.ASTORE;
 import static jdk.internal.org.objectweb.asm.Opcodes.BALOAD;
 import static jdk.internal.org.objectweb.asm.Opcodes.BASTORE;
@@ -96,7 +95,7 @@
     private final Type[] argumentTypes;
 
     MethodGenerator(final MethodVisitor mv, final int access, final String name, final String descriptor) {
-        super(ASM4, mv);
+        super(Main.ASM_VERSION, mv);
         this.access        = access;
         this.name          = name;
         this.descriptor    = descriptor;
--- a/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/NullVisitor.java	Tue Aug 19 11:25:14 2014 +0200
+++ b/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/NullVisitor.java	Tue Aug 19 18:14:45 2014 +0530
@@ -29,7 +29,6 @@
 import jdk.internal.org.objectweb.asm.ClassVisitor;
 import jdk.internal.org.objectweb.asm.FieldVisitor;
 import jdk.internal.org.objectweb.asm.MethodVisitor;
-import jdk.internal.org.objectweb.asm.Opcodes;
 
 /**
  * A visitor that does nothing on visitXXX calls.
@@ -37,7 +36,7 @@
  */
 public class NullVisitor extends ClassVisitor {
     NullVisitor() {
-        super(Opcodes.ASM4);
+        super(Main.ASM_VERSION);
     }
 
     @Override
@@ -47,7 +46,7 @@
         final String desc,
         final String signature,
         final String[] exceptions) {
-        return new MethodVisitor(Opcodes.ASM4) {
+        return new MethodVisitor(Main.ASM_VERSION) {
             @Override
             public AnnotationVisitor visitAnnotationDefault() {
                 return new NullAnnotationVisitor();
@@ -67,7 +66,7 @@
         final String desc,
         final String signature,
         final Object value) {
-        return new FieldVisitor(Opcodes.ASM4) {
+        return new FieldVisitor(Main.ASM_VERSION) {
             @Override
             public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
                 return new NullAnnotationVisitor();
@@ -82,7 +81,7 @@
 
     private static class NullAnnotationVisitor extends AnnotationVisitor {
         NullAnnotationVisitor() {
-            super(Opcodes.ASM4);
+            super(Main.ASM_VERSION);
         }
     }
 }
--- a/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ScriptClassInfoCollector.java	Tue Aug 19 11:25:14 2014 +0200
+++ b/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ScriptClassInfoCollector.java	Tue Aug 19 18:14:45 2014 +0530
@@ -55,7 +55,7 @@
     private String javaClassName;
 
     ScriptClassInfoCollector(final ClassVisitor visitor) {
-        super(Opcodes.ASM4, visitor);
+        super(Main.ASM_VERSION, visitor);
     }
 
     ScriptClassInfoCollector() {
@@ -80,7 +80,7 @@
     public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
         final AnnotationVisitor delegateAV = super.visitAnnotation(desc, visible);
         if (SCRIPT_CLASS_ANNO_DESC.equals(desc)) {
-            return new AnnotationVisitor(Opcodes.ASM4, delegateAV) {
+            return new AnnotationVisitor(Main.ASM_VERSION, delegateAV) {
                 @Override
                 public void visit(final String name, final Object value) {
                     if ("value".equals(name)) {
@@ -98,7 +98,7 @@
     public FieldVisitor visitField(final int fieldAccess, final String fieldName, final String fieldDesc, final String signature, final Object value) {
         final FieldVisitor delegateFV = super.visitField(fieldAccess, fieldName, fieldDesc, signature, value);
 
-        return new FieldVisitor(Opcodes.ASM4, delegateFV) {
+        return new FieldVisitor(Main.ASM_VERSION, delegateFV) {
             @Override
             public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
                 final AnnotationVisitor delegateAV = super.visitAnnotation(descriptor, visible);
@@ -117,7 +117,7 @@
 
                     addScriptMember(memInfo);
 
-                    return new AnnotationVisitor(Opcodes.ASM4, delegateAV) {
+                    return new AnnotationVisitor(Main.ASM_VERSION, delegateAV) {
                         // These could be "null" if values are not suppiled,
                         // in which case we have to use the default values.
                         private String  name;
@@ -180,7 +180,7 @@
         final MethodVisitor delegateMV = super.visitMethod(methodAccess, methodName, methodDesc,
                 signature, exceptions);
 
-        return new MethodVisitor(Opcodes.ASM4, delegateMV) {
+        return new MethodVisitor(Main.ASM_VERSION, delegateMV) {
 
             @Override
             public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
@@ -201,7 +201,7 @@
 
                     addScriptMember(memInfo);
 
-                    return new AnnotationVisitor(Opcodes.ASM4, delegateAV) {
+                    return new AnnotationVisitor(Main.ASM_VERSION, delegateAV) {
                         // These could be "null" if values are not suppiled,
                         // in which case we have to use the default values.
                         private String  name;
--- a/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ScriptClassInstrumentor.java	Tue Aug 19 11:25:14 2014 +0200
+++ b/nashorn/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ScriptClassInstrumentor.java	Tue Aug 19 18:14:45 2014 +0530
@@ -73,7 +73,7 @@
     private boolean staticInitFound;
 
     ScriptClassInstrumentor(final ClassVisitor visitor, final ScriptClassInfo sci) {
-        super(Opcodes.ASM4, visitor);
+        super(Main.ASM_VERSION, visitor);
         if (sci == null) {
             throw new IllegalArgumentException("Null ScriptClassInfo, is the class annotated?");
         }
@@ -103,7 +103,7 @@
 
         final FieldVisitor delegateFV = super.visitField(fieldAccess, fieldName, fieldDesc,
                 signature, value);
-        return new FieldVisitor(Opcodes.ASM4, delegateFV) {
+        return new FieldVisitor(Main.ASM_VERSION, delegateFV) {
             @Override
             public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
                 if (ScriptClassInfo.annotations.containsKey(desc)) {
@@ -140,7 +140,7 @@
         final MethodGenerator delegateMV = new MethodGenerator(super.visitMethod(methodAccess, methodName, methodDesc,
                 signature, exceptions), methodAccess, methodName, methodDesc);
 
-        return new MethodVisitor(Opcodes.ASM4, delegateMV) {
+        return new MethodVisitor(Main.ASM_VERSION, delegateMV) {
             @Override
             public void visitInsn(final int opcode) {
                 // call $clinit$ just before return from <clinit>
--- a/nashorn/make/build.xml	Tue Aug 19 11:25:14 2014 +0200
+++ b/nashorn/make/build.xml	Tue Aug 19 18:14:45 2014 +0530
@@ -128,6 +128,7 @@
       <compilerarg value="-Xlint:all"/>
       <compilerarg value="-XDignore.symbol.file"/>
       <compilerarg value="-Xdiags:verbose"/>
+      <compilerarg value="-parameters"/>
     </javac>
     <copy todir="${build.classes.dir}/META-INF/services">
        <fileset dir="${meta.inf.dir}/services/"/>
--- a/nashorn/make/project.properties	Tue Aug 19 11:25:14 2014 +0200
+++ b/nashorn/make/project.properties	Tue Aug 19 18:14:45 2014 +0530
@@ -28,8 +28,8 @@
 
 # source and target levels
 build.compiler=modern
-javac.source=1.7
-javac.target=1.7
+javac.source=1.8
+javac.target=1.8
 
 # nashorn version information
 nashorn.version=0.1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/find_nonfinals.js	Tue Aug 19 18:14:45 2014 +0530
@@ -0,0 +1,87 @@
+/*
+ * 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.
+ */
+
+/**
+ * Nashorn project uses "final" modifier for method parameters
+ * (like 'val' of Scala). This tool finds method parameters that
+ * miss final modifier.
+ */
+
+// Usage: jjs -J-Djava.ext.dirs=<your_nashorn_jar_dir> find_nonfinals.js
+
+var Class = Java.type("java.lang.Class");
+var System = Java.type("java.lang.System");
+var File = Java.type("java.io.File");
+var JarFile = Java.type("java.util.jar.JarFile");
+var Modifier = Java.type("java.lang.reflect.Modifier");
+
+// locate nashorn.jar from java.ext.dirs
+function findNashorn() {
+    var paths = System.getProperty("java.ext.dirs").split(':');
+    for each (var p in paths) {
+        var nashorn = p + File.separator + "nashorn.jar";
+        if (new File(nashorn).exists()) {
+            return nashorn;
+        }
+    }
+}
+
+// analyze a single Class and print info on non-final parameters
+function analyzeClass(cls) {
+    var methods = cls.getDeclaredMethods();
+    for each (var method in methods) {
+        // this requires -parameters option when compiling java sources
+        var params = method.parameters;
+        for each (var p in params) {
+           var modifiers = p.modifiers;
+           if (!Modifier.isFinal(modifiers)) {
+               if (! method.name.startsWith("access$")) {
+                   print(method);
+                   print(" ->", p);
+               }
+           }
+        }
+    }
+}
+
+var jarFile = findNashorn();
+// load each class and use reflection to analyze each Class
+new JarFile(jarFile).stream().forEach(
+    function(entry) {
+        var name = entry.name;
+        if (name.endsWith(".class")) {
+            var clsName = name.substring(0, name.lastIndexOf('.class'));
+            clsName = clsName.replace(/\//g, '.');
+            var cls = Class.forName(clsName);
+            analyzeClass(cls);
+        }
+    }
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/samples/try_with_resource.js	Tue Aug 19 18:14:45 2014 +0530
@@ -0,0 +1,65 @@
+/*
+ * 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 sample implements Java-like try..with..resource construct for nashorn */
+
+if (arguments.length == 0) {
+    print("Usage: jjs try_with_resource.js -- <file name>");
+    exit(1);
+}
+
+// Object.create accepts prototype and properties object
+function With(res) {
+   return Object.create(With.prototype,
+       { res: { value: res } });
+}
+
+// reserved words can be used as property names - for example "try"
+With.prototype.try = function(callback) {
+     try {
+         callback();
+     } finally {
+         this.res.close();
+     }
+}
+
+var BufferedReader = Java.type("java.io.BufferedReader");
+var FileReader = Java.type("java.io.FileReader");
+
+var r = new BufferedReader(new FileReader(arguments[0]));
+
+// Java-like try..with..resource
+With(r).try(function() {
+    var s;
+    while ((s = r.readLine()) != null) {
+        print(s);
+    }
+})