diff options
author | Lukas Rytz <lukas.rytz@gmail.com> | 2014-07-01 16:28:24 +0200 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@gmail.com> | 2014-09-01 14:29:17 +0200 |
commit | e3107465c3e8ac80d1cc6a759e2f3298c2531424 (patch) | |
tree | 5aacf420f8845cabed5088b2209ed1e5a9b74c51 /test/files/jvm/javaReflection | |
parent | 2606bd921e434a6d8edb21f7f04dbfb10045026e (diff) | |
download | scala-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.
Diffstat (limited to 'test/files/jvm/javaReflection')
-rw-r--r-- | test/files/jvm/javaReflection/Classes_1.scala | 84 | ||||
-rw-r--r-- | test/files/jvm/javaReflection/Test.scala | 137 |
2 files changed, 221 insertions, 0 deletions
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 |