Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #4735 from soc/SI-9437
SI-9437 Emit and support parameter names in class files
- Loading branch information
Showing
11 changed files
with
194 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: a; isNamePresent: true; isSynthetic: false | ||
name: _; isNamePresent: true; isSynthetic: false | ||
name: ***; isNamePresent: true; isSynthetic: false | ||
name: unary_!; isNamePresent: true; isSynthetic: false | ||
name: ABC; isNamePresent: true; isSynthetic: false | ||
name: a; isNamePresent: true; isSynthetic: false | ||
name: _; isNamePresent: true; isSynthetic: false | ||
name: ***; isNamePresent: true; isSynthetic: false | ||
name: unary_!; isNamePresent: true; isSynthetic: false | ||
name: ABC; isNamePresent: true; isSynthetic: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Foo(a: Int, `_`: String, *** : Long, `unary_!` : Float, ABC: Double) { | ||
def bar(a: Int, `_`: String, *** : Long, `unary_!` : Float, ABC: Double) = null | ||
} | ||
|
||
object Test extends App { | ||
val constrParams = classOf[Foo].getConstructors.head.getParameters | ||
val methodParams = classOf[Foo].getDeclaredMethods.head.getParameters | ||
|
||
def printParams(params: Array[java.lang.reflect.Parameter]) = { | ||
params.foreach { param => | ||
println(s"name: ${param.getName}; isNamePresent: ${param.isNamePresent}; isSynthetic: ${param.isSynthetic}") | ||
} | ||
} | ||
|
||
printParams(constrParams) | ||
printParams(methodParams) | ||
|
||
val foo = new Foo(a = 1, `_` = "2", *** = 3L, `unary_!` = 4.0f, ABC = 5.0) | ||
foo.bar(a = 1, `_` = "2", *** = 3L, `unary_!` = 4.0f, ABC = 5.0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: a; isNamePresent: true; isSynthetic: false | ||
name: _; isNamePresent: true; isSynthetic: false | ||
name: ***; isNamePresent: true; isSynthetic: false | ||
name: unary_!; isNamePresent: true; isSynthetic: false | ||
name: ABC; isNamePresent: true; isSynthetic: false | ||
name: a; isNamePresent: true; isSynthetic: false | ||
name: _; isNamePresent: true; isSynthetic: false | ||
name: ***; isNamePresent: true; isSynthetic: false | ||
name: unary_!; isNamePresent: true; isSynthetic: false | ||
name: ABC; isNamePresent: true; isSynthetic: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Foo(a: Int, `_`: String, *** : Long, `unary_!` : Float, ABC: Double) { | ||
def bar(a: Int, `_`: String, *** : Long, `unary_!` : Float, ABC: Double) = null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
object Test extends App { | ||
val constrParams = classOf[Foo].getConstructors.head.getParameters | ||
val methodParams = classOf[Foo].getDeclaredMethods.head.getParameters | ||
|
||
def printParams(params: Array[java.lang.reflect.Parameter]) = { | ||
params.foreach { param => | ||
println(s"name: ${param.getName}; isNamePresent: ${param.isNamePresent}; isSynthetic: ${param.isSynthetic}") | ||
} | ||
} | ||
|
||
printParams(constrParams) | ||
printParams(methodParams) | ||
|
||
val foo = new Foo(a = 1, `_` = "2", *** = 3L, `unary_!` = 4.0f, ABC = 5.0) | ||
foo.bar(a = 1, `_` = "2", *** = 3L, `unary_!` = 4.0f, ABC = 5.0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: a; isNamePresent: true; isSynthetic: false | ||
name: _; isNamePresent: true; isSynthetic: false | ||
name: ***; isNamePresent: true; isSynthetic: false | ||
name: unary_!; isNamePresent: true; isSynthetic: false | ||
name: ABC; isNamePresent: true; isSynthetic: false | ||
name: a; isNamePresent: true; isSynthetic: false | ||
name: _; isNamePresent: true; isSynthetic: false | ||
name: ***; isNamePresent: true; isSynthetic: false | ||
name: unary_!; isNamePresent: true; isSynthetic: false | ||
name: ABC; isNamePresent: true; isSynthetic: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import java.io.{File, FileOutputStream} | ||
|
||
import scala.tools.nsc.settings.ScalaVersion | ||
import scala.tools.partest._ | ||
import scala.tools.asm | ||
import asm.{AnnotationVisitor, ClassWriter, FieldVisitor, Handle, MethodVisitor, Opcodes} | ||
import Opcodes._ | ||
|
||
// This test ensures that we can read JDK 8 (classfile format 52) files with | ||
// parameter names. To do that it first uses ASM to generate a class containing | ||
// these additional attributes. Then it runs a normal compile on Scala source | ||
// that uses the class with named arguments. | ||
// Any failure will be dumped to std out. | ||
object Test extends DirectTest { | ||
override def extraSettings: String = "-usejavacp -d " + testOutput.path + " -cp " + testOutput.path | ||
|
||
def generateCode(): Unit = { | ||
val className = "Foo" | ||
|
||
val cw = new ClassWriter(0) | ||
cw.visit(52, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", null); | ||
|
||
val mvC = cw.visitMethod(ACC_PUBLIC, "<init>", "(ILjava/lang/String;JFD)V", null, null); | ||
mvC.visitParameter("a", ACC_FINAL); | ||
mvC.visitParameter("_", ACC_FINAL); | ||
mvC.visitParameter("***", ACC_FINAL); | ||
mvC.visitParameter("unary_!", ACC_FINAL); | ||
mvC.visitParameter("ABC", ACC_FINAL); | ||
mvC.visitCode(); | ||
mvC.visitVarInsn(ALOAD, 0); | ||
mvC.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); | ||
mvC.visitInsn(RETURN); | ||
mvC.visitMaxs(1, 8); | ||
mvC.visitEnd(); | ||
|
||
val mvM = cw.visitMethod(ACC_PUBLIC, "bar", "(ILjava/lang/String;JFD)Lscala/runtime/Null$;", null, null); | ||
mvM.visitParameter("a", ACC_FINAL); | ||
mvM.visitParameter("_", ACC_FINAL); | ||
mvM.visitParameter("***", ACC_FINAL); | ||
mvM.visitParameter("unary_!", ACC_FINAL); | ||
mvM.visitParameter("ABC", ACC_FINAL); | ||
mvM.visitCode(); | ||
mvM.visitInsn(ACONST_NULL); | ||
mvM.visitInsn(ARETURN); | ||
mvM.visitMaxs(1, 8); | ||
mvM.visitEnd(); | ||
|
||
cw.visitEnd(); | ||
|
||
val bytes = cw.toByteArray() | ||
|
||
val fos = new FileOutputStream(new File(s"${testOutput.path}/$className.class")) | ||
try | ||
fos write bytes | ||
finally | ||
fos.close() | ||
|
||
} | ||
|
||
def code = | ||
""" | ||
class Driver { | ||
val constrParams = classOf[Foo].getConstructors.head.getParameters | ||
val methodParams = classOf[Foo].getDeclaredMethods.head.getParameters | ||
def printParams(params: Array[java.lang.reflect.Parameter]) = { | ||
params.foreach { param => | ||
println(s"name: ${param.getName}; isNamePresent: ${param.isNamePresent}; isSynthetic: ${param.isSynthetic}") | ||
} | ||
} | ||
printParams(constrParams) | ||
printParams(methodParams) | ||
val foo = new Foo(a = 1, `_` = "2", *** = 3L, `unary_!` = 4.0f, ABC = 5.0) | ||
foo.bar(a = 1, `_` = "2", *** = 3L, `unary_!` = 4.0f, ABC = 5.0) | ||
} | ||
""" | ||
|
||
override def show(): Unit = { | ||
// redirect err to out, for logging | ||
val prevErr = System.err | ||
System.setErr(System.out) | ||
try { | ||
generateCode() | ||
compile() | ||
Class.forName("Driver").newInstance() | ||
} | ||
finally | ||
System.setErr(prevErr) | ||
} | ||
} |