8055395: Nashorn should use source, target to be 1.8 and use ASM5 version for generated code

Reviewed-by: attila, lagergren, jlaskey
This commit is contained in:
Athijegannathan Sundararajan 2014-08-19 18:14:45 +05:30
parent 47a0c94d3f
commit fa78f33af1
9 changed files with 175 additions and 18 deletions

View File

@ -32,6 +32,7 @@ import java.io.IOException;
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 @@ import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
*
*/
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 {

View File

@ -32,7 +32,6 @@ import static jdk.internal.org.objectweb.asm.Opcodes.ACONST_NULL;
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 @@ public class MethodGenerator extends MethodVisitor {
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;

View File

@ -29,7 +29,6 @@ import jdk.internal.org.objectweb.asm.AnnotationVisitor;
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 @@ import jdk.internal.org.objectweb.asm.Opcodes;
*/
public class NullVisitor extends ClassVisitor {
NullVisitor() {
super(Opcodes.ASM4);
super(Main.ASM_VERSION);
}
@Override
@ -47,7 +46,7 @@ public class NullVisitor extends ClassVisitor {
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 @@ public class NullVisitor extends ClassVisitor {
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 @@ public class NullVisitor extends ClassVisitor {
private static class NullAnnotationVisitor extends AnnotationVisitor {
NullAnnotationVisitor() {
super(Opcodes.ASM4);
super(Main.ASM_VERSION);
}
}
}

View File

@ -55,7 +55,7 @@ public class ScriptClassInfoCollector extends ClassVisitor {
private String javaClassName;
ScriptClassInfoCollector(final ClassVisitor visitor) {
super(Opcodes.ASM4, visitor);
super(Main.ASM_VERSION, visitor);
}
ScriptClassInfoCollector() {
@ -80,7 +80,7 @@ public class ScriptClassInfoCollector extends ClassVisitor {
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 class ScriptClassInfoCollector extends ClassVisitor {
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 @@ public class ScriptClassInfoCollector extends ClassVisitor {
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 @@ public class ScriptClassInfoCollector extends ClassVisitor {
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 @@ public class ScriptClassInfoCollector extends ClassVisitor {
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;

View File

@ -73,7 +73,7 @@ public class ScriptClassInstrumentor extends ClassVisitor {
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 @@ public class ScriptClassInstrumentor extends ClassVisitor {
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 @@ public class ScriptClassInstrumentor extends ClassVisitor {
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>

View File

@ -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/"/>

View File

@ -28,8 +28,8 @@ jdk.asm.src.dir=../jdk/src/share/classes/jdk/internal/org/objectweb/asm
# 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

View File

@ -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);
}
}
)

View File

@ -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);
}
})