summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@gmail.com>2014-07-01 16:28:24 +0200
committerLukas Rytz <lukas.rytz@gmail.com>2014-09-01 14:29:17 +0200
commite3107465c3e8ac80d1cc6a759e2f3298c2531424 (patch)
tree5aacf420f8845cabed5088b2209ed1e5a9b74c51
parent2606bd921e434a6d8edb21f7f04dbfb10045026e (diff)
downloadscala-e3107465c3e8ac80d1cc6a759e2f3298c2531424.tar.gz
scala-e3107465c3e8ac80d1cc6a759e2f3298c2531424.tar.bz2
scala-e3107465c3e8ac80d1cc6a759e2f3298c2531424.zip
Fix InnerClass / EnclosingMethod attributes
This commit seems bigger than it is. Most of it is tests, and moving some code around. The actual changes are small, but a bit subtle. The InnerClass and EnclosingMethod attributes should now be close to the JVM spec (which is summarized in BTypes.scala). New tests make sure that changes to these attributes, and changes to the way Java reflection sees Scala classfiles, don't go unnoticed. A new file, BCodeAsmCommon, holds code that's shared between the two backend (it could hold more, future work). In general, the difficulty with emitting InnerClass / EnclosingMethod is that we need to find out source-level properties. We need to make sure to do enough phase-travelling, and work around destructive changes to the ownerchain in lambdalift (we use originalOwner a lot). The change to JavaMirrors is prompted by the change to the EnclosingMethod attribute, which changes Java reflection's answer to getEnclosingMethod and getEnclosingConstructor. Classes defined in field initializers no longer have an enclosing method, just an enclosing class, which broke an assumption in JavaMirrors. There's one change in erasure. Before this change, when an object declaration implements / overrides a method, and a bridge is required, then the bridge method was actually a ModuleSymbol (it would get the lateMETHOD flag and be emitted as a method anyway). This is confusing, when iterating through the members of a class, you can find two modules with the same name, and one of them doesn't have a module class. Now, such bridge methods will be MethodSymbols. Removed Symbol.originalEnclosingMethod, that is a backend thing and doesn't need to live in the symbol API.
-rw-r--r--src/compiler/scala/tools/nsc/Global.scala2
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeAsmCommon.scala130
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala2
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala49
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala2
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala9
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala62
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala102
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala72
-rw-r--r--src/compiler/scala/tools/nsc/transform/Erasure.scala8
-rw-r--r--src/compiler/scala/tools/nsc/transform/LambdaLift.scala2
-rw-r--r--src/interactive/scala/tools/nsc/interactive/Global.scala2
-rw-r--r--src/reflect/scala/reflect/internal/Symbols.scala20
-rw-r--r--src/reflect/scala/reflect/runtime/JavaMirrors.scala25
-rw-r--r--test/files/jvm/innerClassAttribute.check54
-rw-r--r--test/files/jvm/innerClassAttribute/Classes_1.scala92
-rw-r--r--test/files/jvm/innerClassAttribute/Java_A_1.java10
-rw-r--r--test/files/jvm/innerClassAttribute/Test.scala218
-rw-r--r--test/files/jvm/javaReflection.check259
-rw-r--r--test/files/jvm/javaReflection/Classes_1.scala84
-rw-r--r--test/files/jvm/javaReflection/Test.scala137
-rw-r--r--test/files/run/t5256c.check2
-rw-r--r--versions.properties2
23 files changed, 1090 insertions, 255 deletions
diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala
index 08d2b89ece..0077e261cf 100644
--- a/src/compiler/scala/tools/nsc/Global.scala
+++ b/src/compiler/scala/tools/nsc/Global.scala
@@ -1171,7 +1171,7 @@ class Global(var currentSettings: Settings, var reporter: Reporter)
val erasurePhase = phaseNamed("erasure")
val posterasurePhase = phaseNamed("posterasure")
// val lazyvalsPhase = phaseNamed("lazyvals")
- // val lambdaliftPhase = phaseNamed("lambdalift")
+ val lambdaliftPhase = phaseNamed("lambdalift")
// val constructorsPhase = phaseNamed("constructors")
val flattenPhase = phaseNamed("flatten")
val mixinPhase = phaseNamed("mixin")
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeAsmCommon.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeAsmCommon.scala
new file mode 100644
index 0000000000..e5b4c4a6c2
--- /dev/null
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeAsmCommon.scala
@@ -0,0 +1,130 @@
+/* NSC -- new Scala compiler
+ * Copyright 2005-2014 LAMP/EPFL
+ * @author Martin Odersky
+ */
+
+package scala.tools.nsc.backend.jvm
+
+import scala.tools.nsc.Global
+import PartialFunction._
+
+/**
+ * This trait contains code shared between GenBCode and GenASM that depends on types defined in
+ * the compiler cake (Global).
+ */
+final class BCodeAsmCommon[G <: Global](val global: G) {
+ import global._
+
+ /**
+ * True if `classSym` is an anonymous class or a local class. I.e., false if `classSym` is a
+ * member class. This method is used to decide if we should emit an EnclosingMethod attribute.
+ * It is also used to decide whether the "owner" field in the InnerClass attribute should be
+ * null.
+ */
+ def isAnonymousOrLocalClass(classSym: Symbol): Boolean = {
+ def isDelambdafyLambdaClass(classSym: Symbol): Boolean = {
+ classSym.isAnonymousFunction && (settings.Ydelambdafy.value == "method")
+ }
+
+ assert(classSym.isClass, s"not a class: $classSym")
+
+ !isDelambdafyLambdaClass(classSym) &&
+ (classSym.isAnonymousClass || !classSym.originalOwner.isClass)
+ }
+
+ /**
+ * Returns the enclosing method for non-member classes. In the following example
+ *
+ * class A {
+ * def f = {
+ * class B {
+ * class C
+ * }
+ * }
+ * }
+ *
+ * the method returns Some(f) for B, but None for C, because C is a member class. For non-member
+ * classes that are not enclosed by a method, it returns None:
+ *
+ * class A {
+ * { class B }
+ * }
+ *
+ * In this case, for B, we return None.
+ *
+ * The EnclosingMethod attribute needs to be added to non-member classes (see doc in BTypes).
+ * This is a source-level property, so we need to use the originalOwner chain to reconstruct it.
+ */
+ private def enclosingMethodForEnclosingMethodAttribute(classSym: Symbol): Option[Symbol] = {
+ assert(classSym.isClass, classSym)
+ def enclosingMethod(sym: Symbol): Option[Symbol] = {
+ if (sym.isClass || sym == NoSymbol) None
+ else if (sym.isMethod) Some(sym)
+ else enclosingMethod(sym.originalOwner)
+ }
+ enclosingMethod(classSym.originalOwner)
+ }
+
+ /**
+ * The enclosing class for emitting the EnclosingMethod attribute. Since this is a source-level
+ * property, this method looks at the originalOwner chain. See doc in BTypes.
+ */
+ private def enclosingClassForEnclosingMethodAttribute(classSym: Symbol): Symbol = {
+ assert(classSym.isClass, classSym)
+ def enclosingClass(sym: Symbol): Symbol = {
+ if (sym.isClass) sym
+ else enclosingClass(sym.originalOwner)
+ }
+ enclosingClass(classSym.originalOwner)
+ }
+
+ final case class EnclosingMethodEntry(owner: String, name: String, methodDescriptor: String)
+
+ /**
+ * If data for emitting an EnclosingMethod attribute. None if `classSym` is a member class (not
+ * an anonymous or local class). See doc in BTypes.
+ *
+ * The class is parametrized by two functions to obtain a bytecode class descriptor for a class
+ * symbol, and to obtain a method signature descriptor fro a method symbol. These function depend
+ * on the implementation of GenASM / GenBCode, so they need to be passed in.
+ */
+ def enclosingMethodAttribute(classSym: Symbol, classDesc: Symbol => String, methodDesc: Symbol => String): Option[EnclosingMethodEntry] = {
+ if (isAnonymousOrLocalClass(classSym)) {
+ val methodOpt = enclosingMethodForEnclosingMethodAttribute(classSym)
+ debuglog(s"enclosing method for $classSym is $methodOpt (in ${methodOpt.map(_.enclClass)})")
+ Some(EnclosingMethodEntry(
+ classDesc(enclosingClassForEnclosingMethodAttribute(classSym)),
+ methodOpt.map(_.javaSimpleName.toString).orNull,
+ methodOpt.map(methodDesc).orNull))
+ } else {
+ None
+ }
+ }
+
+ /**
+ * This is basically a re-implementation of sym.isStaticOwner, but using the originalOwner chain.
+ *
+ * The problem is that we are interested in a source-level property. Various phases changed the
+ * symbol's properties in the meantime, mostly lambdalift modified (destructively) the owner.
+ * Therefore, `sym.isStatic` is not what we want. For example, in
+ * object T { def f { object U } }
+ * the owner of U is T, so UModuleClass.isStatic is true. Phase travel does not help here.
+ */
+ def isOriginallyStaticOwner(sym: Symbol): Boolean = {
+ sym.isPackageClass || sym.isModuleClass && isOriginallyStaticOwner(sym.originalOwner)
+ }
+
+ /**
+ * The member classes of a class symbol. Note that the result of this method depends on the
+ * current phase, for example, after lambdalift, all local classes become member of the enclosing
+ * class.
+ */
+ def memberClassesOf(classSymbol: Symbol): List[Symbol] = classSymbol.info.decls.collect({
+ case sym if sym.isClass =>
+ sym
+ case sym if sym.isModule =>
+ val r = exitingPickler(sym.moduleClass)
+ assert(r != NoSymbol, sym.fullLocationString)
+ r
+ })(collection.breakOut)
+}
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
index 2e629485a0..397171049f 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
@@ -23,8 +23,8 @@ import scala.tools.asm
abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
import global._
import definitions._
- import bCodeICodeCommon._
import bTypes._
+ import bCodeICodeCommon._
import coreBTypes._
/*
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
index 18a9bad1b7..aed3654b2c 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala
@@ -789,55 +789,6 @@ abstract class BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
new java.lang.Long(id)
).visitEnd()
}
-
- /*
- * @param owner internal name of the enclosing class of the class.
- *
- * @param name the name of the method that contains the class.
-
- * @param methodType the method that contains the class.
- */
- case class EnclMethodEntry(owner: String, name: String, methodType: BType)
-
- /*
- * @return null if the current class is not internal to a method
- *
- * Quoting from JVMS 4.7.7 The EnclosingMethod Attribute
- * A class must have an EnclosingMethod attribute if and only if it is a local class or an anonymous class.
- * A class may have no more than one EnclosingMethod attribute.
- *
- * must-single-thread
- */
- def getEnclosingMethodAttribute(clazz: Symbol): EnclMethodEntry = { // JVMS 4.7.7
-
- def newEEE(eClass: Symbol, m: Symbol) = {
- EnclMethodEntry(
- internalName(eClass),
- m.javaSimpleName.toString,
- asmMethodType(m)
- )
- }
-
- var res: EnclMethodEntry = null
- val sym = clazz.originalEnclosingMethod
- if (sym.isMethod) {
- debuglog(s"enclosing method for $clazz is $sym (in ${sym.enclClass})")
- res = newEEE(sym.enclClass, sym)
- } else if (clazz.isAnonymousClass) {
- val enclClass = clazz.rawowner
- assert(enclClass.isClass, enclClass)
- val sym = enclClass.primaryConstructor
- if (sym == NoSymbol) {
- log(s"Ran out of room looking for an enclosing method for $clazz: no constructor here: $enclClass.")
- } else {
- debuglog(s"enclosing method for $clazz is $sym (in $enclClass)")
- res = newEEE(enclClass, sym)
- }
- }
-
- res
- }
-
} // end of trait BCClassGen
/* basic functionality for class file building of plain, mirror, and beaninfo classes. */
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala
index 5d187168db..d58368b19d 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala
@@ -19,8 +19,6 @@ import scala.collection.mutable
*
*/
abstract class BCodeIdiomatic extends SubComponent {
- protected val bCodeICodeCommon: BCodeICodeCommon[global.type] = new BCodeICodeCommon(global)
-
val bTypes = new BTypesFromSymbols[global.type](global)
import global._
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala
index 3f3c0d0620..4592031a31 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala
@@ -27,6 +27,7 @@ abstract class BCodeSkelBuilder extends BCodeHelpers {
import global._
import bTypes._
import coreBTypes._
+ import bCodeAsmCommon._
/*
* There's a dedicated PlainClassBuilder for each CompilationUnit,
@@ -153,10 +154,10 @@ abstract class BCodeSkelBuilder extends BCodeHelpers {
cnode.visitSource(cunit.source.toString, null /* SourceDebugExtension */)
}
- val enclM = getEnclosingMethodAttribute(claszSymbol)
- if (enclM != null) {
- val EnclMethodEntry(className, methodName, methodType) = enclM
- cnode.visitOuterClass(className, methodName, methodType.descriptor)
+ enclosingMethodAttribute(claszSymbol, internalName, asmMethodType(_).descriptor) match {
+ case Some(EnclosingMethodEntry(className, methodName, methodDescriptor)) =>
+ cnode.visitOuterClass(className, methodName, methodDescriptor)
+ case _ => ()
}
val ssa = getAnnotPickle(thisName, claszSymbol)
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala b/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala
index 389dbe43eb..7bf61b4f51 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala
@@ -403,9 +403,11 @@ abstract class BTypes {
* Fields in the InnerClass entries:
* - inner class: the (nested) class C we are talking about
* - outer class: the class of which C is a member. Has to be null for non-members, i.e. for
- * local and anonymous classes.
+ * local and anonymous classes. NOTE: this co-incides with the presence of an
+ * EnclosingMethod attribute (see below)
* - inner name: A string with the simple name of the inner class. Null for anonymous classes.
- * - flags: access property flags, details in JVMS, table in 4.7.6.
+ * - flags: access property flags, details in JVMS, table in 4.7.6. Static flag: see
+ * discussion below.
*
*
* Note 1: when a nested class is present in the InnerClass attribute, all of its enclosing
@@ -446,6 +448,13 @@ abstract class BTypes {
* JVMS 4.7.7: the attribute must be present "if and only if it represents a local class
* or an anonymous class" (i.e. not for member classes).
*
+ * The attribute is mis-named, it should be called "EnclosingClass". It has to be defined for all
+ * local and anonymous classes, no matter if there is an enclosing method or not. Accordingly, the
+ * "class" field (see below) must be always defined, while the "method" field may be null.
+ *
+ * NOTE: When a EnclosingMethod attribute is requried (local and anonymous classes), the "outer"
+ * field in the InnerClass table must be null.
+ *
* Fields:
* - class: the enclosing class
* - method: the enclosing method (or constructor). Null if the class is not enclosed by a
@@ -456,9 +465,8 @@ abstract class BTypes {
* Note: the field is required for anonymous classes defined within local variable
* initializers (within a method), Java example below (**).
*
- * Currently, the Scala compiler sets "method" to the class constructor for classes
- * defined in initializer blocks or field initializers. This is probably OK, since the
- * Scala compiler desugars these statements into to the primary constructor.
+ * For local and anonymous classes in initializer blocks or field initializers, and
+ * class-level anonymous classes, the scala compiler sets the "method" field to null.
*
*
* (*)
@@ -520,30 +528,37 @@ abstract class BTypes {
* STATIC flag
* -----------
*
- * Java: static nested classes have the "static" flag in the InnerClass attribute. This is not the
- * case for local classes defined within a static method, even though such classes, as they are
- * defined in a static context, don't take an "outer" instance.
- * Non-static nested classes (inner classes, including local classes defined in a non-static
- * method) take an "outer" instance on construction.
+ * Java: static member classes have the static flag in the InnerClass attribute, for example B in
+ * class A { static class B { } }
+ *
+ * The spec is not very clear about when the static flag should be emitted. It says: "Marked or
+ * implicitly static in source."
+ *
+ * The presence of the static flag does NOT coincide with the absence of an "outer" field in the
+ * class. The java compiler never puts the static flag for local classes, even if they don't have
+ * an outer pointer:
+ *
+ * class A {
+ * void f() { class B {} }
+ * static void g() { calss C {} }
+ * }
+ *
+ * B has an outer pointer, C doesn't. Both B and C are NOT marked static in the InnerClass table.
+ *
+ * It seems sane to follow the same principle in the Scala compiler. So:
*
- * Scala: Explicitouter adds an "outer" parameter to nested classes, except for classes defined
- * in a static context, i.e. when all outer classes are module classes.
* package p
* object O1 {
- * class C1 // static
- * object O2 {
+ * class C1 // static inner class
+ * object O2 { // static inner module
* def f = {
- * class C2 { // static
- * class C3 // non-static, needs outer
+ * class C2 { // non-static inner class, even though there's no outer pointer
+ * class C3 // non-static, has an outer pointer
* }
* }
* }
* }
*
- * Int the InnerClass attribute, the `static` flag is added for all classes defined in a static
- * context, i.e. also for C2. This is different than in Java.
- *
- *
* Mirror Classes
* --------------
*
@@ -799,7 +814,12 @@ abstract class BTypes {
* to the InnerClass table, enclosing nested classes are also added.
* @param outerName The outerName field in the InnerClass entry, may be None.
* @param innerName The innerName field, may be None.
- * @param isStaticNestedClass True if this is a static nested class (not inner class).
+ * @param isStaticNestedClass True if this is a static nested class (not inner class) (*)
+ *
+ * (*) Note that the STATIC flag in ClassInfo.flags, obtained through javaFlags(classSym), is not
+ * correct for the InnerClass entry, see javaFlags. The static flag in the InnerClass describes
+ * a source-level propety: if the class is in a static context (does not have an outer pointer).
+ * This is checked when building the NestedInfo.
*/
case class NestedInfo(enclosingClass: ClassBType,
outerName: Option[String],
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala b/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala
index da3244ba23..0e2f938602 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala
@@ -24,6 +24,10 @@ class BTypesFromSymbols[G <: Global](val global: G) extends BTypes {
import global._
import definitions._
+ val bCodeICodeCommon: BCodeICodeCommon[global.type] = new BCodeICodeCommon(global)
+ val bCodeAsmCommon: BCodeAsmCommon[global.type] = new BCodeAsmCommon(global)
+ import bCodeAsmCommon._
+
// Why the proxy, see documentation of class [[CoreBTypes]].
val coreBTypes = new CoreBTypesProxy[this.type](this)
import coreBTypes._
@@ -114,45 +118,63 @@ class BTypesFromSymbols[G <: Global](val global: G) extends BTypes {
val flags = javaFlags(classSym)
- def classOrModuleClass(sym: Symbol): Symbol = {
- if (sym.isClass) sym
- else if (sym.isModule) sym.moduleClass
- else NoSymbol
- }
-
- /* The InnerClass table must contain all nested classes. Local or anonymous classes (not
- * members) are referenced from the emitted code of the class, so they are in innerClassBuffer.
+ /* The InnerClass table of a class C must contain all nested classes of C, even if they are only
+ * declared but not otherwise referenced in C (from the bytecode or a method / field signature).
+ * We collect them here.
*
- * TODO @lry check the above. For example: class C { def foo = { class D; 0 } }, is D added?
+ * Nested classes that are also referenced in C will be added to the innerClassBufferASM during
+ * code generation, but those duplicates will be eliminated when emitting the InnerClass
+ * attribute.
*
- * For member classes however, there might be no reference at all in the code of the class.
- * The JMV spec still requires those classes to be added to the InnerClass table, so we collect
- * them here.
+ * Why doe we need to collect classes into innerClassBufferASM at all? To collect references to
+ * nested classes, but NOT nested in C, that are used within C.
*/
- val memberClassSymbols = exitingErasure {
- // time travel to a time before lambdalift (which lifts local classes up to the class, making them members)
- for (sym <- List(classSym, classSym.linkedClassOfClass);
- memberClass <- sym.info.decls.map(classOrModuleClass) if memberClass.isClass)
- yield memberClass
+ val nestedClassSymbols = {
+ // The lambdalift phase lifts all nested classes to the enclosing class, so if we collect
+ // member classes right after lambdalift, we obtain all nested classes, including local and
+ // anonymous ones.
+ val nestedClasses = exitingPhase(currentRun.lambdaliftPhase)(memberClassesOf(classSym))
+
+ // If this is a top-level class, and it has a companion object, the member classes of the
+ // companion are added as members of the class. For example:
+ // class C { }
+ // object C {
+ // class D
+ // def f = { class E }
+ // }
+ // The class D is added as a member of class C. The reason is that the InnerClass attribute
+ // for D will containt class "C" and NOT the module class "C$" as the outer class of D.
+ // This is done by buildNestedInfo, the reason is Java compatibility, see comment in BTypes.
+ // For consistency, the InnerClass entry for D needs to be present in C - to Java it looks
+ // like D is a member of C, not C$.
+ val linkedClass = exitingPickler(classSym.linkedClassOfClass) // linkedCoC does not work properly in late phases
+ val companionModuleMembers = {
+ // phase travel to exitingPickler: this makes sure that memberClassesOf only sees member classes,
+ // not local classes of the companion module (E in the exmaple) that were lifted by lambdalift.
+ if (isTopLevelModuleClass(linkedClass)) exitingPickler(memberClassesOf(linkedClass))
+ else Nil
+ }
+
+ nestedClasses ++ companionModuleMembers
}
/**
* For nested java classes, the scala compiler creates both a class and a module (and therefore
- * a module class) symbol. For example, in `class A { class B {} }`, the memberClassSymbols
+ * a module class) symbol. For example, in `class A { class B {} }`, the nestedClassSymbols
* for A contain both the class B and the module class B.
* Here we get rid of the module class B, making sure that the class B is present.
*/
- val memberClassSymbolsNoJavaModuleClasses = memberClassSymbols.filter(s => {
+ val nestedClassSymbolsNoJavaModuleClasses = nestedClassSymbols.filter(s => {
if (s.isJavaDefined && s.isModuleClass) {
- // We could also search in memberClassSymbols for s.linkedClassOfClass, but sometimes that
+ // We could also search in nestedClassSymbols for s.linkedClassOfClass, but sometimes that
// returns NoSymbol, so it doesn't work.
- val nb = memberClassSymbols.count(mc => mc.name == s.name && mc.owner == s.owner)
- assert(nb == 2, s"Java member module without member class: $s - $memberClassSymbols")
+ val nb = nestedClassSymbols.count(mc => mc.name == s.name && mc.owner == s.owner)
+ assert(nb == 2, s"Java member module without member class: $s - $nestedClassSymbols")
false
} else true
})
- val memberClasses = memberClassSymbolsNoJavaModuleClasses.map(classBTypeFromSymbol)
+ val memberClasses = nestedClassSymbolsNoJavaModuleClasses.map(classBTypeFromSymbol)
val nestedInfo = buildNestedInfo(classSym)
@@ -207,10 +229,8 @@ class BTypesFromSymbols[G <: Global](val global: G) extends BTypes {
val isNested = !innerClassSym.rawowner.isPackageClass
if (!isNested) None
else {
- // Phase travel to a time where the owner chain is in its original form. Example:
- // object T { def f { class C } }
- // C is an inner class (not a static nested class), see InnerClass summary in BTypes.
- val isStaticNestedClass = enteringPickler(innerClassSym.isStatic)
+ // See comment in BTypes, when is a class marked static in the InnerClass table.
+ val isStaticNestedClass = isOriginallyStaticOwner(innerClassSym.originalOwner)
// After lambdalift (which is where we are), the rawowoner field contains the enclosing class.
val enclosingClassSym = {
@@ -220,15 +240,20 @@ class BTypesFromSymbols[G <: Global](val global: G) extends BTypes {
// nested class, the symbol for D is nested in the module class C (not in the class C).
// For the InnerClass attribute, we use the class symbol C, which represents the situation
// in the source code.
- assert(innerClassSym.isStatic, innerClassSym.rawowner)
- innerClassSym.rawowner.linkedClassOfClass
+
+ // Cannot use innerClassSym.isStatic: this method looks at the owner, which is a package
+ // at this pahse (after lambdalift, flatten).
+ assert(isOriginallyStaticOwner(innerClassSym.originalOwner), innerClassSym.originalOwner)
+
+ // phase travel for linkedCoC - does not always work in late phases
+ exitingPickler(innerClassSym.rawowner.linkedClassOfClass)
}
else innerClassSym.rawowner
}
val enclosingClass: ClassBType = classBTypeFromSymbol(enclosingClassSym)
val outerName: Option[String] = {
- if (innerClassSym.originalEnclosingMethod != NoSymbol) {
+ if (isAnonymousOrLocalClass(innerClassSym)) {
None
} else {
val outerName = innerClassSym.rawowner.javaBinaryName
@@ -266,23 +291,10 @@ class BTypesFromSymbols[G <: Global](val global: G) extends BTypes {
* for such objects will get a MODULE$ flag and a corresponding static initializer.
*/
final def isStaticModuleClass(sym: Symbol): Boolean = {
- /* The implementation of this method is tricky because it is a source-level property. Various
- * phases changed the symbol's properties in the meantime.
- *
- * (1) Phase travel to to pickler is required to exclude implementation classes; they have the
+ /* (1) Phase travel to to pickler is required to exclude implementation classes; they have the
* lateMODULEs after mixin, so isModuleClass would be true.
- *
- * (2) We cannot use `sym.isStatic` because lambdalift modified (destructively) the owner. For
- * example, in
- * object T { def f { object U } }
- * the owner of U is T, so UModuleClass.isStatic is true. Phase travel does not help here.
- * So we basically re-implement `sym.isStaticOwner`, but using the original owner chain.
+ * (2) isStaticModuleClass is a source-level property. See comment on isOriginallyStaticOwner.
*/
-
- def isOriginallyStaticOwner(sym: Symbol): Boolean = {
- sym.isPackageClass || sym.isModuleClass && isOriginallyStaticOwner(sym.originalOwner)
- }
-
exitingPickler { // (1)
sym.isModuleClass &&
isOriginallyStaticOwner(sym.originalOwner) // (2)
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala
index 2392033760..2593903b9d 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala
@@ -26,6 +26,9 @@ abstract class GenASM extends SubComponent with BytecodeWriters with GenJVMASM {
import icodes.opcodes._
import definitions._
+ val bCodeAsmCommon: BCodeAsmCommon[global.type] = new BCodeAsmCommon(global)
+ import bCodeAsmCommon._
+
// Strangely I can't find this in the asm code
// 255, but reserving 1 for "this"
final val MaximumJvmParameters = 254
@@ -621,7 +624,7 @@ abstract class GenASM extends SubComponent with BytecodeWriters with GenJVMASM {
* That means non-member classes (anonymous). See Section 4.7.5 in the JVMS.
*/
def outerName(innerSym: Symbol): String = {
- if (innerSym.originalEnclosingMethod != NoSymbol)
+ if (isAnonymousOrLocalClass(innerSym))
null
else {
val outerName = javaName(innerSym.rawowner)
@@ -636,13 +639,16 @@ abstract class GenASM extends SubComponent with BytecodeWriters with GenJVMASM {
else
innerSym.rawname + innerSym.moduleSuffix
- // add inner classes which might not have been referenced yet
- // TODO @lry according to the spec, all nested classes should be added, also local and
- // anonymous. This seems to add only member classes - or not? it's exitingErasure, so maybe
- // local / anonymous classes have been lifted by lambdalift. are they in the "decls" though?
- exitingErasure {
- for (sym <- List(csym, csym.linkedClassOfClass); m <- sym.info.decls.map(innerClassSymbolFor) if m.isClass)
- innerClassBuffer += m
+ // This collects all inner classes of csym, including local and anonymous: lambdalift makes
+ // them members of their enclosing class.
+ innerClassBuffer ++= exitingPhase(currentRun.lambdaliftPhase)(memberClassesOf(csym))
+
+ // Add members of the companion object (if top-level). why, see comment in BTypes.scala.
+ val linkedClass = exitingPickler(csym.linkedClassOfClass) // linkedCoC does not work properly in late phases
+ if (isTopLevelModule(linkedClass)) {
+ // phase travel to exitingPickler: this makes sure that memberClassesOf only sees member classes,
+ // not local classes that were lifted by lambdalift.
+ innerClassBuffer ++= exitingPickler(memberClassesOf(linkedClass))
}
val allInners: List[Symbol] = innerClassBuffer.toList filterNot deadCode.elidedClosures
@@ -656,7 +662,8 @@ abstract class GenASM extends SubComponent with BytecodeWriters with GenJVMASM {
// sort them so inner classes succeed their enclosing class to satisfy the Eclipse Java compiler
for (innerSym <- allInners sortBy (_.name.length)) { // TODO why not sortBy (_.name.toString()) ??
val flagsWithFinal: Int = mkFlags(
- if (innerSym.rawowner.hasModuleFlag) asm.Opcodes.ACC_STATIC else 0,
+ // See comment in BTypes, when is a class marked static in the InnerClass table.
+ if (isOriginallyStaticOwner(innerSym.originalOwner)) asm.Opcodes.ACC_STATIC else 0,
javaFlags(innerSym),
if(isDeprecated(innerSym)) asm.Opcodes.ACC_DEPRECATED else 0 // ASM pseudo-access flag
) & (INNER_CLASSES_FLAGS | asm.Opcodes.ACC_DEPRECATED)
@@ -1222,10 +1229,10 @@ abstract class GenASM extends SubComponent with BytecodeWriters with GenJVMASM {
null /* SourceDebugExtension */)
}
- val enclM = getEnclosingMethodAttribute()
- if(enclM != null) {
- val EnclMethodEntry(className, methodName, methodType) = enclM
- jclass.visitOuterClass(className, methodName, methodType.getDescriptor)
+ enclosingMethodAttribute(clasz.symbol, javaName, javaType(_).getDescriptor) match {
+ case Some(EnclosingMethodEntry(className, methodName, methodDescriptor)) =>
+ jclass.visitOuterClass(className, methodName, methodDescriptor)
+ case _ => ()
}
// typestate: entering mode with valid call sequences:
@@ -1286,45 +1293,6 @@ abstract class GenASM extends SubComponent with BytecodeWriters with GenJVMASM {
writeIfNotTooBig("" + c.symbol.name, thisName, jclass, c.symbol)
}
- /**
- * @param owner internal name of the enclosing class of the class.
- *
- * @param name the name of the method that contains the class.
-
- * @param methodType the method that contains the class.
- */
- case class EnclMethodEntry(owner: String, name: String, methodType: asm.Type)
-
- /**
- * @return null if the current class is not internal to a method
- *
- * Quoting from JVMS 4.7.7 The EnclosingMethod Attribute
- * A class must have an EnclosingMethod attribute if and only if it is a local class or an anonymous class.
- * A class may have no more than one EnclosingMethod attribute.
- *
- */
- private def getEnclosingMethodAttribute(): EnclMethodEntry = { // JVMS 4.7.7
- var res: EnclMethodEntry = null
- val clazz = clasz.symbol
- val sym = clazz.originalEnclosingMethod
- if (sym.isMethod) {
- debuglog("enclosing method for %s is %s (in %s)".format(clazz, sym, sym.enclClass))
- res = EnclMethodEntry(javaName(sym.enclClass), javaName(sym), javaType(sym))
- } else if (clazz.isAnonymousClass) {
- val enclClass = clazz.rawowner
- assert(enclClass.isClass, enclClass)
- val sym = enclClass.primaryConstructor
- if (sym == NoSymbol) {
- log("Ran out of room looking for an enclosing method for %s: no constructor here.".format(enclClass))
- } else {
- debuglog("enclosing method for %s is %s (in %s)".format(clazz, sym, enclClass))
- res = EnclMethodEntry(javaName(enclClass), javaName(sym), javaType(sym))
- }
- }
-
- res
- }
-
def genField(f: IField) {
debuglog("Adding field: " + f.symbol.fullName)
diff --git a/src/compiler/scala/tools/nsc/transform/Erasure.scala b/src/compiler/scala/tools/nsc/transform/Erasure.scala
index ec4deb6be0..d5ffa1fa5b 100644
--- a/src/compiler/scala/tools/nsc/transform/Erasure.scala
+++ b/src/compiler/scala/tools/nsc/transform/Erasure.scala
@@ -468,8 +468,12 @@ abstract class Erasure extends AddInterfaces
if (!bridgeNeeded)
return
- val newFlags = (member.flags | BRIDGE | ARTIFACT) & ~(ACCESSOR | DEFERRED | LAZY | lateDEFERRED)
- val bridge = other.cloneSymbolImpl(root, newFlags) setPos root.pos
+ var newFlags = (member.flags | BRIDGE | ARTIFACT) & ~(ACCESSOR | DEFERRED | LAZY | lateDEFERRED)
+ // If `member` is a ModuleSymbol, the bridge should not also be a ModuleSymbol. Otherwise we
+ // end up with two module symbols with the same name in the same scope, which is surprising
+ // when implementing later phases.
+ if (member.isModule) newFlags = (newFlags | METHOD) & ~(MODULE | lateMETHOD | STABLE)
+ val bridge = other.cloneSymbolImpl(root, newFlags) setPos root.pos
debuglog("generating bridge from %s (%s): %s to %s: %s".format(
other, flagsToString(newFlags),
diff --git a/src/compiler/scala/tools/nsc/transform/LambdaLift.scala b/src/compiler/scala/tools/nsc/transform/LambdaLift.scala
index f85d8222f0..d69c9d9a65 100644
--- a/src/compiler/scala/tools/nsc/transform/LambdaLift.scala
+++ b/src/compiler/scala/tools/nsc/transform/LambdaLift.scala
@@ -449,6 +449,8 @@ abstract class LambdaLift extends InfoTransform {
if (sym.isClass) sym.owner = sym.owner.toInterface
if (sym.isMethod) sym setFlag LIFTED
liftedDefs(sym.owner) ::= tree
+ // TODO: this modifies the ClassInfotype of the enclosing class, which is associated with another phase (explicitouter).
+ // This breaks type history: in a phase travel to before lambda lift, the ClassInfoType will contain lifted classes.
sym.owner.info.decls enterUnique sym
debuglog("lifted: " + sym + " from " + oldOwner + " to " + sym.owner)
EmptyTree
diff --git a/src/interactive/scala/tools/nsc/interactive/Global.scala b/src/interactive/scala/tools/nsc/interactive/Global.scala
index 95027a26b1..174254d523 100644
--- a/src/interactive/scala/tools/nsc/interactive/Global.scala
+++ b/src/interactive/scala/tools/nsc/interactive/Global.scala
@@ -142,8 +142,6 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "")
// don't keep the original owner in presentation compiler runs
// (the map will grow indefinitely, and the only use case is the backend)
override protected def saveOriginalOwner(sym: Symbol) { }
- override protected def originalEnclosingMethod(sym: Symbol) =
- abort("originalOwner is not kept in presentation compiler runs.")
override def forInteractive = true
override protected def synchronizeNames = true
diff --git a/src/reflect/scala/reflect/internal/Symbols.scala b/src/reflect/scala/reflect/internal/Symbols.scala
index b6cce4524b..d132b76f30 100644
--- a/src/reflect/scala/reflect/internal/Symbols.scala
+++ b/src/reflect/scala/reflect/internal/Symbols.scala
@@ -74,15 +74,6 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
}
}
- protected def originalEnclosingMethod(sym: Symbol): Symbol = {
- if (sym.isMethod || sym == NoSymbol) sym
- else {
- val owner = sym.originalOwner
- if (sym.isLocalDummy) owner.enclClass.primaryConstructor
- else originalEnclosingMethod(owner)
- }
- }
-
def symbolOf[T: WeakTypeTag]: TypeSymbol = weakTypeOf[T].typeSymbolDirect.asType
abstract class SymbolContextApiImpl extends SymbolApi {
@@ -2122,16 +2113,6 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
* is not one. */
def enclosingPackage: Symbol = enclosingPackageClass.companionModule
- /** Return the original enclosing method of this symbol. It should return
- * the same thing as enclMethod when called before lambda lift,
- * but it preserves the original nesting when called afterwards.
- *
- * @note This method is NOT available in the presentation compiler run. The
- * originalOwner map is not populated for memory considerations (the symbol
- * may hang on to lazy types and in turn to whole (outdated) compilation units.
- */
- def originalEnclosingMethod: Symbol = Symbols.this.originalEnclosingMethod(this)
-
/** The method or class which logically encloses the current symbol.
* If the symbol is defined in the initialization part of a template
* this is the template's primary constructor, otherwise it is
@@ -3532,7 +3513,6 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
override def rawInfo: Type = NoType
override def accessBoundary(base: Symbol): Symbol = enclosingRootClass
def cloneSymbolImpl(owner: Symbol, newFlags: Long) = abort("NoSymbol.clone()")
- override def originalEnclosingMethod = this
}
protected def makeNoSymbol: NoSymbol = new NoSymbol
diff --git a/src/reflect/scala/reflect/runtime/JavaMirrors.scala b/src/reflect/scala/reflect/runtime/JavaMirrors.scala
index 6584d80de3..b7f229b6e5 100644
--- a/src/reflect/scala/reflect/runtime/JavaMirrors.scala
+++ b/src/reflect/scala/reflect/runtime/JavaMirrors.scala
@@ -807,31 +807,26 @@ private[scala] trait JavaMirrors extends internal.SymbolTable with api.JavaUnive
// field containing the cache for structural calls.
if (mods.isStatic) module.moduleClass.orElse(clazz) else clazz
- /** Methods which need to be treated with care
- * because they either are getSimpleName or call getSimpleName:
+ /**
+ * Certain method of the Java reflection api cannot be used on classfiles created by Scala.
+ * See the comment in test/files/jvm/javaReflection/Test.scala. The methods are
*
* public String getSimpleName()
* public boolean isAnonymousClass()
* public boolean isLocalClass()
* public String getCanonicalName()
- *
- * A typical manifestation:
- *
- * // java.lang.Error: sOwner(class Test$A$1) has failed
- * // Caused by: java.lang.InternalError: Malformed class name
- * // at java.lang.Class.getSimpleName(Class.java:1133)
- * // at java.lang.Class.isAnonymousClass(Class.java:1188)
- * // at java.lang.Class.isLocalClass(Class.java:1199)
- * // (see t5256c.scala for more details)
+ * public boolean isSynthetic()
*
* TODO - find all such calls and wrap them.
* TODO - create mechanism to avoid the recurrence of unwrapped calls.
*/
implicit class RichClass(jclazz: jClass[_]) {
- // `jclazz.isLocalClass` doesn't work because of problems with `getSimpleName`
- // hence we have to approximate by removing the `isAnonymousClass` check
-// def isLocalClass0: Boolean = jclazz.isLocalClass
- def isLocalClass0: Boolean = jclazz.getEnclosingMethod != null || jclazz.getEnclosingConstructor != null
+ // As explained in the javaReflection test, Class.isLocalClass is true for all non-member
+ // nested classes in Scala. This is fine per se, however the implementation may throw an
+ // InternalError. We therefore re-implement it here.
+ // TODO: this method should be renamed to `isLocalOrAnonymousClass`.
+ // due to bin compat that's only possible in 2.12, we cannot introduce a new alias in 2.11.
+ def isLocalClass0: Boolean = jclazz.getEnclosingClass != null && !jclazz.isMemberClass
}
/**
diff --git a/test/files/jvm/innerClassAttribute.check b/test/files/jvm/innerClassAttribute.check
new file mode 100644
index 0000000000..20518aa49e
--- /dev/null
+++ b/test/files/jvm/innerClassAttribute.check
@@ -0,0 +1,54 @@
+#partest !-Ydelambdafy:method
+-- A4 --
+A4$$anonfun$f$1 / null / null / 17
+A4$$anonfun$f$1 / null / null / 17
+A4 / f / (Lscala/collection/immutable/List;)Lscala/collection/immutable/List;
+-- A19 --
+A19$$anonfun$1 / null / null / 17
+A19$$anonfun$2 / null / null / 17
+A19$$anonfun$3 / null / null / 17
+A19$$anonfun$1 / null / null / 17
+A19$$anonfun$2 / null / null / 17
+A19$$anonfun$3 / null / null / 17
+A19 / null / null
+A19 / null / null
+A19 / null / null
+-- A20 --
+A20$$anonfun$4 / null / null / 17
+fun1: attribute for itself and the two child closures `() => ()` and `() => () => 1`
+A20$$anonfun$4 / null / null / 17
+A20$$anonfun$4$$anonfun$apply$1 / null / null / 17
+A20$$anonfun$4$$anonfun$apply$3 / null / null / 17
+fun2 () => (): itself and the outer closure
+A20$$anonfun$4 / null / null / 17
+A20$$anonfun$4$$anonfun$apply$1 / null / null / 17
+fun3 () => () => (): itself, the outer closure and its child closure
+A20$$anonfun$4 / null / null / 17
+A20$$anonfun$4$$anonfun$apply$3 / null / null / 17
+A20$$anonfun$4$$anonfun$apply$3$$anonfun$apply$2 / null / null / 17
+fun4: () => 1: itself and the two outer closures
+A20$$anonfun$4 / null / null / 17
+A20$$anonfun$4$$anonfun$apply$3 / null / null / 17
+A20$$anonfun$4$$anonfun$apply$3$$anonfun$apply$2 / null / null / 17
+enclosing: nested closures have the apply method of the outer closure
+A20 / null / null
+A20$$anonfun$4 / apply / ()Lscala/Function0;
+A20$$anonfun$4 / apply / ()Lscala/Function0;
+A20$$anonfun$4$$anonfun$apply$3 / apply / ()Lscala/Function0;
+#partest -Ydelambdafy:method
+-- A4 --
+null / null / null
+-- A19 --
+null / null / null
+null / null / null
+null / null / null
+-- A20 --
+fun1: attribute for itself and the two child closures `() => ()` and `() => () => 1`
+fun2 () => (): itself and the outer closure
+fun3 () => () => (): itself, the outer closure and its child closure
+fun4: () => 1: itself and the two outer closures
+enclosing: nested closures have the apply method of the outer closure
+null / null / null
+null / null / null
+null / null / null
+null / null / null
diff --git a/test/files/jvm/innerClassAttribute/Classes_1.scala b/test/files/jvm/innerClassAttribute/Classes_1.scala
index 0875d9160c..9c3ea7f013 100644
--- a/test/files/jvm/innerClassAttribute/Classes_1.scala
+++ b/test/files/jvm/innerClassAttribute/Classes_1.scala
@@ -74,7 +74,7 @@ class A14 {
object A15 {
def f = {
- class B { // static (does not have an outer pointer)
+ class B { // non-static, even though it doesn't have an outer pointer
class C // non-static
}
}
@@ -90,10 +90,98 @@ class A16 {
class V extends A6
new A6 { }
}
+
+ new A6 { }
}
class A17 {
object B {
- class C // not static, has an outer pointer.
+ class C // not static, also has an outer pointer.
+ }
+}
+
+class A18 {
+ def f = {
+ def g = {
+ class A
+ new A6 { }
+ val y = {
+ if ((new Object).hashCode() == 1) {class B {} ; new B} else 2
+ if ((new Object).hashCode() == 1) new A6 { } else "haifish"
+ }
+ }
+ }
+}
+
+class A19 {
+ ((x: Int) => x + 3)
+
+ val x = {
+ ((x: Int) => x + 1)
+ }
+
+ {
+ ((x: Int) => x + 2)
+ }
+}
+
+class A20 {
+ () => {
+ {() => ()}
+ {() => () => 1}
+ }
+}
+
+class A21 {
+ class I1
+ def f = { class J1 }
+}
+object A21 {
+ class I2
+ object I3 {
+ class J2 // static
+ }
+ def g = { class J3 } // non-static
+ val x = { class J4 } // non-static
+ {
+ class J5 // non-static (!)
+ new J5
+ }
+}
+
+class A22 {
+ class C
+ object C {
+ class D // inner class of C$, not of C. Not added to the inner class table of C, only to C$
+ }
+}
+
+class A23 {
+ def f = {
+ val a = new Java_A_1()
+ val c = new Java_A_1.C()
+ val d = new Java_A_1.C.D()
+ val e = new c.E()
+ val f = new a.F()
+ val g = new f.G()
+ }
+}
+
+trait A24Sym
+
+trait A24Base {
+ // trait with concrete members: interface plus (absract) impl class
+ trait DefinitionsApi {
+ def Abs: A24Sym
+ def Conc: A24Sym = new A24Sym { }
+ }
+}
+
+trait A24 extends A24Base {
+ class DefinitionsClass extends DefinitionsApi {
+ // bridge methods are generated for Abs and Conc. there used to be a bug: the bridge symbol was a ModuleSymbol,
+ // calling companionClass would return NoSymbol. i changed erasure to make the bridge symbol is a MethodSymbol.
+ object Abs extends A24Sym
+ override object Conc extends A24Sym
}
}
diff --git a/test/files/jvm/innerClassAttribute/Java_A_1.java b/test/files/jvm/innerClassAttribute/Java_A_1.java
new file mode 100644
index 0000000000..3357d05e2b
--- /dev/null
+++ b/test/files/jvm/innerClassAttribute/Java_A_1.java
@@ -0,0 +1,10 @@
+public class Java_A_1 {
+ public static class C {
+ public static class D { }
+ public class E { }
+ }
+
+ public class F {
+ public class G { }
+ }
+}
diff --git a/test/files/jvm/innerClassAttribute/Test.scala b/test/files/jvm/innerClassAttribute/Test.scala
index 6cf60ab92d..882edbcdd7 100644
--- a/test/files/jvm/innerClassAttribute/Test.scala
+++ b/test/files/jvm/innerClassAttribute/Test.scala
@@ -43,10 +43,30 @@ object Test extends BytecodeTest {
assertSame(node.access, flags)
}
- def assertEnclosingMethod(enclosingMethod: EnclosingMethod, outerClass: String, name: String, descriptor: String) = {
- assertSame(enclosingMethod.outerClass, outerClass)
- assertSame(enclosingMethod.name, name)
- assertSame(enclosingMethod.descriptor, descriptor)
+ def assertEnclosingMethod(className: String, outerClass: String, name: String, descriptor: String) = {
+ val encl = enclosingMethod(className)
+ assertSame(encl.outerClass, outerClass)
+ assertSame(encl.name, name)
+ assertSame(encl.descriptor, descriptor)
+ }
+
+ def assertNoEnclosingMethod(className: String) = {
+ assertSame(enclosingMethod(className).outerClass, null)
+ }
+
+ def printInnerClassNodes(className: String) = {
+ for (n <- innerClassNodes(className)) {
+ println(s"${n.name} / ${n.outerName} / ${n.innerName} / ${n.access}")
+ }
+ }
+
+ def printEnclosingMethod(className: String) = {
+ val e = enclosingMethod(className)
+ println(s"${e.outerClass} / ${e.name} / ${e.descriptor}")
+ }
+
+ def lambdaClass(anonfunName: String, lambdaName: String): String = {
+ if (classpath.findClass(anonfunName).isDefined) anonfunName else lambdaName
}
def testA1() = {
@@ -78,13 +98,11 @@ object Test extends BytecodeTest {
}
def testA4() = {
- val List(an1) = innerClassNodes("A4")
- assertAnonymous(an1, "A4$$anonfun$f$1")
- val List(an2) = innerClassNodes("A4$$anonfun$f$1")
- assertAnonymous(an2, "A4$$anonfun$f$1")
- assertEnclosingMethod(
- enclosingMethod("A4$$anonfun$f$1"),
- "A4", "f", "(Lscala/collection/immutable/List;)Lscala/collection/immutable/List;")
+ println("-- A4 --")
+ printInnerClassNodes("A4")
+ val fun = lambdaClass("A4$$anonfun$f$1", "A4$lambda$$f$1")
+ printInnerClassNodes(fun)
+ printEnclosingMethod(fun)
}
def testA5() = {
@@ -93,7 +111,7 @@ object Test extends BytecodeTest {
val List(b2) = innerClassNodes("A5$B$2$")
assertLocal(b2, "A5$B$2$", "B$2$")
assertEnclosingMethod(
- enclosingMethod("A5$B$2$"),
+ "A5$B$2$",
"A5", "f", "()Ljava/lang/Object;")
}
@@ -136,56 +154,175 @@ object Test extends BytecodeTest {
assertLocal(k, "A14$K$1", "K$1")
assertEnclosingMethod(
- enclosingMethod("A14$K$1"),
+ "A14$K$1",
"A14", "f", "()Ljava/lang/Object;")
assertAnonymous(anon, "A14$$anon$1")
assertEnclosingMethod(
- enclosingMethod("A14$$anon$1"),
+ "A14$$anon$1",
"A14", "g", "()V")
}
def testA15() = {
val List(b) = innerClassNodes("A15")
- assertLocal(b, "A15$B$3", "B$3", flags = publicStatic)
+ assertLocal(b, "A15$B$3", "B$3")
val List(_, c) = innerClassNodes("A15$B$3")
- // TODO this is a bug in the backend, C should be a member. Instead, its outerClass is null
- // assertMember(c, "A15$B$3", "C")
- assertLocal(c, "A15$B$3$C", "C")
+ assertMember(c, "A15$B$3", "C")
+
+ assertEnclosingMethod(
+ "A15$B$3",
+ "A15$", "f", "()V")
+ assertNoEnclosingMethod("A15$B$3$C")
}
def testA16() = {
- val List(anon1, anon2, u, v) = innerClassNodes("A16")
- // TODO there's a bug in the backend: anon$2 has outerClass A16, but anonymous classes should have outerClass null
- // assertAnonymous(anon1, "A16$$anon$2")
- assertMember(anon1, "A16", null, name = Some("A16$$anon$2"), flags = Flags.ACC_PUBLIC | Flags.ACC_FINAL)
+ val List(anon1, anon2, anon3, u, v) = innerClassNodes("A16")
+ assertAnonymous(anon1, "A16$$anon$2")
assertAnonymous(anon2, "A16$$anon$3")
- // TODO this is a bug in the backend, U should not be a member, its outerClass should be null
- // assertLocal(u, "A16$U$1", "U$1")
- assertMember(u, "A16", "U$1")
+ assertAnonymous(anon3, "A16$$anon$4")
+
+ assertLocal(u, "A16$U$1", "U$1")
assertLocal(v, "A16$V$1", "V$1")
assertEnclosingMethod(
- enclosingMethod("A16$$anon$2"),
- "A16", "<init>", "()V")
+ "A16$$anon$2",
+ "A16", null, null)
+ assertEnclosingMethod(
+ "A16$$anon$3",
+ "A16", null, null)
+ assertEnclosingMethod(
+ "A16$$anon$4",
+ "A16", null, null)
+
assertEnclosingMethod(
- enclosingMethod("A16$$anon$3"),
- "A16", "<init>", "()V")
- // TODO this is a bug, there should be an enclosingMethod attribute in U
- // assertEnclosingMethod(
- // enclosingMethod("A16$U$1"),
- // "A16", "<init>", "()V")
+ "A16$U$1",
+ "A16", null, null)
assertEnclosingMethod(
- enclosingMethod("A16$V$1"),
- "A16", "<init>", "()V")
+ "A16$V$1",
+ "A16", null, null)
}
def testA17() = {
val List(b, c) = innerClassNodes("A17$B$")
assertMember(b, "A17", "B$")
- // TODO this is a bug, should not be static.
- assertMember(c, "A17$B$", "C", name = Some("A17$B$C"), flags = publicStatic) // (should be) not static, has an outer pointer.
+ assertMember(c, "A17$B$", "C", name = Some("A17$B$C")) // not static, has an outer pointer.
+ }
+
+ def testA18() = {
+ val List(anon1, anon2, a, b) = innerClassNodes("A18")
+ assertAnonymous(anon1, "A18$$anon$5")
+ assertAnonymous(anon2, "A18$$anon$6")
+
+ assertLocal(a, "A18$A$1", "A$1")
+ assertLocal(b, "A18$B$4", "B$4")
+
+ assertEnclosingMethod(
+ "A18$$anon$5",
+ "A18", "g$1", "()V")
+ assertEnclosingMethod(
+ "A18$$anon$6",
+ "A18", "g$1", "()V")
+
+ assertEnclosingMethod(
+ "A18$A$1",
+ "A18", "g$1", "()V")
+ assertEnclosingMethod(
+ "A18$B$4",
+ "A18", "g$1", "()V")
+ }
+
+ def testA19() = {
+ println("-- A19 --")
+
+ printInnerClassNodes("A19")
+
+ val fun1 = lambdaClass("A19$$anonfun$1", "A19$lambda$1")
+ val fun2 = lambdaClass("A19$$anonfun$2", "A19$lambda$2")
+ val fun3 = lambdaClass("A19$$anonfun$3", "A19$lambda$3")
+
+ printInnerClassNodes(fun1)
+ printInnerClassNodes(fun2)
+ printInnerClassNodes(fun3)
+
+ printEnclosingMethod(fun1)
+ printEnclosingMethod(fun2)
+ printEnclosingMethod(fun3)
+ }
+
+ def testA20() = {
+ println("-- A20 --")
+
+ printInnerClassNodes("A20")
+
+ val fun1 = lambdaClass("A20$$anonfun$4", "A20$lambda$1")
+ val fun2 = lambdaClass("A20$$anonfun$4$$anonfun$apply$1", "A20$lambda$$$anonfun$5$1")
+ val fun3 = lambdaClass("A20$$anonfun$4$$anonfun$apply$3", "A20$lambda$$$anonfun$5$2")
+ val fun4 = lambdaClass("A20$$anonfun$4$$anonfun$apply$3$$anonfun$apply$2", "A20$lambda$$$anonfun$7$1")
+
+ println("fun1: attribute for itself and the two child closures `() => ()` and `() => () => 1`")
+ printInnerClassNodes(fun1)
+ println("fun2 () => (): itself and the outer closure")
+ printInnerClassNodes(fun2)
+ println("fun3 () => () => (): itself, the outer closure and its child closure")
+ printInnerClassNodes(fun3)
+ println("fun4: () => 1: itself and the two outer closures")
+ printInnerClassNodes(fun4)
+
+ println("enclosing: nested closures have the apply method of the outer closure")
+ printEnclosingMethod(fun1)
+ printEnclosingMethod(fun2)
+ printEnclosingMethod(fun3)
+ printEnclosingMethod(fun4)
+ }
+
+ def testA21() = {
+ val List(i1c, i2c, i3c, j1) = innerClassNodes("A21")
+ assertMember(i1c, "A21", "I1")
+ assertMember(i2c, "A21", "I2", flags = publicStatic)
+ assertMember(i3c, "A21", "I3$", flags = publicStatic)
+ assertLocal(j1, "A21$J1$1", "J1$1")
+
+ val List(i2m, i3m, j3, j4, j5) = innerClassNodes("A21$")
+ assertMember(i2m, "A21", "I2", flags = publicStatic)
+ assertMember(i3m, "A21", "I3$", flags = publicStatic)
+ assertLocal(j3, "A21$J3$1", "J3$1")
+ assertLocal(j4, "A21$J4$1", "J4$1")
+ assertLocal(j5, "A21$J5$1", "J5$1") // non-static!
+
+ val List(i3x, j2x) = innerClassNodes("A21$I3$J2")
+ assertMember(j2x, "A21$I3$", "J2", name = Some("A21$I3$J2"), flags = publicStatic)
+
+ assertNoEnclosingMethod("A21$I3$J2")
+ assertEnclosingMethod("A21$J3$1", "A21$", "g", "()V")
+ assertEnclosingMethod("A21$J4$1", "A21$", null, null)
+ assertEnclosingMethod("A21$J5$1", "A21$", null, null)
+ }
+
+ def testA22() = {
+ val List(cc) = innerClassNodes("A22$C")
+ assertMember(cc, "A22", "C")
+ val List(cm, d) = innerClassNodes("A22$C$")
+ assertMember(cm, "A22", "C$")
+ assertMember(d, "A22$C$", "D", name = Some("A22$C$D"))
+ }
+
+ def testA23() {
+ val List(c, d, e, f, g) = innerClassNodes("A23")
+ assertMember(c, "Java_A_1", "C", flags = publicStatic)
+ assertMember(d, "Java_A_1$C", "D", flags = publicStatic)
+ assertMember(e, "Java_A_1$C", "E")
+ assertMember(f, "Java_A_1", "F")
+ assertMember(g, "Java_A_1$F", "G")
+ }
+
+ def testA24() {
+ val List(defsCls, abs, conc, defsApi, defsApiImpl) = innerClassNodes("A24$DefinitionsClass")
+ assertMember(defsCls, "A24", "DefinitionsClass")
+ assertMember(abs, "A24$DefinitionsClass", "Abs$")
+ assertMember(conc, "A24$DefinitionsClass", "Conc$")
+ assertMember(defsApi, "A24Base", "DefinitionsApi", flags = publicAbstractInterface)
+ assertMember(defsApiImpl, "A24Base", "DefinitionsApi$class", flags = Flags.ACC_PUBLIC | Flags.ACC_ABSTRACT)
}
def show(): Unit = {
@@ -204,5 +341,12 @@ object Test extends BytecodeTest {
testA15()
testA16()
testA17()
+ testA18()
+ testA19()
+ testA20()
+ testA21()
+ testA22()
+ testA23()
+ testA24()
}
}
diff --git a/test/files/jvm/javaReflection.check b/test/files/jvm/javaReflection.check
new file mode 100644
index 0000000000..aeb894f741
--- /dev/null
+++ b/test/files/jvm/javaReflection.check
@@ -0,0 +1,259 @@
+#partest !-Ydelambdafy:method
+A$$anonfun$$lessinit$greater$1 / null (canon) / $anonfun$$lessinit$greater$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / public A(int) (constr) / null (meth)
+- properties : true (local) / false (member)
+A$$anonfun$$lessinit$greater$1$$anonfun$apply$1 / null (canon) / $anonfun$apply$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A$$anonfun$$lessinit$greater$1 (cls) / null (constr) / public final scala.Function0 A$$anonfun$$lessinit$greater$1.apply() (meth)
+- properties : true (local) / false (member)
+A$$anonfun$2 / null (canon) / $anonfun$2 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$$anonfun$3 / null (canon) / $anonfun$3 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$$anonfun$4 / null (canon) / $anonfun$4 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$$anonfun$f$1 / null (canon) / $anonfun$f$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$$anonfun$f$2 / null (canon) / $anonfun$f$2 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$D$$anonfun$1 / null (canon) / anonfun$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A$D$ (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+AO$$anonfun$5 / null (canon) / anonfun$5 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class AO$ (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+AT$$anonfun$6 / null (canon) / $anonfun$6 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / interface AT (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+#partest -Ydelambdafy:method
+A$D$lambda$1 / A$D$lambda$1 (canon) / A$D$lambda$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+A$lambda$$$anonfun$7$1 / A$lambda$$$anonfun$7$1 (canon) / A$lambda$$$anonfun$7$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+A$lambda$$$lessinit$greater$1 / A$lambda$$$lessinit$greater$1 (canon) / A$lambda$$$lessinit$greater$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+A$lambda$$f$1 / A$lambda$$f$1 (canon) / A$lambda$$f$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+A$lambda$$f$2 / A$lambda$$f$2 (canon) / A$lambda$$f$2 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+A$lambda$1 / A$lambda$1 (canon) / A$lambda$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+A$lambda$2 / A$lambda$2 (canon) / A$lambda$2 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+A$lambda$3 / A$lambda$3 (canon) / A$lambda$3 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+AO$lambda$1 / AO$lambda$1 (canon) / AO$lambda$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+AT$class$lambda$1 / AT$class$lambda$1 (canon) / AT$class$lambda$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+#partest
+A / A (canon) / A (simple)
+- declared cls: List(class A$B, interface A$C, class A$D$)
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+A$$anon$1 / null (canon) / $anon$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$$anon$3 / null (canon) / $anon$3 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$$anon$4 / null (canon) / $anon$4 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$$anon$5 / null (canon) / $anon$5 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$$anon$6 / null (canon) / $anon$6 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$$anon$7 / null (canon) / $anon$7 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / public A(int) (constr) / null (meth)
+- properties : true (local) / false (member)
+A$B / A.B (canon) / B (simple)
+- declared cls: List()
+- enclosing : class A (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+A$C / A.C (canon) / C (simple)
+- declared cls: List()
+- enclosing : class A (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+A$D$ / A.D$ (canon) / D$ (simple)
+- declared cls: List(class A$D$B, interface A$D$C, class A$D$D$)
+- enclosing : class A (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+A$D$$anon$2 / null (canon) / anon$2 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A$D$ (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$D$B / Malformed class name (canon) / Malformed class name (simple)
+- declared cls: List()
+- enclosing : class A$D$ (declaring cls) / class A$D$ (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+A$D$C / Malformed class name (canon) / Malformed class name (simple)
+- declared cls: List()
+- enclosing : class A$D$ (declaring cls) / class A$D$ (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+A$D$D$ / Malformed class name (canon) / Malformed class name (simple)
+- declared cls: List()
+- enclosing : class A$D$ (declaring cls) / class A$D$ (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+A$D$KB$1 / null (canon) / Malformed class name (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A$D$ (cls) / null (constr) / public void A$D$.f() (meth)
+- properties : Malformed class name (local) / false (member)
+A$E$1 / null (canon) / E$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$F$1 / null (canon) / F$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$G$2$ / null (canon) / G$2$ (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$H$1 / null (canon) / H$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$I$1 / null (canon) / I$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$J$2$ / null (canon) / J$2$ (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / public java.lang.Object A.f() (meth)
+- properties : true (local) / false (member)
+A$K$1 / null (canon) / K$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$L$1 / null (canon) / L$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$M$2$ / null (canon) / M$2$ (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$N$1 / null (canon) / N$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$O$1 / null (canon) / O$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$P$2$ / null (canon) / P$2$ (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+A$Q$1 / null (canon) / Q$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / public A(int) (constr) / null (meth)
+- properties : true (local) / false (member)
+A$R$1 / null (canon) / R$1 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / public A(int) (constr) / null (meth)
+- properties : true (local) / false (member)
+A$S$2$ / null (canon) / S$2$ (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class A (cls) / public A(int) (constr) / null (meth)
+- properties : true (local) / false (member)
+AO / AO (canon) / AO (simple)
+- declared cls: List(class AO$B, interface AO$C, class AO$D$)
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+AO$ / AO$ (canon) / AO$ (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+AO$$anon$8 / null (canon) / anon$8 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / class AO$ (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+AO$B / AO.B (canon) / B (simple)
+- declared cls: List()
+- enclosing : class AO (declaring cls) / class AO (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+AO$C / AO.C (canon) / C (simple)
+- declared cls: List()
+- enclosing : class AO (declaring cls) / class AO (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+AO$D$ / AO.D$ (canon) / D$ (simple)
+- declared cls: List()
+- enclosing : class AO (declaring cls) / class AO (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+AT / AT (canon) / AT (simple)
+- declared cls: List(class AT$B, interface AT$C, class AT$D$)
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+AT$$anon$9 / null (canon) / $anon$9 (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / interface AT (cls) / null (constr) / null (meth)
+- properties : true (local) / false (member)
+AT$B / AT.B (canon) / B (simple)
+- declared cls: List()
+- enclosing : interface AT (declaring cls) / interface AT (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+AT$C / AT.C (canon) / C (simple)
+- declared cls: List()
+- enclosing : interface AT (declaring cls) / interface AT (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+AT$D$ / AT.D$ (canon) / D$ (simple)
+- declared cls: List()
+- enclosing : interface AT (declaring cls) / interface AT (cls) / null (constr) / null (meth)
+- properties : false (local) / true (member)
+AT$class / AT$class (canon) / AT$class (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+T / T (canon) / T (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
+T$class / T$class (canon) / T$class (simple)
+- declared cls: List()
+- enclosing : null (declaring cls) / null (cls) / null (constr) / null (meth)
+- properties : false (local) / false (member)
diff --git a/test/files/jvm/javaReflection/Classes_1.scala b/test/files/jvm/javaReflection/Classes_1.scala
new file mode 100644
index 0000000000..11963e2770
--- /dev/null
+++ b/test/files/jvm/javaReflection/Classes_1.scala
@@ -0,0 +1,84 @@
+// See Test.scala for comments
+
+trait T { def f = 1 }
+
+class A {
+ // member class
+ class B
+ // member trait
+ trait C
+ // member object
+ object D {
+ class B
+ trait C
+ object D
+ new T { }
+ (() => -1)
+ def f = { class KB }
+ }
+
+ // anonymous class, not a member
+ new T { }
+
+ // anonymous function, not a member
+ (() => 1)
+
+ def f = {
+ class E
+ trait F
+ object G
+ new T { }
+ (() => 2)
+
+ if (new Object().hashCode == 1) {
+ class H
+ trait I
+ object J
+ new T { }
+ (() => 3)
+ } else {
+ ()
+ }
+ }
+
+ {
+ class K
+ trait L
+ object M
+ new T { }
+ (() => 4)
+ }
+
+ val x = {
+ class N
+ trait O
+ object P
+ new T { }
+ (() => 5)
+ }
+
+ def this(x: Int) {
+ this()
+ class Q
+ trait R
+ object S
+ new T { }
+ (() => () => 5)
+ }
+}
+
+object AO {
+ class B
+ trait C
+ object D
+ new T { }
+ (() => 1)
+}
+
+trait AT {
+ class B
+ trait C
+ object D
+ new T { }
+ (() => 1)
+}
diff --git a/test/files/jvm/javaReflection/Test.scala b/test/files/jvm/javaReflection/Test.scala
new file mode 100644
index 0000000000..5b6ef1b573
--- /dev/null
+++ b/test/files/jvm/javaReflection/Test.scala
@@ -0,0 +1,137 @@
+/**
+Interesting aspects of Java reflection applied to scala classes. TL;DR: you should not use
+getSimpleName / getCanonicalName / isAnonymousClass / isLocalClass / isSynthetic.
+
+ - Some methods in Java reflection assume a certain structure in the class names. Scalac
+ can produce class files that don't respect this structure. Certain methods in reflection
+ therefore give surprising answers or may even throw an exception.
+
+ In particular, the method "getSimpleName" assumes that classes are named after the Java spec
+ http://docs.oracle.com/javase/specs/jls/se8/html/jls-13.html#jls-13.1
+
+ Consider the following Scala example:
+ class A { object B { class C } }
+
+ The classfile for C has the name "A$B$C", while the classfile for the module B has the
+ name "A$B$".
+
+ For "cClass.getSimpleName, the implementation first strips the name of the enclosing class,
+ which produces "C". The implementation then expects a "$" character, which is missing, and
+ throws an InternalError.
+
+ Consider another example:
+ trait T
+ class A { val x = new T {} }
+ object B { val x = new T {} }
+
+ The anonymous classes are named "A$$anon$1" and "B$$anon$2". If you call "getSimpleName",
+ you get "$anon$1" (leading $) and "anon$2" (no leading $).
+
+ - There are certain other methods in the Java reflection API that depend on getSimpleName.
+ These should be avoided, they yield unexpected results:
+
+ - isAnonymousClass is always false. Scala-defined classes are never anonymous for Java
+ reflection. Java reflection insepects the class name to decide whether a class is
+ anonymous, based on the name spec referenced above.
+ Also, the implementation of "isAnonymousClass" calls "getSimpleName", which may throw.
+
+ - isLocalClass: should be true true for local classes (nested classes that are not
+ members), but not for anonymous classes. Since "isAnonymousClass" is always false,
+ Java reflection thinks that all Scala-defined anonymous classes are local.
+ The implementation may also throw, since it uses "isAnonymousClass":
+ class A { object B { def f = { class KB; new KB } } }
+ (new A).B.f.getClass.isLocalClass // boom
+
+ - getCanonicalName: uses "getSimpleName" in the implementation. In the first example,
+ cClass.getCanonicalName also fails with an InternalError.
+
+ - Scala-defined classes are never synthetic for Java reflection. The implementation
+ checks for the SYNTHETEIC flag, which does not seem to be added by scalac (maybe this
+ will change some day).
+*/
+
+object Test {
+
+ def tr[T](m: => T): String = try {
+ val r = m
+ if (r == null) "null"
+ else r.toString
+ } catch { case e: InternalError => e.getMessage }
+
+ def assertNotAnonymous(c: Class[_]) = {
+ val an = try {
+ c.isAnonymousClass
+ } catch {
+ // isAnonymousClass is implemented using getSimpleName, which may throw.
+ case e: InternalError => false
+ }
+ assert(!an, c)
+ }
+
+ def ruleMemberOrLocal(c: Class[_]) = {
+ // if it throws, then it's because of the call from isLocalClass to isAnonymousClass.
+ // we know that isAnonymousClass is always false, so it has to be a local class.
+ val loc = try { c.isLocalClass } catch { case e: InternalError => true }
+ if (loc)
+ assert(!c.isMemberClass, c)
+ if (c.isMemberClass)
+ assert(!loc, c)
+ }
+
+ def ruleMemberDeclaring(c: Class[_]) = {
+ if (c.isMemberClass)
+ assert(c.getDeclaringClass.getDeclaredClasses.toList.map(_.getName) contains c.getName)
+ }
+
+ def ruleScalaAnonClassIsLocal(c: Class[_]) = {
+ if (c.getName contains "$anon$")
+ assert(c.isLocalClass, c)
+ }
+
+ def ruleScalaAnonFunInlineIsLocal(c: Class[_]) = {
+ // exclude lambda classes generated by delambdafy:method. nested closures have both "anonfun" and "lambda".
+ if (c.getName.contains("$anonfun$") && !c.getName.contains("$lambda$"))
+ assert(c.isLocalClass, c)
+ }
+
+ def ruleScalaAnonFunMethodIsToplevel(c: Class[_]) = {
+ if (c.getName.contains("$lambda$"))
+ assert(c.getEnclosingClass == null, c)
+ }
+
+ def showClass(name: String) = {
+ val c = Class.forName(name)
+
+ println(s"${c.getName} / ${tr(c.getCanonicalName)} (canon) / ${tr(c.getSimpleName)} (simple)")
+ println( "- declared cls: "+ c.getDeclaredClasses.toList.sortBy(_.getName))
+ println(s"- enclosing : ${c.getDeclaringClass} (declaring cls) / ${c.getEnclosingClass} (cls) / ${c.getEnclosingConstructor} (constr) / ${c.getEnclosingMethod} (meth)")
+ println(s"- properties : ${tr(c.isLocalClass)} (local) / ${c.isMemberClass} (member)")
+
+ assertNotAnonymous(c)
+ assert(!c.isSynthetic, c)
+
+ ruleMemberOrLocal(c)
+ ruleMemberDeclaring(c)
+ ruleScalaAnonClassIsLocal(c)
+ ruleScalaAnonFunInlineIsLocal(c)
+ ruleScalaAnonFunMethodIsToplevel(c)
+ }
+
+ def main(args: Array[String]): Unit = {
+ def isAnonFunClassName(s: String) = s.contains("$anonfun$") || s.contains("$lambda$")
+
+ val classfiles = new java.io.File(sys.props("partest.output")).listFiles().toList.map(_.getName).collect({
+ // exclude files from Test.scala, just take those from Classes_1.scala
+ case s if !s.startsWith("Test") && s.endsWith(".class") => s.substring(0, s.length - 6)
+ }).sortWith((a, b) => {
+ // sort such that first there are all anonymous funcitions, then all other classes.
+ // within those cathegories, sort lexically.
+ // this makes the check file smaller: it differs for anonymous functions between -Ydelambdafy:inline/method.
+ // the other classes are the same.
+ if (isAnonFunClassName(a)) !isAnonFunClassName(b) || a < b
+ else !isAnonFunClassName(b) && a < b
+ })
+
+ classfiles foreach showClass
+ }
+} \ No newline at end of file
diff --git a/test/files/run/t5256c.check b/test/files/run/t5256c.check
index 7fcd0eb722..3eb7b13a97 100644
--- a/test/files/run/t5256c.check
+++ b/test/files/run/t5256c.check
@@ -2,5 +2,5 @@ class A$1
Test.A$1
java.lang.Object {
def foo(): Nothing
- def <init>(): A$1
+ def <init>(): Test.A$1
}
diff --git a/versions.properties b/versions.properties
index cd53a343f6..454df6b757 100644
--- a/versions.properties
+++ b/versions.properties
@@ -27,7 +27,7 @@ actors-migration.version.number=1.1.0
jline.version=2.12
# external modules, used internally (not shipped)
-partest.version.number=1.0.0
+partest.version.number=1.0.1
scalacheck.version.number=1.11.4
# TODO: modularize the compiler