summaryrefslogtreecommitdiff
path: root/test/files/jvm/innerClassEnclMethodJavaReflection.scala
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@gmail.com>2015-04-01 16:47:22 +0200
committerLukas Rytz <lukas.rytz@gmail.com>2015-04-01 16:47:22 +0200
commitdcd9a83916f9e0128ef6869def82d4f23bdea0e0 (patch)
treee58331aebe8ba06fd1c15faf36fdbb3f77b489b9 /test/files/jvm/innerClassEnclMethodJavaReflection.scala
parentebf0976c363c67e6a46c66d70b39704f1ce5e74a (diff)
parentfcc20fe4d3ac5caceb50965bc202b880e61f984c (diff)
downloadscala-dcd9a83916f9e0128ef6869def82d4f23bdea0e0.tar.gz
scala-dcd9a83916f9e0128ef6869def82d4f23bdea0e0.tar.bz2
scala-dcd9a83916f9e0128ef6869def82d4f23bdea0e0.zip
Merge commit 'fcc20fe' into merge/2.11-to-2.12-apr-1
Diffstat (limited to 'test/files/jvm/innerClassEnclMethodJavaReflection.scala')
-rw-r--r--test/files/jvm/innerClassEnclMethodJavaReflection.scala65
1 files changed, 65 insertions, 0 deletions
diff --git a/test/files/jvm/innerClassEnclMethodJavaReflection.scala b/test/files/jvm/innerClassEnclMethodJavaReflection.scala
new file mode 100644
index 0000000000..ee39cb43bf
--- /dev/null
+++ b/test/files/jvm/innerClassEnclMethodJavaReflection.scala
@@ -0,0 +1,65 @@
+import scala.reflect.io._
+import java.net.URLClassLoader
+
+object Test extends App {
+ val jarsOrDirectories = Set("partest.lib", "partest.reflect", "partest.comp") map sys.props
+
+ object AllowedMissingClass {
+ // Some classes in scala-compiler.jar have references to jline / ant classes, which seem to be
+ // not on the classpath. We just skip over those classes.
+ // PENDING: for now we also allow missing $anonfun classes: the optimizer may eliminate some closures
+ // that are refferred to in EnclosingClass attributes. SI-9136
+ val allowedMissingPackages = Set("jline", "org.apache.tools.ant", "$anonfun")
+
+ def ok(t: Throwable) = {
+ allowedMissingPackages.exists(p => t.getMessage.replace('/', '.').contains(p))
+ }
+
+ def unapply(t: Throwable): Option[Throwable] = t match {
+ case _: NoClassDefFoundError | _: ClassNotFoundException | _: TypeNotPresentException if ok(t) => Some(t)
+ case _ => None
+ }
+ }
+
+ jarsOrDirectories foreach testClasses
+
+ def testClasses(jarOrDirectory: String): Unit = {
+ val classPath = AbstractFile.getDirectory(new java.io.File(jarOrDirectory))
+
+ def flatten(f: AbstractFile): Iterator[AbstractFile] =
+ if (f.isClassContainer) f.iterator.flatMap(flatten)
+ else Iterator(f)
+
+ val classFullNames = flatten(classPath).filter(_.hasExtension("class")).map(_.path.replace("/", ".").replaceAll(".class$", ""))
+
+ // it seems that Class objects can only be GC'd together with their class loader
+ // (http://stackoverflow.com/questions/2433261/when-and-how-are-classes-garbage-collected-in-java)
+ // if we just use the same class loader for the entire test (Class.forName), we run out of PermGen
+ // even with that, we still neeed a PermGen of 90M or so, the default 64 is not enough. I tried
+ // using one class loader per 100 classes, but that didn't help, the classes didn't get GC'd.
+ val classLoader = new URLClassLoader(Array(classPath.toURL))
+
+ val faulty = new collection.mutable.ListBuffer[(String, Throwable)]
+
+ def tryGetClass(name: String) = try {
+ Some[Class[_]](classLoader.loadClass(name))
+ } catch {
+ case AllowedMissingClass(_) => None
+ }
+
+ for (name <- classFullNames; cls <- tryGetClass(name)) {
+ try {
+ cls.getEnclosingMethod
+ cls.getEnclosingClass
+ cls.getEnclosingConstructor
+ cls.getDeclaredClasses
+ } catch {
+ case AllowedMissingClass(_) =>
+ case t: Throwable => faulty += ((name, t))
+ }
+ }
+
+ if (faulty.nonEmpty)
+ println(faulty.toList mkString "\n")
+ }
+}